-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntLinkedlist.java
107 lines (90 loc) · 1.35 KB
/
IntLinkedlist.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
class IntLinkedlist
{
Node headnode,lastnode;
public void insert(int value)
{
Node node=new Node(value,null);
if(headnode==null)
{
headnode=node;
}
if(lastnode!=null)
{
lastnode.setNextnode(node);
}
lastnode=node;
}
public void printList()
{
if(headnode== null)
{
System.out.println("LinkedList Empty");
}
Node node=headnode;
while(node!=null)
{
System.out.println(node.getValue());
node=node.getnextNode();
}
}
public void removeALL()
{
headnode=null;
}
public void removeTail()
{
Node node=headnode;
Node previousnode=node;
while(true)
{
//node=node.getnextNode();
if(node.getnextNode()==null)
{
previousnode.setNextnode(null);
break;
}
previousnode=node;
node=node.getnextNode();
}
}
}
class Node
{
int value;
Node nextnode;
public Node(int value, Node nextnode )
{
this.value=value;
this.nextnode=nextnode;
}
public void setValue(int value)
{
this.value=value;
}
public int getValue()
{
return value;
}
public void setNextnode(Node nextnode)
{
this.nextnode=nextnode;
}
public Node getnextNode()
{
return nextnode;
}
}
class Testing
{
public static void main(String args[])
{
IntLinkedlist ilist=new IntLinkedlist();
ilist.insert(4);
ilist.insert(5);
ilist.insert(6);
ilist.insert(7);
ilist.printList();
ilist.removeTail();
ilist.printList();
}
}