Linked list is a normal data structure.here I show how to implements it.
Step 1. Define a structure
1
2
3
4
5
6
7
8
9
|
public
class
ListNode
{
public
ListNode Next;
public
int
Value;
public
ListNode(
int
NewValue)
{
Value = NewValue;
}
}
|
Step 2. implements the functions
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
|
public
class
Clist
{
private
ListNode Head;
private
ListNode Tail;
private
ListNode Current;
private
int
ListCountValue;
public
Clist()
{
ListCountValue =
0
;
Head =
null
;
Tail =
null
;
}
public
void
Append(
int
DataValue)
{
ListNode NewNode =
new
ListNode(DataValue);
if
(ListCountValue ==
0
)
{
Head = NewNode;
Tail = NewNode;
}
else
{
Tail.Next = NewNode;
Tail = NewNode;
}
Current = NewNode;
ListCountValue +=
1
;
}
public
void
Insert(
int
DataValue)
{
ListNode NewNode =
new
ListNode(DataValue);
if
(ListCountValue ==
0
)
{
Append(DataValue);
return
;
}
if
(Current == Tail)
{
Tail.Next = NewNode;
Tail = NewNode;
Current = Tail;
ListCountValue +=
1
;
}
if
((Current != Head) && (Current != Tail))
{
NewNode.Next = Current.Next;
Current.Next = NewNode;
Current = NewNode;
ListCountValue +=
1
;
}
}
public
void
Delete()
{
if
(ListCountValue !=
0
)
{
if
(Current == Head)
{
Head = Current.Next;
Current = Head;
ListCountValue -=
1
;
return
;
}
else
{
Current = Current.Next;
ListCountValue -=
1
;
}
}
}
public
void
printAllListNode()
{
Current = Head;
for
(
int
i =
0
; i < ListCountValue; i++)
{
System.out.println(Current.Value);
Current = Current.Next;
}
}
}
|
Step 3. Test class for testing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
class
Test
{
public
static
void
main(String[] args)
{
Clist clist =
new
Clist();
clist.Append(
12
);
clist.Append(
22
);
clist.Insert(
66
);
clist.Insert(
33
);
clist.Delete();
clist.printAllListNode();
}
}
|
we will see:
1
2
3
4
|
12
22
66
33
|