0% found this document useful (0 votes)
40 views

BDA Lab Experiment - Queues

This document describes implementing a singly linked list in Java. Key points: - A linked list is a linear data structure where each element points to the next. It allows efficient insertion/deletion anywhere. - The Node class defines each element with an integer data and Node link. - The linkedList class implements functions for insertion/deletion at various positions and displaying the list. - The main method demonstrates usage by prompting the user to insert/delete and displaying the list.

Uploaded by

4416 Likhitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

BDA Lab Experiment - Queues

This document describes implementing a singly linked list in Java. Key points: - A linked list is a linear data structure where each element points to the next. It allows efficient insertion/deletion anywhere. - The Node class defines each element with an integer data and Node link. - The linkedList class implements functions for insertion/deletion at various positions and displaying the list. - The main method demonstrates usage by prompting the user to insert/delete and displaying the list.

Uploaded by

4416 Likhitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Faculty Name: Ravula Kartheek M.Tech, (Ph.

D)
BIG DATA ANALYTICS LAB
EXPERIMENT 1: WEEK 1, 2:

1. Implement the following Data structures in Java


a) Linked Lists b) Stacks c) Queues d) Set e) Map

a) Linked Lists

Java Program to Implement Singly Linked List

A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the
simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the
sequence. This structure allows for efficient insertion or removal of elements from any position in the
sequence. In a singly linked list each node has only one link which points to the next node in the list.
1. /*
2. * Java Program to Implement Singly Linked List
3. */
4.
5. import java.util.Scanner;
6.
7. /* Class Node */
8. class Node
9. {
10. protected int data;
11. protected Node link;
12.
13. /* Constructor */
14. public Node()
15. {
16. link = null;
17. data = 0;
18. }
19. /* Constructor */
20. public Node(int d,Node n)
21. {
22. data = d;
23. link = n;
24. }
25. /* Function to set link to next Node */
26. public void setLink(Node n)
27. {
28. link = n;
29. }
30. /* Function to set data to current Node */
31. public void setData(int d)
32. {
33. data = d;
34. }
35. /* Function to get link to next node */
36. public Node getLink()
37. {
38. return link;
39. }
40. /* Function to get data from current Node */
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
41. public int getData()
42. {
43. return data;
44. }
45. }
46.
47. /* Class linkedList */
48. class linkedList
49. {
50. protected Node start;
51. protected Node end ;
52. public int size ;
53.
54. /* Constructor */
55. public linkedList()
56. {
57. start = null;
58. end = null;
59. size = 0;
60. }
61. /* Function to check if list is empty */
62. public boolean isEmpty()
63. {
64. return start == null;
65. }
66. /* Function to get size of list */
67. public int getSize()
68. {
69. return size;
70. }
71. /* Function to insert an element at begining */
72. public void insertAtStart(int val)
73. {
74. Node nptr = new Node(val, null);
75. size++ ;
76. if(start == null)
77. {
78. start = nptr;
79. end = start;
80. }
81. else
82. {
83. nptr.setLink(start);
84. start = nptr;
85. }
86. }
87. /* Function to insert an element at end */
88. public void insertAtEnd(int val)
89. {
90. Node nptr = new Node(val,null);
91. size++ ;
92. if(start == null)
93. {
94. start = nptr;
95. end = start;
96. }
97. else
98. {
99. end.setLink(nptr);
100. end = nptr;
101. }
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
102. }
103. /* Function to insert an element at position */
104. public void insertAtPos(int val , int pos)
105. {
106. Node nptr = new Node(val, null);
107. Node ptr = start;
108. pos = pos - 1 ;
109. for (int i = 1; i < size; i++)
110. {
111. if (i == pos)
112. {
113. Node tmp = ptr.getLink() ;
114. ptr.setLink(nptr);
115. nptr.setLink(tmp);
116. break;
117. }
118. ptr = ptr.getLink();
119. }
120. size++ ;
121. }
122. /* Function to delete an element at position */
123. public void deleteAtPos(int pos)
124. {
125. if (pos == 1)
126. {
127. start = start.getLink();
128. size--;
129. return ;
130. }
131. if (pos == size)
132. {
133. Node s = start;
134. Node t = start;
135. while (s != end)
136. {
137. t = s;
138. s = s.getLink();
139. }
140. end = t;
141. end.setLink(null);
142. size --;
143. return;
144. }
145. Node ptr = start;
146. pos = pos - 1 ;
147. for (int i = 1; i < size - 1; i++)
148. {
149. if (i == pos)
150. {
151. Node tmp = ptr.getLink();
152. tmp = tmp.getLink();
153. ptr.setLink(tmp);
154. break;
155. }
156. ptr = ptr.getLink();
157. }
158. size-- ;
159. }
160. /* Function to display elements */
161. public void display()
162. {
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
163. System.out.print("\nSingly Linked List = ");
164. if (size == 0)
165. {
166. System.out.print("empty\n");
167. return;
168. }
169. if (start.getLink() == null)
170. {
171. System.out.println(start.getData() );
172. return;
173. }
174. Node ptr = start;
175. System.out.print(start.getData()+ "->");
176. ptr = start.getLink();
177. while (ptr.getLink() != null)
178. {
179. System.out.print(ptr.getData()+ "->");
180. ptr = ptr.getLink();
181. }
182. System.out.print(ptr.getData()+ "\n");
183. }
184. }
185.
186. /* Class SinglyLinkedList */
187. public class SinglyLinkedList
188. {
189. public static void main(String[] args)
190. {
191. Scanner scan = new Scanner(System.in);
192. /* Creating object of class linkedList */
193. linkedList list = new linkedList();
194. System.out.println("Singly Linked List Test\n");
195. char ch;
196. /* Perform list operations */
197. do
198. {
199. System.out.println("\nSingly Linked List Operations\n");
200. System.out.println("1. insert at begining");
201. System.out.println("2. insert at end");
202. System.out.println("3. insert at position");
203. System.out.println("4. delete at position");
204. System.out.println("5. check empty");
205. System.out.println("6. get size");
206. int choice = scan.nextInt();
207. switch (choice)
208. {
209. case 1 :
210. System.out.println("Enter integer element to insert");
211. list.insertAtStart( scan.nextInt() );
212. break;
213. case 2 :
214. System.out.println("Enter integer element to insert");
215. list.insertAtEnd( scan.nextInt() );
216. break;
217. case 3 :
218. System.out.println("Enter integer element to insert");
219. int num = scan.nextInt() ;
220. System.out.println("Enter position");
221. int pos = scan.nextInt() ;
222. if (pos <= 1 || pos > list.getSize() )
223. System.out.println("Invalid position\n");
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
224. else
225. list.insertAtPos(num, pos);
226. break;
227. case 4 :
228. System.out.println("Enter position");
229. int p = scan.nextInt() ;
230. if (p < 1 || p > list.getSize() )
231. System.out.println("Invalid position\n");
232. else
233. list.deleteAtPos(p);
234. break;
235. case 5 :
236. System.out.println("Empty status = "+ list.isEmpty());
237. break;
238. case 6 :
239. System.out.println("Size = "+ list.getSize() +" \n");
240. break;
241. default :
242. System.out.println("Wrong Entry \n ");
243. break;
244. }
245. /* Display List */
246. list.display();
247. System.out.println("\nDo you want to continue (Type y or n)
\n");
248. ch = scan.next().charAt(0);
249. } while (ch == 'Y'|| ch == 'y');
250. }
251. }

III B.Tech (CSE - Data Science) R20


Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
OUTPUT:
Singly Linked List Test

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
5
Empty status = true

Singly Linked List = empty

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
5

Singly Linked List = 5

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
7

Singly Linked List = 7->5

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
2
Enter integer element to insert
4

Singly Linked List = 7->5->4

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
2
Enter integer element to insert
2

Singly Linked List = 7->5->4->2

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
9

Singly Linked List = 9->7->5->4->2

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
3
Enter integer element to insert
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
3
Enter position
3

Singly Linked List = 9->7->3->5->4->2

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
3
Enter integer element to insert
2
Enter position
2

Singly Linked List = 9->2->7->3->5->4->2

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
6
Size = 7

Singly Linked List = 9->2->7->3->5->4->2

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
4

Singly Linked List = 9->2->7->5->4->2


III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2

Singly Linked List = 9->7->5->4->2

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1

Singly Linked List = 7->5->4->2

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
3

Singly Linked List = 7->5->2

Do you want to continue (Type y or n)

Singly Linked List Operations

III B.Tech (CSE - Data Science) R20


Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1

Singly Linked List = 5->2

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2

Singly Linked List = 5

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1

Singly Linked List = empty

Do you want to continue (Type y or n)

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
5
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
Empty status = true

Singly Linked List = empty

Do you want to continue (Type y or n)

III B.Tech (CSE - Data Science) R20


Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
Java Program to Implement Doubly Linked List
A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the
simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the
sequence. This structure allows for efficient insertion or removal of elements from any position in the
sequence. In a doubly linked list each node has two links one pointing to the next node in the list and one
pointing to the previous node in the list.
1. /*
2. * Java Program to Implement Doubly Linked List
3. */
4.
5. import java.util.Scanner;
6.
7. /* Class Node */
8. class Node
9. {
10. protected int data;
11. protected Node next, prev;
12.
13. /* Constructor */
14. public Node()
15. {
16. next = null;
17. prev = null;
18. data = 0;
19. }
20. /* Constructor */
21. public Node(int d, Node n, Node p)
22. {
23. data = d;
24. next = n;
25. prev = p;
26. }
27. /* Function to set link to next node */
28. public void setLinkNext(Node n)
29. {
30. next = n;
31. }
32. /* Function to set link to previous node */
33. public void setLinkPrev(Node p)
34. {
35. prev = p;
36. }
37. /* Funtion to get link to next node */
38. public Node getLinkNext()
39. {
40. return next;
41. }
42. /* Function to get link to previous node */
43. public Node getLinkPrev()
44. {
45. return prev;
46. }
47. /* Function to set data to node */
48. public void setData(int d)
49. {
50. data = d;
51. }
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
52. /* Function to get data from node */
53. public int getData()
54. {
55. return data;
56. }
57. }
58.
59. /* Class linkedList */
60. class linkedList
61. {
62. protected Node start;
63. protected Node end ;
64. public int size;
65.
66. /* Constructor */
67. public linkedList()
68. {
69. start = null;
70. end = null;
71. size = 0;
72. }
73. /* Function to check if list is empty */
74. public boolean isEmpty()
75. {
76. return start == null;
77. }
78. /* Function to get size of list */
79. public int getSize()
80. {
81. return size;
82. }
83. /* Function to insert element at begining */
84. public void insertAtStart(int val)
85. {
86. Node nptr = new Node(val, null, null);
87. if(start == null)
88. {
89. start = nptr;
90. end = start;
91. }
92. else
93. {
94. start.setLinkPrev(nptr);
95. nptr.setLinkNext(start);
96. start = nptr;
97. }
98. size++;
99. }
100. /* Function to insert element at end */
101. public void insertAtEnd(int val)
102. {
103. Node nptr = new Node(val, null, null);
104. if(start == null)
105. {
106. start = nptr;
107. end = start;
108. }
109. else
110. {
111. nptr.setLinkPrev(end);
112. end.setLinkNext(nptr);
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
113. end = nptr;
114. }
115. size++;
116. }
117. /* Function to insert element at position */
118. public void insertAtPos(int val , int pos)
119. {
120. Node nptr = new Node(val, null, null);
121. if (pos == 1)
122. {
123. insertAtStart(val);
124. return;
125. }
126. Node ptr = start;
127. for (int i = 2; i <= size; i++)
128. {
129. if (i == pos)
130. {
131. Node tmp = ptr.getLinkNext();
132. ptr.setLinkNext(nptr);
133. nptr.setLinkPrev(ptr);
134. nptr.setLinkNext(tmp);
135. tmp.setLinkPrev(nptr);
136. }
137. ptr = ptr.getLinkNext();
138. }
139. size++ ;
140. }
141. /* Function to delete node at position */
142. public void deleteAtPos(int pos)
143. {
144. if (pos == 1)
145. {
146. if (size == 1)
147. {
148. start = null;
149. end = null;
150. size = 0;
151. return;
152. }
153. start = start.getLinkNext();
154. start.setLinkPrev(null);
155. size--;
156. return ;
157. }
158. if (pos == size)
159. {
160. end = end.getLinkPrev();
161. end.setLinkNext(null);
162. size-- ;
163. }
164. Node ptr = start.getLinkNext();
165. for (int i = 2; i <= size; i++)
166. {
167. if (i == pos)
168. {
169. Node p = ptr.getLinkPrev();
170. Node n = ptr.getLinkNext();
171.
172. p.setLinkNext(n);
173. n.setLinkPrev(p);
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
174. size-- ;
175. return;
176. }
177. ptr = ptr.getLinkNext();
178. }
179. }
180. /* Function to display status of list */
181. public void display()
182. {
183. System.out.print("\nDoubly Linked List = ");
184. if (size == 0)
185. {
186. System.out.print("empty\n");
187. return;
188. }
189. if (start.getLinkNext() == null)
190. {
191. System.out.println(start.getData() );
192. return;
193. }
194. Node ptr = start;
195. System.out.print(start.getData()+ " <-> ");
196. ptr = start.getLinkNext();
197. while (ptr.getLinkNext() != null)
198. {
199. System.out.print(ptr.getData()+ " <-> ");
200. ptr = ptr.getLinkNext();
201. }
202. System.out.print(ptr.getData()+ "\n");
203. }
204. }
205.
206. /* Class DoublyLinkedList */
207. public class DoublyLinkedList
208. {
209. public static void main(String[] args)
210. {
211. Scanner scan = new Scanner(System.in);
212. /* Creating object of linkedList */
213. linkedList list = new linkedList();
214. System.out.println("Doubly Linked List Test\n");
215. char ch;
216. /* Perform list operations */
217. do
218. {
219. System.out.println("\nDoubly Linked List Operations\n");
220. System.out.println("1. insert at begining");
221. System.out.println("2. insert at end");
222. System.out.println("3. insert at position");
223. System.out.println("4. delete at position");
224. System.out.println("5. check empty");
225. System.out.println("6. get size");
226.
227. int choice = scan.nextInt();
228. switch (choice)
229. {
230. case 1 :
231. System.out.println("Enter integer element to insert");
232. list.insertAtStart( scan.nextInt() );
233. break;
234. case 2 :
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
235. System.out.println("Enter integer element to insert");
236. list.insertAtEnd( scan.nextInt() );
237. break;
238. case 3 :
239. System.out.println("Enter integer element to insert");
240. int num = scan.nextInt() ;
241. System.out.println("Enter position");
242. int pos = scan.nextInt() ;
243. if (pos < 1 || pos > list.getSize() )
244. System.out.println("Invalid position\n");
245. else
246. list.insertAtPos(num, pos);
247. break;
248. case 4 :
249. System.out.println("Enter position");
250. int p = scan.nextInt() ;
251. if (p < 1 || p > list.getSize() )
252. System.out.println("Invalid position\n");
253. else
254. list.deleteAtPos(p);
255. break;
256. case 5 :
257. System.out.println("Empty status = "+ list.isEmpty());
258. break;
259. case 6 :
260. System.out.println("Size = "+ list.getSize() +" \n");
261. break;
262. default :
263. System.out.println("Wrong Entry \n ");
264. break;
265. }
266. /* Display List */
267. list.display();
268. System.out.println("\nDo you want to continue (Type y or n)
\n");
269. ch = scan.next().charAt(0);
270.
271. } while (ch == 'Y'|| ch == 'y');
272. }
273. }

III B.Tech (CSE - Data Science) R20


Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
OUTPUT:
Doubly Linked List Test

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
5

Doubly Linked List = 5

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
2

Doubly Linked List = 2 <-> 5

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
2
Enter integer element to insert
6

Doubly Linked List = 2 <-> 5 <-> 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

III B.Tech (CSE - Data Science) R20


Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
7

Doubly Linked List = 7 <-> 2 <-> 5 <-> 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
3
Enter integer element to insert
3
Enter position
3

Doubly Linked List = 7 <-> 2 <-> 3 <-> 5 <-> 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2

Doubly Linked List = 7 <-> 3 <-> 5 <-> 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
6. get size
3
Enter integer element to insert
4
Enter position
4

Doubly Linked List = 7 <-> 3 <-> 5 <-> 4 <-> 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
6
Size = 5

Doubly Linked List = 7 <-> 3 <-> 5 <-> 4 <-> 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1

Doubly Linked List = 3 <-> 5 <-> 4 <-> 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2

III B.Tech (CSE - Data Science) R20


Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
Doubly Linked List = 3 <-> 4 <-> 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2

Doubly Linked List = 3 <-> 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1

Doubly Linked List = 6

Do you want to continue (Type y or n)

Doubly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1

Doubly Linked List = empty

Do you want to continue (Type y or n)

Doubly Linked List Operations


III B.Tech (CSE - Data Science) R20
Faculty Name: Ravula Kartheek M.Tech, (Ph.D)
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
5
Empty status = true

Doubly Linked List = empty

Do you want to continue (Type y or n)

III B.Tech (CSE - Data Science) R20

You might also like