-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListSort.java
More file actions
163 lines (125 loc) · 2.83 KB
/
LinkedListSort.java
File metadata and controls
163 lines (125 loc) · 2.83 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//kelly briceno
//stacks and queues assignment
// Linked list class
public class LinkedListSort {
// inserts a new node at the end of the list
public boolean add(int data) {
// Check if the element is the first node in the list
if(head == null) {
n = new node(data);
n.link = null;
head = n;
return true;
} else {
temp = n;
n = new node(data);
n.link = null;
temp.link = n;
return true;
}
}
// Inserts a new node after the one specified
public boolean addAfter(int val, int data) {
node j = search(val);
n = new node(data);
n.link = j.link;
j.link = n;
current = head;
do {
if (current.link == n) {
// make n the last node of the list again
n = temp.link;
return true;
}
current = current.link;
} while (current != null);
return false;
}
// Replaces the existing element with the one specified
public boolean replace(int val, int data) {
node j = search(val);
j.data = data;
return true;
}
// Removes the node at the end of the list
public boolean remove() {
current = head;
do {
if (current.link == temp) {
n = temp;
n.link = null;
temp = current;
return true;
}
current = current.link;
} while (current != null);
return false;
}
// Removes the node at the specified value
public boolean remove(int val) {
node j = search(val);
// Check if the searched value is the head
if (j == head) {
j = head.link;
head = j;
return true;
}
// If the val does not exist in the list
if(j == null) return false;
// executes if val is anywhere else in the list
current = head;
do {
if (current.link == j) {
current.link = j.link;
return true;
}
current = current.link;
} while (current != null);
return false;
}
// Searches the node of the specified value
// Returns memory address
public node search(int val) {
current = head;
while (current != null) {
// get the target
if (current.data == val) {
return current;
}
current = current.link;
}
return null;
}
// Prints all the elements in the list
public void print() {
current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.link;
}
System.out.println();
}
// Default constructor
public LinkedListSort() {
head = null;
}
//PROTECTED
protected node n,
temp,
head,
current;
public void bubbleS() {
node border = null; // first node in sprted part
while (border != head) {
node current = head;
while (current.next != border) {
if (current.element > current.next.element) {
current.element = current.next.element;
current.next.element = v;
}
current = current.next;
}
border = current;
}
}
}