Question

In: Computer Science

Design, Develop and Implement the following operations on Singly Linked List (SLL) with a single field...

  1. Design, Develop and Implement the following operations on Singly Linked List (SLL) with a single field data

a.   Create a SLL for N Data by using front insertion.

b.   Display the status of SLL and count the number of nodes in it

c.   Perform Insertion and Deletion at End of SLL

d.   Perform Insertion at the third position.

e.    Delete the element at the Front of SLL

f.     Perform Deletion at second position of SLL

g.     Display the content.

  1. Design, Develop and Implement a menu driven Program in Java for the following operations on Singly

Linked List (SLL) of Student Data with the fields: USN, Name, Branch, Sem, PhNo

a.   Create a SLL of N Students Data by using front insertion.

b.   Display the status of SLL and count the number of nodes in it

c.   Perform Insertion and Deletion at End of SLL

d.   Perform Insertion and Deletion at Front of SLL

e.     Display the content.

  1. Design, Develop and Implement a Singly Linked List (SLL) with a single string field name using the class Linked List
  1. Create a SLL for N Data by using front insertion
  2. Insert an element at position first, 3 and last.
  3. Delete an element from first, 3 and last.
  4. Display the content

  1. Design, Develop and Implement a Circular Linked List (CLL) with a single field data
    1. Create a CLL for N Data by using front insertion
    2. Display the content.

  1. Design, Develop and Implement a Doubly Linked List (DLL) with a single field data

a.    Create a DLL for N Data by using front insertion.

b.   Perform Insertion and Deletion at End of DLL

d.   Perform Insertion at the third position.

e.     Delete the element in the Front of SLL

f.     Perform Deletion at second position of SLL

g.     Display the content.

Solutions

Expert Solution

// A complete working Java program on linked list 
class LinkedList 
{ 
    Node head;  // head of list 
  
    /* Linked list Node*/
    class Node 
    { 
        int data; 
        Node next; 
        Node(int d) {data = d; next = null; } 
    } 
  
    /* Inserts a new Node at front of the list. */
    public void push(int new_data) 
    { 
        /* 1 & 2: Allocate the Node & 
                  Put in the data*/
        Node new_node = new Node(new_data); 
  
        /* 3. Make next of new Node as head */
        new_node.next = head; 
  
        /* 4. Move the head to point to new Node */
        head = new_node; 
    } 
  
    /* Inserts a new node after the given prev_node. */
    public void insertAfter(Node prev_node, int new_data) 
    { 
        /* 1. Check if the given Node is null */
        if (prev_node == null) 
        { 
            System.out.println("The given previous node cannot be null"); 
            return; 
        } 
  
        /* 2 & 3: Allocate the Node & 
                  Put in the data*/
        Node new_node = new Node(new_data); 
  
        /* 4. Make next of new Node as next of prev_node */
        new_node.next = prev_node.next; 
  
        /* 5. make next of prev_node as new_node */
        prev_node.next = new_node; 
    } 
     
    /* Appends a new node at the end.  This method is  
       defined inside LinkedList class shown above */
    public void append(int new_data) 
    { 
        /* 1. Allocate the Node & 
           2. Put in the data 
           3. Set next as null */
        Node new_node = new Node(new_data); 
  
        /* 4. If the Linked List is empty, then make the 
              new node as head */
        if (head == null) 
        { 
            head = new Node(new_data); 
            return; 
        } 
  
        /* 4. This new node is going to be the last node, so 
              make next of it as null */
        new_node.next = null; 
  
        /* 5. Else traverse till the last node */
        Node last = head;  
        while (last.next != null) 
            last = last.next; 
  
        /* 6. Change the next of last node */
        last.next = new_node; 
        return; 
    } 
  
    /* This function prints contents of linked list starting from 
        the given node */
    public void printList() 
    { 
        Node tnode = head; 
        while (tnode != null) 
        { 
            System.out.print(tnode.data+" "); 
            tnode = tnode.next; 
        } 
    } 
    
