|
CPP Program To Implement Stack Using Single Linked List: |
|
#include
#include
#include
class stack
{
int element;
stack* next;
public:
stack* push(stack*,int);
stack* pop(stack*);
void stack_display(stack*);
}*head,object;
stack* stack::push(stack* head,int key)
{
stack* temp,*temp1;
temp1=head;
temp=new stack;
temp->element=key;
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
while(head->next!=NULL)
head=head->next;
head->next=temp;
head=temp1;
}
return head;
}
stack* stack::pop(stack* head)
{
stack* temp;
if(head!=NULL)
{
temp=head;
if(head->next==NULL)
{
cout<<"\nthe pooped element from the stack is: "<element<next->next!=NULL)
head=head->next;
cout<<"the popped element from the stack is "<next->element;
cout<next=head->next->next;
head=temp;
return head;
}
else
{
cout<<"\n\n STACK IS EMPTY ";
return head;
}
}
void stack::stack_display(stack* head)
{
if(head!=NULL)
{
while(head->next!=NULL)
{
cout<element;
head=head->next;
}
cout<element<<" --->TOP";
cout<>ch;
switch(ch)
{
case 1:
cout<<"\n\nENTER THE ELEMENT TO PUSH STACK : ";
cin>>key;
head=object.push(head,key);
object.stack_display(head);
break;
case 2:
head=object.pop(head);
object.stack_display(head);
break;
case 3:
exit(1);
default:
cout<<"\n\nInvalid choice...\n";
break;
}
}
}
void main()
{
choice();
}
SAMPLE INPUT AND OUTPUT:
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE : 1
ENTER THE FIRST ELEMENT : 10
10
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE: 2
ENTER THE NEXT ELEMENT: 30
10
30
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE: 3
DELETED ELEMENT IS 30
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE: 3
DELETED ELEMENT IS 10
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE: 3
STACK IS EMPTY.
|
|
⇓ Student Projects ⇓
⇑ Student Projects ⇑
|