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.
  3. Print the complete list using toString() method.
  4. Delete head note from both list.
  5. Print the both list using toString() method.

Student_ID

Student_Name

00236

Salman

00663

Suliman

00998

Abdulrahman

  1. Add following methods in the previous lab program
    • DeleteTail()
    • DeleteAll()
    • Duplicate()
    • AddAfter()
    • Length()
    • Find()
    • Reverse()
  1. Demonstrate all the above methods by calling in them in main.

Solutions

Expert Solution

import java.util.*;
class Students implements Cloneable{
   private String student_ID;
   private String student_Name;
   private Students next;
   int length;
  
  
   public Students(String student_ID, String student_Name){
       this.student_ID = student_ID;
       this.student_Name = student_Name;
       this.next = null;
       length = 0;
   }
  
   public String getStudent_ID(){
       return student_ID;
   }
   public String getStudent_Name(){
       return student_Name;
   }
   public Students getNext(){
       return next;
   }
   public int getLength(){
       return length;
   }
  
  
   public void setStudent_ID(String student_ID){
       this.student_ID=student_ID;
   }
   public void setStudent_Name(String student_Name){
       this.student_Name = student_Name;
   }
   public void setNext(Students next){
       this.next = next;
   }
   public void setLength(int length){
       this.length = length;
   }
  
   public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
  
   public String toString(){
       return "Student Id : "+student_ID+", Student Name : "+student_Name+", next-> "+next;
   }
}

public class Test{
  
  
   public static void main(String[] args) throws Exception{
       Students head = addHead();
       Students tail = addTail();
       System.out.println("haed list =>"+head);
       System.out.println("\ntail list =>"+tail);
       deleteTail(tail);
       Students dupicateList = duplicate(head);
       System.out.println("\nduplicate list =>"+tail);
       Students s = new Students("00001","SN");
       addAfter(head, s);
       System.out.println("haed list =>"+head);
       System.out.println("haed list length =>"+length(head));
       Students s1 = find(head,"00001");
       System.out.println("found student name =>"+s1.getStudent_Name());
      
       Students[] sArr=new Students[2];
       sArr[0] = head;
       sArr[1] = tail;
       deleteAll(sArr);
       System.out.println("haed list =>"+head);
       System.out.println("\ntail list =>"+tail);
      
   }
   public static Students addHead(){
       int length = 1;
       Students students1 = new Students("00235","Mohammad");
       Students students2 = new Students("00662","Ahmed");
       Students students3 = new Students("00999","Ali");
       Students students4 = new Students("00171","Fahad");
       students1.setNext(students2);length++;
       students2.setNext(students3);length++;
       students3.setNext(students4);length++;
       students1.setLength(length);
       return students1;
      
   }
  
   public static Students addTail(){
       int length = 1;
       Students students1 = new Students("00236","Salman");
       Students students2 = new Students("00663","Suliman");
       Students students3 = new Students("00998","Abdulrahman");
       students1.setNext(students2);length++;
       students2.setNext(students3);length++;
       students1.setLength(length);
       return students1;
   }
  
   public static void deleteTail(Students list){
       list = null;
   }
  
   public static void deleteAll(Students[] listArr){
      
       for(int i=0; i<listArr.length; i++){
           listArr[i]= null;
       }
      
   }
  
   public static Students duplicate(Students list) throws CloneNotSupportedException {
       Students dupicateList = (Students)list.clone();
       return dupicateList;
   }
  
   public static void addAfter(Students list, Students s){
       int length = list.getLength();
       Students last = null;
       for(int i=0; i<length; i++){
           last = list.getNext();
       }
       last.setNext(s);
       length++;
       list.setLength(length);
   }
  
   public static int length(Students list){
       return list.getLength();
   }
  
   public static Students find(Students list, String student_ID){
       int length = list.getLength();
       Students s = null;
       Students temp = list;
       if(list.getStudent_ID().equals(student_ID) ){
           return list;
       }
       for(int i=1; i<length; i++){
           s = temp.getNext();
           temp = s;
           //System.out.println(s.getStudent_ID());
           if(s.getStudent_ID().equals(student_ID) ){
               return s;
           }
       }
       return s;
   }

  
  
}


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. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
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...
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.
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT