Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitece940b

Browse files
authored
Updated DoublyLinkedList.java
Changes made in Insert and Delete functions.
1 parent53b2b69 commitece940b

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

‎DataStructures/Lists/DoublyLinkedList.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
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
publicvoidinsertTail(intx){
6364
LinknewLink =newLink(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-
publicvoiddeleteHead(){
82+
publicLinkdeleteHead(){
7683
Linktemp =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+
returntemp;
8189
}
8290

8391
/**
8492
* Delete the element at the tail
8593
*
8694
* @return The new tail
8795
*/
88-
publicvoiddeleteTail(){
96+
publicLinkdeleteTail(){
8997
Linktemp =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+
returntemp;
93105
}
94106

95107
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp