-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntLL.java
101 lines (99 loc) · 1.83 KB
/
IntLL.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
public class IntLL {
private class ListNode //Internal class
{
private int data;
private ListNode link;
public ListNode(int aData, ListNode aLink)
{
data = aData;
link = aLink;
}
}
private ListNode head; //First element of the list
private ListNode curr; //Current element of the list of interest
private ListNode prev; //One node behind current
public IntLL()
{
head = curr = prev = null;
}
public void insert(int aData)
{
//Create the link
ListNode newNode = new ListNode(aData, null);
if(head == null) //List is empty
{
head = newNode;
curr = head;
return;
}
//List was not empty
ListNode temp = head;
while(temp.link != null)
{
temp = temp.link;
}
temp.link = newNode;
}
public void insertAfterCurrent(int aData)
{
if(curr == null)
{
return;
}
ListNode newNode = new ListNode(aData, curr.link);
curr.link = newNode;
}
public int getCurrent()
{
if(curr != null)
return curr.data;
else
return 0;
}
public void setCurrent(int aData)
{
if(curr != null)
curr.data = aData;
}
public void moveCurrentForward()
{
if(curr != null)
{
prev = curr;
curr = curr.link;
}
}
public void resetCurrent()
{
curr = head;
}
public void print()
{
for(ListNode temp = head; temp != null; temp = temp.link)
System.out.println(temp.data);
/*
ListNode temp = head;
while(temp != null)
{
System.out.println(temp);
temp = temp.link;
}
*/
}
public void deleteCurrent()
{
if(curr != null && prev != null) //In the middle of the list
{
prev.link = curr.link;
curr = curr.link;
}
else if(curr != null && prev == null) //Curr is at the head
{
head = head.link;
}
else //List is empty
{
System.out.println("Nope");
}
}
}