-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
75 lines (65 loc) · 1.49 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
};
class Queue{
node *head;
public:
Queue();
void enqueue(int num);
void dequeue();
void display();
};
Queue::Queue(){head = NULL;}
void Queue::enqueue(int num){
if (head == NULL){
node *temp = new node;
temp->data = num;
temp->next = NULL;
head = temp;
}
else{
node *temp = head;
while(temp->next != NULL){
temp = temp->next;
}
node* newnode = new node;
newnode->data = num;
newnode->next = NULL;
temp->next = newnode;
}
}
void Queue::dequeue(){
if(head->next == NULL){
cout<<head->data<<endl;
head->data =0;
head->next = NULL;
delete head;
}
else{
node *temp = head;
head = head->next;
cout<<temp->data<<endl;
temp->data = 0;
temp->next = NULL;
delete temp;
}
}
void Queue::display(){
cout<<head->data<<endl;
}
int main(){
Queue aqueue;
aqueue.enqueue(10);aqueue.enqueue(20);aqueue.enqueue(30);aqueue.enqueue(40);aqueue.enqueue(50);
aqueue.dequeue();aqueue.dequeue();aqueue.dequeue();
aqueue.display();
aqueue.dequeue();aqueue.dequeue();
return 0;
}