You are on page 1of 11

STATIC STACK :

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h> //For exit()

#define max 10 //Or use : const int max = 10;

void push(int arr[], int &top)


{
int val;
if(top < (max-1))
{
cout << "\n\tEnter value : ";
cin >> val;
arr[++top] = val;
}
else
{
cout << "\n\tStack Full";
getch();
}
}

void pop(int arr[], int &top)


{
if(top != -1)
{
cout << "\n\t" << arr[top--] << " has been deleted";
}
else
{
cout << "\n\tStack is empty";
}
getch();
}

void showstack(int arr[], int top)


{
int i;
if(top != -1)
{
for(i=top; i>=0; i--)
{
cout << "\n\tElement " << i << " : " << arr[i];
}
}
else
cout << "\n\tStack is Empty";
getch();
}

void main()
{
int top = -1;
int arr[max];
int choice;

do
{
cout << "\n\n\t\t MAIN MENU";
cout << "\n\t\t-----------\n";
cout << "\n\t 1. Push";
cout << "\n\t 2. Pop";
cout << "\n\t 3. Show";
cout << "\n\t 4. Exit";
cout << "\n\n\t Enter Choice : ";

cin >> choice;

switch(choice)
{
case 1 : {
push(arr, top);
break;
}
case 2 : {
pop(arr, top);
break;
}
case 3 : {
showstack(arr, top);
break;
}
default : {
cout << "\n\tInvalid Input";
getch();
exit(0);
}
}
clrscr();
}while(choice != 4);

getch();
}
LINEAR CIRCULAR QUEUE :
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>

#define max 20

char queue[max];

int front, rear;


void add_q(char queue[], int front, char val, int &rear);
char del_q(char queue[], int &front, int rear);
void show_q(char queue[], int front, int rear);

void main()
{
int choice;
char val;
char opt = 'Y';

rear = -1;
front = -1;

clrscr();

do
{
cout << "\n\t\t MAIN MENU";
cout << "\n\t\t-----------";
cout << "\n\t 1. Addition of Queue";
cout << "\n\t 2. Deletion of Queue";
cout << "\n\t 3. Traverse of Queue";
cout << "\n\t 4. Exit";

cout << "\n\n\t Enter your choice : ";


cin >> choice;

clrscr();

switch(choice)
{
case 1 : {
do
{
cout << "\n\t Enter the value to be added : ";
cin >> val;
add_q(queue, front, val, rear);
cout << "\n\n\t Add more elements (Y / N) : ";
cin >> opt;
}while(toupper(opt) == 'Y');

break;
}
case 2 : {
opt = 'Y';
do
{
val = del_q(queue, front, rear);
if(val != -1)
{
cout << "\n\t Value deleted from Queue : " << val;
}
cout << "\n\n\t Delete more elements (Y / N) : ";
cin >> opt;
}while(toupper(opt) == 'Y');

break;
}
case 3 : {
show_q(queue, front, rear);
break;
}
case 4 : {
exit(0);
}
default : {
cout << "\n\tInvalid Input";
getch();
exit(0);
}
}
clrscr();
}while(choice != 4);
}

void add_q(char queue[], int front, char val, int &rear)


{
if((rear + 1) % max == front)
{
cout << "\n\tQueue is full\n";
}
else
{
rear = (rear + 1) % max;
queue[rear] = val;
}
}
char del_q(char queue[], int &front, int rear)
{
char value;
if(front == rear)
{
cout << "\n\tQueue is Empty\n";
value = -1;
}
else
{
front = (front + 1) % max;
value = queue[front];
}

return(value);
}

void show_q(char queue[], int front, int rear)


{
clrscr();
if(front == rear)
{
cout << "\n\tQueue is Empty\n";
getch();
}
else
{
cout << "\n\n\tThe values are : ";
do
{
front = (front + 1) % max;
cout << '\t' << queue[front];
}while(front != rear);
}
}
DYNAMIC STACK :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h> //For exit();

struct node
{
int data;
node *next;
};

class stack
{
node *top;

public:
stack()
{
top = NULL;
}
void push();
void pop();
void disp();
~stack();
};

void stack :: push()


{
node *temp;
temp = new node;

cout << "\n\tEnter Data : ";


cin >> temp->data;

temp->next = top;
top = temp;
}

void stack :: pop()


{
if(top != NULL)
{
node *temp = top;
cout << "\n\t" << top->data << " has been deleted\n";
top = top->next;
delete temp;
}
else
{
cout << "\n\tStack is empty";
}
getch();
}

void stack :: disp()


{
node *temp = top;

while(temp != NULL)
{
cout << '\t' << temp->data;
temp = temp->next;
}
getch();
}

stack :: ~stack() //Destructor function


{
while(top != NULL)
{
node *temp = top;
top = top->next;
delete temp;
}
}

void main()
{
stack st;
int choice;

do
{
cout << "\n\n\t\t MAIN MENU";
cout << "\n\t\t-----------\n";
cout << "\n\t 1. Push";
cout << "\n\t 2. Pop";
cout << "\n\t 3. Display";
cout << "\n\t 4. Exit";
cout << "\n\n\t Enter choice : ";
cin >> choice;

switch(choice)
{
case 1 : {
st.push();
break;
}
case 2 : {
st.pop();
break;
}
case 3 : {
st.disp();
break;
}
default : {
cout << "Invalid Input";
getch();
exit(0);
}
}
clrscr();
}while(choice != 4);
} /*Destructor function will be called automatically
when the scope of the object gets over*/
DYNAMIC QUEUE :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h> //For exit();

struct node
{
int data;
node *next;
};

class queue
{
node *rear, *front;

public:

queue()
{
rear = NULL;
front = NULL;
}

void qinsert();
void qdelete();
void qdisplay();
~queue();
};

void queue :: qinsert()


{
node *temp;
temp = new node;
cout << "\n\t Enter Data : ";
cin >> temp->data;

temp->next = NULL;
if(rear == NULL)
{
rear = temp;
front = temp;
}
else
{
rear->next = temp;
rear = temp;
}
}
void queue :: qdelete()
{
if(front != NULL)
{
node *temp = front;
cout << "\n\t" << front->data << " has been deleted";

front = front->next;
delete temp;

if(front == NULL)
{
rear = NULL;
}
}
else
{
cout << "\n\t" << "Queue is empty";
}
getch();
}

void queue :: qdisplay()


{
node *temp = front;
cout << endl;

while(temp != NULL)
{
cout << '\t' << temp->data;
temp = temp->next;
}
getch();
}

queue :: ~queue()
{
while(front != NULL)
{
node *temp = front;
front = front->next;
delete temp;
}
}

void main()
{
queue qu;
int choice;
do
{
cout << "\n\n\t\t MAIN MENU";
cout << "\n\t\t-----------\n";
cout << "\n\t 1. Insert";
cout << "\n\t 2. Delete";
cout << "\n\t 3. Display";
cout << "\n\t 4. Exit";
cout << "\n\n\t Enter choice : ";
cin >> choice;

switch(choice)
{
case 1 : {
qu.qinsert();
break;
}
case 2 : {
qu.qdelete();
break;
}
case 3 : {
qu.qdisplay();
break;
}
default : {
cout << "Invalid Input";
getch();
exit(0);
}
}
clrscr();
}while(choice != 4);
} /*Destructor function will be called automatically
when the scope of the object gets over*/

You might also like