Implementing a Stack as a Linked Structure - PowerPoint PPT Presentation

About This Presentation
Title:

Implementing a Stack as a Linked Structure

Description:

We need a data member to store the pointer to the top of the stack ... Stack class specification // forward declaration of NodeType (like function prototype) ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 26
Provided by: Penelope69
Learn more at: https://www.cse.unr.edu
Category:

less

Transcript and Presenter's Notes

Title: Implementing a Stack as a Linked Structure


1
Implementing a Stack as a Linked Structure
  • CS 308 Data Structures

2
Implementing 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

3
Dynamic allocation of eachstack element
  • Allocate memory for each new element dynamically
  • ItemType itemPtr
  • ...
  • itemPtr new ItemType
  • itemPtr newItem

4
Dynamic allocation of eachstack element (cont.)
  • How should we preserve the order of the stack
    elements?

5
Chaining the stack elements together
6
Chaining 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

7
Node Type
  • templateltclass ItemTypegt
  • struct NodeType
  • ItemType info
  • NodeType next

8
First 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

9
Stack 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

10
Pushing on a non-empty stack
11
Pushing on a non-empty stack (cont.)
  • The order of changing the pointers is very
    important !!

12
Pushing on an empty stack
13
Function Push
  • template ltclass ItemTypegt
  • void StackTypeltItemTypegtPush(ItemType item)
  • NodeTypeltItemTypegt location
  •  
  • location new NodeTypeltItemTypegt
  • location-gtinfo newItem
  • location-gtnext topPtr
  • topPtr location

14
Popping the top element
15
Popping the top element(cont.)
Need to use a temporary pointer
16
Function Pop
  • template ltclass ItemTypegt
  • void StackTypeltItemTypegtPop(ItemType item)
  • NodeTypeltItemTypegt tempPtr
  •  
  • item topPtr-gtinfo
  • tempPtr topPtr
  • topPtr topPtr-gtnext
  • delete tempPtr

17
Popping the last element on the stack
18
Other Stack functions
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType()
  • topPtr NULL
  •  
  • templateltclass ItemTypegt
  • void StackTypeltItemTypegtMakeEmpty()
  • NodeTypeltItemTypegt tempPtr
  •  
  • while(topPtr ! NULL)
  • tempPtr topPtr
  • topPtr topPtr-gtnext
  • delete tempPtr

19
Other 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()
20
Copy 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)
21
Copy 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)
23
Copy 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!!)
24
Comparing 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)
25
Exercises
  • 2, 3
Write a Comment
User Comments (0)
About PowerShow.com