    /* Returns count of nodes in linked list */
    public int getCount() 
    { 
        Node temp = head; 
        int count = 0; 
        while (temp != null) 
        { 
            count++; 
            temp = temp.next; 
        } 
        return count; 
    } 
    
    // Function to remove the last node 
    // of the linked list / 
    public Node removeLastNode() 
    { 
        if (head == null) 
            return null; 
  
        if (head.next == null) { 
            return null; 
        } 
  
        // Find the second last node 
        Node second_last = head; 
        while (second_last.next.next != null) 
            second_last = second_last.next; 
  
        // Change next of second last 
        second_last.next = null; 
  
        return head; 
    } 
    
    // Function to remove the first node 
    // of the linked list / 
    public Node removeFirstNode() 
    { 
        if (head == null) 
            return null; 
  
        // Move the head pointer to the next node 
        Node temp = head; 
        head = head.next; 
  
        return head; 
    } 
    
    /* Given a reference (pointer to pointer) to the head of a list 
       and a position, deletes the node at the given position */
    public void deleteNode(int position) 
    { 
        // If linked list is empty 
        if (head == null) 
            return; 
  
        // Store head node 
        Node temp = head; 
  
        // If head needs to be removed 
        if (position == 0) 
        { 
            head = temp.next;   // Change head 
            return; 
        } 
  
        // Find previous node of the node to be deleted 
        for (int i=0; temp!=null && i<position-1; i++) 
            temp = temp.next; 
  
        // If position is more than number of nodes 
        if (temp == null || temp.next == null) 
            return; 
  
        // Node temp->next is the node to be deleted 
        // Store pointer to the next of node to be deleted 
        Node next = temp.next.next; 
  
        temp.next = next;  // Unlink the deleted node from list 
    } 
}
 
 class Main{ 
    /* Driver program to test above functions. Ideally this function 
       should be in a separate user class.  It is kept here to keep 
       code compact */
    public static void main(String[] args) 
    { 
        /* Start with the empty list */
        LinkedList llist = new LinkedList(); 
  
        // Insert 6.  So linked list becomes 6->NUllist 
        llist.append(6); 
  
        // Insert 7 at the beginning. So linked list becomes 
        // 7->6->NUllist 
        llist.push(7); 
  
        // Insert 1 at the beginning. So linked list becomes 
        // 1->7->6->NUllist 
        llist.push(1); 
  
        System.out.println("Created Linked list is: "); 
        llist.printList();
        System.out.println("");
        System.out.println("Total No of Nodes: " + llist.getCount());
        
        // Insert 4 at the end. So linked list becomes 
        // 1->7->6->4->NUllist 
        llist.append(4);
        // Delete 4 at the end. So linked list becomes 
        // 1->7->6->NUllist 
        llist.removeLastNode();
        System.out.println("List After Insertion And Removing At Last: ");
        llist.printList();
        // Insert 8, after 7. So linked list becomes 
        // 1->7->8->6->NUllist 
        llist.insertAfter(llist.head.next, 8);
        System.out.println("");
        System.out.println("List After Insertion At 3rd Position: ");
        llist.printList();
        // Delete first node. So linked list becomes 
        // 7->8->6->NUllist 
        llist.removeFirstNode();
        System.out.println("");
        System.out.println("List After Deleting first Node: ");
        llist.printList();
        // Delete node At Position 2. So linked list becomes 
        // 7->8->NUllist 
        llist.deleteNode(2);
        System.out.println("");
        System.out.println("List After Deleting Node At Position 2: ");
        llist.printList();
    } 
} 

Output for first program:

Second Program Linked List For Student details:

// A complete working Java program on linked list 
class LinkedList 
{ 
    Node head;  // head of list 
  
    /* Linked list Node*/
    class Node 
    { 
        String USN; 
        String Name;
        String Branch;
        int sem;
        int phoneNo;
        Node next; 
        Node(String USN,String Name,String Branch,int sem,int phoneNo) { 
            this.USN = USN;
            this.Name = Name;
            this.Branch = Branch;
            this.sem = sem;
            this.phoneNo = phoneNo;
            next = null;
        } 
    } 
  
    /* Inserts a new Node at front of the list. */
    public void push(String USN,String Name,String Branch,int sem,int phoneNo) 
    { 
        /* 1 & 2: Allocate the Node & 
                  Put in the data*/
        Node new_node = new Node(USN,Name,Branch,sem,phoneNo); 
  
        /* 3. Make next of new Node as head */
        new_node.next = head; 
  
        /* 4. Move the head to point to new Node */
        head = new_node; 
    } 
     
    /* Appends a new node at the end.  This method is  
       defined inside LinkedList class shown above */
    public void append(String USN,String Name,String Branch,int sem,int phoneNo) 
    { 
        /* 1. Allocate the Node & 
           2. Put in the data 
           3. Set next as null */
        Node new_node = new Node(USN,Name,Branch,sem,phoneNo); 
  
        /* 4. If the Linked List is empty, then make the 
              new node as head */
        if (head == null) 
        { 
            head = new Node(USN,Name,Branch,sem,phoneNo); 
            return; 
        } 
  
        /* 4. This new node is going to be the last node, so 
              make next of it as null */
        new_node.next = null; 
  
        /* 5. Else traverse till the last node */
        Node last = head;  
        while (last.next != null) 
            last = last.next; 
  
        /* 6. Change the next of last node */
        last.next = new_node; 
        return; 
    } 
  
    /* This function prints contents of linked list starting from 
        the given node */
    public void printList() 
    { 
        Node tnode = head; 
        while (tnode != null) 
        { 
            String USN;String Name;String Branch;int sem;int phoneNo;
            System.out.print("USN: "+ tnode.USN+" Name: "+tnode.Name+" Branch: "+tnode.Branch+" Sem: "+tnode.sem+" Phone No: "+tnode.phoneNo);
            System.out.println("");
            tnode = tnode.next; 
        } 
    } 
    
    /* Returns count of nodes in linked list */
    public int getCount() 
    { 
        Node temp = head; 
        int count = 0; 
        while (temp != null) 
        { 
            count++; 
            temp = temp.next; 
        } 
        return count; 
    } 
    
    // Function to remove the last node 
    // of the linked list / 
    public Node removeLastNode() 
    { 
        if (head == null) 
            return null; 
  
        if (head.next == null) { 
            return null; 
        } 
  
        // Find the second last node 
        Node second_last = head; 
        while (second_last.next.next != null) 
            second_last = second_last.next; 
  
        // Change next of second last 
        second_last.next = null; 
  
        return head; 
    } 
    
    // Function to remove the first node 
    // of the linked list / 
    public Node removeFirstNode() 
    { 
        if (head == null) 
            return null; 
  
        // Move the head pointer to the next node 
        Node temp = head; 
        head = head.next; 
  
        return head; 
    } 
    
}
 
 class Main{ 
    /* Driver program to test above functions. Ideally this function 
       should be in a separate user class.  It is kept here to keep 
       code compact */
    public static void main(String[] args) 
    { 
        /* Start with the empty list */
        LinkedList llist = new LinkedList(); 
  
        // Insert s1.  So linked list becomes s1->NUllist 
        llist.append("A","Test","B1",1,123); 
  
        // Insert s2 at the beginning. So linked list becomes 
        // s2->s1->NUllist 
        llist.push("B","Test2","B2",2,1234); 
  
        // Insert s3 at the beginning. So linked list becomes 
        // s3->s2->s1->NUllist 
        llist.push("C","Test3","B3",3,12345); 
  
        System.out.println("Created Linked list is: "); 
        llist.printList();
        System.out.println("");
        System.out.println("Total No of Nodes: " + llist.getCount());
        
        // Insert s4 at the end. So linked list becomes 
        // s3->s2->s1->s4->NUllist 
        llist.append("D","Test4","B4",4,123456);
        // Delete s4 at the end. So linked list becomes 
        // s3->s2->s1->NUllist 
        llist.removeLastNode();
        System.out.println("List After Insertion And Removing At Last: ");
        llist.printList();
        // Delete first node. So linked list becomes 
        // s2->s1->NUllist 
        llist.removeFirstNode();
        System.out.println("");
        System.out.println("List After Deleting first Node: ");
        llist.printList();
        // Insert node at start. So linked list becomes 
        // s3->s2->s1->NUllist
        llist.push("E","Test5","B5",5,1234567);
        System.out.println("");
        System.out.println("List After Adding Node At Front: ");
        llist.printList();
    } 
} 

