summaryrefslogtreecommitdiff
path: root/labs/lab03_dll/DLinkedList.java
blob: ef0f495f727e31b96a144718c6a58eb278ede433 (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
public class DLinkedList<T>
{
	private Node firstNode;
	private Node lastNode;
	private Node middleNode;
	private int numberOfEntries;
	private int middlePosition;
	
	public DLinkedList()
	{
		firstNode = null;
		lastNode = null;
		middleNode = null;
		numberOfEntries = 0;
		middlePosition = 0;
	}
	
	public void add(T newEntry)
	{
		if(firstNode == null)
		{
			firstNode = new Node(null, newEntry, null);
			lastNode = firstNode;
		}
		else
		{
			Node newNode = new Node(lastNode, newEntry, null);
			lastNode.next = newNode;
			lastNode = newNode;
		}
		
		numberOfEntries++;
		
		if(numberOfEntries % 2 == 1)
		{
			if(middleNode == null)
			{
				middleNode = firstNode;
			}
			else
			{
				middleNode = middleNode.next;
			}
			middlePosition++;
		}
	}
	
	public T getEntry(int givenPosition)
	{
		T result = null;
		
		if((givenPosition >= 1) && (givenPosition <= numberOfEntries))
		{
			result = (getNodeAt(givenPosition)).data;
		}
		
		return result;
	}
	
	private Node getNodeAt(int givenPosition)
	{
		Node currentNode = firstNode;
		
		for(int counter = 1; counter < givenPosition; counter++)
		{
			currentNode = currentNode.next;
		}
		
		return currentNode;
	}
	
	public T getEntry1(int givenPosition)
	{
		T result = null;
		
		if((givenPosition >= 1) && (givenPosition <= numberOfEntries))
		{
			result = (getNodeAt1(givenPosition)).data;
		}
		
		return result;
	}
	
	/**
	 * Modify this method according to the Solution 1
	 */
	private Node getNodeAt1(int givenPosition)
	{
		if(givenPosition > middlePosition)
		{
			//start from end, go backwars
			Node temp = lastNode;
			for(int i = 0;i<numberOfEntries - givenPosition;i++)
			{
				temp = temp.previous;
			}
			return temp;
		}
		else
		{
			//start from front, forwards
			Node temp = firstNode;
			for(int i = 1; i < givenPosition;i++)
			{
				temp = temp.next;
			}
			return temp;
		}
	}

	public T getEntry2(int givenPosition)
	{
		T result = null;
		
		if((givenPosition >= 1) && (givenPosition <= numberOfEntries))
		{
			result = (getNodeAt2(givenPosition)).data;
		}
		
		return result;
	}
	
	/**
	 * Modify this method according to the Solution 2
	 */
	private Node getNodeAt2(int givenPosition)
	{
		if(givenPosition < middlePosition)
		{
			if(givenPosition < middlePosition/2)
			{
				//STart from the front, go forwards
				Node temp = firstNode;
				for(int i = 1;i < givenPosition;i++)
				{
					temp = temp.next;
				}
				return temp;
			}
			else
			{
				//Start from the middle, backwards
				Node temp = middleNode;
				for(int i = middlePosition; i > givenPosition;i--)
				{
					temp = temp.previous;
				}
				return temp;
			}
		}
		else
		{
			if(givenPosition < numberOfEntries*(3.0/4))
			{
				//Start from the middle, go forwards
				Node temp = middleNode;
				for(int i = middlePosition; i < givenPosition; i++)
				{
					temp = temp.next;
				}
				return temp;
			}
			else
			{
				//Start from the end, go backwards
				Node temp = lastNode;
				for(int i = numberOfEntries; i >givenPosition; i--)
				{
					temp = temp.previous;
				}
				return temp;
			}
		}
	}
	
	private class Node
	{
		private Node previous;
		private T data;
		private Node next;
		
		private Node(Node previousNode, T aData, Node nextNode)
		{
			previous = previousNode;
			data = aData;
			next = nextNode;
		}
	}
}