12_lewis_409167_labMan12

12_lewis_409167_labMan12 - 250 Chapter 12: Collections...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 250 Chapter 12: Collections Chapter 12: Collections Lab Exercises Topic Lab Exercises Linked Lists Linked List of Integers Recursive Processing of Linked List Linked List of Objects Doubly Linked Lists Queues An Array Queue Implementation A Linked Queue Implementation Queue Manipulation Stacks An Array Stack Implementation A Linked Stack Implementation Stack Manipulation Matching Parentheses Chapter 12: Collections 251 A Linked List of Integers File IntList.java contains definitions for a linked list of integers. The class contains an inner class IntNode that holds information for a single node in the list (a node has a value and a reference to the next node) and the following IntList methods: & public IntList()constructor; creates an empty list of integers & public void addToFront(int val)takes an integer and puts it on the front of the list & public void addToEnd(int val)takes an integer and puts it on the end of the list & public void removeFirst()removes the first value from the list & public void print()prints the elements in the list from first to last File IntListTest.java contains a driver that allows you to experiment with these methods. Save both of these files to your directory, compile and run IntListTest, and play around with it to see how it works. Then add the following methods to the IntList class. For each, add an option to the driver to test it. 1. public int length()returns the number of elements in the list 2. public String toString()returns a String containing the print value of the list. 3. public void removeLast()removes the last element of the list. If the list is empty, does nothing. 4. public void replace(int oldVal, int newVal)replaces all occurrences of oldVal in the list with newVal. Note that you can still use the old nodes; just replace the values stored in those nodes. // **************************************************************** // FILE: IntList.java // // Purpose: Defines a class that represents a list of integers // // **************************************************************** public class IntList { private IntNode front; //first node in list //----------------------------------------- // Constructor. Initially list is empty. //----------------------------------------- public IntList() { front = null; } //----------------------------------------- // Adds given integer to front of list. //----------------------------------------- public void addToFront(int val) { front = new IntNode(val,front); } //-------------------------------------- // Adds given integer to end of list. //-------------------------------------- public void addToEnd(int val) { IntNode newnode = new IntNode(val,null); //if list is empty, this will be the only node in it if (front == null) front = newnode; 252 Chapter 12: Collections else { //make temp point to last thing in list IntNode temp = front; while (temp.next != null) temp = temp.next; //link new node into list temp.next = newnode; } } //-------------------------------------------...
View Full Document

Page1 / 29

12_lewis_409167_labMan12 - 250 Chapter 12: Collections...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online