Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Stack Implementation using Linked list (http://www.go4expert.com/articles/stack-implementation-using-linked-list-t4772/)

seeguna 20Jun2007 16:12

Stack Implementation using Linked list
 
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);
        }
       
    }     
}


shabbir 20Jun2007 18:17

Re: Stack Implementation using Linked list
 
clrscr(); means it will not compile in the MS compiler. :eek:

seeguna 21Jun2007 11:14

Re: Stack Implementation using Linked list
 
I agree ur point (i.e clrscr() function not worked in MS Compiler)but I just run that program in Turbo compiler.
Regards
Guna

shabbir 21Jun2007 11:29

Re: Stack Implementation using Linked list
 
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.

Shishir191 27Jul2007 12:30

Re: Stack Implementation using Linked list
 
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()
{

}

seeguna 27Jul2007 12:40

Re: Stack Implementation using Linked list
 
Ya........ I accepted
but it is for beginners.....

back from retirement 30Nov2008 12:31

Re: Stack Implementation using Linked list
 
In my compiler it is showing....
Code:

General Protection Exception
MYSTACK.C 46
MYSTACK(2) 0x23E7:0x00D0 Processor Fault

Can anyone tell me why??? :(

xpi0t0s 30Nov2008 17:35

Re: Stack Implementation using Linked list
 
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?

hkp819 4Dec2008 18:08

Re: Stack Implementation using Linked list
 
this program of stack using link list is very helpful for me. It is help full for the beginners..........


All times are GMT +5.5. The time now is 15:51.