Skip to content

Commit 3ae6bb5

Browse files
Single linked list in Java
1 parent 11a8671 commit 3ae6bb5

File tree

5 files changed

+778
-0
lines changed

5 files changed

+778
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package singleLinkedList;
7+
8+
public class ConcatenateDemo
9+
{
10+
public static void main(String[] args)
11+
{
12+
SingleLinkedList List1 = new SingleLinkedList();
13+
SingleLinkedList List2 = new SingleLinkedList();
14+
15+
System.out.println("Enter first list :- ");
16+
List1.createList();
17+
System.out.println("Enter second list :- ");
18+
List2.createList();
19+
20+
System.out.print("First ");
21+
List1.displayList();
22+
System.out.print("Second ");
23+
List2.displayList();
24+
25+
List1.concatenate(List2);
26+
System.out.print("First ");
27+
List1.displayList();
28+
}
29+
30+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package singleLinkedList;
7+
import java.util.Scanner;
8+
9+
public class Demo
10+
{
11+
public static void main(String[] args)
12+
{
13+
int choice,data,k,x;
14+
15+
Scanner scan = new Scanner(System.in);
16+
17+
SingleLinkedList List = new SingleLinkedList();
18+
19+
List.createList();
20+
21+
while(true)
22+
{
23+
System.out.println("1.Display list");
24+
System.out.println("2.Count the number of nodes");
25+
System.out.println("3.Search for an element");
26+
System.out.println("4.Insert in empty list/Insert in beginning of the list");
27+
System.out.println("5.Insert a node at the end of the list");
28+
System.out.println("6.Insert a node after a specified node");
29+
System.out.println("7.Insert a node before a specified node");
30+
System.out.println("8.Insert a node at a given position");
31+
System.out.println("9.Delete first node");
32+
System.out.println("10.Delete last node");
33+
System.out.println("11.Delete any node");
34+
System.out.println("12.Reverse the list");
35+
System.out.println("13.Bubble sort by exchanging data");
36+
System.out.println("14.Bubble sort by exchanging links");
37+
System.out.println("15.MergeSort");
38+
System.out.println("16.Insert Cycle");
39+
System.out.println("17.Detect Cycle");
40+
System.out.println("18.Remove cycle");
41+
System.out.println("19.Quit");
42+
43+
System.out.print("Enter your choice : ");
44+
choice = scan.nextInt();
45+
46+
if(choice==19)
47+
break;
48+
49+
switch(choice)
50+
{
51+
case 1:
52+
List.displayList();
53+
break;
54+
case 2:
55+
List.countNodes();
56+
break;
57+
case 3:
58+
System.out.print("Enter the element to be searched : ");
59+
data=scan.nextInt();
60+
List.search(data);
61+
break;
62+
case 4:
63+
System.out.print("Enter the element to be inserted : ");
64+
data=scan.nextInt();
65+
List.insertInBeginning(data);
66+
break;
67+
case 5:
68+
System.out.print("Enter the element to be inserted : ");
69+
data=scan.nextInt();
70+
List.insertAtEnd(data);
71+
break;
72+
case 6:
73+
System.out.print("Enter the element to be inserted : ");
74+
data=scan.nextInt();
75+
System.out.print("Enter the element after which to insert : ");
76+
x=scan.nextInt();
77+
List.insertAfter(data,x);
78+
break;
79+
case 7:
80+
System.out.print("Enter the element to be inserted : ");
81+
data=scan.nextInt();
82+
System.out.print("Enter the element before which to insert : ");
83+
x=scan.nextInt();
84+
List.insertBefore(data,x);
85+
break;
86+
case 8:
87+
System.out.print("Enter the element to be inserted : ");
88+
data=scan.nextInt();
89+
System.out.print("Enter the position at which to insert : ");
90+
k=scan.nextInt();
91+
List.insertAtPosition(data,k);
92+
break;
93+
case 9:
94+
List.deleteFirstNode();
95+
break;
96+
case 10:
97+
List.deleteLastNode();
98+
break;
99+
case 11:
100+
System.out.print("Enter the element to be deleted : ");
101+
data=scan.nextInt();
102+
List.deleteNode(data);
103+
break;
104+
case 12:
105+
List.reverseList();
106+
break;
107+
case 13:
108+
List.BubbleSortExData();
109+
break;
110+
case 14:
111+
List.BubbleSortExLinks();
112+
break;
113+
case 15:
114+
List.mergeSort();
115+
break;
116+
case 16:
117+
System.out.print("Enter the element at which the cycle has to be inserted : ");
118+
data=scan.nextInt();
119+
List.insertCycle(data);
120+
break;
121+
case 17:
122+
if(List.hasCycle())
123+
System.out.println("List has a cycle");
124+
else
125+
System.out.println("List does not have a cycle");
126+
break;
127+
case 18:
128+
List.removeCycle();
129+
break;
130+
default:
131+
System.out.println("Wrong choice");
132+
}/*End of switch*/
133+
System.out.println();
134+
}/*End of while*/
135+
scan.close();
136+
System.out.println("Exiting");
137+
}/*End of main()*/
138+
}
139+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package singleLinkedList;
7+
8+
public class MergingDemo
9+
{
10+
public static void main(String[] args)
11+
{
12+
SingleLinkedList list1 = new SingleLinkedList();
13+
SingleLinkedList list2 = new SingleLinkedList();
14+
15+
list1.createList();
16+
list2.createList();
17+
18+
list1.BubbleSortExData();
19+
list2.BubbleSortExData();
20+
21+
System.out.println("First List - "); list1.displayList();
22+
System.out.println("Second List - "); list2.displayList();
23+
24+
SingleLinkedList list3;
25+
26+
list3 = list1.merge1(list2);
27+
System.out.println("Merged List - "); list3.displayList();
28+
29+
System.out.println("First List - "); list1.displayList();
30+
System.out.println("Second List - "); list2.displayList();
31+
32+
list3 = list1.merge2(list2);
33+
System.out.println("Merged List - "); list3.displayList();
34+
35+
System.out.println("First List - "); list1.displayList();
36+
System.out.println("Second List - "); list2.displayList();
37+
}
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package singleLinkedList;
7+
8+
public class Node
9+
{
10+
public int info;
11+
public Node link;
12+
13+
public Node(int i)
14+
{
15+
info=i;
16+
link=null;
17+
}
18+
}

0 commit comments

Comments
 (0)