Question
DATA STRUCTURE 3 questions -given the following classes- public class Node { public char data; public Node next; public Node(char d, Node n) { data
DATA STRUCTURE 3 questions
-given the following classes-
public class Node { public char data; public Node next; public Node(char d, Node n) { data = d; next = n; } } public class SLinkedList { public Node head; public Node tail; int size;
public SLinkedList() { head = tail = null; size=0; } public void addFirst(int newData){ if (size ==0){ head = tail= new Node(newData, head); size++;} else head = tail= new Node(newData, head); size++;} } public void add(int newData){ if (size ==0){ head = tail= new Node(newData, head); size++;} else tail.next = new Node(newData, null); tail=tail.next size++;} }
What is the output of the following code?
public class Demo {
public static void main(String[] args) {
SLinkedList list = new SLinkedList();
list.addFirst(a);
list. addFirst (b);
list. add (c);
list. add (d);
list. addFirst (e);
Node temp=list.head;
for (int i = 0; i < list.size; i++) {
System.out.print(temp.data + " ");
temp=temp.next;
}
b. write the method length which calculates the number of elements in the list. input none output none
c. write the method remove which removes the last element from the list. input none output none
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started