Stack Implementation using Linked list

seeguna's Avatar author of Stack Implementation using Linked list
This is an article on Stack Implementation using Linked list in C++.
A Simple program for implementing Linked Stack

Code: Cpp
#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);
        }
       
    }     
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
clrscr(); means it will not compile in the MS compiler.
0
seeguna's Avatar
Go4Expert Member
I agree ur point (i.e clrscr() function not worked in MS Compiler)but I just run that program in Turbo compiler.
Regards
Guna
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
Shishir191's Avatar, Join Date: Jul 2007
Go4Expert Member
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()
{

}
0
seeguna's Avatar
Go4Expert Member
Ya........ I accepted
but it is for beginners.....
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
In my compiler it is showing....
Code:
General Protection Exception
MYSTACK.C 46
MYSTACK(2) 0x23E7:0x00D0 Processor Fault
Can anyone tell me why???
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?
0
hkp819's Avatar
Banned
this program of stack using link list is very helpful for me. It is help full for the beginners..........