Suppose that a singly linked list is implemented as a class LinkedList that has
ID: 3791986 • Letter: S
Question
Suppose that a singly linked list is implemented as a class LinkedList that has a single instance variable head of class Node. No sentinels are used, so that head.data is the first object in the list, and the last node is indicated by a next link with value null. Write a method of the class called oddEntries that removes all entries at even positions of the list. For example, if the list starts as 1, 2, 3, 4, 5 the method would turn it into 1, 3, 5. The class Queue is to be implemented as a doubly linked list of DNode objects. Each DNode is a standard doubly linked node, with 3 instance variables Object data, Dnode next, and DNode prev. As usual, DNode has 3 get methods and 3 set methods (one for each instance variable) and a 3 argument constructor. A partial implementation of the class Queue follows. The two most important methods have been omitted. class Queue {private DNode front, rear; private int size; public Queue () {front = rear null; size 0;} public int size () {return size;} public boolean empty() {return size == 0;} Identify the two missing methods. For each give the name, parameters and return type. Give a complete implementation of ONE of the two missing Queue methods. (You can choose either of them.)Explanation / Answer
Class Odd_entries
{
Node head;
class Node
{ int data;
Node next;
Node(int d){ data = d; next = null;}
}
void deleteAlt()
{
if (head == null)
return;
Node prev = head;
Node now = head.next;
while(prev!=null && now!=null)
{
prev.next = now.next;
now = null;
prev = prev.next;
if(prev != null)
now = prev.next;
}
}
public void push(){
}
void printlist(){
}
public static void main(String....){
}
}
problem 19:
a) 1:Insert()
public void insert(Item item) { return size;}
2: Delete()
public void delete(){ return item }
b) public item delete()
{
Item item = front.item;
front = front.next;
if(isEmpty())
{ rear = null;}
size--;
return item;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.