stack and queues using linked structures kruse and ryba ch 4
TRANSCRIPT
![Page 1: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/1.jpg)
Stack and Queues using Linked Structures
Kruse and Ryba Ch 4
![Page 2: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/2.jpg)
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
![Page 3: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/3.jpg)
Dynamic allocation of eachstack element
• Allocate memory for each new element dynamically
ItemType* itemPtr;
...
itemPtr = new ItemType;
*itemPtr = newItem;
![Page 4: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/4.jpg)
Dynamic allocation of eachstack element (cont.)
• How should we preserve the order of the stack elements?
![Page 5: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/5.jpg)
Chaining the stack elements together
![Page 6: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/6.jpg)
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
![Page 7: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/7.jpg)
Node Type
template<class ItemType>struct NodeType { ItemType info; NodeType* next;};
![Page 8: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/8.jpg)
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
![Page 9: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/9.jpg)
Stack class specification// forward declaration of NodeType (like function prototype)template<class ItemType>struct NodeType; template<class ItemType>class StackType { public: StackType(); ~StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: NodeType<ItemType>* topPtr;};
![Page 10: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/10.jpg)
Pushing on a non-empty stack
![Page 11: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/11.jpg)
Pushing on a non-empty stack (cont.)
• The order of changing the pointers is very important !!
![Page 12: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/12.jpg)
Pushing on an empty stack
![Page 13: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/13.jpg)
Function Push template <class ItemType>void StackType<ItemType>::Push(ItemType
item){
NodeType<ItemType>* location;
location = new NodeType<ItemType>; location->info = newItem; location->next = topPtr; topPtr = location;}
![Page 14: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/14.jpg)
Popping the top element
![Page 15: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/15.jpg)
Popping the top element(cont.)
Need to use a temporary pointer
![Page 16: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/16.jpg)
Function Poptemplate <class ItemType>void StackType<ItemType>::Pop(ItemType& item){ NodeType<ItemType>* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr;}
![Page 17: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/17.jpg)
Popping the last element on the stack
![Page 18: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/18.jpg)
Other Stack functionstemplate<class ItemType>StackType<ItemType>::StackType()StackType(){
topPtr = NULL;}
template<class ItemType>void StackType<ItemType>::MakeEmpty()MakeEmpty(){
NodeType<ItemType>* tempPtr;
while(topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; }}
![Page 19: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/19.jpg)
Other Stack functions (cont.)template<class ItemType>bool StackType<ItemType>::IsEmpty()IsEmpty() const{ return(topPtr == NULL);}
template<class ItemType>bool StackType<ItemType>::IsFull()IsFull() const{ NodeType<ItemType>* location;
location = new NodeType<ItemType>; if(location == NULL) return true; else { delete location; return false; }}
template<class ItemType>StackType<ItemType>::~StackType()StackType(){ MakeEmpty();}
![Page 20: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/20.jpg)
Copy Constructors for stacks
• Suppose we want to make a copy of a stack, will the following work?
template<class ItemType>void StackType(StackType<ItemType> oldStack, StackType<ItemType>& copy){ StackType<ItemType> tempStack; ItemType item; while(!oldStack.IsEmpty()) { oldStack.Pop(item); tempStack.Push(item); }
while(!tempStack.IsEmpty()) { tempStack.Pop(item); copy.Push(item); }}
![Page 21: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/21.jpg)
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
(StackType<int> myStack=yourStack;)
![Page 22: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/22.jpg)
![Page 23: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/23.jpg)
Copy constructor for stacks template<class ItemType>Stack Type<ItemType>::StackType(const StackType<ItemType>& anotherStack){
NodeType<ItemType>* ptr1; NodeType<ItemType>* ptr2;
if(anotherStack.topPtr == NULL) topPtr = NULL; else { topPtr = new NodeType<ItemType>; topPtr->info = anotherStack.topPtr->info; ptr1 = anotherStack.topPtr->next; ptr2 = topPtr; while(ptr1 !=NULL) { ptr2->next = new NodeType<ItemType>; ptr2 = ptr2->next; ptr2->info = ptr1->info; ptr1 = ptr1->next; }
ptr2->next = NULL; }}
Alternatively, copy one stack to another using the assignment operator (you need to overload it though!!)
![Page 24: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/24.jpg)
Comparing stack implementations 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)
![Page 25: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/25.jpg)
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
![Page 26: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/26.jpg)
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
![Page 27: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/27.jpg)
Queue class specification// forward declaration of NodeType (like function prototype)template<class ItemType>struct NodeType; template<class ItemType>class QueueType { public: QueueType(); ~QueueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType); void Dequeue(ItemType&); private: NodeType<ItemType>* qFront; NodeType<ItemType>* qRear;};
![Page 28: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/28.jpg)
Enqueuing (non-empty queue)
![Page 29: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/29.jpg)
Enqueuing (empty queue)
• We need to make qFront point to the new node also
New Node
newNode
qFront = NULL
qRear = NULL
![Page 30: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/30.jpg)
Function Enqueuetemplate <class ItemType>void QueueType<ItemType>::Enqueue(ItemType
newItem){ NodeType<ItemType>* newNode;
newNode = new NodeType<ItemType>; newNode->info = newItem; newNode->next = NULL; if(qRear == NULL) qFront = newNode; else qRear->next = newNode; qRear = newNode;}
![Page 31: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/31.jpg)
Dequeueing (the queue contains more than one element)
![Page 32: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/32.jpg)
Dequeueing (the queue contains only one element)
• We need to reset qRear to NULL also
Node
qFront
qRear
After dequeue:
qFront = NULL
qRear = NULL
![Page 33: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/33.jpg)
Function Dequeuetemplate <class ItemType>void QueueType<ItemType>::Dequeue(ItemType& item){ NodeType<ItemType>* tempPtr; tempPtr = qFront; item = qFront->info; qFront = qFront->next; if(qFront == NULL) qRear = NULL; delete tempPtr;}
![Page 34: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/34.jpg)
qRear, qFront revisited
• The relative positions of qFront and qRear are important!
![Page 35: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/35.jpg)
Other Queue functions
template<class ItemType>void QueueType<ItemType>::MakeEmpty()MakeEmpty(){
NodeType<ItemType>* tempPtr;
while(qFront != NULL) { tempPtr = qFront; qFront = qFront->next; delete tempPtr; }
qRear=NULL;}
![Page 36: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/36.jpg)
Other Queue functions (cont.)template<class ItemType>bool QueueType<ItemType>::IsEmpty()IsEmpty() const{
return(qFront == NULL);}
template<class ItemType>bool QueueType<ItemType>::IsFull()IsFull() const{
NodeType<ItemType>* ptr;
ptr = new NodeType<ItemType>; if(ptr == NULL) return true; else { delete ptr; return false; }}
![Page 37: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/37.jpg)
Other Queue functions (cont.)
template<class ItemType>
QueueType<ItemType>::~QueueType()QueueType()
{
MakeEmpty();
}
![Page 38: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/38.jpg)
A circular linked queue design
![Page 39: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/39.jpg)
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
![Page 40: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/40.jpg)
Comparing queue implementations(cont.)
![Page 41: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/41.jpg)
Comparing queue implementations
• 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
(cont.)
![Page 42: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/42.jpg)
Comparing queue implementations(cont.)
![Page 43: Stack and Queues using Linked Structures Kruse and Ryba Ch 4](https://reader035.vdocuments.net/reader035/viewer/2022062511/5518d0ad550346881f8b5d0e/html5/thumbnails/43.jpg)
Comparing queue implementationsBig-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)