Stack Implementation using Linked List

Discussion in 'C++' started by seeguna, Jun 20, 2007.

  1. 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);
    		}
    		
        }      
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    clrscr(); means it will not compile in the MS compiler. :eek:
     
  3. seeguna

    seeguna New Member

    Joined:
    Jun 20, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Technical Consultant
    Location:
    Chennai
    I agree ur point (i.e clrscr() function not worked in MS Compiler)but I just run that program in Turbo compiler.
    Regards
    Guna
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  5. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    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()
    {

    }
     
  6. seeguna

    seeguna New Member

    Joined:
    Jun 20, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Technical Consultant
    Location:
    Chennai
    Ya........ I accepted
    but it is for beginners.....
     
  7. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    In my compiler it is showing....
    Code:
    General Protection Exception
    MYSTACK.C 46
    MYSTACK(2) 0x23E7:0x00D0 Processor Fault
    
    Can anyone tell me why??? :(
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  9. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    this program of stack using link list is very helpful for me. It is help full for the beginners..........
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice