#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; class node { public: node *next; int data; }; int n; class stack : public node { node *head; int tos,x; public: stack() { cout<<"\n\t\tSTACKS USING SINGLE LINKED LISTS\n"; tos=-1; cout<<"\n\nENTER THE STACK SIZE : "; cin>>n; } void push() { if(tos < 0 ) { cout <<"\n\nenter a element : "; cin >> x; head =new node; head->next=NULL; head->data=x; tos ++; } else { node *temp,*temp1; temp=head; if(tos >= (n-1)) { cout <<"\n\nstack over flow\n"; return; } cout <<"\n\nenter a element : "; cin >> x; while(temp->next != NULL) temp=temp->next; temp1=new node; temp->next=temp1; temp1->next=NULL; temp1->data=x; tos++; } } void display() { node *temp; temp=head; if (tos < 0) { cout <<"\nSTACK IS EMPTY\n"; return; } cout<<"\n\nThe elements are : "; while(temp != NULL) { cout <<temp->data<< " "; temp=temp->next; } cout<<"\n"; } void pop() { node *temp; temp=head; if( tos < 0 ) { cout <<"\nStack under flow\n"; return; } if(tos==0) { cout<<"\n\nThe removed element is :"<<temp->data; temp->next=NULL; tos--; return ; } if(tos>0) { while(temp->next->next!=NULL) { temp=temp->next; } if(temp->next->next==NULL) { cout<<"\n\nthe removed element is :"<<temp->next->data; } temp->next=NULL; tos--; return ; } } }; main() { stack s1; int x; while(1) { cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ur choice:"; cin>>x; switch(x) { case 1: s1.push(); break; case 2: s1.pop(); break; case 3: s1.display(); break; case 4: exit(0); default: cout<<"\ninvalid input..try again\n"; break; } } return 0; }
Labels
Popular Posts
-
Binary Tree: A Tree in which each node has a degree of atmost 2. i.e. it can have either 0,1 or 2 children. Here, leaves a...
-
BUBBLE SORT : Bubble sort is a simple sorting algorithm ...
-
Keywords – II Enum : An enum type is a special data type that enables for a variable to be a set of predefined co...
Blog Archive
- March 2017 (1)
- March 2016 (1)
- October 2015 (1)
- September 2015 (1)
- August 2015 (5)
- July 2015 (2)
- June 2015 (4)
- January 2015 (4)
- December 2014 (5)
- November 2014 (1)
- October 2014 (1)
- September 2014 (3)
Powered by Blogger.