-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueusingLL.cpp
More file actions
80 lines (80 loc) · 1.53 KB
/
QueueusingLL.cpp
File metadata and controls
80 lines (80 loc) · 1.53 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
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
class queue{
public:
node* head;
node* tail;
int size;
queue(){
head=tail=NULL;
size=0;
}
void pushback(int val){
node* temp = new node(val);
if(size==0) head=tail=temp;
else{
tail->next=temp;
tail=temp;
}
size++;
}
void popfront(){
if(size==0) {
cout<<"queue is empty";
return;
}
// node* temp = head;
head = head->next;
// delete temp;
// size--;
// if(head == NULL) tail = NULL;
size--;
}
int front(){
if(size==0) {
cout<<"queue is empty";
return -1;
}
return head->data;
}
int rearorback(){
if(size==0) {
cout<<"queue is empty";
return -2;
}
return tail->data;
}
void display(){
node* temp = head;
while(temp){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
};
int main(){
queue q;
q.pushback(10);
q.pushback(20);
q.pushback(30);
q.pushback(80);
q.pushback(40);
q.pushback(50);
q.display();
q.popfront();
q.display();
q.rearorback();
q.display();
cout << "Front element: " << q.front() << endl;
cout << "Rear element: " << q.rearorback() << endl;
}