Singly linked list append method - java

hey guys i am having trouble trying to implement the appened method for singly linked list.
here is the code:
public void append ( int item ) {
//inserts item to the end of the list
if ( head == null){
head = new LinkInt();
curr = head;
curr.elem = item;
}
else{
LinkInt temp = head;
while ( temp.next != null){
temp = temp.next;}
temp.elem = item;
}
}
and here is my print method ( not sure if its correct as well ):
public void print () {
//outprint the array
//ie. <1, 2, |3, 4>
if ( head == null) {
System.out.print("<");
System.out.print(">");
}
else{
LinkInt temp = head;
System.out.print("<");
while ( temp != null) {
if ( temp == curr){
System.out.print( "|" + temp.elem + ","); }
else{
System.out.print( temp.elem );
System.out.print(",");}
temp = temp.next;
}
System.out.print(">");
}
}
}
heres the problem:
let say appened 3 ->>> i get <|3>
but if i do appened 5 after ->>>> i get <|5> which delete my first item.
Help me out please :(

after these statement :
while ( temp.next != null)
{
temp = temp.next;
}
do this:
tmp1= new LinkInt();
tmp1.elem = item;
tmp1.next = null
tmp.next = tmp1
instead of this:
temp.elem = item;
try this for print method:
public void print ()
{
//outprint the array
//ie. <1, 2, |3, 4>
if ( head == null)
{
System.out.print("<");
System.out.print(">");
}
else
{
LinkInt temp = head;
System.out.print("<");
while ( temp->next != null)
{
System.out.print( "|" + temp.elem + ",");
temp = temp.next;
}
System.out.print("|" + temp.elem);}
System.out.print(">");
}
}

LinkInt temp = head;
while ( temp.next != null){
temp = temp.next;
}
temp.elem = item;
What this does is - temp.next is null when 3 is already inserted. Hence, it goes to temp.elem = item and overwrites your existing value. Do something like this:-
LinkInt temp = head;
while ( temp.next != null){
temp = temp.next;
}
//temp.elem = item; -Not needed.
temp1= new LinkInt();
temp1.elem = item;
temp1.next = null;
temp.next = temp1;

Have the method this way
public void append(int item)
{
LinkInt l = new LinkInt();
l.elem = item;
if ( head == null )
head = l;
else {
LinkInt tmp = head;
while ( tmp.next != null)
tmp = tmp.next;
tmp.next = l;
}

Related

How to make a linked list sorted?

Write a java method in your Linked List Project to perform the sorted insertion in your Linked List. Sorted insertion is one in which, whenever you insert a new element, it is inserted on its sorted location. but I'm not getting the output here is my code.
{
public void insertSorted(int data)
{
Node temp = new Node(data);
if(head == null || head.Data > data)
{
temp.next = head;
head = temp;
}
Node head2 = head;
while(head2.next != null && head2.next.Data < data)
{
head2 = head2.next;
}
temp.next = head.next;
head.next = temp;
}
public void Display()
{
Node t = head;
while(t != null)
{
System.out.print(t.Data+" -> ");
t = t.next;
}
System.out.println("NULL");
}
}
You can try this one it will work as you expected ->
public void seprator1(Node curr) {
if (curr == null || curr.next == null) {
return;
}
Node Even, EvenTail, oddStartingNode, oddEndNode;
Even = EvenTail = oddStartingNode = oddEndNode = null;
while (curr != null) {
if (curr.data % 2 == 0) {
if (Even == null) {
Even = curr;
EvenTail = Even;
} else {
EvenTail.next = curr;
EvenTail = EvenTail.next;
}
} else {
if (oddStartingNode == null) {
oddStartingNode = curr;
oddEndNode = oddStartingNode;
} else {
oddEndNode.next = curr;
oddEndNode = oddEndNode.next;
}
}
curr = curr.next;
}
displayNode(Even);
System.out.println();
displayNode(oddStartingNode);
}
private void displayNode(Node node) {
if (node == null) {
return;
}
System.out.print(node.data+" ");
displayNode(node.next);
}

Priorityqueue implemented via singly linked list insert

I'm using a generic linked list to implement priority queue where I use the comparable function for the insert function where it finds a slot where it's size is bigger than the current node. I have problem actually getting the add function insert the elements according priority queue requirements. The priority queue should go from smallest to biggest.
Edit: I realize the problem lies with inserting a number bigger than the head. During add 11, the function only compares with 5 and add it after 5 which ruins the sequence.
Current output
PQ: 5
PQ: 5,10
PQ: 5,9,10
PQ: 5,11,9,10
PQ: 5,7,11,9,10
PQ: 2,5,7,11,9,10
Desired output
PQ: 2,5,7,9,10,11
My add function
public class PQLinkedList<T extends Comparable<T>>{
private class Node{
private T data;
private Node next;
// Constructor that takes in data to input for the node
public Node(T data) {
this.data = data;
this.next = null;
}
}
private int length = 0;
private Node head;
int cmp = 0;
public PQLinkedList() {
head = null;
}
//Compare with each element till it finds a suitable position to insert itself
//Ascending order
//INCOMPLETE
//Compare part of this code is not complete!
public void addPQ( T data) {
Node node = new Node(data);
Node temp2 = null;
if ( head == null) {
addFirst(data);
}
else {
Node curr = head;
int count = getSize();
while ( count != 0) {
cmp = data.compareTo(curr.data);
if ( cmp < 0 ) {
addFirst(data);
break;
}
else if (cmp>=0 ){
if ( curr.next == null) {
curr.next = node;
break;
}
// if there curr.next is not empty
// Move all the nodes towards to tail by 1
else {
// after = one space after pos
// PROBLEM
temp2 = curr.next;
Node after = curr.next;
while( after != null) {
after = after.next;
}
node.next = temp2;
curr.next = node;
break;
}
}
else {
curr = curr.next;
}
count--;
}
}
length++;
}
private void addFirst(T data) {
Node node = new Node(data);
if ( head == null ) {
head = node;
}
else {
Node temp = head;
head = node;
node.next = temp;
}
length++;
}
//TO-DO
public void remove( T data) {
if ( !search(data)) {
System.out.println("Linked list does not contain that element!");
return;
}
else if ( head.data == data) {
head = head.next;
}
else {
// If curr is the node to be deleted.
// link from previous node to curr, link from curr to next node
//Traverse through the linkedlist till it finds the node to be deleted and skip it
Node curr = head;
while ( curr != null & curr.next != null ) {
if ( curr.next.data == data) {
//Check if the node 2 next after curr is null
//if so, remove curr.next which contains the value that we want to delete
if ( curr.next.next != null) {
curr.next = curr.next.next;
}
//curr.next.next is null so just curr.next which contains the value we want to delete
else {
curr.next = null;
}
}
//Traverse the curr node
else {
curr = curr.next;
}
}
length--;
}
}
// Retrieves and removes the head of the priority queue
public T poll() {
if ( isEmpty()) {
System.out.println("Linked list is empty!");
return null;
}
else {
Node temp = null;
temp = head ;
head = head.next;
length--;
return temp.data;
}
}
// Retrieves the head of the priority queue
public T peek() {
if ( isEmpty()) {
System.out.println("Linked list is empty!");
return null;
}
else {
return head.data;
}
}
public void clear() {
Node curr = head;
while ( curr != null) {
curr = curr.next;
}
}
public int getSize() {
return length;
}
public boolean search(T data) {
if ( isEmpty()) {
System.out.println("Linked list is empty!");
return false;
}
else {
Node node = head;
while ( node != null ) {
if ( node.data == data) {
return true;
}
node = node.next;
}
return false;
}
}
public boolean isEmpty() {
if ( length == 0) {
return true;
}
return false;
}
#Override
public String toString() {
String str = "PQ: ";
Node node = head;
while ( node != null) {
str = str + node.data;
if ( node.next != null) {
str = str + ",";
}
node = node.next;
}
return str;
}
}
My main
public class priorityQueueImplementation {
public static void main(String[] args) {
PQLinkedList<Integer> test = new PQLinkedList<Integer>();
test.add(5);
test.add(10);
test.add(9);
test.add(11);
test.add(7);
test.add(2);
System.out.println( test.toString());
}
}
Since this is a singly linked list, we cannot add node to previous node. We can only add to subsequent node. During node insertion, we need to compare data with the list. One every node, there are 5 cases:
current is null, data become the new head
data is smaller than current, data become the new head
data is greater than current, data is smaller then next. Insert data between current and next
data is greater than current, next is null. Insert data after current
data is greater than current, data is greater then next. Wait for next iteration
public void addPQ(T data) {
Node node = new Node(data);
Node curr = head;
if (head == null) {
// current is null, data become the new head
addFirst(data);
} else {
while (curr != null) {
int cmpLeft = data.compareTo(curr.data);
if (cmpLeft < 0) {
// data is smaller than current, `data` become the new head
addFirst(data);
break;
} else {
// data is greater than current
if (curr.next == null) {
// next is null. Insert `data` after `current`
addAfter(curr, node);
break;
} else {
int cmpRight = data.compareTo(curr.next.data);
if (cmpRight < 0) {
// data is smaller then next. Insert data between current and next
addAfter(curr, node);
break;
}
}
// data is greater then next. Wait for next iteration
curr = curr.next;
}
}
}
}
private void addAfter(Node currNode, Node newNode) {
if (currNode == null) {
currNode = newNode;
} else {
Node tempTail = currNode.next;
currNode.next = newNode;
newNode.next = tempTail;
}
length++;
}
output
PQ: 2,5,7,9,10,11

Append Method Linked List... It keeps deleting the last input

public void append(int data) {
Node newNode = new Node(data);
if (head == null) {
head = new Node(data);
return;
} else {
Node last = head;
while(last.nextNode != null) {
last = last.nextNode;
}
last.nextNode = newNode;
}
return;
}
public void printList(){
Node temp = head;
while (temp != null && temp.nextNode != null){
System.out.print(temp.data + " " );
temp = temp.nextNode;
}
temp = temp.nextNode;
}
Input: 4,5,6,9,9
Output: 4,5,6,9
This is the output I get it keeps deleting the last input.....
Changes made
You forgot to print last node. You check not only if temp is not null, but also if it has next node after it - therefore you exit while loop before last node was actually printed.
Either change conditions or print last node right after while loop (I would suggest first option).
First option:
public void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.nextNode;
}
}
Second option:
public void printList(){
Node temp = head;
while (temp != null && temp.nextNode != null){
System.out.print(temp.data + " " );
temp = temp.nextNode;
}
// print last node here
System.out.print(temp.data);
temp = temp.nextNode;
}

Delete specific element from LinkedList....?

I am trying to delete an specific element from the linkedlist, however i am getting null pointer exception. Could any one pls fix my below mentioned code...
public void deleteElement(T num)
{
Node<T> ele = new Node<T>(num);
if(head == null){
System.out.println("Underflow");
return;
}
Node<T> temp = head;
while(temp != null)
{
if(temp.data == num){
temp.previous.next = temp.next;
return;
}
else
temp = temp.next;
}
size--;
}
You should modify inside your while loop like this:
while(temp != null)
{
if(temp.data == num) {
if(temp.previous != null) {
temp.previous.next = temp.next;
}
// you have to link-up the next's previous with temp's previous too
if(temp.next != null) {
temp.next.previous = temp.previous;
}
temp = null; // to deference the node and let garbage collector to delete/clear this node
break; // don't return here otherwise size-- won't execute
}
temp = temp.next;
}
Before referencing temp.next and temp.previous as lvalue you should check whether they are null otherwise it will throw NullPointerException.
Hope it helps!
You have to find Node object whose data is equal to T num. Use such loop:
for (Node<T> x = first; x != null; x = x.next) {
if (num.equals(x.data)) {
unlink(x);
return true;
}
}
Where first is pointer to first node. In method remove you have to unlink found Node x from linked list:
T remove(Node<T> x) {
// assert x != null;
final T element = x.data;
final Node<T> next = x.next;
final Node<T> prev = x.prev;
if (prev == null) {
//if x is first node(head)
first = next;
} else {
// link x.next to x.prev.next
prev.next = next;
//unlink x
x.prev = null;
}
if (next == null) {
//if x is last node(tail)
last = prev;
} else {
// link x.prev to x.next.prev
next.prev = prev;
//unlink x
x.next = null;
}
// reset data
x.data = null;
size--;
return element;
}

Delete multiple nodes from linked list java

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;
}
}

Categories

Resources