Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.

Similar presentations


Presentation on theme: "Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item."— Presentation transcript:

1 Review of Stacks and Queues Dr. Yingwu Zhu

2 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

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

4 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

5 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

6 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)

7 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”

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

9 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

10 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;

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

12 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)

13 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. 7.12 Fig. 7.12 View definitions in Fig. 7.12 Fig. 7.12

14 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. 7.12 Fig. 7.12 View definitions in Fig. 7.12 Fig. 7.12

15 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; };

16 Exercises: implementation Close Textbook and Notes Assignment Operator? Destructor?

17 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

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

19 Application of Stack – Function Calls Consider events when a function begins execution Activation Record (AR) created: store the current environment for the function Contents

20 Run-time Stack Functions may call other functions Interrupt their own execution Must store the ARs to be recovered System reset when the first function resumes execution LIFO data structure Run-time stack is used (Example in p.367)

21 Use of Run-time Stack When a function is called, p.368 An AR pushed onto the run-time stack Arguments copied into parameter space Control transferred to starting address of body of the function being called

22 Use of Run-time Stack When a function terminates: Run-time stack popped: get the address of instruction from AR AR used to restore environment of the interrupted function Interrupted function resumes execution

23 Any Question?

24 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

25 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

26 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()

27 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; };

28 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

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

30 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!

31 Implementing Circular List Queue Can you implement it?

32 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?

33 Question Time Any Question?

34 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. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item."

Similar presentations


Ads by Google