Title: Implementing a Stack as a Linked Structure
1Implementing a Stack as a Linked Structure
2Implementing stacks using arrays
- Simple implementation
- The size of the stack must be determined when a
stack object is declared - Space is wasted if we use less elements
- We cannot "push" more elements than the array can
hold
3Dynamic allocation of eachstack element
- Allocate memory for each new element dynamically
- ItemType itemPtr
- ...
- itemPtr new ItemType
- itemPtr newItem
4Dynamic allocation of eachstack element (cont.)
- How should we preserve the order of the stack
elements?
5Chaining the stack elements together
6Chaining the stack elements together (cont.)
- Each node in the stack should contain two parts
- info the user's data
- next the address of the next element in the stack
7Node Type
- templateltclass ItemTypegt
- struct NodeType
- ItemType info
- NodeType next
-
8First and last stack elements
- We need a data member to store the pointer to the
top of the stack - The next element of the last node should contain
the value NULL
9Stack class specification
- // forward declaration of NodeType (like function
prototype) - templateltclass ItemTypegt
- struct NodeType
- Â
- templateltclass ItemTypegt
- class StackType
- public
- StackType()
- StackType()
- void MakeEmpty()
- bool IsEmpty() const
- bool IsFull() const
- void Push(ItemType)
- void Pop(ItemType)
- private
- NodeTypeltItemTypegt topPtr
-
10Pushing on a non-empty stack
11Pushing on a non-empty stack (cont.)
- The order of changing the pointers is very
important !!
12Pushing on an empty stack
13Function Push
- template ltclass ItemTypegt
- void StackTypeltItemTypegtPush(ItemType item)
-
- NodeTypeltItemTypegt location
- Â
- location new NodeTypeltItemTypegt
- location-gtinfo newItem
- location-gtnext topPtr
- topPtr location
-
14Popping the top element
15Popping the top element(cont.)
Need to use a temporary pointer
16Function Pop
- template ltclass ItemTypegt
- void StackTypeltItemTypegtPop(ItemType item)
-
- NodeTypeltItemTypegt tempPtr
- Â
- item topPtr-gtinfo
- tempPtr topPtr
- topPtr topPtr-gtnext
- delete tempPtr
-
17Popping the last element on the stack
18Other Stack functions
- templateltclass ItemTypegt
- StackTypeltItemTypegtStackType()
-
- topPtr NULL
-
- Â
- templateltclass ItemTypegt
- void StackTypeltItemTypegtMakeEmpty()
-
- NodeTypeltItemTypegt tempPtr
- Â
- while(topPtr ! NULL)
- tempPtr topPtr
- topPtr topPtr-gtnext
- delete tempPtr
-
-
19Other Stack functions (cont.)
- templateltclass ItemTypegt
- bool StackTypeltItemTypegtIsEmpty() const
-
- return(topPtr NULL)
-
- Â
- templateltclass ItemTypegt
- bool StackTypeltItemTypegtIsFull() const
-
- NodeTypeltItemTypegt location
- Â
- location new NodeTypeltItemTypegt
- if(location NULL)
- return true
- else
- delete location
- return false
-
-
templateltclass ItemTypegt StackTypeltItemTypegtSta
ckType() MakeEmpty()
20Copy Constructors an example using stacks
- Suppose we want to make a copy of a stack, will
the following work? - templateltclass ItemTypegt
- void StackType(StackTypeltItemTypegt oldStack,
- StackTypeltItemTypegt
copy) -
- StackTypeltItemTypegt tempStack
- ItemType item
- Â
- while(!oldStack.IsEmpty())
- oldStack.Pop(item)
- tempStack.Push(item)
-
while(!tempStack.IsEmpty())
tempStack.Pop(item) copy.Push(item)
21Copy Constructors (cont.)
- Shallow Copy an object is copied to another
object without copying any pointed-to data - Deep Copy makes copies of any pointed-to data
- When do you need a copy constructor?
- (1) When parameters are passed by value
- (2) Return the value of a function Â
- (return thisStack)
- (3) Initializing a variable in a declaration Â
- (StackTypeltintgt myStackyourStack)
22(No Transcript)
23Copy constructor for stacks
- templateltclass ItemTypegt
- Stack TypeltItemTypegtStackType(const
StackTypeltItemTypegt -
anotherStack) -
- NodeTypeltItemTypegt ptr1
- NodeTypeltItemTypegt ptr2
- Â
- if(anotherStack.topPtr NULL)
- topPtr NULL
- else
- topPtr new NodeTypeltItemTypegt
- topPtr-gtinfo anotherStack.topPtr-gtinfo
- ptr1 anotherStack.topPtr-gtnext
- ptr2 topPtr
- while(ptr1 !NULL)
- ptr2-gtnext new NodeTypeltItemTypegt
- ptr2 ptr2-gtnext
- ptr2-gtinfo ptr1-gtinfo
- ptr1 ptr1-gtnext
Alternatively, copy one stack to another using
the assignment operator (you need to overload it
though!!)
24Comparing stack implementations
Big-O Comparison of Stack Operations Big-O Comparison of Stack Operations Big-O Comparison of Stack Operations
Operation Array Implementation Linked Implementation
Class constructor O(1) O(1)
MakeEmpty O(1) O(N)
IsFull O(1) O(1)
IsEmpty O(1) O(1)
Push O(1) O(1)
Pop O(1) O(1)
Destructor O(1) O(N)
25Exercises