Output for second program:

Third Porgram to store string :

// A complete working Java program on linked list 
class LinkedList 
{ 
    Node head;  // head of list 
  
    /* Linked list Node*/
    class Node 
    {  
        String Name;
        Node next; 
        Node(String Name) { 
            this.Name = Name;
            next = null;
        } 
    } 
  
    /* Inserts a new Node at front of the list. */
    public void push(String Name) 
    { 
        /* 1 & 2: Allocate the Node & 
                  Put in the data*/
        Node new_node = new Node(Name); 
  
        /* 3. Make next of new Node as head */
        new_node.next = head; 
  
        /* 4. Move the head to point to new Node */
        head = new_node; 
    } 
     
    /* Appends a new node at the end.  This method is  
       defined inside LinkedList class shown above */
    public void append(String Name) 
    { 
        /* 1. Allocate the Node & 
           2. Put in the data 
           3. Set next as null */
        Node new_node = new Node(Name); 
  
        /* 4. If the Linked List is empty, then make the 
              new node as head */
        if (head == null) 
        { 
            head = new Node(Name); 
            return; 
        } 
  
        /* 4. This new node is going to be the last node, so 
              make next of it as null */
        new_node.next = null; 
  
        /* 5. Else traverse till the last node */
        Node last = head;  
        while (last.next != null) 
            last = last.next; 
  
        /* 6. Change the next of last node */
        last.next = new_node; 
        return; 
    } 
  
    /* This function prints contents of linked list starting from 
        the given node */
    public void printList() 
    { 
        Node tnode = head; 
        while (tnode != null) 
        { 
            String Name;
            System.out.print(tnode.Name + " ");
            tnode = tnode.next; 
        } 
    } 
    
    /* Returns count of nodes in linked list */
    public int getCount() 
    { 
        Node temp = head; 
        int count = 0; 
        while (temp != null) 
        { 
            count++; 
            temp = temp.next; 
        } 
        return count; 
    } 
    
    // Function to remove the last node 
    // of the linked list / 
    public Node removeLastNode() 
    { 
        if (head == null) 
            return null; 
  
        if (head.next == null) { 
            return null; 
        } 
  
        // Find the second last node 
        Node second_last = head; 
        while (second_last.next.next != null) 
            second_last = second_last.next; 
  
        // Change next of second last 
        second_last.next = null; 
  
        return head; 
    } 
    
    // Function to remove the first node 
    // of the linked list / 
    public Node removeFirstNode() 
    { 
        if (head == null) 
            return null; 
  
        // Move the head pointer to the next node 
        Node temp = head; 
        head = head.next; 
  
        return head; 
    } 
    
    // function to create and return a Node 
    public Node GetNode(String data) { 
        return new Node(data); 
    }
    
