Thursday, 23 October 2014

Stack(Linked-List)

#include<iostream>
#include<conio.h>
using namespace std;
int item;
struct stack
{
    int data;
    stack *link;
}*ptr,*my_TOP=NULL;
void push()
{
    cout<<"enter the item to be inserted"<<endl;
    cin>>item;
    if(my_TOP==NULL)
    {
    ptr=new stack;
    ptr->data=item;
    ptr->link=NULL;
    my_TOP=ptr;
    }
    else
    {
        ptr=new stack;
        ptr->data=item;
        ptr->link=my_TOP;
        my_TOP=ptr;
    }
}
void pop()
{
    ptr=my_TOP;
    my_TOP=ptr->link;
    delete ptr;
}
void display()
{
    ptr=my_TOP;
    while(ptr!=NULL)
    {
        cout<<ptr->data<<endl;
        ptr=ptr->link;
    }
}
int main()
{
    int option;
    while(option!=4)
    {
    cout<<"enter 1. to push \nenter 2. to display \nenter 3. to pop"<<endl;
    cin>>option;
    switch(option)
    {
        case 1: push();
        break;
        case 2: display();
        break;
        case 3: pop();
        break;
        default: cout<<"wrongly entered"<<endl;
    }
    }
    getch();
    return 0;
}

No comments:

Post a Comment