Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a doubly linked list A department needs to manipulate student records. Th

ID: 3763819 • Letter: C

Question

Create a doubly linked list

A department needs to manipulate student records. The basic operations include add, remove, search and display the data. This system is implemented by using a doubly linked circular list. See the detail requirements for the implementation.

 

Detail Requirements:

   The system will be implemented by a doubly circular linked list. The following classes should be created in this system.

Student class - conta ins two class fields only and several methods:

name - The first and last names are in one string

id - It is a number generated when inserting the object into the list. It must be a unique number.

set/get methods

toString() method in this class to allow the list class to display the content of an object. Organize the data properly for displaying.

Node class - represents a node in the list. The data part contains one object of the Student class. The link part contains two links, next and previous.

CircularList class - The circular list class. It is defined as follow:

The class has two instance variables only:

cursor  (Node type) - always point to the current element in the list

[Note: There are no ‘first' and ‘last' in the list and you should not define another pointer ‘current' in any cases. The ‘cursor' is the ‘current'.]

Explanation / Answer

Complete Program:

// File: Student.java
public class Student
{
private String name;
private int id;

public Student()
{
  name = "";
  id = 0;
}

public Student(String sname, int sid)
{
  name = sname;
  id = sid;
}

public void setName(String sname)
{
  name = sname;
}

public void setID(int sid)
{
  id = sid;
}

public String getName()
{
  return name;
}

public int getID()
{
  return id;
}

public String toString()
{
  return "Name: " + name + ", ID: " + id;
}
}


// File: Node.java
public class Node
{
private Student data;
private Node next;
private Node previous;

public Node()
{
  data = null;
  next = null;
  previous = null;  
}

public Node(Student data)
{
  this(data, null, null);
}

public Node(Student data, Node next, Node previous)
{
  this.data = data;
  this.next = next;
  this.previous = previous;
}

public Student getData()
{
  return data;
}

public void setData(Student data)
{
  this.data = data;
}

public Node getNext()
{
  return next;
}

public void setNext(Node next)
{
  this.next = next;
}

public Node getPrevious()
{
  return previous;
}

public void setPrevious(Node previous)
{
  this.previous = previous;
}
}


// File: CircularList.java
public class CircularList
{
private Node head;
private Node cursor;

public CircularList()
{
  head = null;
  cursor = null;
}

public void insertFirst(Student data)
{
  Node newNode = new Node(data);
  
  if(head == null)
  {
   newNode.setNext(newNode);
   newNode.setPrevious(newNode);
   head = newNode;
  }
  else
  {
   cursor = head.getPrevious();
   cursor.setNext(newNode);
   newNode.setPrevious(cursor);
   newNode.setNext(head);
   head.setPrevious(newNode);
   head = newNode;
  }
}

public void insertLast(Student data)
{
  Node newNode = new Node(data);
  
  if(head == null)
  {
   newNode.setNext(newNode);
   newNode.setPrevious(newNode);
   head = newNode;
  }
  else
  {
   cursor = head.getPrevious();
   cursor.setNext(newNode);
   newNode.setNext(head);
   head.setPrevious(newNode);
   newNode.setPrevious(cursor);
  }
}

public void insertAtPosition(Student data, int position)
{
  if(position < 0 || position == 0)
  {
   insertFirst(data);
  }
  else if(position > getSize() || position == getSize())
  {
   insertLast(data);
  }
  else
  {
   cursor = head;
   Node newNode = new Node(data);
   for(int i = 0; i < position; i++)
   {
    cursor = cursor.getNext();
   }
   
   newNode.setNext(cursor.getNext());
   cursor.getNext().setPrevious(newNode);
   cursor.setNext(newNode);
   newNode.setPrevious(cursor);
  }
}

public void removeFirst()
{
  if(head == null)
  {
   System.out.println("List is empty!");
  }
  else
  {
   cursor = head.getNext();
   head.getPrevious().setNext(cursor);
   cursor.setPrevious(head.getPrevious());
   head = cursor;
  }
}

public void removeLast()
{
  if(head == null)
  {
   System.out.println("List is empty!");
  }
  else
  {
   cursor = head.getPrevious();
   cursor.getPrevious().setNext(head);
   head.setPrevious(cursor.getPrevious());
   cursor = null;
  }
}

public Student removeAtPosition(int position)
{
  Student data = null;
  
  if(position == 0)
  {
   data = head.getData();
   removeFirst();
  }
  else if(position == getSize() - 1)
  {
   data = head.getPrevious().getData();
   removeLast();
  }
  else
  {
   cursor = head;
   
   for(int i = 0; i < position; i++)
   {
    cursor = cursor.getNext();
   }
   
   data = cursor.getData();
   Node node = cursor.getNext();
   node.setPrevious(cursor.getPrevious());
   cursor.getPrevious().setNext(node);
   cursor = null;
  }
  
  return data;
}

public boolean isEmpty()
{
  return head == null;
}

public int getSize()
{
  int count = 0;
  
  if(head == null)
  {
   return count;
  }
  else
  {
   cursor = head;
   
   do
   {
    cursor = cursor.getNext();
    count++;
   }while(cursor != head);
  }
  return count;
}

public void printList()
{
  if(head == null)
  {
   System.out.println("List is empty!");
  }
  else
  {
   cursor = head;
   
   do
   {
    System.out.print(cursor.getData() + " <-> ");
    cursor = cursor.getNext();
   }while(cursor != head);
   System.out.print(cursor.getData());
  }
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote