Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<stdlib.h>
- using namespace std;
- class node
- {
- public:
- class node *next;
- int data;
- };
- class stack : public node
- {
- node *head;
- int tos;
- public:
- stack()
- {
- tos=-1;
- }
- void push(int x)
- {
- if (tos < 0 )
- {
- head =new node;
- head->next=NULL;
- head->data=x;
- tos ++;
- }
- else
- {
- node *temp,*temp1;
- temp=head;
- if(tos >= 100)
- {
- cout <<"stack over flow";
- return;
- }
- tos++;
- while(temp->next != NULL)
- temp=temp->next;
- temp1=new node;
- temp->next=temp1;
- temp1->next=NULL;
- temp1->data=x;
- }
- }
- int count()
- { node *temp;
- temp=head;
- int c=0;
- if (tos < 0)
- {
- cout <<" stack under flow";
- return 0;
- }
- while(temp != NULL)
- {
- c++;
- temp=temp->next;
- }
- return c;
- }
- void display()
- {
- node *temp;
- temp=head;
- if (tos < 0)
- {
- cout <<" stack under flow";
- return;
- }
- while(temp != NULL)
- {
- cout <<temp->data<< " ";
- temp=temp->next;
- }
- }
- void pop()
- {
- node *temp;
- temp=head;
- if( tos < 0 )
- {
- cout <<"stack under flow";
- return;
- }
- tos--;
- while(temp->next->next!=NULL)
- {
- temp=temp->next;
- }
- temp->next=NULL;
- }
- void SplitStack()
- { stack s2;stack s3; // temp stacks
- while(head!=NULL)
- {
- int temp = head->data;
- if(temp%2==0)
- s2.push(temp);
- else
- s3.push(temp);
- head=head->next;
- }
- cout<<"Stack S2 (Even) : "<<endl;
- s2.display();
- cout<<endl<<"Stack S3 (Odd) : "<<endl;
- s3.display();
- }
- };
- main()
- {
- stack s1;
- s1.push(70); s1.push(25); s1.push(30); s1.push(15); s1.push(0); s1.push(11);
- s1.display();
- cout<<endl<<"The Number of Data in Stack : " << s1.count() <<endl;
- s1.pop();s1.pop();
- s1.push(66); s1.push(77); s1.push(3);
- s1.SplitStack();
- system("pause");
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement