Homework: Stack Implementation with Singly-Linked List
This homework is necessary preparation for the lab. Make sure you type your answers in files you bring to the lab so that you will not have to waste time entering your code during the lab.
-
Carefully review the
Queue2implementation ofQueuekernel represented as a singly-linked list ofNodes. In particular, look at the representation fields (and the convention and correspondence) and the code increateNewRepand the constructor, and draw a picture (in the style used in the Linked Data Structures slides) of theQueue2representation right after the constructor is executed. -
Carefully review the
Stack2skeleton ofStackkernel represented as a singly-linked list ofNodes. In particular, look at the representation fields and the convention and correspondence, and draw a picture (in the style used in the Linked Data Structures slides) of the representation aStack2object should have right after the constructor is executed. -
Complete the implementation of the
Stackkernel represented as a singly-linked list ofNodes in the skeleton provided,Stack2. You need to write bodies for the private methodcreateNewRepand for the kernel methodspush,pop, andlength.Note that although the
Stack2implementation is similar to theQueue2implementation, there are two significant differences: (1) theQueue2representation has three fields (instance variables): preFront, rear, and length, whileStack2only has two fields: top and length; (2) the singly-linked list representingQueue2has one extra "smart" node at the front of the list, while the singly-linked list representingStack2does not include this extra node. -
Develop a complete test plan for the
Stack2constructor and kernel methods you implemented and enter them inStackTest.
For the homework, turn in the pictures (can be hand-drawn) and the
printouts of the completed Stack2.java and StackTest.java files.