34
Dynamic Data Structures

Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Embed Size (px)

Citation preview

Page 1: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Dynamic Data Structures

Page 2: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

STACKS

Page 3: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Stack

• A container of objects that are inserted and removed according to the last-in first-out principle (LIFO)

Page 4: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Abstract Data Type (ADT)

• An ADT is a mathematical model of a data structure that specifies thetype of data stored, the operations supported on them, and thetypes of parameters of the operations. The ADT specifies what eachoperation does but not how it does it.

• This is analogous to how information is stored in the cloud. You canpush data there and you don’t know how exactly it is stored.

Page 5: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Stack ADTMain operations supported:

• push(e): Insert element e at the top of the stack.

• pop(): Remove the top element from the stack; an error occurs if thestack is empty.

• top(): Return a reference to the top element on the stack, withoutremoving it; an error occurs if the stack is empty.

Additional Operations

• size(): Return the number of elements in the stack.

• empty(): Return true if the stack is empty and false otherwise.

Page 6: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value
Page 7: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Stack Implementation (Array-Based)

typedef struct

{

int numbers[CAPACITY];

int top;

} Stack;

Page 8: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

pushing and popping

• Initially, top is set to -1 and numbers is filled with garbagevalues.

• When we insert a value into the 0th element of numbers , weset top to 0. When we insert a value into the 1st element ofnumbers , we set top to 1. And so on.

• When we want to pop a number off the stack, we simplydecrement top. We don’t actually have to change any of thevalues of numbers . When we go to insert another value intonumbers , though, we’ll overwrite the previous last value.

Page 9: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Operations on Stacks: size()

int size()

{

return top + 1;

}

bool empty()

{

if (top < 0)

return true;

else

return false;

}

Page 10: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Operations on Stacks: push()

void push(int newElement)

{

if (size() < CAPACITY)

{

top = top + 1;

numbers[top] = newElement;

}

}

Page 11: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Operations on Stacks: pop()

int pop()

{

if (!empty()) //if (empty() == false)

{

int temp = numbers[top];

top = top – 1;

return temp;

}

}

Page 12: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Operations on Stacks: top()

// return the element at the top of the stack

int top()

{

if (!empty()) //if (empty() == false)

{

return numbers[top];

}

}

Page 13: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Stacks

• Question: if our Stack is being implemented using an array, why notjust do away with the idea of the stack and just have the plain array?

Page 14: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Stack Running Times

Page 15: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Limitation of Array-Implementation of Stack

• Array Implementation of Stack is simple and efficient and is widelyused

• Limitation: implementation must assume a fixed upper bound onCAPACITY. The application may need less or more space.

• Another Implementation: using linked lists.

Page 16: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Typical Applications

1. Parsing code, including:

a. HTML and XML, and

b. Matching parentheses in C++,

2. Allocating memory for function calls

3. Evaluating Reverse-Polish (RPN a.k.a Postfix Notation) expressions

4. Tracking undo and redo operations in applications

5. Going forward and back in a web browser

6. Assembly language, and

Page 17: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Stack Exercise 1

• Read the following page on Postfix Notation and then implement the Postfix Algorithm contained there in:

https://en.wikipedia.org/wiki/Reverse_Polish_notation

Questions:

• What is the difference between RPN and Prefix Notation?

• What is the Shunting-yard algorithm used for?

• What algorithm is Edsger Dijkstra most famous for and what are the application areas of that algorithm?

Page 18: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Stack Exercise 2

• Write a program that implements a stack.

• The user is repeatedly asked to enter a choice (pushing a value,popping a value or ending the program) and the program respondsbased on the choice of the user.

• When the main function of your program runs the output illustratedon the next page should be reproducible.

Page 19: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Stack Exercise 2 (Cont’d)

Page 20: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Queues

Page 21: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Queues

• A container of objects that are inserted and removed according to the first-in first-out principle (FIFO)

• Elements enter the queue at the rear and are removed from the front

The queue ADT supports the following operations:

• enqueue(e): Insert element e at the back of the queue.

• dequeue(): Remove element at the front of the queue; an error occurs ifthe queue is empty.

• front(): Return, but do not remove, a reference to the front element in thequeue; an error occurs if the queue is empty.

Page 22: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Other operations

• size(): Return the number of elements in the queue.

• empty(): Return true if the queue is empty and false otherwise.

Page 23: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Queue Operations

Page 24: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value
Page 25: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Queue Implementation (Array-Based)

typedef struct

{

int front;

int numbers[CAPACITY];

int back;

} Queue;

Page 26: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Queue details

• By storing a number that represents the start of the queue (i.e.front), we avoid having to shift all the elements of the arraydown every time we remove an element.

• We don’t actually need to keep the first element of the queue atthe 0th index of the array; instead, we know that the firstelement of the queue is actually stored at the index equal tofront, which we constantly update.

• The price of not having to shift all the elements of the arrayevery time we remove an element is the additional storagerequired for front.

Page 27: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

What happens when the Queue is full?

Queue after 16 enqueues and 5 dequeues

Page 28: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Cyclic ArrayInstead of viewing the array on the range 0, …, 15, consider

the indices being cyclic: …, 15, 0, 1, …, 15, 0, 1, …, 15, 0, 1, …

This is referred to as a circular array

Page 29: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Cyclic Array

Now, the next push may be performed in the next available

location of the circular array:

Page 30: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Operations on Queues: size()

Pseudocode

int size()

{

return size;

// assumes the queue has a variable called size

}

Page 31: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Operations on Queues: size()

Pseudocode

bool isEmpty()

{

if (size == 0)

return true;

else

return false;

}

Page 32: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Operations on Queues: enqueue()

Pseudocodevoid enqueue(newElement)

{

if (size() < CAPACITY)

{

numbers[back] = newElement;

// cyclic array operation

back = (back + 1) % CAPACITY;

size++;

}

}

Page 33: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Operations on Queues: dequeue()

Pseudocodeint dequeue()

{

if (!isEmpty()) {

int temp = numbers[front];

front = (front + 1) % CAPACITY;

size--;

return temp;

}

}

Page 34: Dynamic Data Structures - intuitionke.weebly.com · •Write a program that implements a stack. •The user is repeatedly asked to enter a choice (pushing a value, popping a value

Running time and Array Implementation Limitation

• Running time: all operations have running time of O(1)

• Limitations: similar to those of Stacks. Array can’t grow in size or can be too big.