Project 2

advertisement
CSCI 2320 Data Structure Project 2
Use Multi-dimensional Array to Simulate Linked List for STACK
Construction
Executive Summary:
Nodes in a linked list contain two parts: a data part that stores an element of the
list; a next part that points to a node containing the successor of this list element or that is
null if this is the last element in the list. This suggests that each node can be represented
as a struct and the linked list can be represented as an array of structs.
You are asked to use 2D array to simulate the linked list and use it to construct the
STACK with six basic functions: Construction, Empty, Push, Top, pop, and Display.
Project Objective: in completing this project, you will
 Enhance your ability to manipulate multi-dimensional array
 Familiar with the idea of linked list
 Enable yourself to perform Six basic function of the STACK by simulated linked
list
Due Dates and Honor:
This project will be due by the beginning of the class on 2/23/2015. This is an
independent programming project, and it is very important that you understand and abide
by the policy concerning programming projects. Remember, your personal honor and
integrity is far more important than your grade on the project.
Detailed Specification:
Write the construction and five basic functions for the STACK listed below by
using 2D array to simulate Linked List approach. Details for each function:
Construction( ): construct an empty 2D array with capacity of 25 to simulate
the OS.
1. Empty( ): test if the STACK is empty
2. Push( ): Add a value at the top of the stack in a valid random space of the 2D
array
3. Top( ): Read the value at the top of the stack
4. Pop( ): Remove the value at the top of the stack
5. Display( ): Displays all the elements in the stack using from Top to Bottom
ordering. (Show array index, data value, and next array index)
After you finished the six functions, create an empty stack (with capacity=25) and then
start push 10 values (1,2,3,4,5,6,7,8,9,10) by Push( ) consecutively. After that, you will
have 10 elements in your STACK. Run Display ( ) to print out the STACK. Then
perform 3 times of Push( ) of value (20,30,40) on valid positions, run Display( ) to print
out the list every time you insert a new element. Then perform 3 times of Top() and
Pop( ), again, run Display( ) to print our the list every time you delete an element.
To visualize the requirement, the code should looks like:
Push(1)
Push(2)
Push(3)
Push(4)
Push(5)
Push(6)
Push(7)
Push(8)
Push(9)
Push(10)
Display()
Push(20)
Display()
Push(30)
Display()
Push(40)
Display()
Top()
Pop()
Display()
Top()
Pop()
Display()
Top()
Pop()
Display()
What to Submit:
 A print out results of your program.
 Source code of your program.
Here are some examples:
Initially, you will have an empty STACK looks like:
Then, you try to push a random value. (Suppose the value is 5.) You also need to
randomly generate the index value for you to store this “link list”. Assume the random
index value is 7, then your 2D array will looks like: (-1 indicates NULL)
Suppose we insert another random value (32) and the random index value is 2 (You will
need a section of code to check if random index value is valid or not), then 2D array
would looks like:
Download