Skip to content

Commit 56860e8

Browse files
committed
Queue implementation using linked list
1 parent 80825d9 commit 56860e8

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Queue/Queue-intro.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
class Node // class to create a node
6+
{
7+
public:
8+
int data;
9+
Node *next;
10+
11+
Node(int val)
12+
{
13+
data = val;
14+
next = NULL;
15+
}
16+
};
17+
18+
class Queue
19+
{
20+
Node *head;
21+
Node *tail;
22+
23+
public:
24+
Queue()
25+
{
26+
head = tail = NULL;
27+
}
28+
29+
void push(int data) // to push data in queue
30+
{
31+
Node *newNode = new Node(data);
32+
33+
if (empty()) // if queue is empty
34+
{
35+
head = tail = newNode;
36+
}
37+
else
38+
{
39+
tail->next = newNode;
40+
tail = newNode;
41+
}
42+
}
43+
44+
void pop() // to pop date from queue
45+
{
46+
if (empty()) // if queue is empty
47+
{
48+
cout << "Queue is empty\n";
49+
return;
50+
}
51+
52+
Node *temp = head;
53+
head = head->next;
54+
delete temp;
55+
}
56+
57+
int front() // to return front value of queue
58+
{
59+
if (empty()) // if queue is empty
60+
{
61+
cout << "Queue is empty\n";
62+
return 0;
63+
}
64+
65+
return head->data;
66+
}
67+
68+
bool empty() // to check if queue is empty
69+
{
70+
return head == NULL;
71+
}
72+
};
73+
74+
int main()
75+
{
76+
Queue q;
77+
78+
q.push(1);
79+
q.push(2);
80+
q.push(3);
81+
82+
while (!q.empty())
83+
{
84+
cout << q.front() << " ";
85+
q.pop();
86+
}
87+
cout << endl;
88+
89+
return 0;
90+
}

Queue/Queue-intro.exe

45.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)