Stack Queue and Circular Queue

This commit is contained in:
Anup Kumar Panwar 2016-08-09 15:50:07 +05:30
parent c0fba99df6
commit 045eb06531
17 changed files with 412 additions and 23 deletions

View File

@ -6,7 +6,8 @@ int main(){
cin>>n;
cout<<"Enter Number of indeces u want to rotate the array to left=\t";
cin>>k;
int a[n];cout<<"Enter elements of array=\t";
int a[n];
cout<<"Enter elements of array=\t";
for(int i=0;i<n;i++){
cin>>a[i];
}

View File

@ -92,7 +92,7 @@ int main()
switch (choice)
{
case 1 : cout<<"\nEnter the element to be inserted : ";
cin>>x;;
cin>>x;
insert(x); break;
case 2 : cout<<"\nEnter the element to be removed : ";
cin>>x;

View File

@ -0,0 +1,77 @@
#include<iostream>
using namespace std;
int queue[10];
int front=0;
int rear=0;
int count=0;
void push(int x)
{
if(count==10)
{
cout<<"\nOverflow";
}
else
{
queue[rear]=x;
rear=(rear+1)%10;
count++;
}
}
void pop()
{
if (front==rear)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\n"<<queue[front]<<" deleted";
front=(front+1)%10;
count--;
}
}
void show()
{
for (int i = 0; i<count; i++)
{
cout<<queue[(i+front)%10]<<"\t";
}
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. Print";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
push(x);
}
else if (ch==2)
{
pop();
}
else if (ch==3)
{
show();
}
}
while(ch!=0);
return 0;
}

View File

@ -15,7 +15,7 @@ void insert(int x)
node *t=start;
if (start!=NULL)
{
while(t->next!=NULL)
while(t->next!=NULL)
{
t=t->next;
}

78
Queue Using Array.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<iostream>
using namespace std;
int queue[10];
int front=0;
int rear=0;
void push(int x)
{
if(rear==10)
{
cout<<"\nOverflow";
}
else
{
queue[rear++]=x;
}
}
void pop()
{
if (front==rear)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\n"<<queue[front++]<<" deleted";
for (int i = front; i < rear; i++)
{
queue[i-front]=queue[i];
}
rear=rear-front;
front=0;
}
}
void show()
{
for (int i = front; i < rear; i++)
{
cout<<queue[i]<<"\t";
}
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. Print";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
push(x);
}
else if (ch==2)
{
pop();
}
else if (ch==3)
{
show();
}
}
while(ch!=0);
return 0;
}

View File

@ -0,0 +1,90 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *next;
};
node *front, *rear;
void push(int x)
{
if (rear==NULL)
{
node *n= new node;
n->val=x;
n->next=NULL;
rear=n;
front=n;
}
else
{
node *n = new node;
n->val=x;
n->next=NULL;
rear->next=n;
rear=n;
}
}
void pop()
{
if (rear==front)
{
cout<<"\nUnderflow";
}
else
{
node *t = front;
cout<<"\n"<<t->val<<" deleted";
front=front->next;
delete t;
}
}
void show()
{
node *t=front;
while(t!=NULL)
{
cout<<t->val<<"\t";
t=t->next;
}
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. Print";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
push(x);
}
else if (ch==2)
{
pop();
}
else if (ch==3)
{
show();
}
}
while(ch!=0);
return 0;
}

68
Stack Using Array.cpp Normal file
View File

@ -0,0 +1,68 @@
#include<iostream>
using namespace std;
int stack[10];
int top=0;
void push(int x)
{
if(top==10)
{
cout<<"\nOverflow";
}
else
{
stack[top++]=x;
}
}
void pop()
{
if (top==0)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\n"<<stack[--top]<<" deleted";
}
}
void show()
{
for (int i = 0; i < top; i++)
{
cout<<stack[i]<<"\n";
}
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. Print";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
push(x);
}
else if (ch==2)
{
pop();
}
else if (ch==3)
{
show();
}
}
while(ch!=0);
return 0;
}

View File

@ -0,0 +1,75 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *next;
};
node *top;
void push(int x)
{
node *n = new node;
n->val=x;
n->next=top;
top=n;
}
void pop()
{
if (top==NULL)
{
cout<<"\nUnderflow";
}
else
{
node *t = top;
cout<<"\n"<<t->val<<" deleted";
top=top->next;
delete t;
}
}
void show()
{
node *t=top;
while(t!=NULL)
{
cout<<t->val<<"\n";
t=t->next;
}
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. Print";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
push(x);
}
else if (ch==2)
{
pop();
}
else if (ch==3)
{
show();
}
}
while(ch!=0);
return 0;
}