Module on Pointers, Stacks, and Queues
Summary and Main Topics
This module applies previous concepts (e.g., pointers, linked lists) to
widely-used dynamic data structures. Topics covered include:
- More about pointers
- Variations of linked lists
- Stacks
- Queues
Day
| Topic
| Preparation
| In-class
| Due Date
| Availability for Extra Credit
|
Friday, April 19
| Hour Test 3
| Covers through Linked Lists in the previous Module
|
|
|
Monday, April 22
| Abstract data types
Stacks, queues
| Readings on
|
|
|
|
Tuesday, April 23
| Abstract data types
Stacks
| reading on stacks
| lab on stacks
|
|
|
Wednesday, April 24
| Abstract data types
Queues
| reading on queues
| lab on queues
| Due: Monday, April 29
|
|
Friday, April 26
| Bash scripts
Using Bash scripts for testing
|
reading on Bash Scripts
| lab on bash scripts
|
| Wednesday, May 8
|
Monday, April 29
| Project
| Examples: Stack implementations
-
main program using stack operations
according to some implementation
-
approach 1, in which the stack contains
an array of pointers and the stack just points to strings as entered
-
approach 2, in which the stack
contains an array of the strings themselves and strings are copied onto the
stack and copied back
-
approach 3, in which the stack
contains an array of pointers and space is allocated to copy the string
during each push operation.
-
approach 4, in which the stack
contains an array of the strings themselves and strings are copied onto the
stack and just a reference is to the string is returned by a pop.
| Variations with Stacks
| Due: Friday, May 3
|
|
Project: Variations with Stacks
The readings for this project show four different approaches for
implementing a stack based on an underlying array structure. In
particular, each approach uses a fixed size array to store strings on a
stack. When all items in the array are in use, the stack is full and
further push operations will fail.
The variations among the stack implementations involve what is actually
stored during a push operation and what is returned during a pop operation.
In brief, some possibilities are:
-
push operation (given a pointer to a string)
-
the stack contains a reference to the original string pushed
-
the stack contains a new copy of the string being pushed
-
push operation makes a copy of the string being pushed, and
the stack contains a reference to the new copy
-
pop operation (returns a pointer to a string)
-
the string reference returned identifies a reference stored in the stack
-
the string reference returned identifies the location of a string located
within the stack itself
-
the pop operation makes a new copy of the string reference in the stack and
returns a reference to this new copy
The main program in the reading pushes several items onto several stacks
and then pops the stacks and prints the results. All four stack approaches
work fine in this simple context.
Project, Part 1: Code Analysis
Examine each of the four implementation approaches supplied in the project
reading. For each approach, identify:
-
what information is stored by push (e.g., a string, a copy of a
string, a pointer to the original string, a pointer to a copy of the
string)
-
what information is returned by pop (e.g., a pointer from the
stack, a pointer to a string stored on the stack, a pointer to a copy of
the string designated on the stack)
In each case, briefly justify your conclusions.
Project, Part 2: Experimentation
Create a main program that creates a single stack and then uses the
standard stack operations in the following sequence:
-
initial three strings with variables a, b, c (the initialization is up
to you, but the three strings should have different values)
-
push a, b, and c onto the stack
-
pop the stack three times to get strings stored as variables d, e, and f
-
print the strings referenced by a, b, c, d, e, f
-
change strings a and e to a text not previously used (use strcpy
from the string library)
-
print the strings referenced by a, b, c, d, e, f
-
define two new strings g and h, initialized to previously unused values
-
push g and h onto the stack
-
change string g to a new value (use strcpy or access a specific
character with a subscript — str[2] = 'q')
-
print the strings referenced by a, b, c, d, e, f, g, h
Review the output obtained with each of the four stack implementation
approaches, and explain the results obtained (what variables remain the
same, what changes and how).
Project, Part 3: A Stack of Pictures
Suppose you wanted to use a stack to store successive pictures taken by a
Scribbler 2 robot. For each of the four approaches identified for strings,
-
Explain whether the approach for strings could be easily modified for
pictures.
-
If a similar approach is possible, outline what adjustments would be
needed.
-
If the approach is not easily adapted to pictures, briefly explain why.
Project, Part 4: Your Preference for a Stack of Pictures
In reviewing various approaches for implementing an array-based stack of
pictures, how would you proceed? Would you adapt one of the approaches
discussed for strings? Would you use a different approach (explain)?
Grading
This project will be worth 25 points, based on the following rubric:
- Part 1: Discussion of how each of the four implementations for
strings work (8 points)
- Part 2: Discussion of output received when running the experiment
for all four implementation approaches (8 points)
- Part 3: Discussion of what string approaches extend to pictures (5 points)
- Part 4: Description of your preferred approach for implementing an
array-based stack for pictures (4 points)