Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) { head = h; tail = t; } public int

image text in transcribed

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions