0% found this document useful (0 votes)
10 views5 pages

Dstack

Dynamic stack program in c++

Uploaded by

ahmedilyas5440
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Dstack

Dynamic stack program in c++

Uploaded by

ahmedilyas5440
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

using namespace std;


struct Node{
int data;
Node *ptr;
};
class DStack{
Node *top;
public:
DStack(){
top=NULL;
}
void push(int a){
Node *temp=new Node;
temp->data=a;
temp->ptr=NULL;
if(top==NULL){
top=temp;
}
else{
temp->ptr=top;
top=temp;
}

}
void display(){
if(top==NULL){
cout<<"Stack is underflow"<<endl;
return;
}
else{
Node *temp=top;
while(temp!=NULL){
cout<<temp->data;
cout<<""<<endl;
temp=temp->ptr;
}
}
}

int pop(){
if(top==NULL){
cout<<"Stack is underflow"<<endl;
return 0;
}
else{
Node *temp=top;
int num=temp->data;
top=temp->ptr;
delete temp;
return num;
}
}

void Search(){
int f=0;
if(top==NULL){
cout<<"Stack is underflow"<<endl;
return;
}
else{
Node *temp=top;
int b;
cout<<"Enter the value to search"<<endl;
cin>>b;
while(temp!=NULL){
if(temp->data==b){
cout<<"The value is found"<<endl;
f=1;
}
temp=temp->ptr;
}
}
if(f==0){
cout<<"The value is not found"<<endl;
}
}

void update(){
if(top==NULL){
cout<<"Stack is underflow"<<endl;
return;
}
else{
int f=0;
Node *temp=top;
int New,old;
cout<<"Enter the old value to update"<<endl;
cin>>old;
cout<<"Enter the new value to update"<<endl;
cin>>New;
while(temp!=NULL){
if(temp->data==old){
temp->data=New;
cout<<"The value is updated"<<endl;
f=1;
break;
}
temp=temp->ptr;
}
if(f==0){
cout<<"The value not found"<<endl;
}
}
}
};

int main(){
DStack s1,s2;
int ch;
while(true){
cout<<""<<endl;
cout<<"Menu "<<endl;
cout<<"1. push "<<endl;
cout<<"2. Display s1 "<<endl;
cout<<"3. Display s2 "<<endl;
cout<<"4. pop "<<endl;
cout<<"5. To Cut the top value of s1 and past in s2 "<<endl;
cout<<"6. Search "<<endl;
cout<<"7. Update "<<endl;
cout<<"8. Exit "<<endl;
cout<<"Enter the choice"<<endl;
cin>>ch;
switch(ch){
case 1:
cout<<"Enter the value to push"<<endl;
int val;
cin>>val;
s1.push(val);
break;
case 2:
s1.display();
break;
case 3:
s2.display();
break;
case 4:
s1.pop();
break;
case 5:
s2.push(s1.pop());
break;
case 6:
s1.Search();
break;
case 7:
s1.update();
break;
case 8:
return 0;
break;
default:
cout<<"Invalid input"<<endl;
break;
}
}

return 0;
}

You might also like