Question

In: Computer Science

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():

  1. 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

  1. Print the complete list using toString() method.
  2. Create another list using AddTail(). The list must be in the order shown below.

Student_ID

Student_Name

00236

Salman

00663

Suliman

00998

Abdulrahman

  1. Print the complete list using toString() method.
  2. Delete head note from both list.
  3. Print the both list using toString() method.

in java

Solutions

Expert Solution

Here is the completed code for this problem. Go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// StudentLinkedList.java

public class StudentLinkedList {

      //nodes to represent head and tail

      private StudentNode head;

      private StudentNode tail;

      //current size of the list

      private int size;

     

      //constructor initializing empty list

      public StudentLinkedList() {

            head = null;

            tail = null;

            size = 0;

      }

     

      //method to add a student to the head

      public void addHead(String name, String id) {

            //creating a new node with given name and id

            StudentNode node = new StudentNode(name, id);

            //if this is first value, adding as both head and tail

            if (size == 0) {

                  head = node;

                  tail = node;

            } else {

                  //adding before head, and updating head

                  node.next = head;

                  head = node;

            }

            //updating size

            size++;

      }

     

      //method to add a student to the tail

      public void addTail(String name, String id) {

            //creating a new node with given name and id

            StudentNode node = new StudentNode(name, id);

            //if this is first value, adding as both head and tail

            if (size == 0) {

                  head = node;

                  tail = node;

            } else {

                  //adding after tail, updating tail

                  tail.next = node;

                  tail = node;

            }

            size++;

      }

     

      //removes a node from the head, if not empty

      public void deleteHead() {

            //proceeding only if list is not empty

            if (size > 0) {

                  //removing head node

                  head = head.next;

                  //setting tail to null if head become null

                  if (head == null) {

                        tail = null;

                  }

                  //updating size

                  size--;

            }

      }

     

      //returns a String representation of the list

      public String toString() {

            String data = "";

            //appending each node's toString() value to a String and returning it

            for (StudentNode node = head; node != null; node = node.next) {

                  data += node.toString() + " ";

            }

            return data;

      }

     

      //an inner class representing a node in the linked list

      class StudentNode {

            //attributes

            String name;

            String id;

            StudentNode next;

           

            //constructor taking name and id of a student

            public StudentNode(String name, String id) {

                  this.name = name;

                  this.id = id;

                  next = null;

            }

           

            //returns a String containing name and id details

            public String toString() {

                  return "[name: " + name + ", id: " + id + "]";

            }

      }//end of StudentNode class

     

      //main method for testing

      public static void main(String[] args) {

            //creating a list, adding some students using addHead() method

            StudentLinkedList list1 = new StudentLinkedList();

            list1.addHead("Mohammad", "00235");

            list1.addHead("Ahmed", "00662");

            list1.addHead("Ali", "00999");

            list1.addHead("Fahad", "00171");

            //printing the list

            System.out.println(list1);

                 

            //creating a list, adding some students using addTail() method

            StudentLinkedList list2 = new StudentLinkedList();

            list2.addTail("Salman", "00236");

            list2.addTail("Suliman", "00663");

            list2.addTail("Abdulrahman", "00998");

            //printing the list

            System.out.println(list2);

      }

} //end of StudentLinkedList class

/*OUTPUT*/

[name: Fahad, id: 00171] [name: Ali, id: 00999] [name: Ahmed, id: 00662] [name: Mohammad, id: 00235]

[name: Salman, id: 00236] [name: Suliman, id: 00663] [name: Abdulrahman, id: 00998]


Related Solutions

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...
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...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
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...
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...
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++; }...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer data element. Initialize the Min-heap to contain 5 nodes, with the values 8, 12, 24, 32, 42. The program should allow for the insertion and deletion of nodes while maintaining a Min-Heap. The program should allow the user to output data in Preorder, Inorder and Postorder. The program should loop with menu items for each of the above objectives and the choice to quit.
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer data element. Initialize the Min-heap to contain 5 nodes, with the values 8, 12, 24, 32, 42. The program should allow for the insertion and deletion of nodes while maintaining a Min-Heap. The program should allow the user to output data in Preorder, Inorder and Postorder. The program should loop with menu items for each of the above objectives and the choice to quit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT