Stack%20and%20Queues%20using%20Linked%20Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Stack%20and%20Queues%20using%20Linked%20Structures

Description:

Big-O Comparison of Stack Operations. Implementing queues using arrays. Simple implementation ... Array Implementation. Operation. Big-O Comparison of Queue ... – PowerPoint PPT presentation

Number of Views:265
Avg rating:3.0/5.0
Slides: 44
Provided by: penelope
Learn more at: https://www.cs.bu.edu
Category:

less

Transcript and Presenter's Notes

Title: Stack%20and%20Queues%20using%20Linked%20Structures


1
Stack and Queues using Linked Structures
  • Kruse and Ryba Ch 4

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 for 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
Implementing queues using arrays
  • Simple implementation
  • The size of the queue must be determined when a
    stack object is declared
  • Space is wasted if we use less elements
  • We cannot "enqueue" more elements than the array
    can hold

26
Implementing queues using linked lists
  • Allocate memory for each new element dynamically
  • Link the queue elements together
  • Use two pointers, qFront and qRear, to mark the
    front and rear of the queue

27
Queue class specification
  • // forward declaration of NodeType (like function
    prototype)
  • templateltclass ItemTypegt
  • struct NodeType
  •  
  • templateltclass ItemTypegt
  • class QueueType
  • public
  • QueueType()
  • QueueType()
  • void MakeEmpty()
  • bool IsEmpty() const
  • bool IsFull() const
  • void Enqueue(ItemType)
  • void Dequeue(ItemType)
  • private
  • NodeTypeltItemTypegt qFront
  • NodeTypeltItemTypegt qRear

28
Enqueuing (non-empty queue)
29
Enqueuing (empty queue)
  • We need to make qFront point to the new node also

New Node
qFront NULL
qRear NULL
newNode
30
Function Enqueue
  • template ltclass ItemTypegt
  • void QueueTypeltItemTypegtEnqueue(ItemType
    newItem)
  • NodeTypeltItemTypegt newNode
  •  
  • newNode new NodeTypeltItemTypegt
  • newNode-gtinfo newItem
  • newNode-gtnext NULL
  • if(qRear NULL)
  • qFront newNode
  • else
  • qRear-gtnext newNode
  • qRear newNode

31
Dequeueing (the queue contains more than one
element)
32
Dequeueing (the queue contains only one element)
  • We need to reset qRear to NULL also

qFront
After dequeue qFront NULL qRear NULL
Node
qRear
33
Function Dequeue
  • template ltclass ItemTypegt
  • void QueueTypeltItemTypegtDequeue(ItemType item)
  • NodeTypeltItemTypegt tempPtr
  •  
  • tempPtr qFront
  • item qFront-gtinfo
  • qFront qFront-gtnext
  • if(qFront NULL)
  • qRear NULL
  • delete tempPtr

34
qRear, qFront revisited
  • The relative positions of qFront and qRear are
    important!

35
Other Queue functions
  •  
  • templateltclass ItemTypegt
  • void QueueTypeltItemTypegtMakeEmpty()
  • NodeTypeltItemTypegt tempPtr
  •  
  • while(qFront ! NULL)
  • tempPtr qFront
  • qFront qFront-gtnext
  • delete tempPtr
  • qRearNULL

36
Other Queue functions (cont.)
  • templateltclass ItemTypegt
  • bool QueueTypeltItemTypegtIsEmpty() const
  • return(qFront NULL)
  •  
  • templateltclass ItemTypegt
  • bool QueueTypeltItemTypegtIsFull() const
  • NodeTypeltItemTypegt ptr
  •  
  • ptr new NodeTypeltItemTypegt
  • if(ptr NULL)
  • return true
  • else
  • delete ptr
  • return false

37
Other Queue functions (cont.)
  • templateltclass ItemTypegt
  • QueueTypeltItemTypegtQueueType()
  • MakeEmpty()

38
A circular linked queue design
39
Comparing queue implementations
  • Memory requirements
  • Array-based implementation
  • Assume a queue (size 100) of strings (80 bytes
    each)
  • Assume indices take 2 bytes
  • Total memory (80 bytes x 101 slots) (2 bytes x
    2 indexes) 8084 bytes
  • Linked-list-based implementation
  • Assume pointers take 4 bytes
  • Total memory per node 80 bytes 4 bytes 84
    bytes

40
Comparing queue implementations (cont.)
41
Comparing queue implementations
(cont.)
  • Memory requirements
  • Array-based implementation
  • Assume a queue (size 100) of short integers
    (2 bytes each)
  • Assume indices take 2 bytes
  • Total memory (2 bytes x 101 slots) (2 bytes x
    2 indexes) 206 bytes
  • Linked-list-based implementation
  • Assume pointers take 4 bytes
  • Total memory per node 2 bytes 4 bytes 6
    bytes

42
Comparing queue implementations (cont.)
43
Comparing queue implementations
Big-O Comparison of Queue Operations Big-O Comparison of Queue Operations Big-O Comparison of Queue 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)
Enqueue O(1) O(1)
Dequeue O(1) O(1)
Destructor O(1) O(N)
Write a Comment
User Comments (0)
About PowerShow.com