Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Data Structure (Part I) Stacks and Queues

2 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 variable called top. –Given a stack S=(a 0, a 1,…, a n-1 ), we say a 0 is the bottom element, a n-1 is the top element. a i is on top of element a i-1.

3 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

4 Examples of Stack Stack 01234 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)

5 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.

6 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 function terminates.

7 System Stack local varaibles return address previous frame pointer main: al: fp return address previous frame pointer return address void al() { return; } int main () { …… al(); …… return 0; } previous frame pointer return address

8 The Stack ADT class Stack { public: Stack (int=10); ~ Stack(); void Push(int d); int Pop(); //Check if the stack is empty bool IsEmpty(); };

9 Implementing Stack Using Array class Stack { private: Resize(); int top, capacity; int *stackarray; };

10 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; }

11 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.

12 Push() and Pop() Push –Note that if the stack is full, invoke Resize() to enlarge the capacity 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; }

13 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; }

14 Output Stack Elements A B C D E EDC B A The order is reversed and the stack becomes empty! E D C B A Buffer EDC B A

15 The Queue ADT class Queue { public: Queue (int=10); ~ Queue(); void Push(int d); int Pop(); //Check if the queue is empty bool IsEmpty(); bool IsFull(); };

16 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

17 Implementation Using Array Suppose the length of array is n. –This approach runs into a problem when rear equals to n-1. –Shifting all elements to the left could take O(n) time. AABABC rear BC front BCD rearfront

18 Circular Queue A BC D 01234567 ABCD 0 1 23 4 5 67 rear front rear front (1) Push (A) Push (B) Push (C) Push (D) Pop () Push (E) Push (F) Push (G) Push (H) Push (I) rear front E FG H I E rear F G HI

19 Implementing Circular Queue class Queue { private: Resize(); int front, rear; int top, capacity; int *queuearray; };

20 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.

21 Constructor for Circular Queue 0 1 23 4 5 67 rear front 0 1 23 4 5 67 rear A BC D E FG H How do know whether the queue is empty or full?

22 Constructor for Circular Queue Queue:: Queue(int s) { capacity = s; front = rear = 0; allocate memory space of size s for queuearray; }

23 IsEmpty() and Pop() Before returning the value, first check if the queue is empty. bool Queue:: IsEmpty() { if (front == rear) return true; return false; }

24 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]; }

25 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; }

26 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; }


Download ppt "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."

Similar presentations


Ads by Google