Question

In: Computer Science

class listnodes{ int data;//first part of the node(data) listnodes link;//second part of the node(address) listnodes() {...

class listnodes{
int data;//first part of the node(data)
listnodes link;//second part of the node(address)
listnodes()
{
data=0;
link=null;
}
listnodes(int d,listnodes l)//10|null
{
data=d;
link=l;
}
}
class singlelinkedlist{
void display(listnodes head){
listnodes current=head;
while(current.link!=null){
System.out.print(current.data+"-->");
current=current.link;
}
System.out.print(current.data);
  
}
public listnodes insert(listnodes head,int data)
{
//create new node
listnodes newnode=new listnodes(data,null);
//link the newnode to the head node
newnode.link=head;
//make newnode the first node
head=newnode;
return head;//return the first node
}
//insert at a position(after or before a node)
public listnodes InsertAtPostion(listnodes head,int data,int position){
listnodes newnode=new listnodes(data,null);   
listnodes previous=head;
int count=1;
while(count<=position-1){//(count<position)
previous=previous.link;
count++;
}
// listnodess current=previous.link;
listnodes current=previous;
current=previous.link;
newnode.link=current;
previous.link=newnode;
return head;


}
public listnodes deletefirst(listnodes head){
listnodes temp=head;//rename
head=head.link;//move head
temp.link=null;//temp alone
return temp;
}
public listnodes deletelast(listnodes head){

if(head==null){
return head;
}
listnodes last=head;
listnodes previoustolast=head;
while(last.link!=null){
previoustolast=last;
last=last.link;
}
previoustolast.link=null;
return last;
}
public int length(listnodes head){
listnodes curr=head;
int c=0;
while(curr!=null){
c++;
curr=curr.link;
  
}
return c;
}
public boolean find(listnodes head,int searchkey){
listnodes curr=head;
while(curr!=null){
if(curr.data==searchkey)
{
return true;
}
curr=curr.link;
}
return false;
}
}

public class linkedlist {
public static void main(String[] args) {
//craete first node
listnodes head=new listnodes(10,null);
//create an object of class where all the methods are   
singlelinkedlist sl=new singlelinkedlist();
//insert at postion
sl.InsertAtPostion(head, 30, 1);

//insert at front
listnodes newhead=sl.insert(head,20);
sl.display(newhead);

//delete last
System.out.println(" ");
System.out.println("Delete a node at end");
listnodes l=sl.deletelast(head);
sl.display(l);
System.out.println(" ");
//delete first
System.out.println("\nDelete a node at begining and return head: \n");
listnodes first=sl.deletefirst(head);
sl.display(first);
System.out.println(" \n");
//find length
System.out.println("length is="+sl.length(head));
System.out.println(" ");
//Search a node
System.out.println("Search for a node");
if(sl.find(head, 10)){
System.out.println("key found");}
else
System.out.println("key not found");
}
}

ON THIS CODE

Write an application and perform the following:

-Create at least three classes such as:

1. listnode- for data and link, constructors

2. Singlelinkedlist-for method definition

3. linkedlist-for objects

-Insert a node at tail/end in a linked list.

-and display all the nodes in the list.

-Delete a node at a position in the list

Solutions

Expert Solution

updated singlelinkedlist.java

Add insertLast and deleteAtPosition in this class 

class singlelinkedlist {
        void display(listnodes head) {
                listnodes current = head;
                while (current.link != null) {
                        System.out.print(current.data + "-->");
                        current = current.link;
                }
                System.out.print(current.data);

        }

        public listnodes insert(listnodes head, int data) {
//create new node
                listnodes newnode = new listnodes(data, null);
//link the newnode to the head node
                newnode.link = head;
//make newnode the first node
                head = newnode;
                return head;// return the first node
        }
        
        //insert at last 
        public listnodes inserLast(listnodes head , int data) {
                
                        listnodes temp=head;    
                
                        while(temp.link!=null)
                          temp=temp.link;
        
                        listnodes newnode=new listnodes(data,null);
                        temp.link=newnode;
                
                return head;
        }
        
        
        
        

//insert at a position(after or before a node)
        public listnodes InsertAtPostion(listnodes head, int data, int position) {
                listnodes newnode = new listnodes(data, null);
                listnodes previous = head;
                int count = 1;
                while (count <= position - 1) {// (count<position)
                        previous = previous.link;
                        count++;
                }
// listnodess current=previous.link;
                listnodes current = previous;
                current = previous.link;
                newnode.link = current;
                previous.link = newnode;
                return head;

        }

        public listnodes deletefirst(listnodes head) {
                listnodes temp = head;// rename
                head = head.link;// move head
                temp.link = null;// temp alone
                return temp;
        }

        public listnodes deletelast(listnodes head) {

                if (head == null) {
                        return head;
                }
                listnodes last = head;
                listnodes previoustolast = head;
                while (last.link != null) {
                        previoustolast = last;
                        last = last.link;
                }
                previoustolast.link = null;
                return last;
        }
        
        public listnodes deleteAtPosition(listnodes head,int position) {
                
                int i=1;
                listnodes temp=head;
                
                if(position==1) 
                        deletefirst(head);
                else {

                        while(position-1>i) {
                         temp=temp.link;
                         i++;
                    }
                        
                        temp.link=temp.link.link;       
                }
                
                return head;
        }
        
        

        public int length(listnodes head) {
                listnodes curr = head;
                int c = 0;
                while (curr != null) {
                        c++;
                        curr = curr.link;

                }
                return c;
        }

        public boolean find(listnodes head, int searchkey) {
                listnodes curr = head;
                while (curr != null) {
                        if (curr.data == searchkey) {
                                return true;
                        }
                        curr = curr.link;
                }
                return false;
        }
}
updated linkedlist.java 

for testing purpose added code here : 


import java.util.Scanner;

public class linkedlist {
        public static void main(String[] args) {
//craete first node
//create an object of class where all the methods are   
                singlelinkedlist s1 = new singlelinkedlist();
                listnodes head = new listnodes(10,null);
                s1.inserLast(head,20);
                s1.inserLast(head,30);
                s1.inserLast(head,20);
                s1.inserLast(head,30);
        
                s1.display(head);
                
                System.out.println("\n==========================");

                Scanner sc=new Scanner(System.in);
                System.out.println("Enter position from where elements is to be deleted ");
                int position=sc.nextInt();
                
                s1.deleteAtPosition(head,position);
                
                s1.display(head);
                
        }
}

Sample outputs :


Related Solutions

import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
Please show it with python class Node {     int data;     Node left, right;    ...
Please show it with python class Node {     int data;     Node left, right;     public Node(int item)     {         data = item;         left = right = null;     } } public class BinaryTree { // Root of the tree implemented in Node class Node root; Node findLowestCommonAncestor(int node1, int node2) {     return findLowestCommonAncestor(root, node1, node2); } // This function returns pointer to LCA of two given // values node1 and node2. This function assumes that...
class nodeType                    // class used to implement a node { public:         int data;   &n
class nodeType                    // class used to implement a node { public:         int data;                        // data member of node         nodeType * next;        // pointer member of node }; int main() {         int x;         nodeType * head =________ ;                     // initialize head pointer         nodeType * tail = _______ ;                        // initialize tail pointer _______ * p;                                                 // create an auxiliary pointer to a node         for (x = 0; x < 10; ++x)         {                 p =   _________ nodeType; // create a node ___________ = x + 10;                                // store...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr);...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr. c++
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT