Professional Documents
Culture Documents
Singly Linked Lists are a type of data structure. It is a type of list. In a singly linked
list each node in the list stores the contents of the node and a pointer or reference to
the next node in the list. It does not store any pointer or reference to the previous
node. It is called a singly linked list because each node only has a single link to
another node. To store a single linked list, you only need to store a reference or
pointer to the first node in that list. The last node has a pointer to nothingness to
indicate that it is the last node.
Here is the pictorial view of inserting an element in the middle of a singly linked list:
Here is the pictorial view of deleting an element in the middle of a singly linked list:
Output:
Adding: 3
Adding: 32
Adding: 54
Adding: 89
Traversing to all nodes..
Deleted: 3
Traversing to all nodes..
Deleted: 89
32
54
76
public class DoublyLinkList<T> {
if (isEmpty()) {
newNode.next = null;
newNode.prev = null;
first = newNode;
last = newNode;
} else {
first.prev = newNode;
newNode.next = first;
newNode.prev = null;
first = newNode;
}
}
if (first.next == null) {
first = null;
last = null;
} else {
first = first.next;
first.prev = null;
}
System.out.println(temp.toString() + " is popped from the list");
}
}
public void removeLast() {
Node temp = last;
if (!isEmpty()) {
if (first.next == null) {
first = null;
last = null;
} else {
last = last.prev;
last.next = null;
}
}
System.out.println(temp.toString() + " is popped from the list");
}
}
DoublyLinkListDemo.java
public class DoublyLinkListDemo {
newList.displayList();
}
}
Post order : 45
Pre order : 45
In order : 45
Do you want to continue (Type y or n)
y
Enter integer element to insert
12
Post order : 12 45
Pre order : 45 12
In order : 12 45
Do you want to continue (Type y or n)
y
Enter integer element to insert
67
Post order : 12 67 45
Pre order : 45 12 67
In order : 12 45 67
Do you want to continue (Type y or n)
y
Enter integer element to insert
23
Post order : 23 12 67 45
Pre order : 45 12 23 67
In order : 12 23 45 67
Do you want to continue (Type y or n)
y
Enter integer element to insert
96
Post order : 23 12 96 67 45
Pre order : 45 12 23 67 96
In order : 12 23 45 67 96
Do you want to continue (Type y or n)
y
Enter integer element to insert
32
Post order : 32 23 12 96 67 45
Pre order : 45 12 23 32 67 96
In order : 12 23 32 45 67 96
Do you want to continue (Type y or n)
y
Enter integer element to insert
24
Post order : 24 32 23 12 96 67 45
Pre order : 45 12 23 32 24 67 96
In order : 12 23 24 32 45 67 96
Do you want to continue (Type y or n)