P1+FA+C

(vi FinalAssignment1.c) 1 # include  2 # include  3 struct stack 4 { 5 int data; 6 struct stack *link; 7 }; 8 struct stack *push(struct stack *p, int value) 9 { 10 struct stack *temp; 11 temp=(struct stack *)malloc(sizeof(struct stack)); 12 if(temp==NULL) 13 { 14 temp*2; 15 printf("No Memory available\n"); 16 exit(0); 17 } 18 temp->data = value; 19 temp->link = p; 20 p = temp; 21 return(p); 22 } 23 24 struct stack *pop(struct stack *p, int *value) 25 { 26 struct stack *temp; 27 if(p==NULL) 28 { 29 printf(" The stack is empty and cannot pop element\n"); 30 exit(0); 31 } 32 *value = p->data; 33 temp = p; 34 p = p->link; 35 free(temp); 36 return(p); 37 } 38 39 int main(void) 40 { 41 struct stack *top = NULL; 42 char command; 43 int n,value; 44 do 45 { 46 printf("Command:\n"); 47 scanf("%c", &command); 48 49 switch (command) 50 { 51 case '+': 52 printf("Enter the element to be pushed in stack\n"); 53 scanf("%d",&value); 54 top = push(top,value); 55 break; 56 57 case 'P': 58 top = pop(top,&value); 59 printf("The value poped is %d\n",value); 60 break; 61 62 case 'S': 63 printf("%i", value); 64 }} 65 while(command!='Q'); 66 67 return 0; 68 }

This is a good example... of a linked list acting like a stack. It shows stack-like behavior. Not a big deal that it only handles integers. Rik's main complaint this time? The name(s) of authors is nowhere to be found. I downloaded this, then had to go back to Moodle and see where it came from. Mr. FinalAssignment1.c will be credited 8-) PLEASE include your name(s) somewhere (in a comment, the output, or the fileName, for example). Next time it's a point-loser.