Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 2100
Data Structures

Michael Goldwasser

Fall 2015

Dept. of Math & Computer Science

Homework Assignment 04

Singly Linked Lists and Circular Lists

Contents:


Overview

Topic: Singly Linked Lists and Circular Lists
Related Reading: Ch. 3.2, 5.1.5, 5.2.5, and class notes
Due: Friday, October 9, 10:00am


Collaboration Policy

For this assignment, you must work individually.

Please make sure you adhere to the policies on academic integrity in this regard.


Problems to be Submitted (20 points)

  1. (7 points)

    Our implementation of the LinkedStack destructor piggybacks on our existing implementation of the pop method as its way to delete all of the nodes allocated to a list which is being destroyed.

    Give a more direct implementation of the LinkedStack::~LinkedStack() method that does not make calls to any other methods. You should instead use a loop to traverse the list while deleting nodes (and not losing track of the rest of the list as you go).

  2. (7 points)

    Examine the source code for our LinkedQueue implementation. That class maintains an instance variable, n, equal to the current number of nodes in the queue. This allows for the size() method to be easily implementing in O(1) time.

    If the instance variable n did not exist, the size() method could be implemented using O(n) time by traversing the circular linked list to count the number of node. Give a valid such implementation of size().

  3. (6 points)

    Returning to the original source code for our LinkedQueue implementation. Lines 47-50 of the pop() method includes a conditional that differentiates between the case of popping the last element from a queue. Consider a modification to that code where we replace the entire four-line conditional, and instead always execute the command
        cursor->next = old->next;
    in all cases.

    What are the consequences of this change? Explain why the class would or would not work with such a modification.


Michael Goldwasser
CSCI 2100, Fall 2015
Last modified: Sunday, 04 October 2015
Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit