-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleLinkedList.java
90 lines (78 loc) · 1.38 KB
/
SingleLinkedList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
package SeparateChaining;
public class SingleLinkedList
{
private Node start;
public SingleLinkedList()
{
start=null;
}
public void displayList()
{
Node p;
if(start==null)
{
System.out.println("___");
return;
}
p=start;
while(p!=null)
{
System.out.print(p.info + " ");
p=p.link;
}
System.out.println();
}/*End of displayList()*/
public studentRecord search(int x)
{
Node p=start;
while(p!=null)
{
if(p.info.getstudentId()==x)
break;
p=p.link;
}
if(p==null)
{
System.out.println(x + " not found in list");
return null;
}
else
return p.info;
}/*End of search()*/
public void insertInBeginning(studentRecord data)
{
Node temp=new Node(data);
temp.link=start;
start=temp;
}
public void deleteNode(int x)
{
if(start==null)
{
System.out.println("Value " + x + " not present\n");
return;
}
/*Deletion of first node*/
if(start.info.getstudentId()==x)
{
start=start.link;
return;
}
/*Deletion in between or at the end*/
Node p=start;
while(p.link!=null)
{
if(p.link.info.getstudentId()==x)
break;
p=p.link;
}
if(p.link==null)
System.out.println("Value " + x + " not present\n");
else
p.link = p.link.link;
}
}