Untitled

 avatar
unknown
plain_text
a year ago
1.8 kB
13
Indexable
#include<iostream>
using namespace std;

struct node{
    int data;
    node*next;
};
node*top=0,*newnode;

void display(){
if(top==0){
    cout<<"Stack is empty"<<endl;
}
else{
    node*temp=top;
    cout<<"Stack is : ";
    while(temp!=0){
        cout<<temp->data<<" ";
        temp=temp->next;
    }
}cout<<endl;
}
void push(){
newnode=(node*)malloc(sizeof(node));
cout<<"enter element: ";
cin>>newnode->data;
newnode->next=top;
top=newnode;
display();
}

void pop(){
if(top==0){
    cout<<"Stack is empty(Underflow Condition)"<<endl;
}
else{
    cout<<"Deleted element is : "<<top->data<<endl;
    top=top->next;
    display();
}
}

void peek(){
if(top==0){
    cout<<"Stack is empty"<<endl;
}
else{
    cout<<"Topmost element is : "<<top->data<<endl;
    display();
}
}

void isEmptyOrFull(){
if(top==0){
    cout<<"Stack is empty"<<endl;
}
else if(newnode==NULL){
    cout<<"Stack is full"<<endl;
    display();
}
else{
    cout<<"Stack is neither full nor empty"<<endl;
    display();
}
}


int main(){
int choice=1;
cout<<endl;
while(choice){
    cout<<endl;
    cout<<"\tMenu for Stack"<<endl;
    cout<<"1.Push "<<endl;
    cout<<"2.Pop"<<endl;
    cout<<"3.Peek"<<endl;
    cout<<"4.Empty or Full"<<endl;
    cout<<"5.Exit"<<endl;
    cout<<"enter your choice: "<<endl;
    cin>>choice;
    if(choice==5){
        cout<<"Exit of program"<<endl;
        break;
    }
    else{
        switch(choice){
            case 1: push();
            break;
            case 2:pop();
            break;
            case 3:peek();
            break;
            case 4:isEmptyOrFull();
            break;
            default: cout<<"Invalid choice"<<endl;
        }
    }
}
return 0;
}
Editor is loading...