LinkedList Algorithm - java

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.

Related

How can I add it delete Node First and delete Node Last

How can I add it and delete Node First and delete Node Last in a double-link list
Just want to add delete Node First and delete Node Last. I did not know how to add it in your programming, but I want to help it solve it
i have 3 class
class node .
class doublyLinkedListMain .
class doublyLinkedList .
I want to add special programming in class doublyLinkedList this one delete Node First and delete Node Last
class doublyLinkedList
{
Node head;
public void push(int newdata)
{
Node NewNode = new Node(newdata);
NewNode.next = head;
NewNode.prev = null;
if (head != null) head.prev = NewNode;
head = NewNode;
}
public void insertAfter(Node PrevNode, int newdata)
{
if (PrevNode == null)
{
System.out.println("The given previous node cannot be null");
return;
}
Node NewNode = new Node(newdata);
NewNode.next = PrevNode.next;
PrevNode.next = NewNode;
NewNode.prev = PrevNode;
if (NewNode.next != null)
NewNode.next.prev = NewNode;
}
public void append(int newdata)
{
Node NewNode = new Node(newdata);
Node last = head;
NewNode.next = null;
if (head == null)
{
NewNode.prev = null;
head = NewNode;
return;
}
while (last.next != null) last = last.next;
last.next = NewNode;
NewNode.prev = last;
return;
}
void insertBefore(Node NextNode, int newdata)
{
if (NextNode == null)
{
System.out.println("the given next node cannot be NULL");
return;
}
Node NewNode = new Node(newdata);
NewNode.data = newdata;
NewNode.prev = NextNode.prev;
NextNode.prev = NewNode;
NewNode.next = NextNode;
if (NewNode.prev != null)
NewNode.prev.next = NewNode;
else head = NewNode;
}
void deleteNode( Node del)
{
if (head == null || del == null) return;
if (head == del) head = head.next;
if (del.next != null) del.next.prev = del.prev;
if (del.prev != null) del.prev.next = del.next;
return;
}
void printList()
{
Node n = head;
while (n != null)
{
System.out.print(n.data+" ");
n = n.next;
}
System.out.print(" \n ") ;
}
}
sorry, but you have different mistakes in your code. Firstly, for a list you need two or three pointers as attribute in your List class (Node head /* begin */, tail /* end */, current /* yes, the current element on which you have access */ - I have learned it with a current pointer but depending on your implementation you can do it without it too). Because of that, you must change some of your code. For example, you need a hasAccess() : boolean method, if you use a current pointer, which return current != null and your methods must consider and use tail. Furthermore, you should add a method isEmpty():
public boolean isEmpty() {
return head == null;
}
This method checks if the list is empty. In the case, that the list is empty (the method returns true), you cannot do some things, but you had seen that yet. But, because of the new pointer tail, you have its much easier to delete the last Node (see below).
Please, change all your attributes to private and code a getter and a setter for each of them. For example in the class Node for the attribute next:
private Node next;
// other code...
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
I have solved the problem with "delete Node First and delete Node Last" in the following way:
public class DoublyLinkedList {
private Node head; // begin
private Node tail; // end
public void deleteFirstElement() {
if (!isEmpty()) {
head = head.getNext();
}
}
public void deleteLastElement() {
if (!isEmpty()) {
tail = tail.getPrev();
}
}
// other code
}
Of course, you have to proof your other code, if it works. I hope that I could help you.

Reverse singly linked list

I have the below program to reverse elements in a singly linked list. I can't get it to work. I have used simple swapping of variables technique to swap the nodes but when I print, it doesn't go beyond the first node.
public static void reverseLinkedList(Node head) {
Node current = head;
Node temp = null;
while (current != null && current.next != null) {
temp = current;
current = current.next;
current.next = temp;
}
}
public static void printData(Node head) {
Node currentNode = head;
while (true) {
System.out.println(currentNode.data);
if (currentNode.next != null) {
currentNode = currentNode.next;
} else {
break;
}
}
}
I prefer to return the head node after the function. Keeps thing simple
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;
}
Alternatively you can opt for the simpler recursive version
Node reverse(Node head) {
if(head == null) {
return head;
}
if(head.next == null) {
return head;
}
Node newHeadNode = reverse(head.next);
head.next.next = head;
head.next = null;
return newHeadNode;
}
You are assigning the next of the current variable to itself which is wrong. You can do like below.
public void reverseLL(Node head) {
Node currentNode = head, prevLink = null, nextNode = null;
while (currentNode != null) {
nextNode = currentNode.next;
currentNode.next = prevLink;
prevLink = currentNode;
currentNode = nextNode;
}
head = prevLink;
}
In your algorithm the first two nodes of the list creates a loop after the first iteration of the while loop.It's better to use the below algorithm.
public static void reverseLinkedList(Node head) {
Node current = head;
Node prev = head;
Node next = head.next;
while (current != null && current.next != null) {
current=next;
next=current.next;
current.next=prev;
prev=current;
}
head=current;
}

