Archive for Ἰούλιος 2009
Stack Implementation (C++ Language)
Το πρόγραμμα αυτό υλοποιεί την δομή της στοίβας (LIFO= Last In First Out) με δυναμικό τρόπο. Μπορεί να λειτουργήσει και ως παράδειγμα χρησιμοποιήσης δεικτών τόσο στην C όσο και στην C++ ενώ μπορεί να κατανοήσει κανείς το πότε έχουμε κλήση κατ’ αναφορά (Call by Reference) και πότε κλήση κατα τιμή (Call by Value).
Author: filotass || E-mail: filotass@gmail.com
<span style=’color:#7f0055; ‘>#</span><span style=’color:#7f0055; ‘>include </span><span style=’color:#2a00ff; ‘><</span><span style=’color:#3f3fbf; ‘>malloc.h</span><span style=’color:#2a00ff; ‘>></span><span style=’color:#7f0055; ‘> </span><span style=’color:#3f7f59; ‘>//Library for dynamic memory structures</span>
<span style=’color:#3f7f59; ‘>//Operations that can be executed on the Heap structure</span>
<span style=’color:#7f0055; font-weight:bold; ‘>void</span> setStackProperties(<span style=’color:#7f0055; font-weight:bold; ‘>int</span>* &<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>, <span style=’color:#7f0055; font-weight:bold; ‘>int</span>& max_size, <span style=’color:#7f0055; font-weight:bold; ‘>int</span>& n);
<span style=’color:#7f0055; font-weight:bold; ‘>void</span> showStack(<span style=’color:#7f0055; font-weight:bold; ‘>int</span>* <span style=’color:#7f0055; font-weight:bold; ‘>stack</span>, <span style=’color:#7f0055; font-weight:bold; ‘>int</span> n, <span style=’color:#7f0055; font-weight:bold; ‘>int</span> max_size);
<span style=’color:#7f0055; font-weight:bold; ‘>void</span> push (<span style=’color:#7f0055; font-weight:bold; ‘>int</span>* &<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>, <span style=’color:#7f0055; font-weight:bold; ‘>int</span>& n, <span style=’color:#7f0055; font-weight:bold; ‘>int</span> max_size);
<span style=’color:#7f0055; font-weight:bold; ‘>void</span> pop(<span style=’color:#7f0055; font-weight:bold; ‘>int</span>* &<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>, <span style=’color:#7f0055; font-weight:bold; ‘>int</span>& n);
<span style=’color:#3f7f59; ‘>//Main program</span>
<span style=’color:#7f0055; font-weight:bold; ‘>int</span> <span style=’color:#7f0055; font-weight:bold; ‘>main</span>(<span style=’color:#7f0055; font-weight:bold; ‘>void</span>)
{
<span style=’color:#7f0055; font-weight:bold; ‘>int</span>* <span style=’color:#7f0055; font-weight:bold; ‘>stack</span>; <span style=’color:#3f7f59; ‘>//stack (dynamic) structure</span>
<span style=’color:#7f0055; font-weight:bold; ‘>int</span> max_size; <span style=’color:#3f7f59; ‘>//max capacity of items on stack</span>
<span style=’color:#7f0055; font-weight:bold; ‘>int</span> n; <span style=’color:#3f7f59; ‘>//current number of items on stack</span>
<span style=’color:#7f0055; font-weight:bold; ‘>int</span> selection; <span style=’color:#3f7f59; ‘>//variable that stores the user’s selection</span>
setStackProperties(<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>,max_size,n);
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>1.Insert Item</span><span style=’color:#2a00ff; ‘>\n</span><span style=’color:#2a00ff; ‘>2.Export Item</span><span style=’color:#2a00ff; ‘>\n</span><span style=’color:#2a00ff; ‘>3.Show stack</span><span style=’color:#2a00ff; ‘>\n</span><span style=’color:#2a00ff; ‘>4.Reset stack</span><span style=’color:#2a00ff; ‘>\n</span><span style=’color:#2a00ff; ‘>5.Exit</span><span style=’color:#2a00ff; ‘>\n</span><span style=’color:#2a00ff; ‘>”</span>);
<span style=’color:#7f0055; font-weight:bold; ‘>do</span>{
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>>:</span><span style=’color:#2a00ff; ‘>”</span>);
scanf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>%d</span><span style=’color:#2a00ff; ‘>”</span>,&selection);
<span style=’color:#7f0055; font-weight:bold; ‘>if</span> (selection==1) {
push(<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>,n, max_size);
}<span style=’color:#7f0055; font-weight:bold; ‘>else</span> <span style=’color:#7f0055; font-weight:bold; ‘>if</span>(selection==2){
pop(<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>,n);
}<span style=’color:#7f0055; font-weight:bold; ‘>else</span> <span style=’color:#7f0055; font-weight:bold; ‘>if</span>(selection==3){
showStack(<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>,n,max_size);
}<span style=’color:#7f0055; font-weight:bold; ‘>else</span> <span style=’color:#7f0055; font-weight:bold; ‘>if</span>(selection==4){
free(<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>);
setStackProperties(<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>,max_size,n);
}
}
<span style=’color:#7f0055; font-weight:bold; ‘>while</span> (selection!=5);
<span style=’color:#7f0055; font-weight:bold; ‘>return</span> 0;
}
<span style=’color:#3f7f59; ‘>//Implementation of the operations that can be executed on a stack</span>
<span style=’color:#3f7f59; ‘>//Hint: Wherever you see the “&” operator it is a Call by Reference</span>
<span style=’color:#3f7f59; ‘>//otherwise it is a Call by Value and the variable is being duplicated</span>
<span style=’color:#7f0055; font-weight:bold; ‘>void</span> setStackProperties(<span style=’color:#7f0055; font-weight:bold; ‘>int</span>* &<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>, <span style=’color:#7f0055; font-weight:bold; ‘>int</span>& max_size, <span style=’color:#7f0055; font-weight:bold; ‘>int</span>& n)
{
n=0;
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>Give size of stack (max):</span><span style=’color:#2a00ff; ‘>”</span>);
<span style=’color:#7f0055; font-weight:bold; ‘>do</span>{
scanf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>%d</span><span style=’color:#2a00ff; ‘>”</span>,&max_size);
<span style=’color:#7f0055; font-weight:bold; ‘>if</span> (max_size <= 0)
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>Give a positive integer:</span><span style=’color:#2a00ff; ‘>”</span>);
}<span style=’color:#7f0055; font-weight:bold; ‘>while</span> (max_size<=0);
<span style=’color:#7f0055; font-weight:bold; ‘>stack</span> = (<span style=’color:#7f0055; font-weight:bold; ‘>int</span>*) calloc (max_size, <span style=’color:#7f0055; font-weight:bold; ‘>sizeof</span>(<span style=’color:#7f0055; font-weight:bold; ‘>int</span>));
<span style=’color:#7f0055; font-weight:bold; ‘>if</span> (<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>==<span style=’color:#7f0055; font-weight:bold; ‘>NULL</span>) exit (1);
}
<span style=’color:#7f0055; font-weight:bold; ‘>void</span> showStack(<span style=’color:#7f0055; font-weight:bold; ‘>int</span>* <span style=’color:#7f0055; font-weight:bold; ‘>stack</span>, <span style=’color:#7f0055; font-weight:bold; ‘>int</span> n,<span style=’color:#7f0055; font-weight:bold; ‘>int</span> max_size)
{
<span style=’color:#7f0055; font-weight:bold; ‘>int</span> i;
<span style=’color:#7f0055; font-weight:bold; ‘>for</span>(i=0;i<n;i++)
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>%d</span><span style=’color:#2a00ff; ‘> </span><span style=’color:#2a00ff; ‘>”</span>, <span style=’color:#7f0055; font-weight:bold; ‘>stack</span>[i]);
<span style=’color:#7f0055; font-weight:bold; ‘>for</span>(i=n;i<max_size;i++)
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>_ </span><span style=’color:#2a00ff; ‘>”</span>);
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>\n</span><span style=’color:#2a00ff; ‘>”</span>);
}
<span style=’color:#7f0055; font-weight:bold; ‘>void</span> push (<span style=’color:#7f0055; font-weight:bold; ‘>int</span>* &<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>, <span style=’color:#7f0055; font-weight:bold; ‘>int</span>& n, <span style=’color:#7f0055; font-weight:bold; ‘>int</span> max_size)
{
<span style=’color:#7f0055; font-weight:bold; ‘>int</span> item;
<span style=’color:#7f0055; font-weight:bold; ‘>if</span> (n < max_size){
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>Give Item: </span><span style=’color:#2a00ff; ‘>”</span>);
scanf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>%d</span><span style=’color:#2a00ff; ‘>”</span>,&item);
<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>[n]=item;
n+=1;
}<span style=’color:#7f0055; font-weight:bold; ‘>else</span>{
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>The stack is Full!</span><span style=’color:#2a00ff; ‘>\n</span><span style=’color:#2a00ff; ‘>”</span>);
}
}
<span style=’color:#7f0055; font-weight:bold; ‘>void</span> pop(<span style=’color:#7f0055; font-weight:bold; ‘>int</span>* &<span style=’color:#7f0055; font-weight:bold; ‘>stack</span>, <span style=’color:#7f0055; font-weight:bold; ‘>int</span>& n)
{
<span style=’color:#7f0055; font-weight:bold; ‘>int</span> item;
<span style=’color:#7f0055; font-weight:bold; ‘>if</span> (n>0){
n-=1;
item = <span style=’color:#7f0055; font-weight:bold; ‘>stack</span>[n];
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>Exported Item: </span><span style=’color:#2a00ff; ‘>%d</span><span style=’color:#2a00ff; ‘>\n</span><span style=’color:#2a00ff; ‘>”</span>,item);
}<span style=’color:#7f0055; font-weight:bold; ‘>else</span>{
printf(<span style=’color:#2a00ff; ‘>”</span><span style=’color:#2a00ff; ‘>Empty stack</span><span style=’color:#2a00ff; ‘>\n</span><span style=’color:#2a00ff; ‘>”</span>);
}
}
</pre>
#include <stdio.h> //Library for I/O #include <malloc.h> //Library for dynamic memory structures //Operations that can be executed on the Stack structure void setStackProperties(int* &stack, int& max_size, int& n); void showStack(int* stack, int n, int max_size); void push (int* &stack, int& n, int max_size); void pop(int* &stack, int& n); //Main program int main(void) { int* stack; //stack (dynamic) structure int max_size; //max capacity of items on stack int n; //current number of items on stack int selection; //variable that stores the user's selection setStackProperties(stack,max_size,n); printf("1.Insert Item\n2.Export Item\n3.Show stack\n4.Reset stack\n5.Exit\n"); do{ printf(">:"); scanf("%d",&selection); if (selection==1) { push(stack,n, max_size); }else if(selection==2){ pop(stack,n); }else if(selection==3){ showStack(stack,n,max_size); }else if(selection==4){ free(stack); setStackProperties(stack,max_size,n); } } while (selection!=5); return 0; } //Implementation of the operations that can be executed on a stack //Hint: Wherever you see the "&" operator it is a Call by Reference //otherwise it is a Call by Value and the variable is being duplicated void setStackProperties(int* &stack, int& max_size, int& n) { n=0; printf("Give size of stack (max):"); do{ scanf("%d",&max_size); if (max_size <= 0) printf("Give a positive integer:"); }while (max_size<=0); stack = (int*) calloc (max_size, sizeof(int)); if (stack==NULL) exit (1); } void showStack(int* stack, int n,int max_size) { int i; for(i=0;i<n;i++) printf("%d ", stack[i]); for(i=n;i<max_size;i++) printf("_ "); printf("\n"); } void push (int* &stack, int& n, int max_size) { int item; if (n < max_size){ printf("Give Item: "); scanf("%d",&item); stack[n]=item; n+=1; }else{ printf("The stack is Full!\n"); } } void pop(int* &stack, int& n) { int item; if (n>0){ n-=1; item = stack[n]; printf("Exported Item: %d\n",item); }else{ printf("Empty stack\n"); } }