-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLDemo.java
108 lines (101 loc) · 1.91 KB
/
LLDemo.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class Node1
{
private int data;
private Node1 link;
Node1()
{
data=0;
link=null;
}
Node1(int newdata,Node1 newlink)
{
setData(newdata);
link=newlink;
}
public void setData(int new_data)
{
data=new_data;
}
public void setLink(Node1 new_link)
{
link=new_link;
}
public int getData(){return data;}
public Node1 getLink(){return link;}
}
class LinkList1
{
private Node1 head;
LinkList1(){head=null;}
public void addToStart(int var)
{
head=new Node1(var,head);
}
public int size()
{
Node1 pos=head;
int count=0;
while(pos!=null)
{
count++;
pos=pos.getLink();
}
return count;
}
public void displayList()
{
Node1 pos=head;
while(pos!=null)
{
System.out.print(pos.getData()+"<-");
pos=pos.getLink();
}
System.out.println("null");
}
public boolean search(int val)
{
return(find(val)!=null);
}
private Node1 find(int value)
{
Node1 pos=head;
while(pos!=null)
{
if(pos.getData()==value)
return pos.getLink();
pos=pos.getLink();
}
return null;
}
public boolean deleteHeadNode()
{
while(head!=null)
{
head=head.getLink();
return true;
}
return false;
}
}
public class LLDemo
{
public static void main(String []args)
{
LinkList1 list=new LinkList1();
for(int i=0;i<10;++i)
{
list.addToStart(i);
}
System.out.println("Size="+list.size());
list.displayList();
if(list.search(4))
System.out.println("Present");
else
System.out.println("Not Found");
list.deleteHeadNode();
list.displayList();
while(list.deleteHeadNode())
;
list.displayList();
}
}