Implement a single link list with the following data and methods. public class L
ID: 3857102 • Letter: I
Question
Implement a single link list with the following data and methods. public class LNode { int STDnum: string STDname: double gpa: LNode next: public ListNode(int stdnum, string sname, double sgpa, sLNode snext) { STDnum = stdnum: STDname = sname: gpa = sgpa: Next = snext: } } Write the methods: 1. Add a student: input all the data variables from the user. Add the student at the end of the list. Handle all the possible cases for addition. 2. Remove the student from the end of the list. Handle all possible cases for deletion. 3. Print the traversal of the existing list. 4. Print the size of the list. Modify the Add and Remove methods in question 1 such that: a. Add a student anywhere in the list at the user specified index b. Remove a student anywhere in the list at the user specified index. Modify the list in Q1 to a doubly link list by adding LNode prex: And inplement the methods in Q1 and Q2 according to that.Explanation / Answer
Given code for all the questions
Question 1
public class StudentList{
private LNode head;
private int size;
class LNode {
int STDnum;
String STDname;
double gpa;
LNode next;
public LNode(int stdnum, String sname, double sgpa, LNode snext){
STDnum = stdnum;
STDname = sname;
gpa = sgpa;
next = snext;
}
}
public StudentList(){
size = 0;
head = null;
}
//add student to end of list
public void AddStudent(int stdnum, String sname, double sgpa){
LNode n = new LNode(stdnum, sname, sgpa, null);
if(head == null) //empty list
head = n;
else{
LNode curr = head;
while(curr.next != null)
curr = curr.next;
curr.next = n;
}
size++;
}
//remove student at end
public void Remove(){
if(head == null)//empty list nothing to do
return;
if(head.next == null) //single element
head = null;
else{
LNode prev = null, curr = head;
while(curr.next != null){
prev = curr;
curr = curr.next;
}
prev.next = null;
}
size--;
}
//prints the list
public void print()
{
LNode curr = head;
while(curr != null){
System.out.println(curr.STDnum + " " + curr.STDname + " " + curr.gpa);
curr = curr.next;
}
}
//returns size
public int size(){
return size;
}
}
Question 2
public class StudentList{
private LNode head;
private int size;
class LNode {
int STDnum;
String STDname;
double gpa;
LNode next;
public LNode(int stdnum, String sname, double sgpa, LNode snext){
STDnum = stdnum;
STDname = sname;
gpa = sgpa;
next = snext;
}
}
public StudentList(){
size = 0;
head = null;
}
//add student at specified index if index is witin the size of the list
public void AddStudent(int index, int stdnum, String sname, double sgpa){
int currIdx = 0;
LNode prev = null, curr = head;
while(currIdx < index && curr != null){
prev = curr;
curr = curr.next;
}
//found a matching current node at specifid index
if(curr != null){
LNode n = new LNode(stdnum, sname, sgpa, null);
if(curr == head){ //inserting node at beginning
n.next = head;
head = n;
}
else{
prev.next = n;
n.next = curr;
}
size++;
}
}
//remove student at end
public void Remove(int index){
int currIdx = 0;
LNode prev = null, curr = head;
while(currIdx < index && curr != null){
prev = curr;
curr = curr.next;
}
//found a matching current node at specifid index
if(curr != null){
if(curr == head){ //deleting head node
head = head.next;
}
else{
prev.next = curr.next;
}
size--;
}
}
//prints the list
public void print()
{
LNode curr = head;
while(curr != null){
System.out.println(curr.STDnum + " " + curr.STDname + " " + curr.gpa);
curr = curr.next;
}
}
//returns size
public int size(){
return size;
}
}
Question 3
public class StudentList{
private LNode head;
private int size;
class LNode {
int STDnum;
String STDname;
double gpa;
LNode next;
LNode prev;
public LNode(int stdnum, String sname, double sgpa, LNode snext, LNode sprev){
STDnum = stdnum;
STDname = sname;
gpa = sgpa;
next = snext;
prev = sprev;
}
}
public StudentList(){
size = 0;
head = null;
}
//add student at specified index if index is witin the size of the list
public void AddStudent(int index, int stdnum, String sname, double sgpa){
int currIdx = 0;
LNode curr = head;
while(currIdx < index && curr != null){
curr = curr.next;
}
//found a matching current node at specifid index
if(curr != null){
LNode n = new LNode(stdnum, sname, sgpa, null, null);
if(curr == head){ //inserting node at beginning
n.next = head;
head.prev = n;
head = n;
}
else{
curr.prev.next = n;
n.prev = curr.prev;
n.next = curr;
curr.prev = n;
}
size++;
}
}
//remove student at end
public void Remove(int index){
int currIdx = 0;
LNode curr = head;
while(currIdx < index && curr != null){
curr = curr.next;
}
//found a matching current node at specifid index
if(curr != null){
if(curr == head){ //deleting head node
head = head.next;
head.prev = null;
}
else{
curr.prev.next = curr.next;
curr.next.prev = curr.prev;
}
size--;
}
}
//prints the list
public void print()
{
LNode curr = head;
while(curr != null){
System.out.println(curr.STDnum + " " + curr.STDname + " " + curr.gpa);
curr = curr.next;
}
}
//returns size
public int size(){
return size;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.