inserting node in a linklist using java

I m beginner at java. I am trying to implement simple linklist structure using java.
I have written following code that inserts node at the end of the linklist.
public static Node insert(Node head,int data) {
if(head == null)
{
Node temp = new Node(data);
head = temp;
return head;
}
else
{
Node temp = new Node(data);
Node current = head;
while(current != null)
{
current = current.next;
}
current = temp;
return head;
}
}
The Node class is defined as follows
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
The class LinkListDemo has the insert(),display() and main() method as follows..
class LinkListDemo
{
public static Node insert(Node head,int data) {
if(head == null)
{
Node temp = new Node(data);
head = temp;
return head;
}
else
{
Node temp = new Node(data);
Node current = head;
while(current != null)
{
current = current.next;
}
current = temp;
return head;
}
}
public static void display(Node head) {
Node start = head;
while(start != null) {
System.out.print(start.data + " ");
start = start.next;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Node head = null;
int N = sc.nextInt();
while(N-- > 0) {
int ele = sc.nextInt();
head = insert(head,ele);
}
display(head);
sc.close();
}
}
INPUT: 4 2 3 4 1
I gave input as 4(number of nodes to be inserted) 2 3 4 1(corresponding node values)
I expected output to be 2 3 4 1
but the output is only 2.
Please help me to correct my mistake. Thanks in advance.
The problem is in the else part of your insert method. You are looping till current becomes null and then assign the new node temp to it. Assigning the reference to the new node(temp) will not append (or link) to the end of the list.
The correct way is to go to the last node and then link the new node i.e, make the last node's next point to the new node.
It should be like
while(current.next != null) {
current = current.next;
}
current.next = temp;
In your code for insert(), you should have
while(current.next != null)
{
current = current.next;
}
In your code, your current variable will always end up being null, resulting in your node not actually being inserted. This fix makes your current variable the last node in the list, so that you can set the last node's pointer to your new node.
I am inserting node using following code
public void addFirst(int e) {
if (head == null) {
head = new LinkListNode(e);
tail = head;
size = 1;
} else {
LinkListNode nextNode = new LinkListNode(e);
nextNode.setNext(head);
head = nextNode;
size++;
}
}
Working fine...
Just Change little bit code
Node current = head;
while(current.next != null)
{
current = current.next;
}
current.next= temp;
and return head

Why does this linked list insert code work, and the other code doesn't?

I'm learning linked lists in Java and I don't understand why this doesn't work:
public static Node insert(Node head, int data) {
if (head == null) return new Node(data);
else {
Node tail = head;
while (tail != null) tail = tail.next;
tail = new Node(data);
return head;
}
}
While this works very well:
public static Node insert(Node head, int data) {
if (head == null) return new Node(data);
else {
Node tail = head;
while (tail.next != null) tail = tail.next;
tail.next = new Node(data);
return head;
}
}
In both codes the null end nodes are instantiated. Why does not this produce the same result?
The Node class is as follows:
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
In the first case you are creating a new Node but not connecting it at anything, so it will just be detached from the list.
In the second case you are creating a new Node and connecting it to your actual tail Node, so it will be attached to the list.
Here a graphical representation:
while (tail != null) tail = tail.next;
The loop terminates when tail.next is null.
Then you do this:
tail = new Node(data);
Which leaves the next pointer of the last non-null tail to be null.
[head]---[1 (head.next)]---[2 (1.next)]---...---...---[last]---[nil (last.next)]
Change your Node class to below.
class Node {
int data;
Node next;
Node(int d) {
this.data = d;
this.next = null;
}
}
Set your head to new node in case you are inserting the first node.
public static Node insert(Node head, int data) {
if (head == null) {
head = new Node(data);
return head;
}
else {
Node tail = head;
while (tail.next != null) tail = tail.next;
tail.next = new Node(data);
return head;
}
}
The reason why your first case is not working because below will attach the node not after it rather you are creating a new node altogether and assigning it to your current pointed node.
while (tail != null) tail = tail.next;
tail = new Node(data);

Java Linked List - add method

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;

Categories

Resources