-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
73 lines (71 loc) · 1.87 KB
/
Copy pathLinkedList.java
File metadata and controls
73 lines (71 loc) · 1.87 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
package edu.cscc;
//Kelly Waddell 11/12/2019 Linked List Class
public class LinkedList<T> {
private Node<T> head;
public LinkedList() {
head = null;
}
//Adds element before all others
public void addFirst(T content) {
Node ptr = head;
head = new Node<>(content,ptr);
}
//Adds element after all other elements
public void addLast(T content) {
Node last = new Node<>(content,null);
if (head == null) {
head = last;
} else {
Node ptr = head;
while(ptr.getNext() != null) {
ptr = ptr.getNext();
}
ptr.setNext(last);
}
}
//Delets the first element
public boolean deleteFirst() {
if (head == null) {
return false;
} else {
head = head.getNext();
return true;
}
}
//Deletes the Last element
public boolean deleateLast() {
if (head == null) {
return false;
}
else if (head.getNext() == null) {
head = null;
return true;
} else {
Node ptr = head;
while (ptr.getNext().getNext() != null) {
ptr = ptr.getNext();
}
ptr.setNext(null);
return true;
}
}
//Prints the Head
public Node getHead() {
return head;
}
public String toString() {
String str;
if (head == null) {
str = "<empty>";
} else {
Node ptr = head;
str = "("+ptr.getContent().toString()+")";
while(ptr.getNext() != null) {
str = str + "->";
ptr = ptr.getNext();
str = str + "(" + ptr.getContent().toString() + ")";
}
}
return str;
}
}