Here is a implementation of LinkedList algorithm. Algorithm inserts node at beginner, after a given node, or at the end of list.
package LinkedList;
class LinkedList {
Node Head;
class Node {
int data;
Node Next;
public Node(int d) {
data = d;
Next = null;
}
}
public void insert(int value) {
if (Head ==null) {
Head = new Node(value);
return;
}
Node new_node = new Node(value);
new_node.Next = Head;
Head = new_node;
}
public void display() {
Node a = Head;
while (a != null) {
System.out.println("value:" + a.data);
a = a.Next;
}
}
public void insertMiddle(int valueToInsert, Node prev_node) {
if (Head == null) {
System.out.println("Cant put value after last node");
}
Node new_node = new Node(valueToInsert);
new_node.Next = prev_node.Next;
prev_node.Next = new_node;
}
public void last(int value){
Node new_node = new Node(value);
if(Head == null){
Head = new Node(value);
return;
}
new_node.Next = null;
Node last = Head;
while(last != null){
last = last.Next ;
}
last = new_node;
return;
}
}
public class LinkedList_Insertion {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList list = new LinkedList();
list.insert(8);
list.insert(20);
list.insert(0);
list.insertMiddle(999, list.Head.Next);
list.display();
System.out.println("--------------");
list.last(10000);
list.display();
}
}
In the above code, While using method insert:
public void insert(int value) {
if(Head ==null){
Head = new Node(value);
return;
}
Node new_node = new Node(value);
new_node.Next = Head;
Head = new_node;
}
why don't we use Head.next = new_node;
Similarly, for method :
public void last(int value){
Node new_node = new Node(value);
if(Head == null){
Head = new Node(value);
return;
}
new_node.Next = null;
Node last = Head;
while(last != null){
last = last.Next ;
}
last = new_node;
return;
}
why don't we use last.next = new_node;?
I often end up doing same mistake again and again. If someone can clear this concept, I will be grateful.
Looking forward to your response!
Head case:
You can have Head->node1->node2->node3->...->lastNode
If you do Head.next = newNode, then node1->node2->node3->...->lastNode will be lost.
If you had a doubly-linked list, you could do Head.prev = newNode; Head = Head.prev (prev means previous).
Last case:
This code:
public void last(int value){
Node new_node = new Node(value);
if(Head == null){
Head = new Node(value);
return;
}
new_node.Next = null;
Node last = Head;
while(last != null){
last = last.Next ;
}
last = new_node;
return;
}
looks weird, the condition should actually be while (last.next != null), but even then you are not inserting, you first get a reference to the element that is last in your list, then you make that reference point to another object, it should actually be last.next = newNode, you are right.
Implementing linked lists is a good way to understand how java references work, keep practicing and also try to implement a doubly-linked list.
Your Second Code seems to be wrong, You are right it has to be last.next = newNode
now answering your 1st question consider 3 nos in linked list 100(head),200,300.
here head node with value 100 points to next node with value 200 which must in turn point to the node with value 300. Now let us suppose you want to insert 50 before 100, so when you do
Head.next = new_node;
The node with value 100 is made to point to next node with value 50, so now we have 100,50 in the linked list with the head as 100 still which is wrong
so we do
new_node.Next = Head;
Head = new_node;
In this case linked list becomes 50,100,200,300.
Thus we do it this way.
I'm trying to delete multiples nodes that meets a criteria from a linked list. The program is a bit complex so I'll get state the gist of it. The nodes in my linked list has the following characteristics (a name associated with a number):
Name Number
Dog 1
Cat 1
Rat 2
Donkey 3
Fish 1
I want to be able to delete the nodes with the number 1. My delete function:
public void Delete(Int N) {
Node current = Head;
Node previous = Head;
while (current.getNum() != N) {
if (current.getNextNode() == null) {
System.out.print("Not found");
} else {
previous = current;
current = current.getNextNode();
}
}
if (current == Head) {
Head = Head.getNextNode();
} else {
Node A = current.getNextNode();
previous.setNextNode(A);
}
}
This works but it only removes the first occurrence. I know it may be due to the lack of or appropriate loop structure but I've been working on this for hours and I'm getting confused along the way. I've tried doing a trace table manually but that's not working either.
How can the function be edited so it loops through the entire linked lists and removes the nodes that matches the criteria?
This should remove the matching Node instances from the linked list:
public void delete(int n) {
int count = 0;
Node prev = null, next;
for (Node current = head; current != null; current = next) {
next = current.getNextNode();
if (current.getNum() == n) {
count++;
if (prev != null) {
prev.setNextNode(next);
} else {
head = next;
}
} else {
prev = current;
}
}
System.out.print(count > 0 ? ("Number deleted: " + count) : "Not found");
}
Your loop while (current.getNum() != N) will end after the first occurrence of a node that has the number N. If you want to go through the entire list then the loop should look like
while (current != null) {
//do something with the list
current = current.getNextNode();
}
Specifically for this case you want to remove a node.
Node prev = null;
while (current != null) {
Node next = current.getNextNode()
if(current.getNum() == N){
//condition to remove current node has been found.
if(prev == null){
Head = next;
} else {
prev.setNextNode(next);
}
} else {
//only advance prev if we haven't deleted something
prev = current;
}
current = current.getNextNode();
}
If you want to delete a node in your linkedlist, you can use any of the following ways
a new linked list only with the nodes in which number is not equals to N
or modify the existing one.
I have used the first way, creating a new linked list with element in which number is not equals to N.
class Node {
public String name;
public int number;
public Node next;
}
public class LinkedListTest {
public static void main(String[] args) {
LinkedListTest obj = new LinkedListTest();
Node head = obj.createLinkList();
Node startPointer = head;
while (startPointer != null) {
System.out.println(startPointer.name + " " + startPointer.number);
startPointer = startPointer.next;
}
System.out.println("***********");
Node newNodeHead = obj.deleteNode(1, head);
startPointer = newNodeHead;
while (startPointer != null) {
System.out.println(startPointer.name + " " + startPointer.number);
startPointer = startPointer.next;
}
}
public Node deleteNode(int n, Node head) {
Node current = head;
Node newNodestartPointer = null;
Node newNodeCurrent = null;
while (current != null) {
if (!(current.number == n)) {
if (newNodestartPointer == null) {
newNodestartPointer = new Node();
newNodestartPointer.name = current.name;
newNodestartPointer.number = current.number;
newNodestartPointer.next = null;
newNodeCurrent = newNodestartPointer;
} else {
Node newNode = new Node();
newNode.name = current.name;
newNode.number = current.number;
newNodeCurrent.next = newNode;
newNodeCurrent = newNode;
}
}
current = current.next;
}
return newNodestartPointer;
}
public Node createLinkList() {
Node head = null;
Node newNode = new Node();
newNode.name = "Dog";
newNode.number = 1;
newNode.next = null;
head = newNode;
newNode = new Node();
newNode.name = "Cat";
newNode.number = 1;
newNode.next = null;
head.next = newNode;
Node prevNode = newNode;
newNode = new Node();
newNode.name = "Rat";
newNode.number = 2;
newNode.next = null;
prevNode.next = newNode;
prevNode = newNode;
newNode = new Node();
newNode.name = "Donkey";
newNode.number = 3;
newNode.next = null;
prevNode.next = newNode;
return head;
}
}
the method 'insertAscending' only gives me the first number even after i enter new ones. can anyone help with what i'm doing wrong? Thanks.
public class LinkedList13 {
// Private inner class Node
private class Node{
int data;
Node link;
public Node(){
data = Integer.MIN_VALUE;
link = null;
}
public Node(int x, Node p){
data = x;
link = p;
}
}
// End of Node class
public Node head;
public LinkedList13(){
head = null;
}
public void insertAscending(int data){
Node node = new Node();
node.data = data;
if (head == null)
head = node;
Node p = head;
while (p.link != null)
{
if (p.link.data > data)
{ node.link = p.link;
p.link = node;
break;
}
p= p.link;
}
}
}
Hint: is (p.link != null) ever true?
First of all, you should return after setting the head of the list (when the first element is added).
Second of all, you should handle the case where the newly inserted node is the smallest in the list (and therefore should come first). Your loop never compares the added node to the head of the list.
Finally, if the added element wasn't inserted in the while loop, it should be inserted after the while loop.
public void insertAscending(int data)
{
Node node = new Node();
node.data = data;
if (head == null) {
head = node;
return;
} else if (node.data < head.data) {
node.link = head;
head = node;
return;
}
Node p = head;
boolean added=false;
while (p.link != null)
{
if (p.link.data > data)
{
node.link = p.link;
p.link = node;
added = true;
break;
}
p = p.link;
}
if (!added)
p.link = node;
}
Check out your if condition if(p.link.data > data) the only way a node gets into the list is when that is true. This means, that, if the value of data being inserted it greater than (or equal to) everything that's been inserted so far, it will be discarded.
An easy way to fix this is change break to return and add p.link=node at the end (after the loop).
This question already has answers here:
How to reverse a singly-linked list in blocks of some given size in O(n) time in place?
(4 answers)
Closed 4 years ago.
Can someone tell me why my code dosent work? I want to reverse a single linked list in java: This is the method (that doesnt work correctly)
public void reverseList(){
Node before = null;
Node tmp = head;
Node next = tmp.next;
while(tmp != null){
if(next == null)
return;
tmp.next = before;
before = tmp;
tmp = next;
next = next.next;
}
}
And this is the Node class:
public class Node{
public int data;
public Node next;
public Node(int data, Node next){
this.data = data;
this.next = next;
}
}
On input 4->3->2->1 I got output 4. I debugged it and it sets pointers correctly but still I dont get why it outputs only 4.
Node next = tmp.next;
while(tmp != null){
So what happens when tmp == null?
You almost got it, though.
Node before = null;
Node tmp = head;
while (tmp != null) {
Node next = tmp.next;
tmp.next = before;
before = tmp;
tmp = next;
}
head = before;
Or in nicer (?) naming:
Node reversedPart = null;
Node current = head;
while (current != null) {
Node next = current.next;
current.next = reversedPart;
reversedPart = current;
current = next;
}
head = reversedPart;
ASCII art:
<__<__<__ __ : reversedPart : head
(__)__ __ __
head : current: > > >
public Node<E> reverseList(Node<E> node) {
if (node == null || node.next == null) {
return node;
}
Node<E> currentNode = node;
Node<E> previousNode = null;
Node<E> nextNode = null;
while (currentNode != null) {
nextNode = currentNode.next;
currentNode.next = previousNode;
previousNode = currentNode;
currentNode = nextNode;
}
return previousNode;
}
The method for reversing a linked list is as below;
Reverse Method
public void reverseList() {
Node<E> curr = head;
Node<E> pre = null;
Node<E> incoming = null;
while(curr != null) {
incoming = curr.next; // store incoming item
curr.next = pre; // swap nodes
pre = curr; // increment also pre
curr = incoming; // increment current
}
head = pre; // pre is the latest item where
// curr is null
}
Three references are needed to reverse a list: pre, curr, incoming
... pre curr incoming
... --> (n-1) --> (n) --> (n+1) --> ...
To reverse a node, you have to store previous element, so that you can use the simple stament;
curr.next = pre;
To reverse the current element's direction. However, to iterate over the list, you have to store incoming element before the execution of the statement above because as reversing the current element's next reference, you don't know the incoming element anymore, that's why a third reference needed.
The demo code is as below;
LinkedList Sample Class
public class LinkedList<E> {
protected Node<E> head;
public LinkedList() {
head = null;
}
public LinkedList(E[] list) {
this();
addAll(list);
}
public void addAll(E[] list) {
for(int i = 0; i < list.length; i++)
add(list[i]);
}
public void add(E e) {
if(head == null)
head = new Node<E>(e);
else {
Node<E> temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = new Node<E>(e);
}
}
public void reverseList() {
Node<E> curr = head;
Node<E> pre = null;
Node<E> incoming = null;
while(curr != null) {
incoming = curr.next; // store incoming item
curr.next = pre; // swap nodes
pre = curr; // increment also pre
curr = incoming; // increment current
}
head = pre; // pre is the latest item where
// curr is null
}
public void printList() {
Node<E> temp = head;
System.out.print("List: ");
while(temp != null) {
System.out.print(temp + " ");
temp = temp.next;
}
System.out.println();
}
public static class Node<E> {
protected E e;
protected Node<E> next;
public Node(E e) {
this.e = e;
this.next = null;
}
#Override
public String toString() {
return e.toString();
}
}
}
Test Code
public class ReverseLinkedList {
public static void main(String[] args) {
Integer[] list = { 4, 3, 2, 1 };
LinkedList<Integer> linkedList = new LinkedList<Integer>(list);
linkedList.printList();
linkedList.reverseList();
linkedList.printList();
}
}
Output
List: 4 3 2 1
List: 1 2 3 4
If this isn't homework and you are doing this "manually" on purpose, then I would recommend using
Collections.reverse(list);
Collections.reverse() returns void, and your list is reversed after the call.
We can have three nodes previous,current and next.
public void reverseLinkedlist()
{
/*
* Have three nodes i.e previousNode,currentNode and nextNode
When currentNode is starting node, then previousNode will be null
Assign currentNode.next to previousNode to reverse the link.
In each iteration move currentNode and previousNode by 1 node.
*/
Node previousNode = null;
Node currentNode = head;
while (currentNode != null)
{
Node nextNode = currentNode.next;
currentNode.next = previousNode;
previousNode = currentNode;
currentNode = nextNode;
}
head = previousNode;
}
public void reverse() {
Node prev = null; Node current = head; Node next = current.next;
while(current.next != null) {
current.next = prev;
prev = current;
current = next;
next = current.next;
}
current.next = prev;
head = current;
}
// Java program for reversing the linked list
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
// Function to reverse the linked list
Node reverse(Node node) {
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
// prints content of double linked list
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.head = new Node(85);
list.head.next = new Node(15);
list.head.next.next = new Node(4);
list.head.next.next.next = new Node(20);
System.out.println("Given Linked list");
list.printList(head);
head = list.reverse(head);
System.out.println("");
System.out.println("Reversed linked list ");
list.printList(head);
}
}
OUTPUT: -
Given Linked list
85 15 4 20
Reversed linked list
20 4 15 85
I know the recursive solution is not the optimal one, but just wanted to add one here:
public class LinkedListDemo {
static class Node {
int val;
Node next;
public Node(int val, Node next) {
this.val = val;
this.next = next;
}
#Override
public String toString() {
return "" + val;
}
}
public static void main(String[] args) {
Node n = new Node(1, new Node(2, new Node(3, new Node(20, null))));
display(n);
n = reverse(n);
display(n);
}
static Node reverse(Node n) {
Node tail = n;
while (tail.next != null) {
tail = tail.next;
}
reverseHelper(n);
return (tail);
}
static Node reverseHelper(Node n) {
if (n.next != null) {
Node reverse = reverseHelper(n.next);
reverse.next = n;
n.next = null;
return (n);
}
return (n);
}
static void display(Node n) {
for (; n != null; n = n.next) {
System.out.println(n);
}
}
}
I don't get it... why not doing this :
private LinkedList reverseLinkedList(LinkedList originalList){
LinkedList reversedList = new LinkedList<>();
for(int i=0 ; i<originalList.size() ; i++){
reversedList.add(0, originalList.get(i));
}
return reversedList;
}
I find this easier.
A more elegant solution would be to use recursion
void ReverseList(ListNode current, ListNode previous) {
if(current.Next != null)
{
ReverseList(current.Next, current);
ListNode temp = current.Next;
temp.Next = current;
current.Next = previous;
}
}
I tried the below code and it works fine:
Node head = firstNode;
Node current = head;
while(current != null && current.next != null){
Node temp = current.next;
current.next = temp.next;
temp.next = head;
head = temp;
}
Basically one by one it sets the next pointer of one node to its next to next node, so from next onwards all nodes are attached at the back of the list.
Node reverse_rec(Node start) {
if (start == null || start -> next == null) {
return start;
}
Node new_start = reverse(start->next);
start->next->next = start;
start->next = null;
return new_start;
}
Node reverse(Node start) {
Node cur = start;
Node bef = null;
while (cur != null) {
Node nex = cur.next;
cur.next = bef;
bef = cur;
cur = nex;
}
return bef;
}
I think your problem is that your initially last element next attribute isn't being changed becuase of your condition
if(next == null)
return;
Is at the beginning of your loop.
I would move it right after tmp.next has been assigned:
while(tmp != null){
tmp.next = before;
if(next == null)
return;
before = tmp;
tmp = next;
next = next.next;
}
Use this.
if (current== null || current.next==null) return current;
Node nextItem = current.next;
current.next = null;
Node reverseRest = reverse(nextItem);
nextItem.next = current;
return reverseRest
or Java Program to reverse a Singly Linked List
package com.three;
public class Link {
int a;
Link Next;
public Link(int i){
a=i;
}
}
public class LinkList {
Link First = null;
public void insertFirst(int a){
Link objLink = new Link(a);
objLink.Next=First;
First = objLink;
}
public void displayLink(){
Link current = First;
while(current!=null){
System.out.println(current.a);
current = current.Next;
}
}
public void ReverseLink(){
Link current = First;
Link Previous = null;
Link temp = null;
while(current!=null){
if(current==First)
temp = current.Next;
else
temp=current.Next;
if(temp==null){
First = current;
//return;
}
current.Next=Previous;
Previous=current;
//System.out.println(Previous);
current = temp;
}
}
public static void main(String args[]){
LinkList objLinkList = new LinkList();
objLinkList.insertFirst(1);
objLinkList.insertFirst(2);
objLinkList.insertFirst(3);
objLinkList.insertFirst(4);
objLinkList.insertFirst(5);
objLinkList.insertFirst(6);
objLinkList.insertFirst(7);
objLinkList.insertFirst(8);
objLinkList.displayLink();
System.out.println("-----------------------------");
objLinkList.ReverseLink();
objLinkList.displayLink();
}
}
You can also try this
LinkedListNode pointer = head;
LinkedListNode prev = null, curr = null;
/* Pointer variable loops through the LL */
while(pointer != null)
{
/* Proceed the pointer variable. Before that, store the current pointer. */
curr = pointer; //
pointer = pointer.next;
/* Reverse the link */
curr.next = prev;
/* Current becomes previous for the next iteration */
prev = curr;
}
System.out.println(prev.printForward());
package LinkedList;
import java.util.LinkedList;
public class LinkedListNode {
private int value;
private LinkedListNode next = null;
public LinkedListNode(int i) {
this.value = i;
}
public LinkedListNode addNode(int i) {
this.next = new LinkedListNode(i);
return next;
}
public LinkedListNode getNext() {
return next;
}
#Override
public String toString() {
String restElement = value+"->";
LinkedListNode newNext = getNext();
while(newNext != null)
{restElement = restElement + newNext.value + "->";
newNext = newNext.getNext();}
restElement = restElement +newNext;
return restElement;
}
public static void main(String[] args) {
LinkedListNode headnode = new LinkedListNode(1);
headnode.addNode(2).addNode(3).addNode(4).addNode(5).addNode(6);
System.out.println(headnode);
headnode = reverse(null,headnode,headnode.getNext());
System.out.println(headnode);
}
private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
current.setNext(prev);
if(next == null)
return current;
return reverse(current,next,next.getNext());
}
private void setNext(LinkedListNode prev) {
this.next = prev;
}
}
public class ReverseLinkedList {
public static void main(String args[]){
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("a");
linkedList.add("b");
linkedList.add("c");
linkedList.add("d");
linkedList.add("e");
linkedList.add("f");
System.out.println("Original linkedList:");
for(int i = 0; i <=linkedList.size()-1; i++){
System.out.println(" - "+ linkedList.get(i));
}
LinkedList<String> reversedlinkedList = reverse(linkedList);
System.out.println("Reversed linkedList:");
for(int i = 0; i <=reversedlinkedList.size()-1; i++){
System.out.println(" - "+ reversedlinkedList.get(i));
}
}
public static LinkedList<String> reverse(LinkedList<String> linkedList){
for(int i = 0; i < linkedList.size()/2; i++){
String temp = linkedList.get(i);
linkedList.set(i, linkedList.get(linkedList.size()-1-i));
linkedList.set((linkedList.size()-1-i), temp);
}
return linkedList;
}
}
To reverse a singly linked list you should have three nodes, top, beforeTop and AfterTop. Top is the header of singly linked list, hence beforeTop would be null and afterTop would be next element of top and with each iteration move forward beforeTop is assigned top and top is assigned afterTop(i.e. top.next).
private static Node inverse(Node top) {
Node beforeTop=null, afterTop;
while(top!=null){
afterTop=top.next;
top.next=beforeTop;
beforeTop=top;
top=afterTop;
}
return beforeTop;
}
Using Recursion It's too easy :
package com.config;
import java.util.Scanner;
public class Help {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Node head = null;
Node temp = null;
int choice = 0;
boolean flage = true;
do{
Node node = new Node();
System.out.println("Enter Node");
node.data = sc.nextInt();
if(flage){
head = node;
flage = false;
}
if(temp!=null)
temp.next = node;
temp = node;
System.out.println("Enter 0 to exit.");
choice = sc.nextInt();
}while(choice!=0);
Help.getAll(head);
Node reverse = Help.reverse(head,null);
//reverse = Help.reverse(head, null);
Help.getAll(reverse);
}
public static void getAll(Node head){
if(head==null)
return ;
System.out.println(head.data+"Memory Add "+head.hashCode());
getAll(head.next);
}
public static Node reverse(Node head,Node tail){
Node next = head.next;
head.next = tail;
return (next!=null? reverse(next,head) : head);
}
}
class Node{
int data = 0;
Node next = null;
}
Node Reverse(Node head) {
Node n,rev;
rev = new Node();
rev.data = head.data;
rev.next = null;
while(head.next != null){
n = new Node();
head = head.next;
n.data = head.data;
n.next = rev;
rev = n;
n=null;
}
return rev;
}
Use above function to reverse single linked list.
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
check more details about complexity analysis
http://javamicro.com/ref-card/DS-Algo/How-to-Reverse-Singly-Linked-List?
public static LinkedList reverseLinkedList(LinkedList node) {
if (node == null || node.getNext() == null) {
return node;
}
LinkedList remaining = reverseLinkedList(node.getNext());
node.getNext().setNext(node);
node.setNext(null);
return remaining;
}
/**
* Reverse LinkedList
* #author asharda
*
*/
class Node
{
int data;
Node next;
Node(int data)
{
this.data=data;
}
}
public class ReverseLinkedList {
static Node root;
Node temp=null;
public void insert(int data)
{
if(root==null)
{
root=new Node(data);
}
else
{
temp=root;
while(temp.next!=null)
{
temp=temp.next;
}
Node newNode=new Node(data);
temp.next=newNode;
}
}//end of insert
public void display(Node head)
{
while(head!=null)
{
System.out.println(head.data);
head=head.next;
}
}
public Node reverseLinkedList(Node head)
{
Node newNode;
Node tempr=null;
while(head!=null)
{
newNode=new Node(head.data);
newNode.next=tempr;
tempr=newNode;
head=head.next;
}
return tempr;
}
public static void main(String[] args) {
ReverseLinkedList r=new ReverseLinkedList();
r.insert(10);
r.insert(20);
r.insert(30);
r.display(root);
Node t=r.reverseLinkedList(root);
r.display(t);
}
}
public class SinglyLinkedListImpl<T> {
private Node<T> head;
public void add(T element) {
Node<T> item = new Node<T>(element);
if (head == null) {
head = item;
} else {
Node<T> temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = item;
}
}
private void reverse() {
Node<T> temp = null;
Node<T> next = null;
while (head != null) {
next = head.next;
head.next = temp;
temp = head;
head = next;
}
head = temp;
}
void printList(Node<T> node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String a[]) {
SinglyLinkedListImpl<Integer> sl = new SinglyLinkedListImpl<Integer>();
sl.add(1);
sl.add(2);
sl.add(3);
sl.add(4);
sl.printList(sl.head);
sl.reverse();
sl.printList(sl.head);
}
static class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
super();
this.data = data;
}
}
}
public class Linkedtest {
public static void reverse(List<Object> list) {
int lenght = list.size();
for (int i = 0; i < lenght / 2; i++) {
Object as = list.get(i);
list.set(i, list.get(lenght - 1 - i));
list.set(lenght - 1 - i, as);
}
}
public static void main(String[] args) {
LinkedList<Object> st = new LinkedList<Object>();
st.add(1);
st.add(2);
st.add(3);
st.add(4);
st.add(5);
Linkedtest.reverse(st);
System.out.println("Reverse Value will be:"+st);
}
}
This will be useful for any type of collection Object.
Data structures class, implementing a singly linked-list with head, tail and current nodes. Having trouble with a method, could use a nudge in the right direction.
From the assignment, write the method:
add( item ) : adds the item (String) after the current node in the list and sets the current pointer to refer to the new node.
My attempt:
public void add(String item)
{
if(curr != null)
{
Node newNode = new Node(item, curr.next);
curr.next = newNode;
curr = newNode;
}
else
{
head = tail = new Node(item, null);
curr = head;
}
}
My add method only seems to work when I'm adding items to the middle of the list, not on either end. If I use it to add a few items and then print the list, only the first one I added will be on the list, while my prepend and append methods have tested just fine.
Is there any glaring issue with my code? I feel like I'm missing something obvious.
All:
public class LinkedList {
Node head = null; /* Head of the list */
Node tail = null; /* Tail of the list */
Node curr = null; /* Current node in the list */
public void prepend(String item) {
if (head == null) {
head = tail = new Node(item, null);
curr = head;
} else {
head = new Node(item, head);
curr = head;
}
}
public void append(String item) {
if (head == null) {
head = tail = new Node(item, null);
curr = tail;
} else {
tail.next = new Node(item, null);
tail = tail.next;
curr = tail;
}
}
public void add(String item) {
if (curr != null) {
Node newNode = new Node(item, curr.next);
curr.next = newNode;
curr = newNode;
} else {
head = tail = new Node(item, null);
curr = head;
}
}
public void delete() {
if (curr.next == null) {
Node temp = head;
while (temp.next != curr) {
System.out.println(temp.item);
temp = temp.next;
}
temp.next = null;
curr = head;
}
}
public void find(String item) {
Node temp = new Node(curr.item, curr.next);
if (item.equals(temp.item))
curr = temp;
else {
temp = temp.next;
while (temp.next != null && temp != curr) {
if (item.equals(temp.item))
curr = temp;
}
}
}
public String get() {
if (curr != null)
return curr.item;
else
return "";
}
public boolean next() {
if (curr != tail) {
curr = curr.next;
return true;
} else
return false;
}
public void start() {
curr = head;
}
public void end() {
curr = tail;
}
public boolean empty() {
if (head == null)
return true;
else
return false;
}
}
Node class:
class Node {
Node next;
String item;
Node(String item, Node next) {
this.next = next;
this.item = item;
}
}
There is indeed a problem in add: it doesn't update tail when nodes already exist. Consider this sequence of actions:
LinkedList list = new LinkedList();
list.add("one");
list.add("two");
list.append("three");
If you were to then print it using this:
public void print() {
Node curr = this.head;
while(curr != null) {
System.out.println(curr.item);
curr = curr.next;
}
}
Like this:
list.print();
You'd get the following output:
one
three
This happens because tail -- which append relies on -- continues to point to the first Node after the second add operation is performed.
I don't see any problems here, so I would guess the issue is elsewhere.
Okay, the only issue I see there is in delete:
public void delete()
{
Node temp = head;
while(temp != null && temp.next != curr) {
System.out.println(temp.item);
temp=temp.next;
}
if (temp != null && temp.next != null) {
temp.next = temp.next.next;
}
curr = head;
}
I think I have found your problem.
If you use append() you add it directly after the tail. But when you have added previous nodes after the tail you don't set your tail to the new node. This means that once you call append() twice you loose all the nodes that you have added after the first append().
Brief example:
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add("First add");
list.append("First Append");
list.add("Second add");
list.prepend("First prepend");
list.add("Third add");
list.prepend("Second prepend");
list.add("fourth add");
list.append("Second Append");
list.add("Fifth add");
list.add("Sixth add");
list.start();
do {
System.out.println(list.get().toString());
} while (list.next());
}
Output:
Second prepend
fourth add
First prepend
Third add
First add
First Append
Second Append
Conclusion: "Second Add" is lost, as well as "Fifth add" and "Sixth add" because your next() method stops as soon as it reaches the tail. You need to always update the tail if you add a new node in the end.
Hope this helps.
Cheers, Chnoch
I think the problem is
if (curr != null) {
Node newNode = new Node(item, curr.next); //<-- here (curr.next)
//and
Node(String item, Node next) {
this.next = next; //<-- here
Try (Edited):
Node newNode = new Node(item, curr); // pass curr to the constructor of Node
curr = newNode;