Skip to content

Commit 83de67f

Browse files
authored
Update Queues using LL
1 parent 989fa8f commit 83de67f

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

Course 2 - Data Structures in JAVA/Lecture 10 - Queues/Queues using LL

+95
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,98 @@ Sample Output 2:
6262
-1
6363
1
6464
*/
65+
/*
66+
Following is the structure of the node class for a Singly Linked List
67+
68+
class Node {
69+
int data;
70+
Node next;
71+
72+
public Node(int data) {
73+
this.data = data;
74+
this.next = null;
75+
}
76+
77+
}
78+
79+
*/
80+
81+
public class Queue {
82+
83+
//Define the data members
84+
private Node front;
85+
private Node rear;
86+
private int size;
87+
88+
89+
public Queue() {
90+
//Implement the Constructor
91+
front=null;
92+
rear=null;
93+
size=0;
94+
}
95+
96+
97+
98+
/*----------------- Public Functions of Stack -----------------*/
99+
100+
101+
public int getSize() {
102+
//Implement the getSize() function
103+
return size;
104+
}
105+
106+
107+
public boolean isEmpty() {
108+
//Implement the isEmpty() function
109+
return size==0;
110+
}
111+
112+
113+
public void enqueue(int data) {
114+
//Implement the enqueue(element) function
115+
Node newNode=new Node(data);
116+
if (front==null)
117+
{
118+
front=newNode;
119+
rear=newNode;
120+
}
121+
else
122+
{
123+
rear.next=newNode;
124+
rear=newNode;
125+
}
126+
size=size+1;
127+
}
128+
129+
130+
public int dequeue() {
131+
//Implement the dequeue() function
132+
if (front!=null)
133+
{
134+
int temp=front.data;
135+
front=front.next;
136+
size=size-1;
137+
return temp;
138+
139+
}
140+
else
141+
{
142+
return -1;
143+
}
144+
}
145+
146+
147+
public int front() {
148+
//Implement the front() function
149+
if (front!=null)
150+
{
151+
return front.data;
152+
}
153+
else
154+
{
155+
return -1;
156+
}
157+
158+
}
159+
}

0 commit comments

Comments
 (0)