-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedList.java
129 lines (111 loc) · 3.59 KB
/
SinglyLinkedList.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
package sll;
/**
* File Name: SSL.java
*
* Software Quality Assurance Corporation
* Code Review Project
*
* (c) Copyright 2023 SQAC
* ALL RIGHTS RESERVED
*
* Functional description: This class implements a singly-linked list
* that support insertion and deletion of nodes.
*
* Input: None.
* Output: None except error messages.
* Supported Requirements: TBD
* Classes in this file: this class
* Related Documents: None; this file provides in-code documentation.
* Update History:
* Error Messages:
* Constraints: None.
* Assumptions: None.
**/
public class SinglyLinkedList {
private Node headNode;
private int size;
/**
Specified current node in the list before the provided node.
@param node the node that will be added to the list.
@param current the node that a new node should be added before.
@throws: incase current node is null then it throws NullPointerException.
*/
public void insert(Node node, Node current) {
// Verify head node is presents current node or not
if (current == null) {
// We are going to set a new head node
if (headNode == null) {
// if the list is empty, make the new node the head
headNode = node;
} else {
// insert the new node after the last node in the list
Node last = headNode;
while (last.getNext() != null) {
last = last.getNext();
}
last.setNext(node);
}
size++;
} else if (current == headNode) {
// insert the new node before the head node
node.setNext(current);
headNode = node;
size++;
} else {
// current node is not null
Node n = new Node();
n.setData(current.getData());
n.setNext(current.getNext());
current.setData(node.getData());
current.setNext(n);
size++;
}
}
/** Find the first node with the given value */
public Node find(String value) {
// starting your search at the head node
Node p = headNode;
// Explore the list until you reach the very end or the node with the specified value.
while (p != null) {
// Verify if the data in the current node matches the specified value.
if (p.getData()!=null&&p.getData().compareTo(value) == 0) {
// go to the following node
return p;
}
p = p.getNext();
}
return null;
}
/** Remove the node referenced by current */
public void remove(Node current) {
if (current == null) {
throw new NullPointerException("Node is null");
}
Node next = current.getNext();
if (next == null) {
return; // nothing to remove
}
current.setData(next.getData());
current.setNext(next.getNext());
size--;
}
// the number of nodes in the list is returned.
public int size() {
return size;
}
/** gives the linked list's string representation back.*/
public String toString() {
Node p = headNode;
String r = "";
while (p != null) {
r += p.getData() + " ";
p = p.getNext();
}
return r;
}
// returns the connected list's head node.
// the connected list's head node.
public Node head() {
return headNode;
}
}