Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.

Similar presentations


Presentation on theme: "Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of."— Presentation transcript:

1 Review of Stacks and Queues Dr. Yingwu Zhu

2 Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of Queue class, see Textbook for details!

3 How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item Pop operation Properties Ordered collection of items Be accessed at only the top

4 Building a Stack Class Design a stack class  stack.h (header file) Implement the stack class  stack.cpp (implementation file)

5 Building a Stack Class Need to determine the data structure to store data items (array or linked list?) Need to determine the algorithms to perform operations on the data items

6 Building a Stack Class What data items do we need? An array/linked-list to hold stack elements An integer/pointer to indicate the top of stack What operations do we need? Constructor: build an empty stack Empty: check if a stack is empty Push: add an element on the top Top: return the top element Pop: remove the top element Display: show all the stack elements

7 A Static Array-Based Stack (p327) A static array to hold the stack elements, the position 0 as the bottom of the stack Problem?  the stack capacity cannot be changed during run time (waste of space or insufficient room for more data items)

8 Dynamic Array-Based Stack Advantage: allow users to specify the stack capacity in its declaration But, we need to do more ! (Due to the dynamically-allocated memory) Constructor: memory allocation and data member initialization Destructor: reclaim the dynamically-allocated memory, avoid “memory leak” Copy constructor: NO “shallow copy” (p339) Assignment operator: assign one object to another, NO “shallow copy”

9 Example Codes Explanation (p336) Figure7.6: const keyword Destructor (p338-9, Figure 7.7) Deallocate array allocated in constructor Avoid memory leak problem

10 Example Codes Explanation Copy constructor (p339) Initialization Passing value parameter Return a function value Creating a temporary storage value Default copy constructor: member-by- member copy Ensure deep copy

11 Example Codes Explanation Assignment operator (=) The default assignment operator only member- by-member copy, causing “shallow copy” and “memory leak” (by old array) Ensure deep copy Check if it is self-assignment (Fig 7.9, p343) If NO, then destroy the old array, allocate a new one Never forget: return *this;

12 Let’s do it typedef int DataType; class Stack { private: int myCapacity; int myTop; int* myArray; public: ……. };

13 Linked List-Based Stack Advantage: grow and shrink as needed Need only one data member: Pointer myTop Nodes allocated (but not part of stack class) Node declaration in Fig 7.11 (p. 353) Question: Is head or tail of a linked list used for stack top myTop ?

14 Implementing Linked Stack Operations Constructor: simply assign null pointer to myTop Empty: check myTop == NULL (0) Push: insertion at the head of the list Top: return the data to which myTop points View definitions in Fig Fig View definitions in Fig Fig. 7.12

15 Implementing Linked Stack Operations Pop Delete first node in the linked list ptr = myTop; myTop = myTop->next; delete ptr; Output Traverse the list for (ptr = myTop; ptr != 0; ptr = ptr->next) out data << endl; View definitions in Fig Fig View definitions in Fig Fig. 7.12

16 Stack.h typedef int DataType; class Stack { public: Stack(); Stack(const Stack& org); void push(const DataType& v); void pop(); DataType top() const; ~Stack(); private: class Node { public: DataType data; Node* next; Node(DataType v, Node* p) : data(v), next(0) { } }; typedef Node* NodePtr; NodePtr myTop; };

17 Exercises: implementation Close Textbook and Notes Copy constructor? Assignment Operator? Destructor?

18 Implementing Linked Stack Operations Destructor (p. 361) Traverse list to deallocate nodes “Never burn bridges before crossing them” Copy constructor (p. 360) Traverse linked list, copying each into new node Deep copy Watch for the empty object to be copied

19 Implementing Linked Stack Operations Assignment operator (=), p.361 Rule out self-assignment Destroy the old list, this->~Stack(); code reuse Similar to copy constructor

20 Any Question?

21 Introduction to Queue A sequence of data items (FIFO) Items can be removed only at the front Items can be added only at the back

22 Introduction to Queue Basic operations Construct a queue Check if empty Enqueue: add an element to back Dequeue: remove an element from front Front: return the first element Skip array-based queues Linked-list based queues Data members? Operations: above

23 Linked List-Based Queue (Chp. 8.3) Advantage: grow and shrink as needed Two data members: myFont, myBack Why need myBack? Avoid list traversal when enqueue()

24 Queue.h typedef int DataType; class Queue { public: //constructor //… member functions private: class Node { public: DataType data; Node* next; Node(DataType v, Node* p) : data(v), next(0) { } }; typedef Node* NodePtr; NodePtr myFront, myback; };

25 Linked Queue, p.418 Constructor: initializes myFront and myBack Front, p420 return myFront->data Dequeue, p421 Delete the first node (watch for empty queue) Equeue, p420 Insert the node at the back

26 Linked Queue Copy constructor: deep copy, p418 Assignment operator, p419 Watch for self-assignment Deep copy Avoid memory leak Destructor: reclaim memory, p418

27 Circular Linked Queue, p423 Treat the linked list as circular Last node points to the first node Alternatively keep pointer to last node rather than first node, so only needs one data member!

28 Implementing Circular List Queue Can you implement it?

29 Application of Queues Disk Scheduling in OS Disk requests from OS Disk has a queue Disk serves requests in queue by FIFO In reality, it may be not always FIFO, priority?

30 Question Time Any Question?

31 Lecture Reviews Difference between Stacks and Queues as ADT Different implementations of Stacks and Queues (Dynamic) Array or Linked List Strengths and weakness When we need copy constructor, destructor, assignment operator? Undertand Stacks and Queues via their applications


Download ppt "Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of."

Similar presentations


Ads by Google