    // function to insert a Node at required position 
    public Node insertAtPos(int position, String data) { 
        if (position < 1) 
            System.out.print("Invalid position"); 
  
        // if position is 1 then new node is 
        // set infornt of head node 
        // head node is changing. 
        if (position == 1) { 
            Node newNode = new Node(data); 
            newNode.next = head; 
            head = newNode; 
        } else { 
            while (position-- != 0) { 
                if (position == 1) { 
                    // adding Node at required position 
                    Node newNode = GetNode(data); 
  
                    // Making the new Node to point to 
                    // the old Node at the same position 
                    newNode.next = head.next; 
  
                    // Replacing current with new Node 
                    // to the old Node to point to the new Node 
                    head.next = newNode; 
                    break; 
                } 
                head = head.next; 
            } 
            if (position != 1) 
                System.out.print("Position out of range"); 
        } 
        return head; 
    }
    
    /* Given a reference (pointer to pointer) to the head of a list 
       and a position, deletes the node at the given position */
    public void deleteNode(int position) 
    { 
        // If linked list is empty 
        if (head == null) 
            return; 
  
        // Store head node 
        Node temp = head; 
  
        // If head needs to be removed 
        if (position == 0) 
        { 
            head = temp.next;   // Change head 
            return; 
        } 
  
        // Find previous node of the node to be deleted 
        for (int i=0; temp!=null && i<position-1; i++) 
            temp = temp.next; 
  
        // If position is more than number of nodes 
        if (temp == null || temp.next == null) 
            return; 
  
        // Node temp->next is the node to be deleted 
        // Store pointer to the next of node to be deleted 
        Node next = temp.next.next; 
  
        temp.next = next;  // Unlink the deleted node from list 
    } 
    
}
 
 class Main{ 
    /* Driver program to test above functions. Ideally this function 
       should be in a separate user class.  It is kept here to keep 
       code compact */
    public static void main(String[] args) 
    { 
        /* Start with the empty list */
        LinkedList llist = new LinkedList(); 
  
        // Insert A.  So linked list becomes s1->NUllist 
        llist.append("A"); 
  
        // Insert B at the beginning. So linked list becomes 
        // B->A->NUllist 
        llist.push("B"); 
  
        // Insert C at the beginning. So linked list becomes 
        // C->B->A->NUllist 
        llist.push("C"); 
  
        System.out.println("Created Linked list is: "); 
        llist.printList();
        System.out.println("");
        System.out.println("Total No of Nodes: " + llist.getCount());
        
        // Insert D at the position first. So linked list becomes 
        // D->C->B->A->NUllist 
        llist.push("D");
        // Insert E at the position third. So linked list becomes 
        // E->D->C->B->A->NUllist 
        llist.insertAtPos(1,"E");
        // Insert F at the last. So linked list becomes 
        // E->D->C->B->A->F->NUllist 
        llist.append("F");
        System.out.println("List After Insertion At First,Third And Last: ");
        llist.printList();
        // Delete first node. So linked list becomes 
        // D->C->B->NUllist 
        llist.removeFirstNode();
        llist.deleteNode(3);
        llist.removeLastNode();
        System.out.println("");
        System.out.println("List After Deleting First,Third And Last Node: ");
        llist.printList();
    } 
} 

3rd Output:

4th Program (CLL):

class Main 
{
 
static class Node
{
    int data;
    Node next;
};
 
static Node addToEmpty(Node last, int data)
{
    // This function is only for empty list
    if (last != null)
    return last;
 
    // Creating a node dynamically.
    Node temp = new Node();
 
    // Assigning the data.
    temp.data = data;
    last = temp;
 
    // Creating the link.
    last.next = last;
 
    return last;
}
 
static Node addBegin(Node last, int data)
{
    if (last == null)
        return addToEmpty(last, data);
 
    Node temp = new Node();
 
    temp.data = data;
    temp.next = last.next;
    last.next = temp;
 
    return last;
}
 
static void traverse(Node last)
{
    Node p;
 
    // If list is empty, return.
    if (last == null)
    {
        System.out.println("List is empty.");
        return;
    }
 
    // Pointing to first Node of the list.
    p = last.next;
 
    // Traversing the list.
    do
    {
        System.out.print(p.data + " ");
        p = p.next;
 
    }
    while(p != last.next);
 
}
 
// Driven code
public static void main(String[] args)
{
    Node last = null;
 
    last = addBegin(last, 4);
    last = addBegin(last, 2);
    last = addBegin(last, 8);
    last = addBegin(last, 12);
    last = addBegin(last, 10);
 
    System.out.println("Circular Linked List: ");
    traverse(last);
}
}

