Stack
#include <iostream>
using namespace std;
struct node {
int data;
node *next;
node *back;
};
node *start;
node *top;
node *newn;
node *temp;
void Push(int x ){
newn = new( node);
newn->data = x;
newn->next = NULL;
newn->back = NULL;
if(start== NULL){
start = newn;
top = start;
} else{
top->next = newn;
newn->back = top;
top = newn;
}
}
void Options(){
cout<< "Select an option:"<<endl;
cout<< "\t1. Push."<<endl;
cout<< "\t2. Pop."<<endl;
cout<< "\t3. Show."<<endl;
cout<< "\t4. Exit."<<endl;
}
void Pop(){
if(start == NULL){
cout<< "Stack Empty. . ."<<endl;
} else{
cout<< "Pop value is: "<<top->data<<endl<<endl;
top = top->back;
top->next = NULL;
}
}
void Show(){
cout<< "The data are: "<<endl;
while(temp != NULL){
cout<<temp->data<< ", ";
temp = temp->next;
}
}
void main(){
int opt, inp;
start = NULL;
do{
Options();
cin>>opt;
system( "CLS");
switch (opt){
case 1:
cout<< "Enter data: ";
cin>>inp;
Push(inp);
break;
case 2:
Pop();
break;
case 3:
Show();
break;
case 4:
break;
default:
cout<< "Wrong option"<<endl;
}
} while(opt != 4);
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment