Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

how to creat circular qeue in c++

#include<iostream>
using namespace std;
#define size 5
int CQ[size], front = -1, rear = -1, iteam;
void insertion();
void deletion();
void display();
void main()
{
while (1){
int ch;
cout << "Enter your choice " << endl;
cout << "1)Insertion " << endl;
cout << "2)Deletion " << endl;
cout << "3)Dispaly" << endl;
cout << "4)Exit" << endl;
cin >> ch;
switch (ch)
{
case 1:
system("cls");
insertion();
break;
case 2:
system("cls");
deletion();
break;
case 3:
system("cls");
display();
break;
case 4:
system("cls");
exit(0);
break;
default:
cout << "invalid input" << endl;

}
}
system("pause");
}

void insertion()
{

if (front == -1)
{
front = 0;
}
rear = (rear + 1) % size;
cout << "Enter value" << endl;
cin >> iteam;
CQ[rear] = iteam;
}
void deletion()
{
if (front == -1 && rear == -1)
{
cout << " Circular Queue is Empty" << endl;
}
else
{
iteam = CQ[size];
front = (front + 1) % size;
}
}
void display()
{
if (front == -1 && rear == -1)
{
cout << " Circular Queue is Empty" << endl;
}
else
{
cout << "Values in Queue" << endl;
for (int i = front; i <= rear; i++)
{
cout << CQ[i] << endl;
}
}
}

how to push pop in stack

#include<iostream>
using namespace std;
void push(int);
void pop();
void view();
int top = -1;
int stack[5];
void main_of_stack();
void main_of_stack()
{
int w = 1;
while (w == 1)
{
int ch;
cout << "Enter your choice" << endl;  
cout << "1) push " << endl;
cout << "2) pop " << endl;
cout << "3) view" << endl;
cout << "4) Exit" << endl;
cin >> ch;
switch (ch)
{
case 1:
int n;
cout << "enter value you want to insert" << endl;
cin >> n;
push(n);
break;
case 2:
pop();
break;
case 3:
view();
break;
case 4:

w++;

break;
default:
cout << "invalid input" << endl;

}
}
}



void main()
{
main_of_stack();
}

void push(int x)
{
cout << "Push the value" << endl;
if (top >= 5)
{
cout << "Stack is full or over flow" << endl;
}
else
{
stack[top] = x;
top++;
}
}
void pop()
{
if (top == -1)
{
cout << "stack is empty or overflow" << endl;
}
else
{
top--;
}
}
void view()
{
if (top == -1)
{
cout << "empty stck" << endl;
}
else{
int cho;
cout << "1)top to bottom" << endl;
cout << "2)bottom to top" << endl;
cin >> cho;
switch (cho)
{
case 1:

for (int i = top; i >= 0; i--)
{
cout << stack[i] << endl;
}

break;
case 2:

for (int i = 0; i <= top; i++)
{
cout << stack[i] << endl;
}

break;

default:
main_of_stack();
}
}
}