Sunday, December 6, 2020

6.Perform Stack operations using Linked List implementation

#include <iostream>

using namespace std;


struct node

{

int data;

node *next;

};

class stack

{

private:

node *top;

public:

stack()

{

top= NULL;

}

void push()


{

int a;

cout<<"\nEnter the element to PUSH: ";

cin>>a;

node *tmp=new node();

tmp->data=a;

tmp->next=top;

top=tmp;

}

void pop()

{

if(top==NULL)

{

cout<<"\nStack is Empty.";

}

else

{

node *temp=NULL;

temp=top;

cout<<"\nPoped element is "<<temp->data;

top=top->next;

delete temp;

}

}

void display()

{

cout<<"\nThe Stack is:\n";

node *h=top;

cout<<"\n";

if(h==NULL)

cout<<"\nStack is Empty.";

while(h!=NULL)

{

cout<<" "<<h->data;

h=h->next;

}

}


~stack()

{

cout<<"\nThank u...";

}


};

int main()

{

stack ob;

int ch;


do

{

    cout<<"\n ____STACK OPERATION USING LISNKED LIST____";

cout<<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT";

cout<<"\nEnter ur Choice: ";

cin>>ch;

switch(ch)

{

case 1:

ob.push();

break;


case 2:

ob.pop();

break;


case 3:

ob.display();

break;


case 4:

cout<<"\nEXIT";

break;


default:

cout<<"\nSorry! Invalid Choice";

}


}

while(ch!=4);

return 0;

}

No comments:

Post a Comment