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);
        }
       
    }     
}
Go4Expert Founder
20Jun2007,18:17   #2
shabbir's Avatar
clrscr(); means it will not compile in the MS compiler.
Go4Expert Member
21Jun2007,11:14   #3
seeguna's Avatar
I agree ur point (i.e clrscr() function not worked in MS Compiler)but I just run that program in Turbo compiler.
Regards
Guna
Go4Expert Founder
21Jun2007,11:29   #4
shabbir's Avatar
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.
Go4Expert Member
27Jul2007,12:30   #5
Shishir191's Avatar
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()
{

}
Go4Expert Member
27Jul2007,12:40   #6
seeguna's Avatar
Ya........ I accepted
but it is for beginners.....
Contributor
30Nov2008,12:31   #7
back from retirement's Avatar
In my compiler it is showing....
Code:
General Protection Exception
MYSTACK.C 46
MYSTACK(2) 0x23E7:0x00D0 Processor Fault
Can anyone tell me why???
Mentor
30Nov2008,17:35   #8
xpi0t0s's Avatar
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?
Banned
4Dec2008,18:08   #9
hkp819's Avatar
this program of stack using link list is very helpful for me. It is help full for the beginners..........