18
Crux Data Structures -2 Stacks and Queues Monday, 6 July 15 Ankush Singla

12. Stacks and Queues

Embed Size (px)

DESCRIPTION

codes

Citation preview

Page 1: 12. Stacks and Queues

Crux

Data Structures -2

Stacks and Queues

Monday, 6 July 15

Ankush Singla

Page 2: 12. Stacks and Queues

Linked List doubts?

Monday, 6 July 15 2

Page 3: 12. Stacks and Queues

Recursion and Pile of Books

Monday, 6 July 15 3

Page 4: 12. Stacks and Queues

Stacks

Monday, 6 July 15 4

Page 5: 12. Stacks and Queues

Stacks

class Stack{ // accessor methods int size(); boolean isEmpty(); Object top() throws StackEmptyException; // update methods void push (Object element); Object pop() throws StackEmptyException;

}

Monday, 6 July 15 5

Page 6: 12. Stacks and Queues

How to implement Stack Class?

1.  Arrays 2.  Linked List

Monday, 6 July 15 6

Page 7: 12. Stacks and Queues

Dynamic Arrays

Monday, 6 July 15 7

Page 8: 12. Stacks and Queues

Lets Implement Our Own Stack Class Using a

Dynamic Array

Monday, 6 July 15 8

Page 9: 12. Stacks and Queues

Your Turn: Implement Stack Class Using Linked List

Monday, 6 July 15 9

Page 10: 12. Stacks and Queues

Lets solve few problems

1.  Given an expression check if brackets are balanced e.g. { a + [ b+ (c + d)] + (e + f) }

2.  Reverse a Stack with the help of another empty stack

Monday, 6 July 15 10

Page 11: 12. Stacks and Queues

Queues

Monday, 6 July 15 11

Page 12: 12. Stacks and Queues

Queue

class Queue{ // accessor methods int size(); boolean isEmpty(); Object front() throws QueueEmptyException; // update methods void enqueue(Object element); Object dequeue() throws QueueEmptyException;

}

Monday, 6 July 15 12

Page 13: 12. Stacks and Queues

How to implement Queue Class?

1.  Linked List 2.  Arrays

Monday, 6 July 15 13

Page 14: 12. Stacks and Queues

Lets Implement Our Own Queue Class Using Arrays

Monday, 6 July 15 14

Page 15: 12. Stacks and Queues

Your Turn: Implement Queue Class Using Linked List

Monday, 6 July 15 15

Page 16: 12. Stacks and Queues

Lets solve few problems

1.  Reverse a Queue 2.  Implement a Stack using Two Queues

Monday, 6 July 15 16

Page 17: 12. Stacks and Queues

Everyone Done with this?

Given a integer input find number of permutations of input which are greater than input. e.g. Input 123 -> Output = 5

Monday, 6 July 15 17

Page 18: 12. Stacks and Queues

Thank You!

Ankush Singla +91-9971489388 [email protected]

Monday, 6 July 15 18