Data Structures

Stacks, Queues and Linked Lists

Queue

#HeaderFile
const int MAX = 10;
class CQueue

{

private:

int front;

int rear;

int arr[MAX];

public:

CQueue();

void enqueue(int el);

int dequeue();

bool isEmpty;

void display();
 };
 CQueue::CQueue()

{

front = rear = -1;

}
 void CQueue::enqueue(int el)

{

if (front == -1)

{

front = rear = 0;

arr[rear] = el;

}

else if (front == 0 && rear < MAX - 1)

{

rear++; arr[rear] = el;

}

else if (front <= rear&&rear < MAX - 1)

{

rear++; arr[rear] = el;

}

else if (front>0 && rear == MAX - 1)

{

rear = 0; arr[rear] = el;

}

else if (rear < front1&&front - rear>1)

{

rear++; arr[rear] = el;

}

else if (front - rear == 1 || (front1== && rear == MAX - 1));

cout << "Queue is Full" << endl;

}

};




#Driver.cpp

#include <iostream>
 #include "CQueue.h"

using namespace std;

int main()
 {
 int choice;
 int n=1;
 int index=0;
 CQueue q;
 while(n!=-1){
 cout << "\n 1- Enqueue \n 2- Dequeue \n 3- Exit \n 4- Is Empty \n 5- Display \n" << endl;
 cin>>choice;
 switch(choice){
 case 1:
 int value;
 cout<<"\n Enter the element that you want to enter: ";
 cin>>value;
 q.enqueue(value);
 break;
 case 2:
 // if(q.dequeue()!=0){
 cout<<"\n The following element has been removed from the Queue: "<<q.dequeue();
 //}
 break;
 case 3:
 n=-2;
 break;
 case 4:
 cout<<q.isEmpty();
 break;
 case 5:
 q.display();
 break;
 }
 n++;
 }
 return 0;
 }

Stack

#HeaderFile
#include <iostream>
 #include <Stack.h>

const int maximum=50;

using namespace std;

class Stack{
 private: int arr[maximum];
 private: int top=-1;

public:
 Stack();
 void push(int element);
 int pop();
 int peek();
 bool isFull();

Stack(){
 top=-1;
 }
 void push(int element){
 top++;
 if(top==50){
 cout<<"Stack is already full";

}
 else{
 arr[top]=element;
 }

}
 int pop(){
 if(top==-1){
 cout<<"Stack is already empty";
 }
 else{
 int element= arr[top];
 top--;
 return element;
 }




}
 int peek(){
 if (top==-1){
 cout<<"Stack is empty";
 return NULL;
 }
 else{
 int element = arr[top];
 return element;
 }




}
 void display(){
 if(top==-1){
 cout<<"Stack is empty";
 }
 else{
 for(int i=0;i<=top;i++){
 int element=arr[i];
 cout<<element<<endl;
 }
 }

}
 bool SisFull(){
 if(top==maximum-1){
 cout<<"Stack is full";
 return true;
 }
 else{
 return false;
 }
 }

};
#Driver.cpp
#include "Stack.h"
 #include<iostream>
 using namespace std;
 int main(){
 Stack S;
 int choice;
 int input;
 while(true){
 cout<<"Select from the menu"<<endl<<"1 Push"<<endl<<"2 pop"<<endl<<"3 peek"<<endl<<"4 Display"<<endl<<"5 is full"<<endl<<"6 Exit"<<endl;
 do{
 cout<<"Enter your choice";
 cin>>choice;
 }while(choice<1 || choice>6);
 switch(choice){
 case 1:
 cout<<"Enter an integer";
 cin>>input;
 break;
 case 2:
 cout<<"Popped element is"<< endl;
 cout<<S.pop();
 break;
 case 3:
 cout<<"Top most element is";
 cout<<S.peek();
 break;
 case 4:
 cout<<"Stack has folowing elements";
 S.display();
 break;
 case 5:
 cout<<S.SisFull();
 break;
 case6:
 // goto up:
 break;

}
 return 0;

}

}
Advertisements