-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuelink (1).cpp
More file actions
87 lines (72 loc) · 1.57 KB
/
queuelink (1).cpp
File metadata and controls
87 lines (72 loc) · 1.57 KB
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
#include<iostream>
using namespace std;
struct node{
int data;
struct node *next;
};
class queue{
node *front , *rear;
public:
queue(){
front=NULL;
rear=NULL;
}
~queue(){
node *temp=new node;
while(front!=NULL){
temp=front;
front=front->next;
delete temp;
}
}
void enqueue(int);
int dequeue();
void display();
bool isempty();
};
void queue::enqueue(int item){
node *curr=new(nothrow) node;
curr->data=item;
if(rear==NULL){
curr->next=rear;
rear=curr;
front=curr;
return;
}
curr->next=NULL;
rear->next=curr;
rear=curr;
}
int queue::dequeue(){
int x;
if(rear==NULL){
cout<<"queue is empty"<<endl;
return INT_MIN;
}
node *temp=new(nothrow) node;
if(temp==NULL){
cout<<"Failed"<<endl;
return INT_MIN;
}
temp=front;
x=temp->data;
front=front->next;
if(front==NULL) rear=NULL;
delete temp;
return x;
}
bool queue::isempty(){
return rear==NULL;
}
void queue::display(){
if(rear==NULL){
cout<<"queue is empty"<<endl;
return;
}
node* temp=front;
while(temp!=rear){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<endl;
}