1 / 34

# Review of Stacks and Queues - PowerPoint PPT Presentation

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 Pop operation Properties Ordered collection of items Be accessed at only the top. Building a Stack Class.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Review of Stacks and Queues' - kairos

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Review of Stacks and Queues

Dr. Yingwu Zhu

• Last-in-First-out (LIFO) data structure

• Push operation

• Removing an item

• Pop operation

• Properties

• Ordered collection of items

• Be accessed at only the top

• Design a stack class  stack.h (header file)

• Implement the stack class  stack.cpp (implementation file)

• 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

• 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

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

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

• Figure7.6: const keyword

• Destructor (p338-9, Figure 7.7)

• Deallocate array allocated in constructor

• Avoid memory leak problem

• 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

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

typedef int DataType;

class Stack {

private:

int myCapacity;

int myTop;

int* myArray;

public:

…….

};

• Advantage: grow and shrink as needed

• Need only one data member:

• Pointer myTop

• Nodes allocated (but not part of stack class)

• Node declaration inFig 7.11 (p. 353)

• 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

• Pop

• Delete first node in the linked listptr = myTop;myTop = myTop->next;delete ptr;

• Output

• Traverse the listfor (ptr = myTop; ptr != 0; ptr = ptr->next) out << ptr->data << endl;

View definitions in Fig. 7.12

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;

};

• Close Textbook and Notes

• Assignment Operator?

• Destructor?

• 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

• Assignment operator (=), p.361

• Rule out self-assignment

• Destroy the old list, this->~Stack(); code reuse

• Similar to copy constructor

Consider events when a function begins execution

• Activation Record (AR) created: store the current environment for the function

• Contents

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

• 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

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

• A sequence of data items (FIFO)

• Items can be removed only at the front

• Items can be added only at the back

• 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

• Data members?

• Operations: above

• Advantage: grow and shrink as needed

• Two data members: myFont, myBack

• Why need myBack?

• Avoid list traversal when enqueue()

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;

};

• Constructor: initializesmyFront 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

• Copy constructor: deep copy, p418

• Assignment operator, p419

• Watch for self-assignment

• Deep copy

• Avoid memory leak

• Destructor: reclaim memory, p418

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

• Can you implement it?

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

• Any Question? 

• 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