-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.cpp
100 lines (91 loc) · 1.85 KB
/
queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//ime
#include<iostream>
using namespace std;
int insert(int queue[],int size,int &front,int &rear,int item){
if((rear==size-1 && front==0)|| front==rear+1){
cout<<"\noverflow..";
return 0;
}
else if(front==-1){
front++;
rear=front;
}
else if(rear==size-1)
rear=0;
else
rear++;
queue[rear]=item;
return 1;
}
int del(int queue[],int size,int &front,int &rear){
int item;
if(front==-1){
cout<<"\nunderflow.....";
return -1;
}
else{
item=queue[front];
if(front==rear){
front=-1;
rear=-1;
}
else if(front==size-1)
front=0;
else
front++;
}
return item;
}
//just for demo not working properly.......///////
void display(int queue[],int size,int front,int rear){
if(front==-1){
cout<<"\nqueue is empty......";
return;
}
else{
cout<<queue[front]<<"<-";
while(front!=rear){
if(front==size-1)
front=0;
else
front++;
}
cout<<queue[front];
}
}
int main(){
int size;
cout<<"\nsize of your queue:";
cin>>size;
int queue[size]={-1};
int ch,front=-1,rear=-1;
do{
cout<<"\nwant to insert elements(1)."
<<"\nwant to delete elements(2)."
<<"\nwant to dispaly elements(3).\n";
cin>>ch;
switch(ch){
case 1 :int item,k;
cout<<"\nenter item to be inserted:";
cin>>item;
k=insert(queue,size,front,rear,item);
if(k==0)
cout<<"\nsorry item not inserted:"<<item;
else
cout<<"\nitem inserted:"<<k;
break;
case 2 :int ret;
ret=del(queue,size,front,rear);
if(ret==-1)
cout<<"\nsorry...queue ids empty";
else
cout<<"\nitem deleted:"<<ret;
break;
case 3 :display(queue,size,front,rear);
break;
default:cout<<"\nno such command>>...";
break;
}
}while(ch==1 || ch==2||ch==3);
return 0;
}