Output:

Program 5 (DLL):

// Class for Doubly Linked List 
class DLL { 
    Node head; // head of list 
  
    /* Doubly Linked list Node*/
    class Node { 
        int data; 
        Node prev; 
        Node next; 
  
        // Constructor to create a new node 
        // next and prev is by default initialized as null 
        Node(int d) { data = d; } 
    } 
    
    // Function to delete a node in a Doubly Linked List.
    // head_ref --> pointer to head node pointer.
    // del --> data of node to be deleted.
    void deleteNode(Node del)
    {
 
        // Base case
        if (head == null || del == null) {
            return;
        }
 
        // If node to be deleted is head node
        if (head == del) {
            head = del.next;
        }
 
        // Change next only if node to be deleted
        // is NOT the last node
        if (del.next != null) {
            del.next.prev = del.prev;
        }
 
        // Change prev only if node to be deleted
        // is NOT the first node
        if (del.prev != null) {
            del.prev.next = del.next;
        }
 
        // Finally, free the memory occupied by del
        return;
    }
 
    
    //Deleting The last Node
    void deleteNodeAtEnd(){
        if(head == null)  
        {  
            return;
        }  
        Node ptr = head; 
        Node prevPtr = null;
        while( ptr.next != null ){
            prevPtr = ptr;
            ptr = ptr.next;
        }
        prevPtr.next = null;
    }
  
    // Adding a node at the front of the list 
    public void push(int new_data) 
    { 
        /* 1. allocate node  
        * 2. put in the data */
        Node new_Node = new Node(new_data); 
  
        /* 3. Make next of new node as head and previous as NULL */
        new_Node.next = head; 
        new_Node.prev = null; 
  
        /* 4. change prev of head node to new node */
        if (head != null) 
            head.prev = new_Node; 
  
        /* 5. move the head to point to the new node */
        head = new_Node; 
    } 
  
    /* Given a node as prev_node, insert a new node after the given node */
    public void InsertAfter(Node prev_Node, int new_data) 
    { 
  
        /*1. check if the given prev_node is NULL */
        if (prev_Node == null) { 
            System.out.println("The given previous node cannot be NULL "); 
            return; 
        } 
  
        /* 2. allocate node  
        * 3. put in the data */
        Node new_node = new Node(new_data); 
  
        /* 4. Make next of new node as next of prev_node */
        new_node.next = prev_Node.next; 
  
        /* 5. Make the next of prev_node as new_node */
        prev_Node.next = new_node; 
  
        /* 6. Make prev_node as previous of new_node */
        new_node.prev = prev_Node; 
  
        /* 7. Change previous of new_node's next node */
        if (new_node.next != null) 
            new_node.next.prev = new_node; 
    } 
  
    // Add a node at the end of the list 
    void append(int new_data) 
    { 
        /* 1. allocate node  
        * 2. put in the data */
        Node new_node = new Node(new_data); 
  
        Node last = head; /* used in step 5*/
  
        /* 3. This new node is going to be the last node, so 
        * make next of it as NULL*/
        new_node.next = null; 
  
        /* 4. If the Linked List is empty, then make the new 
        * node as head */
        if (head == null) { 
            new_node.prev = null; 
            head = new_node; 
            return; 
        } 
  
        /* 5. Else traverse till the last node */
        while (last.next != null) 
            last = last.next; 
  
        /* 6. Change the next of last node */
        last.next = new_node; 
  
        /* 7. Make last node as previous of new node */
        new_node.prev = last; 
    } 
  
