
public class ConsCell
{
private int head;
private ConsCell tail;
public ConsCell(int h, ConsCell t)
{
head = h;
tail = t;
}
public int getHead()
{
return head;
}
public ConsCell getTail()
{
return tail;
}
public void setTail(ConsCell t)
{
tail = t;
}
}
public class IntList
{
private ConsCell start;
public IntList (ConsCell s)
{
start = s;
}
public IntList cons(int h)
{
return new IntList(new ConsCell(h, start));
}
public int length()
{
int len = 0;
ConsCell cell = start;
while (cell != null)
{
len++;
cell = cell.getTail();
}
return len;
}
public void print()
{
System.out.print("[");
ConsCell a = start;
while(a != null)
{
System.out.print(a.getHead());
a = a.getTail();
if(a != null)
System.out.print(",");
}
System.out.println("]");
}
//exercise 5
public IntList reverse()
{
IntList Reverselist = new IntList(null);
ConsCell begin = start;
while (begin != null) {
Reverselist = Reverselist.cons(begin.getHead());
begin = begin.getTail();
}
return Reverselist;
}
}
public class Driver
{
public static void main (String[] args)
{
IntList a = new IntList (null);
IntList b = a.cons(2);
IntList c = b.cons(1);
IntList d = c.reverse();
int x = a.length() + b.length() + c.length();
a.print();
b.print();
c.print();
d.print();
System.out.println(x);
}
}
The defination and implementation of the required method :
public boolean contains(int n)
{
ConsCell cell = start;
while (cell != null)
{
if(cell.h==n)
return true;
cell = cell.getTail();
}
return false;
}
public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) {...
public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count; } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
Study the recursive instance method length()of MyLinkedList2 class and its associated auxiliary method. Then solve (a), (b), and (c) below in a similar way: (a) Convert the method public Element find(Object obj)of MyLinkedList2 class to a recursive one. Use an auxiliary private method. (b) Convert the method public String toString( )of MyLinkedList2 class to a recursive one such that it returns the string representation of a linked list in the form: {ValueDatum1 valueDatum2 valueDatum3 . . . valueDatumN-1 valueDatumN} Use an auxiliary private method....
Here is the IntegerLinkedList_incomplete
class:
public class IntegerLinkedList {
static class Node {
/** The element stored at this node */
private int element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n...
public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left; } public Buildbst getRight() { return right; } public void setRight(Buildbst right) { this.right = right; } }...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...
Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...
public class LinkedList { // The LinkedList Node class private class Node{ int data; Node next; Node(int gdata) { this.data = gdata; this.next = null; } } // The LinkedList fields Node head; // Constructor LinkedList(int gdata) { this.head = new Node(gdata); }...