A Simple program for implementing Linked Stack Code: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int data; struct node *link; }; struct node *top=NULL,*temp; void main() { int choice,data; clrscr(); while(1)//infinite loop is used to insert/delete infinite number of nodes { printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n"); printf("\nEnter ur choice:"); scanf("%d",&choice); switch(choice) { case 1: temp=(struct node *)malloc(sizeof(struct node)); printf("Enter a node data :"); scanf("%d",&data); temp->data=data; temp->link=top; top=temp; break; case 2: if(top!=NULL) { printf("The poped element is %d",top->data); top=top->link; } else { printf("\nStack Underflow"); } break; case 3: temp=top; if(temp==NULL) { printf("\nStack is empty\n"); } while(temp!=NULL) { printf("->%d->",temp->data); temp=temp->link; } break; case 4: exit(0); } } }
I agree ur point (i.e clrscr() function not worked in MS Compiler)but I just run that program in Turbo compiler. Regards Guna
If you want it to run in MS compiler comment the clrscr line. Also why don't you get some standard compiler as TC is pretty much outdated now.
Hi, I think you should use functions for the operations of the stack. Becuase you have written every thing in the main , so its increased the size of main and it looks complex. With the help of using functions i think its very easy to debug or understand the code. Another suggestion is to use the template class so that Stack can be used as a generic. like void Push(Node **Base,int Val) //May be the parameters are different according to your logic but you should use the function. { } int Pop() { }
In my compiler it is showing.... Code: General Protection Exception MYSTACK.C 46 MYSTACK(2) 0x23E7:0x00D0 Processor Fault Can anyone tell me why???
What input did you give? (Include all data; both commands and node::data values given.) Did you change the code? Or do you mean the compiler itself crashed when compiling the code?
this program of stack using link list is very helpful for me. It is help full for the beginners..........