-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntListD.java
More file actions
121 lines (101 loc) · 1.97 KB
/
IntListD.java
File metadata and controls
121 lines (101 loc) · 1.97 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
public class IntListD {
public IntNodeD head;
/**
* A new list has head pointing nowhere.
*/
public IntListD()
{
head = null;
}
/**
* Displays contents of the list.
*/
public void display()
{
IntNodeD p = new IntNodeD();
p = head;
while(p != null){
System.out.println(p.data);
p = p.next;
}
}
/**
* In an unordered list we can just add to the front.
*
* @param newdata The new element to be inserted into the list.
*/
public void insert(int newData)
{
IntNodeD p = new IntNodeD();
p.data = newData;
p.prev = null;
if(head!=null) {
head.prev = p;
}
p.next = head;
head = p;
}
/*
public void oInsert(int val) {
IntNode n = new IntNode();
IntNode z,q;
n.data = val;
if(head == null || val <= head.data) {
n.next = head;
head = n;
}else {
z = head.next;
q = head;
while(z!=null && z.data < val) {
q = z;
z = z.next;
}
n.next = z;
q.next = n;
}
}
/**
* Search the list for the value val.
*
* @param val the value to be searched for
* @return reference to the found node (null if not found)
*
public IntNode search(int val)
{
while(head!=null && head.data == val){
return head;
}
if(head!=null){
IntNode p = head;
while(p!=null & p.next!=null){
if(p.next.data == val){
return p.next;
}else{
p = head.next;
}
}
}
return null;
}
/**
* Find first occurrence of val (if it exists) and remove it from the list.
*
* @param val the value to be removed
*
public void remove(int val)
{
// this takes care of the first element if its data value is equivalent to val
if(head!=null && head.data == val){
head = head.next;
}
IntNode presentNode = head;
while(presentNode != null && presentNode.next != null){
if(presentNode.next.data == val){
presentNode.next = presentNode.next.next;
return;
}else {
presentNode = presentNode.next;
}
}
} */
}