data structure (part i) stacks and queues. introduction to stack an stack is a ordered list in which...

26
Data Structure (Part I) Stacks and Queues

Upload: kyleigh-ghent

Post on 16-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Data Structure (Part I)

Stacks and Queues

Page 2: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Introduction to Stack

• An stack is a ordered list in which insertion and deletions are made at one end.– The location of stack end is stored in a variabl

e called top.

– Given a stack S=(a0, a1,… , an-1), we say a0 is the bottom element, an-1 is the top element.

• ai is on top of element ai-1.

Page 3: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Stack

• Last-In-First-Out (LIFO)

Add (A)

Add (B)

Add (C)

Add (D)

Add (E)

Pop ()

Which one is popped by the function Pop()?

A

B

C

D

E

Add a new item on the top of stack.

Get the item the item at the top of stack and remove it from the stack. top

0

1

2

3

4

5

Page 4: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Examples of Stack

Stack

0 1 2 3 4

0

1

2

3

4

(0, 0)(0, 1)(1, 1)(1, 0)(2, 0)(2, 1)(3, 1)(3, 0)(4, 0)(4, 1)

(3, 2)

Page 5: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

System Stack

• System stack is used by a program at runtime to process function calls.

• Whenever a function is invoked, the program create a structure referred to as an activation record or stack frame.

Page 6: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

System Stack

– Frame pointer (fp): a pointer to current stack frame.

– Placed on top of the system stack.• Previous frame pointer:

– a pointer to the previous stack frame of the invoking function.

• Return address:– The location of the statement to be executed after the fun

ction terminates.

Page 7: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

System Stack

local varaibles

return address

previous frame pointer

main:

al:

fpreturn address

previous frame pointer

return address

void al()

{

return;

}

int main ()

{

……

al();

……

return 0;

}

previous frame pointer

return address

Page 8: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

The Stack ADT

class Stack{

public:Stack (int=10);~ Stack();void Push(int d);int Pop(); //Check if the stack is emptybool IsEmpty();

};

Page 9: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Implementing Stack Using Array

class Stack

{

private:

Resize();

int top, capacity;

int *stackarray;

};

Page 10: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Constructor for Stack

• The variable top indicate next element to be popped.– Initially, top is assigned -1.

Stack::Stack(int s){ capacity = s; top = -1; allocate memory space of size s for stackarray; }

Page 11: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Push() and Pop()

• Pop– Immediately return the value at the top.

• For example:int d = stackarray[top];

top--;

return d;

– Before returning the value, first check if the stack is empty. If so, throw an exception.

Page 12: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Push() and Pop()

• Push– Note that if the stack is full, invoke Resize() to enlarge the capaci

ty of array.– Since top indicates the element to be popped, you must increase

top by 1 first when pushing a new element.void Stack::Push(int num){

if (top+1 >= capacity) Resize();

top++; stackarray[top] = num;}

Page 13: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

How to Resize the Array?

void Stack::Resize()

{newsize = capacity * 2;

Allocate a new array of size newsize;

Copy elements from stackarray to the new array;

deallocate stackarray;

assign the pointer of the new array to stackarray;

capacity = newsize;

}

Page 14: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Output Stack Elements

A

B

C

D

E

E D C B A

The order is reversed and the stack becomes empty!

E

D

C

B

A

Buffer

EDCBA

Page 15: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

The Queue ADT

class Queue{

public:Queue (int=10);~ Queue();void Push(int d);int Pop(); //Check if the queue is emptybool IsEmpty();bool IsFull();

};

Page 16: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Queue

• First-In-First-Out (FIFO)

Push (A)

Push (B)

Push (C)

Push (D)

Push (E)

Pop ()

Which one is deleted by the function Pop()?

A

B

C

D

E

Add a new item on the front of queue.

Get the item the item at the rear of stack and remove it from the queue.

rearfront

Page 17: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Implementation Using Array

• Suppose the length of array is n. – This approach runs into a problem when rear equals t

o n-1.– Shifting all elements to the left could take O(n) time.

A A B A B C

rear rear rear

B C

rearfront

B C D

rearfront

front

Page 18: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Circular Queue

A

B C

D

0 1 2 3 4 5 6 7

A B C D

0

1

2 3

4

5

67

rear

front

rear

rear rear

front

front (1)

Push (A)

Push (B)

Push (C)

Push (D)

Pop ()

Pop ()

Pop ()

Push (E)

Push (F)

Push (G)

Push (H)

Push (I)

rear

rear

frontfront

E

FG

H

I

E

rear

F

rear

G

rear

H I

Page 19: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Implementing Circular Queue

class Queue{

private:Resize();int front, rear;int top, capacity;int *queuearray;

};

Page 20: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Constructor for Circular Queue

• Initially, rear and front are assigned 0.– (rear+1) indicates the next location to insert; (front+1)

indicates the next location to insert. – This gives a situation to determine if the queue is

empty.• Queue is empty: rear == front

– However, this requires one dummy element.

Page 21: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Constructor for Circular Queue

0

1

2 3

4

5

67

rear

front

0

1

2 3

4

5

67

front

rear

A

B C

D

E

FGrear

H

How do know whether the queue is empty or full?

Page 22: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

Constructor for Circular QueueQueue:: Queue(int s)

{

capacity = s;

front = rear = 0;

allocate memory space of size s for queuearray;

}

Page 23: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

IsEmpty() and Pop()

• Before returning the value, first check if the queue is empty.

bool Queue:: IsEmpty()

{

if (front == rear)

return true;

return false;

}

Page 24: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

IsEmpty() and Pop()

• Pop– If the queue is empty, throw an exception.– Immediately return the value at (front+1)%capacity.

int Queue::Pop()

{

if (IsEmpty())

throw “Queue is empty!”;

front = (front + 1) % capacity;

return queuearray [front];

}

Page 25: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

IsFull() and Push()

• Before inserting the value, first check if the stack is full.

bool Queue:: IsFull()

{

int nextrear = (rear + 1) % capacity;

if (nextrear == front)

return true;

return false;

}

Page 26: Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The

IsFull() and Push()

• Push– If the queue is full, call Resize() to enlarge the queue.– Immediately insert the value at (rear+1)%capacity.

void Queue::Push(int num)

{

if (IsFull())

Resize();

rear = (rear + 1) % capacity;

queuearray [rear] = num;

}