-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList2.java
More file actions
79 lines (78 loc) · 2.02 KB
/
LinkedList2.java
File metadata and controls
79 lines (78 loc) · 2.02 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
import java.util.ArrayList;
import java.lang.System;
public class LinkedList2<E> {
private ListNode head;
public LinkedList2(){
head=null;
}
public void showList(){
ListNode position = head;
while(position!=null){
System.out.println(position.getData());
position=position.getLink();
}
}
public int length(){
int count = 0;
ListNode position =head;
while(position!=null){
count++;
position =position.getLink();
}
return count;
}
public void addANodeToStart(E addData){
head = new ListNode(addData,head);
}
public void deleteHeadNode(){
if (head!=null){
head=head.getLink();
}
else {
System.out.println("Deleting from an empty list.");
System.exit(0);
}
}
public boolean onList(E target){
return find(target)!=null;
}
private ListNode find (E target){
boolean found =false;
ListNode position = head;
while(position!=null){
E dataAPosition = position.getData();
if(dataAPosition.equals(target)) found =true;
else position = position.getLink();
}
return position;
}
private ArrayList<E> toArrayList(){
ArrayList<E> list = new ArrayList<E>(length());
ListNode position = head;
while(position !=null){
list.add(position.data);
position = position.link;
}
return list;
}
private class ListNode {
private E data;
private ListNode link;
public ListNode(){
link=null;
data=null;
}
public ListNode (E newData, ListNode linkValue){
data=newData;
link=linkValue;
}
public E getData(){
return data;
}
public ListNode getLink(){
return link;
}
}
public static void main(String[] args){
}
}