Tuesday, May 25, 2010

Source code For Stack implementation using linked list in C++

Stack using Linked list

#include "process.h"
#include "iostream.h"
struct node
{
int info;
node *next;
};

class stack
{
public :
node *top;
stack ()
{
top = NULL;
}
void push ();
void pop ();
void display (node *t);
};

void stack :: push ()
{
node *n;
n = new node;
cout << "enter the element \n”;
cin >> n->info;
n->next = top;
top = n;

}

void stack::pop()
{
node *n;
if (top == NULL)
cout << "stack is empty " <<< "the poped element is " n = top->next;
top = top->next;
}
}
void stack :: display(node *t)
{
cout<<" "if (t->next!=NULL)
display (t->next);
}
void main ()
{
stack s;
int choice;
while (1){
cout<<"1.push 2.pop 3.display 4.exit"<<'\n'; cin>>choice;
switch(choice)
{ case 1 : s.push (); break;
case 2 : s.pop (); break;
case 3 : s.display(s.top); break;
case 4 : exit(0);
}
}
}

0 Comments:

 
^ Scroll to Top