    // This function prints contents of linked list starting from the given node 
    public void printlist(Node node) 
    { 
        Node last = null; 
        System.out.println("Traversal in forward Direction"); 
        while (node != null) { 
            System.out.print(node.data + " "); 
            last = node; 
            node = node.next; 
        } 
        System.out.println(); 
        System.out.println("Traversal in reverse direction"); 
        while (last != null) { 
            System.out.print(last.data + " "); 
            last = last.prev; 
        } 
    }
        
         // Function to delete a node in a Doubly Linked List. 
    // head_ref --> pointer to head node pointer. 
    // del --> pointer to node to be deleted. 
    Node deleteNodeAtGivenPosUtil(Node head, Node del) 
    { 
        // base case 
        if (head == null || del == null) 
            return null; 
  
        // If node to be deleted is head node 
        if (head == del) 
            head = del.next; 
  
        // Change next only if node to be  
        // deleted is NOT the last node 
        if (del.next != null) 
            del.next.prev = del.prev; 
  
        // Change prev only if node to be  
        // deleted is NOT the first node 
        if (del.prev != null) 
            del.prev.next = del.next; 
  
        del = null; 
  
        return head; 
    } 
  
    // Function to delete the node at the 
    // given position in the doubly linked list 
    public void deleteNodeAtGivenPos(Node head, int n) 
    { 
        /* if list in NULL or invalid position is given */
        if (head == null || n <= 0) 
            return; 
  
        Node current = head; 
        int i; 
  
        /* 
        * traverse up to the node at position 'n' from the beginning 
        */
        for (i = 1; current != null && i < n; i++) 
        { 
            current = current.next; 
        } 
          
        // if 'n' is greater than the number of nodes 
        // in the doubly linked list 
        if (current == null) 
            return; 
  
        // delete the node pointed to by 'current' 
        deleteNodeAtGivenPosUtil(head, current); 
    }
}
class Main{
    /* Driver program to test above functions*/
    public static void main(String[] args) 
    { 
        /* Start with the empty list */
        DLL dll = new DLL(); 
  
        // Insert 6. So linked list becomes 6->NULL 
        dll.push(6); 
  
        // Insert 7 at the beginning. So linked list becomes 7->6->NULL 
        dll.push(7); 
  
        // Insert 1 at the beginning. So linked list becomes 1->7->6->NULL 
        dll.push(1);
        System.out.println("Created DLL is: "); 
        dll.printlist(dll.head); 
        System.out.println(""); 
  
        // Insert 4 at the end. So linked list becomes 1->7->6->4->NULL 
        dll.append(4);
        //Delete Node At End 
        dll.deleteNodeAtEnd(); 
        System.out.println("List After Inserting And Deletion At End: "); 
        dll.printlist(dll.head); 
        System.out.println(""); 
        dll.InsertAfter(dll.head.next, 8);
        // Insert 8, at 3rd position. So linked list becomes 1->7->8->6->NULL
        System.out.println("List After Inserting At Third Position: "); 
        dll.printlist(dll.head); 
        System.out.println(""); 
        //Delete First Node 7->8->6->NULL
        dll.deleteNode(dll.head);
        System.out.println("List After Deleting In Front: "); 
        dll.printlist(dll.head); 
        System.out.println(""); 
        dll.deleteNodeAtGivenPos(dll.head,2);
        //Delete First Node 7->6->NULL
        System.out.println("List After Deleting Node At Second Position: "); 
        dll.printlist(dll.head); 
        System.out.println("");
    } 
} 

Output:


Related Solutions

In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last()...
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last() - returns element located at third-to-last in the list - reverse() - reveres the linked list, note, this is not just printing elements in reverse order, this is actually reversing the list
In this homework, you will implement a single linked list to store a list of employees...
In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below: Employee Type Attribute int ID string name string department int salary Employee* next Return Type Function (constructor) Employee(int ID, string name, string department, int salary) EmployeeList class should...
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 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...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT