Question

In: Computer Science

Assume that a singly linked list is implemented with a header node, but no tail node,...

Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to
a. return the size of the linked list
b. print the linked list
c. test if a value x is contained in the linked list
d. add a value x if it is not already contained in the linked list
e. remove a value x if it is contained in the linked list

Solutions

Expert Solution

All the methods are testes using switch case

Source Code

import java.util.*;
public class SinglyLinkedList {    
    class Node{    
        int data;    
        Node next;    
            
        public Node(int data) {    
            this.data = data;    
            this.next = null;    
        }    
    }    
     
    public Node head = null;    
   
    public int size() {       
        Node current = head;    
        int c=0;
        if(head == null) {    
                
            return 0;    
        }    
           
        while(current != null) {     
            c++;  
            current = current.next;    
        }    
        return c;  
    }    
    public void print() {    
        Node current = head;    
        if(head == null) {    
            System.out.println("List is empty");    
            return;    
        }    
           
        System.out.println("Nodes of singly linked list: ");    
        while(current != null) {    
            System.out.print(current.data + " ");    
            current = current.next;    
        }    
        System.out.println();    
    }    
    public int test(int x) {       
        Node current = head;    
        int f=0;
        
            while(current != null) {      
                if(current.data==x)
                    f=1;
                current = current.next;    
            }    
        
        return f;
    } 
    public void addtest(int x) {    
            Node newNode = new Node(x); 
            if(test(x)==0)
            {
                if(head==null)
                {
                    
                    head = newNode;
                    
                }
                
                Node last = head;  
                while (last.next != null) 
                    last = last.next; 
                last.next = newNode; 
                newNode.next=null;
            }
            else
            {
                System.out.println("Already present");
            }
    } 
    public void deleteNode(int key)
    {
        Node temp = head, prev = null;
        if (temp != null && temp.data == key)
        {
            head = temp.next;
            return;
        }
        while (temp != null && temp.data != key)
        {
            prev = temp;
            temp = temp.next;
        }    
        if (temp == null) 
            return;
        prev.next = temp.next;
    }
    public static void main() {    
            
        SinglyLinkedList sl = new SinglyLinkedList();    
        Scanner sc=new Scanner(System.in);   
        while(true)
        {
            System.out.println("Enter 1 to return size of linked list\nEnter 2 to print the linked list\nEnter 3 to test if value x is contained in linked list\nEnter 4 to add a value x if it is not already contained in linked list\nEnter 5 to remove a value x if it is contained in the list");
            System.out.println("Enter 0 to exit");
            System.out.println("Enter your choice");
            int ch=sc.nextInt();
            switch(ch)
            {
             case 0:System.exit(0);
             case 1:System.out.println(sl.size());break;
             case 2:sl.print();break;
             case 3:System.out.println("Enter value to test");
                    int x=sc.nextInt();
                    int f=sl.test(x);
                    if(f==1)
                       System.out.println("Value "+ x +" is contained in the linked list");
                    else
                        System.out.println("Value is not contained in the linked list");
                    break;
             case 4:System.out.println("Enter value to add");
                    int a=sc.nextInt();
                    sl.addtest(a);break;
             case 5:System.out.println("Enter value to remove");
                    int b=sc.nextInt();
                    sl.deleteNode(b);break;
             default:System.out.println("Wrong Input");break;
            }
        }
    }    
}    

Output

Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
1
0
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
2
List is empty
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
4
Enter value to add
1
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
1
1
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
2
Nodes of singly linked list: 
1 
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
4
Enter value to add
2
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
2
Nodes of singly linked list: 
1 2 
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
1
2
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
3
Enter value to test
1
Value 1 is contained in the linked list
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
3
Enter value to test
3
Value is not contained in the linked list
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
3
Enter value to test
2
Value 2 is contained in the linked list
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
5
Enter value to remove
2
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
2
Nodes of singly linked list: 
1 
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
1
1
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
0

Related Solutions

Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
8. Assume you have a singly linked list with no tail pointer. Implement removeTail(). Raise an...
8. Assume you have a singly linked list with no tail pointer. Implement removeTail(). Raise an exception of the method is called on an empty list. template<typename Object> class LinkedList { private: class Node { Object data; Node* next; }; Node *head; public: LinkedList() : head(nullptr) {} Object removeTail(Object data); }; 9. What are iterators? What purpose do they serve? 10. What does it mean to invalidate an iterator? 11. Explain the difference between separate chaining and open addressing in...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
Part 2- - Create and display a singly Linked List with 5 elements (see below)                 Node*...
Part 2- - Create and display a singly Linked List with 5 elements (see below)                 Node* head                  Node*second                  Node*third                 Node*forth                  Node*fifth 2-Assign the data 5, 6, 8, 10, 12 to each node 3-Display the output GIVEN CODE: #include <studio.h> struct Array { int A[10]; int size; int length; }; void Display(struct Array arr) {      int i; printf("\nElements are\n");      for(i=0;ilengthsize) arr->A[arr->length++]=x; } void Insert(struct Array *arr,int index,int x) {      int i;          if(index>=0 && index <=arr->length) {      for(i=arr->length;i>index;i--)      arr->A[i]=arr->A[i-1]; arr->A[index]=x; arr->length++; }...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT