Question

Exercise 2 Add a contains instance method to the IntList class, so that x. contains (n) returns true if the int value n occur

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);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;

}

Add a comment
Know the answer?
Add Answer to:
public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private...

    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...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    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),...

    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...

    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...

    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()...

    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,...

    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 Nod...

    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...

  • 1. private Node head; private Node tail; public class Node { String name; Node next; Node...

    1. private Node head; private Node tail; public class Node { String name; Node next; Node prev; } public void displayInReverse(){ //write your code here to display names in reverse } 2. public class NodeOne { String name; NodeOne next; public NodeOne(String name) { this.name = name; } } // Complete the code snippet below so that the delete method works properly public void delete(String name) { NodeOne temp = head, prev = head; while (temp != null) { if...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT