This question already has answers here:
Delete Last Node of a Linked List
(15 answers)
Closed 5 years ago.
I am trying to create a double linked list and am having trouble with the removeLast method.
public class Node<E> {
E data;
public Node<E> next;
public Node<E> prev;
public Node(E d)
{
data = d;
}
public E getData()
{
return data;
}
}
public class TestList<E> {
Node<E> head;
Node<E> tail;
public void addLast(E data)
{
Node<E> newData = new Node<E>(data);
if (head == null)
{
head = newData;
tail = newData;
}
else
{
Node<E> current = head;
while (current.next != null)
current = current.next;
current.next = newData;
tail = current.next;
}
}
public void removeLast()
{
if (head == null)
System.out.println("List is empty!");
else
{
Node<E> current = tail;
}
}
If for example I had a list of integers with the values 1, 3, 5 with 1 being the head and 5 being the tail, in my removeLast method I would like to know how I could make current.prev point to 3 and current.prev.prev point to 1 as right now it would just point to the next values which in this case would be null.
You have to modify both addLast() and removeLest() with check 3 different situations in both methods.
public final class TestList<E> {
private Node<E> head;
private Node<E> tail;
public void addLast(E data) {
Node<E> node = new Node<>(data);
if (head == null)
head = tail = node;
else if (head == tail) {
tail = node;
head.next = tail;
tail.prev = head;
} else {
tail.next = node;
node.prev = tail;
tail = node;
}
}
public void removeLast() {
if (tail == null)
System.err.println("List is empty!");
else if (head == tail)
head = tail = null;
else {
Node<E> prev = tail.prev;
tail.prev = null;
tail = null;
tail = prev;
tail.next = null;
}
}
private static final class Node<E> {
E data;
Node<E> next;
Node<E> prev;
public Node(E data) {
this.data = data;
}
}
}
You can do:
If the list is already empty, do nothing
Or else, if head is the same as the tail (there is only one element in list), then clear the list
Or else, make tail.prev.next point to null, and then make tail = prev
Like this:
public void removeLast() {
if (head == null) {
System.out.println("List is empty!");
return;
}
if (head == tail) {
head = tail = null;
return;
}
Node<E> prev = tail.prev;
prev.next = null;
tail = prev;
}
Related
package linkedList;
import linkedList.Linkedlist.Node;
public class Linkedlist {
Node head = null;
Node tail = null;
class Node{
int data;
Node next;
Node (int data){
this.data = data;
this.next = null;
}
}
public void addNode(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
tail = newNode;
}else{
tail.next = newNode;
tail = newNode;
}
}
public void print() {
if(head == null) {
System.out.println("list is empty");
return;
}else {
Node disp = head;
while (disp !=null) {
System.out.print(disp.data+" ");
disp = disp.next;
}
}
System.out.println();
}
public void addFirst(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
return;
}else {
newNode.next = head;
head = newNode;
}
}
public void addLast(int data) {
Node newNode = new Node( data);
if(head == null) {
head = newNode;
return;
}else {
Node thisNode = head;
while(thisNode.next != null) {
thisNode = thisNode.next;
}
thisNode.next = newNode;
}
}
public void delete(int data) {
Node value = new Node(data);
if(head == null) {
head = value;
return;
}
Node temp = head;
if(data == 0 ) {
head =temp.next;
return;
}
for (int i = 0; temp != null && i < data - 1; i++) {
temp = temp.next;
if (temp == null || temp.next == null)
return;
Node next = temp.next.next;
temp.next= next;
}
}
public Node reverse(Node head)
{
Node prev = null;
Node current = head;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
public static void main(String[] args) {
Linkedlist a1 = new Linkedlist();
a1.addNode(200);
a1.addNode(300);
a1.addNode(400);
a1.addNode(500);
a1.print();
a1.addFirst(100);
a1.print();
a1.addLast(600);
a1.print();
a1.delete(5);
a1.print();
a1.reverse();
}
}
**how to call Node reverse method to main method...
when i try a1.reverse it show 3 option
head
-tail
-null
and all are show error.**
it is bassically input - 200 300 400 500
then add add 100 at head and 600 at tail
then delet the second position value
then reverse the linkedlist.
i try all methods but when i try to pass "Node head" parameter at reverse method and call it inside main method it show error .
In below doubly linked list example I can add node to front of the doubly linked list and the end of the doubly linked list. I also can traverse the doubly linked list forward and print the node's value successfully. Hoewer when I print the list backwards my tail.previous value is null and I can print only the node's value that is currently in the tail Could you please tell what is wrong. Thank you.
public class DLL {
public Node head;
public Node tail;
/* Doubly Linked list Node*/
public class Node {
public int data;
public Node prev;
public Node next;
// Constructor to create a new node
// next and prev is by default initialized as null
Node(int d) { data = d; }
}
public void addToFront(int data){
System.out.println( data + " will be added to the front!");
Node nn = new Node(data);
nn.next = head;
nn.prev = null;
if (head != null) head.prev = nn;
head = nn;
if (tail == null) tail = nn;
}
public void addToBack(int data){
Node nn = new Node(data);
System.out.println(data + " will be added to the back!");
if (tail != null) tail.next = nn;
tail = nn;
if (head == null) head = nn;
}
public void printForward(){
System.out.println("Printing forward!");
Node runner = head;
while(runner != null){
System.out.println(runner.data);
runner = runner.next;
}
}
public void printBackward(){
System.out.println("Printing backwards");
Node runner = tail;
while (runner != null){
System.out.println(runner.data);
runner = runner.prev;
}
}
}
The test code is below:
public class DDLTest{
public static void main (String[] args){
DLL dl = new DLL();
dl.addToFront(2);
dl.addToFront(1);
dl.addToBack(3);
dl.printForward();
dl.printBackward();
}
}
Your addToBack method is not setting the prev pointer of the new node.
Add this:
if (tail != null) {
tail.next = nn;
nn.prev = tail;
}
you have to point back in addToback method
public void addToBack(int data){
Node nn = new Node(data);
System.out.println(data + " will be added to the back!");
if (tail != null){
tail.next = nn;
nn.prev = tail;
tail = nn;
}
if (head == null) head = nn;
}
I am trying to implement it recursively but it is not returning the correct linked list with all the correct nodes.
I tried printing the node out after the recursive call and that gave me the correct linked list in reverse order but for some reason when i return head that does not work.
public Node<E> insertNodeAtPosition(Node<E> head,E element,int position)
{
Node<E> node = new Node<E>(element,null);
if(head==null){
return node;
}
if(position==0)
{
Node<E> current = head;
head = node;
head.next = current;
}
insertNodeAtPosition(head.next,element,position-1);
return head;
}
I expect the output to include the inserted node but it doesnt appear there
This code:
public static <E> Node<E> insertNodeAtPosition(Node<E> head, E element, int position) {
return insertNodeAtPosition(null, head, element, position);
}
private static <E> Node<E> insertNodeAtPosition(Node<E> prev, Node<E> head, E element, int position) {
if (position == 0) {
Node<E> newNode = new Node<>(element, null);
if (prev != null) {
prev.next = newNode;
}
newNode.next = head;
return newNode;
}
return insertNodeAtPosition(head, head.next, element, position - 1);
}
Will return the newly-inserted node.
EDIT: without the additional method:
private static <E> Node<E> insertNodeAtPosition(Node<E> prev, Node<E> head, E element, int position) {
if (position == 0) {
Node<E> newNode = new Node<>(element, null);
if (prev != null) {
prev.next = newNode;
}
newNode.next = head;
return newNode;
}
return insertNodeAtPosition(head, head.next, element, position - 1);
}
I am trying to insert element at the end of a linked list insertAtEnd(). When I debug the code I see a node(0,null) is being inserted as default in the beginning of the insertion. I think this is causing the problem while iterating through the list. Any suggestions on how fix this?
package com.ds.azim;
public class Node {
//Node has 1. Data Element 2. Next pointer
public int data;
public Node next;
//empty constructor
public Node(){
//
}
public Node(int data){
this.data= data;
this.next = null;
}
public Node(int data, Node next){
this.data = data;
this.next = next;
}
}
//*************************************//
package com.ds.azim;
public class SingleLinkedList {
//Single Linked list has a head tail and has a length
public Node head;
public Node tail;
public int length;
//constructor
public SingleLinkedList(){
head = new Node();
length = 0;
}
public void insertAtFirst(int data){
head = new Node(data,head);
}
public void insertAtEnd(int data){
Node curr = head;
if(curr==null){
insertAtFirst(data);
}else{
while(curr.next!=null){
curr = curr.next;
}
curr.next = new Node(data,null);
}
}
public void show(){
Node curr = head;
while(curr.next!=null){
//do something
System.out.print(curr.data+",");
curr = curr.next;
}
}
public static void main(String[] args){
SingleLinkedList sll = new SingleLinkedList();
sll.insertAtFirst(12);
sll.insertAtFirst(123);
sll.insertAtFirst(890);
sll.insertAtEnd(234);
sll.show();
}
}
Along with removing this part of the code
public SingleLinkedList() {
head = new Node();
length = 0;
}
change your show function as well, because this will not print the last element
while(curr.next!=null){
//do something
System.out.print(curr.data+",");
curr = curr.next;
}
after this while, put one more print statement to print the last element.
System.out.print(curr.data);
this will fix the errors.
Your code initializes the list with a Node containing (0, null) and head pointing to it. To fix this, don't do that.
public SingleLinkedList() {
head = new Node();
length = 0;
}
Also in that code you set length = 0;, but actually the length is 1. Remove both the assignments from the constructor. Then you will have a structure with zero members and the length will be correct.
You have a tail variable which should point to the last node in your list. You should be keeping it up to date:
class SingleLinkedList {
private Node head = null;
private Node tail = null;
public void addAtHead(int data) {
if (head == null) {
addFirst(data);
} else {
head.next = new Node(data, head.next);
if (tail == head)
tail = head.next;
}
}
public void addAtTail(int data) {
if (head == null) {
addFirst(data);
} else {
assert tail != null;
assert tail.next == null;
tail.next = new Node(data);
tail = tail.next;
}
}
private void addFirst(int data) {
assert head == null;
assert tail == null;
head = new Node(data);
tail = head;
}
}
If you want to remove the tail variable, then:
class SingleLinkedList {
private Node head = null;
public void addAtHead(int data) {
if (head == null) {
head = new Node(data);
} else {
head.next = new Node(data, head.next);
}
}
public void addAtTail(int data) {
if (head == null) {
head = new Node(data);
} else {
Node curr = head;
while (curr.next != null)
curr = curr.next;
curr.next = new Node(data);
}
}
}
I searched for this alot found alota similar answers but nothing to help my exact problem.
I'm doing a push method for my double linked list, while the pointers on the head work fine, the tail next and previous pointers do not work please help.
public class MyStack<E> implements MyDeque {
private Node<E> head;
private Node<E> tail;
private int size;
public MyStack() {
head = null;
tail = null;
size = 0;
}
public void push(Object element) {
Node<E> newNode = new Node(element);
if(size == 0) {
Node temp = new Node(head);
head = newNode;
head.next = head;
head.previous = head;
tail = head;
tail.next = head;
tail.previous = temp;
}
else {
newNode.previous = head;
head = newNode;
newNode.next = tail;
(tail.next).previous = tail;
}//else statement
size++;
}//push()
public Object peek() {
if (size==0) return null;
else
return head;
}
public Object pop() {
size--;
if(size == 0)
return null;
else {
Node temp = new Node(head.previous);
head = head.previous;
head.next = tail;
head.previous = temp;
return head;
}//else
}//pop()
#Override
public int size() {
return size;
}
private class Node<E> {
private E data;
private Node<E> next;
private Node<E> previous;
public Node(E data) {
this.data = data;
this.next = null;
this.previous = null;
}
public Node(E data, Node<E> next, Node<E> previous) {
this.data = data;
this.next = next;
this.previous = previous;
}//constructor
public String toString() {
return data+"";
}
}//class Node<E>
public String toString() {
return (head+" Head\n" + head.next +" Head.Next\n" + head.previous+ " Head.previous\n"
+ tail+" Tail\n" + tail.next+" tail.next\n" + tail.previous+" tail.previous\n");
}
}
The case where you push an object when the stack is empty does not look correct. The first node added should be assigned to head. Then the tail becomes the new node's head, this new node becomes the tail node's tail, and the new node itself becomes the tail.
Usually in a stack you add and remove elements at the end (the tail). The code doesn't make it very clear what you are trying to do with the head and tail members. Maybe it would be clearer for you if you name them firstNode and lastNode.
I can actually see a few issues with this code
public class MyStack<E> implements MyDeque {
private Node<E> head;
private Node<E> tail;
private int size;
public MyStack() {
head = null;
tail = null;
size = 0;
}
This looks fine
public void push(Object element) {
Node<E> newNode = new Node(element);
if(size == 0) {
Node temp = new Node(head);
head = newNode;
head.next = head;
head.previous = head;
tail = head;
tail.next = head;
tail.previous = temp;
}
else {
newNode.previous = head;
head = newNode;
newNode.next = tail;
(tail.next).previous = tail;
}//else statement
size++;
}//push()
Bit confused why you are taking in an object instead of your generic E.
You really don't need the temp value here, in fact it looks like you are breaking your previous when you set tail.previous = temp
In your else you aren't setting the tail.previous correctly. The last line should be tail.previous = head. You also missed the head.next
So cleaned up a bit
public void push(E element) {
Node newNode = new Node(E);
if(size == 0) {
head = newNode;
head.next = head;
head.previous = head;
tail = head;
} else {
newNode.previous = head;
head.next = newNode;
newNode.next = tail;
tail.previous = newNode;
head = newNode;
}
size++;
}
In your pop method you probably want to move your size decrement after your size check, otherwise a 1 element stack will return null when popped.
edit: This would probably be easier with a singly linked list, or at least not a circular doubly linked list.
and adding at the end would probably be the more conventional way to do things.