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

Linked List ADT Given Node class, below is an example of implementation for some

ID: 3918226 • Letter: L

Question

Linked List ADT
Given Node class, below is an example of implementation for some linked list operations...
class Node
data-type data
Node next
Node(data-type val)
data = val
next = null/None
// implement get and set methods for data and next
class LinkedList
Node head
Node tail
LinkedList()
Assign null/None to head and tail
addHead(data-type val)
Node n = Node(val)
if (head == null/None) // no elements
head = n
tail = n
else // have elements
set n.next to head
head =n
addTail(data-type val)
Node n = Node(val)
if (head == null/None) // no elements
head = n
tail = n
else // have elements
set tail.next to n
tail =n

Explanation / Answer

You have not mentioned the programming language. Assuming it is Java

LinkedList.java
-------

public class LinkedList<T> {
class Node{
T data;
Node next;

Node(T val){
data = val;
next = null;
}
}

Node head, tail;

public LinkedList(){
head = null;
tail = null;
}


public void addHead(T val){
Node n = new Node(val);
if(head == null){
head = n;
tail = n;
}
else {
n.next = head;
head = n;
}
}

public void addTail(T val){
Node n = new Node(val);
if(head == null){
head = n;
tail = n;
}
else {
tail.next = n;
tail = n;
}
}
}

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