Queue
#include <iostream>
using namespace std;
struct node {
int data;
node *next;
node *back;
};
/*
GLOBAL VARIABLES
*/
node *start;
node *top;
node *nptr;
node *tptr;
//FUNCTION THAT CAN CREAT AN INSERTATION A NEW NODE
void Push(){//this function will just create a new node and link tat to the previous just like in linklist
int i;
cout<< "Enter data: ";
cin>>i;
nptr = new( node);
nptr->data = i;
nptr->next = NULL;
nptr->back = NULL;
if(start== NULL){
start = nptr;
top = start;
} else{
top->next = nptr;
nptr->back = top;
top = nptr;
}
}
void Options(){
cout<< "Please select an option:"<<endl;
cout<< "\t1. Push."<<endl;
cout<< "\t2. Pop."<<endl;
cout<< "\t3. Show the full stack."<<endl;
cout<< "\t4. Exit."<<endl;
}
/*THIS IS WHERE THE CODE IS DIFFERENT FROM STACK*/
void Pop(){
if(start == NULL){ //simplly checking if the stack/linklist is empty or not
cout<< "Stack Empty. . ."<<endl;
}else{
cout<< "The top data is: "<<start->data<<endl<<endl;//AS THE METHOD IS FIFO THE POP BEGINS FROM THE START
if(start->next== NULL){
start = NULL;
} else{
start = start->next; //WE ARE ADVANCING THE START POSITION
start->back = NULL;
}
}
}
void Show(){
if(start== NULL){
cout<< "There is nothing to show"<<endl;
} else{
tptr = start;
cout<< "The data are: "<<endl;
while(tptr != NULL){
cout<<tptr->data<< ", ";
tptr = tptr->next;
}
cout<<endl;
}
}
void main(){
int opt;
start = NULL;
while(1){ // exit(0) incase of the user choose to exit the program so an infinity loop is not a concern
Options();
cin>>opt;
system( "CLS");
switch (opt){
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Show();
break;
case 4:
exit(0);
default:
cout<< "Sorry Wrong selection"<<endl;
}
}
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment