-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkedlist_imple_of_queue.cpp
96 lines (88 loc) · 1.61 KB
/
linkedlist_imple_of_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
#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
Node* front=NULL;
Node* rear=NULL;
//Time complexity should be O(1)
void Enqueue(int x)
{
Node* temp=new Node();
temp->data=x;
temp->next=NULL;
if(front==NULL && rear==NULL)
{
front=temp;
rear=temp;
return;
}
else
{
rear->next=temp;
rear=temp;
}
}
void Dequeue()
{
Node* temp=front;
if(front==NULL)
{
std::cout<<"Already empty cannot pop elements from it"<<"\n";
return;
}
else if(front==rear)
{
front=NULL;
rear==NULL;
}
else{
front=front->next;
}
delete(temp);
}
int Top(){
Node* temp=front;
if(temp->next!=NULL)
{std::cout<<"Currently the top most element of queue is"<<"\n";
std::cout<<temp->data<<"\n";}
else{
std::cout<<"empty queque "<<"\n";
}
}
bool isEmpty()
{ Node* temp=front;
if(temp->next==NULL)
return true;
else
return false;
}
void print( ){
Node* temp=front;
std::cout<<"Currently queue is as follows"<<"\n";
while(temp!=NULL)
{
std::cout<<temp->data<<" ";
temp=temp->next;
}
std::cout<<"\n";
}
int main(){
int top;
bool isempty;
Enqueue(2);
print();
Enqueue(5);
print();
Enqueue(10);
print();
Dequeue();
print();
Top();
isempty=isEmpty();
if(isempty==0)
std::cout<<"The stack is not empty"<<"\n";
else
std::cout<<"The stack is empty"<<"\n";
}