Skip to content

Commit 7dc749e

Browse files
Sorted linked list in Java
1 parent 3ae6bb5 commit 7dc749e

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 sortedLinkedList;
7+
8+
import java.util.Scanner;
9+
10+
public class Demo
11+
{
12+
public static void main(String[] args)
13+
{
14+
int choice,data;
15+
16+
Scanner scan = new Scanner(System.in);
17+
18+
SortedLinkedList List = new SortedLinkedList();
19+
List.createList();
20+
21+
while(true)
22+
{
23+
System.out.println("1.Display List");
24+
System.out.println("2.Insert a new node");
25+
System.out.println("3.Search");
26+
System.out.println("4.Exit");
27+
System.out.print("Enter your choice : ");
28+
choice = scan.nextInt();
29+
if(choice==4)
30+
break;
31+
32+
switch(choice)
33+
{
34+
case 1:
35+
List.displayList();
36+
break;
37+
case 2:
38+
System.out.println("Enter the element to be inserted : ");
39+
data = scan.nextInt();
40+
List.insertInOrder(data);
41+
break;
42+
case 3:
43+
System.out.println("Enter the element to be searched : ");
44+
data=scan.nextInt();
45+
List.search(data);
46+
break;
47+
default:
48+
System.out.println("Wrong choice\n");
49+
}
50+
}/*End of while*/
51+
}/*End of main()*/
52+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 sortedLinkedList;
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+
}
17+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 sortedLinkedList;
7+
8+
import java.util.Scanner;
9+
10+
public class SortedLinkedList
11+
{
12+
private Node start;
13+
14+
public SortedLinkedList()
15+
{
16+
start=null;
17+
}
18+
public void insertInOrder(int data)
19+
{
20+
Node temp=new Node(data);
21+
22+
/*List empty or new node to be inserted before first node*/
23+
if(start==null || data<start.info)
24+
{
25+
temp.link=start;
26+
start=temp;
27+
return;
28+
}
29+
30+
Node p=start;
31+
while(p.link!=null && p.link.info <= data)
32+
p=p.link;
33+
34+
temp.link=p.link;
35+
p.link=temp;
36+
}/*End of insertInOrder()*/
37+
38+
public void createList()
39+
{
40+
int i,n,data;
41+
42+
Scanner scan = new Scanner(System.in);
43+
44+
System.out.print("Enter the number of nodes : ");
45+
n = scan.nextInt();
46+
47+
if(n==0)
48+
return;
49+
50+
for(i=1; i<=n; i++)
51+
{
52+
System.out.print("Enter the element to be inserted : ");
53+
data = scan.nextInt();
54+
insertInOrder(data);
55+
}
56+
}/*End of createList()*/
57+
58+
public void search(int x)
59+
{
60+
if(start==null)
61+
{
62+
System.out.println("List is empty");
63+
return;
64+
}
65+
66+
Node p=start;
67+
int position=1;
68+
while(p!=null && p.info<=x)
69+
{
70+
if(p.info==x)
71+
break;
72+
position++;
73+
p=p.link;
74+
}
75+
if(p==null || p.info!=x)
76+
System.out.println(x + " not found in list");
77+
else
78+
System.out.println(x + " is at position " + position);
79+
}/*End of search()*/
80+
81+
public void displayList()
82+
{
83+
Node p;
84+
if(start==null)
85+
{
86+
System.out.println("List is empty");
87+
return;
88+
}
89+
System.out.print("List is : ");
90+
p=start;
91+
while(p!=null)
92+
{
93+
System.out.print(p.info + " ");
94+
p=p.link;
95+
}
96+
System.out.println();
97+
}/*End of displayList()*/
98+
99+
100+
}

0 commit comments

Comments
 (0)