-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
executable file
·113 lines (92 loc) · 1.96 KB
/
queue.cpp
File metadata and controls
executable file
·113 lines (92 loc) · 1.96 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* File: main.cpp
* Author: ngupta94
*
* Created on January 21, 2014, 11:53 AM
*/
#include <cstdlib>
#include <iostream>
using namespace std;
struct node //one element of stack
{
int data; //data item
node* next; //pointer to next link
};
class queue //a list of links
{
private:
node* head; //pointer to first link
public:
queue() //no-argument constructor
{
head = NULL;
} //no first link
void enqueue(int d); //add data item (one link)
void display(); //display all link
int dequeue();
};
void queue::enqueue(int d) //add data item
{
node *newNode = new node;
newNode->data = d;
newNode->next = head;
head = newNode;
return;
}
int queue::dequeue() {
node *tmp, *cur;
int d;
if (head == NULL) {
cout << "List Empty " << endl;
return(-1);
} else {
cur = head;
while (head != NULL) {
tmp = head;
d = head->data;
head = head->next;
}
delete tmp;
head = cur;
}
return(d);
}
void queue::display() //display all links
{
node* current = head; //set ptr to first link
while (current != NULL) //quit on last link
{
cout << current->data << endl; //print data
current = current->next; //move to next link
}
}
/*
*
*/
int main(int argc, char** argv) {
queue q1;
int ret;
q1.enqueue(56);
ret = q1.dequeue();
if (ret >=0 ) {
cout << "dequeue = " << ret << endl;
}
ret = q1.dequeue();
if (ret >=0 ) {
cout << "dequeue = " << ret << endl;
}
q1.enqueue(17);
q1.enqueue(54);
q1.enqueue(23);
q1.enqueue(87);
ret = q1.dequeue();
if (ret >=0 ) {
cout << "dequeue = " << ret << endl;
}
ret = q1.dequeue();
if (ret >=0 ) {
cout << "dequeue = " << ret << endl;
}
q1.display();
return 0;
}