Linked Lists - Homework 4 - Data Structures | CS 261, Assignments of Data Structures and Algorithms

Material Type: Assignment; Class: DATA STRUCTURES; Subject: Computer Science; University: Oregon State University; Term: Spring 2003;

Typology: Assignments

Pre 2010

Uploaded on 08/31/2009

koofers-user-pak
koofers-user-pak 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 261 — Data Structures
Homework #4: Linked Lists (50 pts)
Due date: Friday, April 25, 2003 (5:00 PM)
This assignment is designed to make you think about the LinkedList data structure. Treat this
paper as a specification for your assignment. Your code will be graded on correctness, readability, and
style. We will grade your code by providing a class that fully tests the various method that you will imple-
ment. Study problems and exercises must be legible and will be graded on correctness.
What to do:
1. Create a new class called ExtendedList that extends the LinkedList class (found in jds/collec-
tion/LinkedList.java) and adds the following methods (30 pts):
(a) Add a constructor that takes a Collection as an argument and initializes the list with the ele-
ments from the collection.
(b) Add a method swap(ExtendedList list) that will, in constant time (i.e., O(1) time), swap
the contents of the receiving list (i.e., the list corresponding to the “this” reference) with an
argument list.
(c) Add a method append(ExtendedList list) that will, in constant time, append the ele-
ments of the argument linked list to the receiving list by reusing the Link fields in the argument.
The argument list is then emptied, so that after the call, it will have no elements.
(d) Add a method unique() that will replace repeated sequences of a single element by a single
occurrence. For example, the list (2 2 2 3 3 1 1 1 5 2 2) would become (2 3 1 5 2).
(e) Add a method reverse() that will reverse the elements in a linked list without creating any
new Link nodes.
Written homework:
Simulate adding the values 62 84 78 13 22 97 40 8 38 59 (in this order) to an initially empty Skip-
List, using a real coin to decide whether or not to create a new node. Repeat twice more with the
same input values, then for each of the three lists, compute the average number of links that are fol-
lowed to find an element (4 pts).
Chapter 8 Study Questions 1 and 8.
Q 8.1) Although sometimes used, why in general is it not a good idea to create a linked list by stor-
ing a link field directly in a data object (2 pts)?
Q 8.8) What is the difference between overriding using replacement and overriding using refine-
ment (2 pts)?
Chapter 8 Exercises 3, 4, 7, and 11.
E 8.3) Imagine an instance of DoubleLink in the middle of a set of three similar links. Draw a
picture that illustrates the state of all data fields following each statement executed in the
method insertLink. Do the same for the method insert in the class LinkedList (3
pts).
E 8.4) Repeat the previous problem, this time using the method remove (3 pts).
pf2

Partial preview of the text

Download Linked Lists - Homework 4 - Data Structures | CS 261 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS 261 — Data Structures

Homework #4: Linked Lists (50 pts) Due date: Friday, April 25, 2003 (5:00 PM)

This assignment is designed to make you think about the LinkedList data structure. Treat this paper as a specification for your assignment. Your code will be graded on correctness, readability, and style. We will grade your code by providing a class that fully tests the various method that you will imple- ment. Study problems and exercises must be legible and will be graded on correctness.

What to do:

  1. Create a new class called ExtendedList that extends the LinkedList class (found in jds/collec- tion/LinkedList.java) and adds the following methods ( 30 pts ):

(a) Add a constructor that takes a Collection as an argument and initializes the list with the ele- ments from the collection.

(b) Add a method swap(ExtendedList list) that will, in constant time (i.e., O(1) time), swap the contents of the receiving list (i.e., the list corresponding to the “this” reference) with an argument list.

(c) Add a method append(ExtendedList list ) that will, in constant time, append the ele- ments of the argument linked list to the receiving list by reusing the Link fields in the argument. The argument list is then emptied, so that after the call, it will have no elements.

(d) Add a method unique() that will replace repeated sequences of a single element by a single occurrence. For example, the list (2 2 2 3 3 1 1 1 5 2 2) would become (2 3 1 5 2).

(e) Add a method reverse() that will reverse the elements in a linked list without creating any new Link nodes.

Written homework:

  • Simulate adding the values 62 84 78 13 22 97 40 8 38 59 (in this order) to an initially empty Skip- List , using a real coin to decide whether or not to create a new node. Repeat twice more with the same input values, then for each of the three lists, compute the average number of links that are fol- lowed to find an element ( 4 pts ).
  • Chapter 8 Study Questions 1 and 8.

Q 8.1) Although sometimes used, why in general is it not a good idea to create a linked list by stor- ing a link field directly in a data object ( 2 pts )?

Q 8.8) What is the difference between overriding using replacement and overriding using refine- ment ( 2 pts )?

  • Chapter 8 Exercises 3, 4, 7, and 11.

E 8.3) Imagine an instance of DoubleLink in the middle of a set of three similar links. Draw a picture that illustrates the state of all data fields following each statement executed in the method insertLink. Do the same for the method insert in the class LinkedList ( 3 pts ).

E 8.4) Repeat the previous problem, this time using the method remove ( 3 pts ).

E 8.7) On should never make changes to a collection while it is being accessed by an Enumeration. To see what problems can occur, consider the following program, which will place nine items into a linked list, then loop over the list and print them out. Along the way, one item is deleted. Without executing the program, what do you expect will be the outcome? After you have made your prediction, execute the program and observe the result. Can you explain what has occurred ( 3 pts )?

public static void main(String [] args) { LinkedList list = new LinkedList(); for (int i = 1; i <= 9; i++) list.addLast(new Integer(i));

Enumeration e = list.elements(); while (e.hasMoreElements()) { Integer v = (Integer)e.nextElement(); if (v.intValue() == 4) list.removeElement(new Integer(5)); System.out.println(“Value is ” + v); } }

E 8.11) Is it possible to add methods to the LinkedList class so as to support the Indexed interface? What is the complexity of the method elementAt? How does this compare to the complexity of the same operation in class Vector ( 3 pts )?

What to turn in:

Since Exercises 8.3 and 8.4 require simple illustrations, you can either use a word processor that allows you to directly add the drawings (such as Microsoft Word) and submit either a Word (.doc), Postscript (.ps), or PDF (.pdf) file or you can still use a text-editor and leave room for hand-drawn illustrations (if you do the latter, you do not need to include the illustrations in the web submission).

In class: a printout of written homework in the file Answers.txt (or Answers.doc, Answers.ps, or Answers.pdf) and a printout of your source file ExtendedList.java.

Via the web: the files ExtendedList.java and Answers.txt—except for the illustrations needed in Exercises 8.3 and 8.4—(or Answers.ps or Answers.pdf with illustrations).