stack & queue

17
Level up your coding skills with the C++ Standard Template Library (STL): Stack & Queue BY JOYJIT CHOUDHURY

Upload: joyjit-choudhury

Post on 05-Apr-2017

69 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Stack & Queue

Level up your coding skills with

the C++ Standard Template

Library (STL):

Stack & Queue

BY JOYJIT CHOUDHURY

Page 2: Stack & Queue

Stack

Stack is a container adapter that gives the programmer

the functionality of a stack - specifically, a LIFO (Last-In,

First–Out) data structure.

A container adapter such as stack is implemented on top

of an underlying container like list, deque or vector.

Defined in the header <stack>

Belongs to the namespace std

(remember : using namespace std)

Page 3: Stack & Queue

Constructing an empty stack

myStack

Page 4: Stack & Queue

Push

myStack

34

myStack

34

21

myStack

34

21

13

myStack

Page 5: Stack & Queue

Size of the stack

Member function size(), returns the number of elements

in the stack.

myStack

34

21

13

myStack.size() = 3

Page 6: Stack & Queue

Accessing the top

myStack

34

21

13

Member function top(), returns a reference to the top element in the

stack. So, not only can you access it, you can also modify it’s value.

myStack

34

21

10myStack.top()

Page 7: Stack & Queue

Pop

Member function pop(), removes the top element of the stack.

Effectively reducing the size of the stack by one.

myStack

34

21

myStack

34

21

13

myStack.pop()

Page 8: Stack & Queue

Whether the stack is empty or not?

Member function empty() returns true if the stack is empty (i.e.

stack size is 0), else returns false

myStack myStack

34

21

13

myStack.empty()=true myStack.empty()=false

Page 9: Stack & Queue

Queue

Queue is a container adapter that gives the programmer

the functionality of a queue - specifically, a FIFO (First-In,

First–Out) data structure.

Defined in the header <queue>

Belongs to the namespace std

(remember : using namespace std)

Page 10: Stack & Queue

Constructing an empty queue

myQ

Page 11: Stack & Queue

Enqueue

Member function push(), is used to enqueue elements to the back

of the queue.

myQ

myQ

myQ

myQ

87

87 43

87 43 13

Page 12: Stack & Queue

Accessing the elements

front(), returns a reference to the first or the ‘oldest’ element in the

queue. It the same element which is removed during the dequeue

operation in a queue.

back(), returns a reference to the the last or the ‘newest’ element in

the queue.

myQ 87 43 13

myQ.front() myQ.back()

Page 13: Stack & Queue

Dequeue

pop(), removes the first or the ‘oldest’ element in the queue.

myQ 87 43 13

myQ 43 13

myQ 13

Page 14: Stack & Queue

Size of the queue

Member function size(), returns the number of elements in the queue.

myQ

myQ.size() = 0

myQ 87 43 13

myQ.size() = 3

Page 15: Stack & Queue

Is the queue empty?

Member function empty() returns true if the queue is empty (i.e. queue

size is 0), else returns false

myQ

myQ.empty() = true

myQ 23 30 99

myQ.size() = false

Page 16: Stack & Queue

That’s not all. There are many other functions

and techniques that could come in handy.

Read about them on cplusplus.com or

cppreference.com or somewhere else. Just

google it !

Page 17: Stack & Queue