-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedlist.java
134 lines (120 loc) · 2.44 KB
/
Linkedlist.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
public class Linkedlist {
public Node front;
public void init() {
this.front = null;
}
public Node FindTail() {
Node current = front;
if (current == null) {
System.out.println("The linklist is empty");
return null;
} else if (current.next == null) {
return current;
} else {
while (current.next != null) {
current = current.next;
}
return current;
}
}
public void addNodeToTheEndOfTheList(String m){
if(front == null){
front = MakeNode(m);
}else{
Node tail;
tail = findTail(front);
tail.next = MakeNode(m);
}
}
public Node MakeNode(String s) {
Node newNode = new Node(s);
return newNode;
}
public void InsertAfter(Node spot, String s) {
Node myNode = MakeNode(s);
myNode.next = spot.next;
spot.next = myNode;
}
public void showList() {
Node current = front;
while (current != null) {
System.out.println(current.name);
current = current.next;
}
}
public Node findTail(Node head) {
Node current = front;
while (current.next != null) {
current = current.next;
}
return current;
}
public void addFront(String s){
Node myNode = MakeNode(s);
Node j = front;
myNode.next=j;
front = myNode;
}
public void insertBefore(Node n, String m) {
Node myNode = MakeNode(m);
Node j = front;
while (j.next != n) {
j = j.next;
}
myNode.next = n;
j.next = myNode;
}
public void showSpec(String start)
{
Node n = this.front;
while(n != null)
{
if(n.name.startsWith(start))
{
System.out.println(n.name);
}
n = n.next;
}
}
public static Linkedlist AddAList(Linkedlist onelist, Linkedlist anotherlist)
{
Linkedlist sumlist = new Linkedlist();
Node lastnode = onelist.front;
while(lastnode.next != null)
{
sumlist.addNodeToTheEndOfTheList(lastnode.name);
lastnode = lastnode.next;
}
lastnode = anotherlist.front;
while(lastnode != null)
{
sumlist.addNodeToTheEndOfTheList(lastnode.name);
lastnode = lastnode.next;
}
return sumlist;
}
public void deleteNode(String data)
{
Node current = this.front;
while (current != null) {
if (current.next.name.equals(data)) {
current.next = current.next.next;
break;
}
current = current.next;
}
}
public void section(String str)
{
Node current = this.front;
int count =0 ;
while(current != null)
{
if(current.name.startsWith(str)){
count = count+1;
System.out.println(current.name);
}
current = current.next;
}
}
}