File tree Expand file tree Collapse file tree 1 file changed +18
-6
lines changed
Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Original file line number Diff line number Diff line change 1+
12/**
23 * This class implements a DoublyLinkedList. This is done using the classes
34 * LinkedList and Link.
@@ -62,34 +63,45 @@ public void insertHead(int x){
6263 public void insertTail (int x ){
6364 Link newLink = new Link (x );
6465 newLink .next = null ; // currentTail(tail) newlink -->
65- tail .next = newLink ; // currentTail(tail) --> newLink -->
66- newLink .previous = tail ; // currentTail(tail) <--> newLink -->
67- tail = newLink ; // oldTail <--> newLink(tail) -->
66+ if (isEmpty ()) { // Check if there are no elements in list then it adds first element
67+ tail =newLink ;
68+ head =tail ;
69+ }
70+ else {
71+ tail .next = newLink ; // currentTail(tail) --> newLink -->
72+ newLink .previous = tail ; // currentTail(tail) <--> newLink -->
73+ tail = newLink ; // oldTail <--> newLink(tail) -->
74+ }
6875 }
6976
7077 /**
7178 * Delete the element at the head
7279 *
7380 * @return The new head
7481 */
75- public void deleteHead (){
82+ public Link deleteHead (){
7683 Link temp = head ;
7784 head = head .next ; // oldHead <--> 2ndElement(head)
7885 head .previous = null ; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
7986 if (head == null )
8087 tail = null ;
88+ return temp ;
8189 }
8290
8391 /**
8492 * Delete the element at the tail
8593 *
8694 * @return The new tail
8795 */
88- public void deleteTail (){
96+ public Link deleteTail (){
8997 Link temp = tail ;
9098 tail = tail .previous ; // 2ndLast(tail) <--> oldTail --> null
9199 tail .next = null ; // 2ndLast(tail) --> null
92-
100+ if (tail ==null )
101+ {
102+ head =null ;
103+ }
104+ return temp ;
93105 }
94106
95107 /**
You can’t perform that action at this time.
0 commit comments