Stacks and Queues The Stack abstract data type

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Bioinformatics Programming
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
Stacks Example: Stack of plates in cafeteria.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
CHAPTER 31 STACKS AND QUEUES. CHAPTER 32 A BABA DCBADCBA CBACBA DCBADCBA EDCBAEDCBA top *Figure 3.1: Inserting and deleting elements in a stack (p.102)
Data Structure Dr. Mohamed Khafagy.
A stack is a data linear data structure in which addition of new element or deletion of an existing element always takes place at the same end. This.
Chapter 3 1. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can.
 Balancing Symbols 3. Applications
CS Data Structures Chapter 3 Stacks and Queues.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
CHAPTER 31 STACKS AND QUEUES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data.
Chapter 3. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
CH3 Stacks and Queues Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in.
CS Data Structures Chapter 3 Stacks and Queues.
Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions.
Chapter 17: Stacks and Queues
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stacks  Introduction  Applications  Implementations  Complex Applications.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
杭州师范大学信息科学与工程学院 袁贞明 STACKS AND QUEUES and their applications.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
CHP-3 STACKS.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to Data Structures CHAPTER 3 STACKS and QUEUES 3.1 The Stack.
Chap 3 Stack and Queue. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Stack Applications Reading: Chap. 3 Weiss.
Stacks Objective After this lecture you will be able to: Describe a stack Describe the representation of stack using linear array Describe the representation.
STACKS AND QUEUES.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
CSCE 3110 Data Structures & Algorithm Analysis
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Stacks.
Stacks Stacks.
Data Structures 5th Week
CS 201 Data Structures and Algorithms
Objectives In this lesson, you will learn to: Define stacks
Stacks.
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures Array Based Stacks.
STACKS.
Stacks Chapter 4.
Data Structures – Week #3
Algorithms and Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
Subject Name: DATA STURCTURES WITH C Subject Code: 10CS35
CSCE 3110 Data Structures & Algorithm Analysis
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Mark Allen Weiss, Addison Wesley
UNIT-I Topics to be covere d 1.Introduction to data structures.
The List, Stack, and Queue ADTs
پشته ها و صف ها.
Stacks and Queues 1.
Stacks.
CSCE 3110 Data Structures & Algorithm Analysis
Stack.
Data Structures – Week #3
CH3. STACKS AND QUEUES.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Stacks and Queues The Stack abstract data type The Queue abstract data type A Mazing problem Evaluation of expressions Multiple Stacks and Queues

The Stack Abstract Data Type A stack is an ordered list in which insertions and deletions are made at one end called the top. Given a stack S = (a0, ... , an-1), we say that a0 is the bottom element, an-1 is the top element, and ai is on the top of element ai-1, 0 <= i < n. Last-In-First-Out(LIFO) list A top B C D

(Ex) System stack Used by a program at run-time to process function calls. Whenever a function is invoked, the program creates a structure, activation record (or stack frame), and places it on top of the system stack. frame 0 old frame pointer return address frame 1 main fp local variable frame 2 a1

structure Stack is objects: a finite ordered list with zero or more elements. functions: for all stack  Stack, item  element, max_stack_size  positive integer Stack CreateS(max_stack_size) ::= create an empty stack whose maximum size is max_stack_size Boolean IsFull(stack, max_stack_size) ::= if (number of elements in stack == max_stack_size) return TRUE else return FALSE Stack Add(stack, item) ::= if (IsFull(stack)) stack_full else insert item into top of stack and return Boolean IsEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUE else return FALSE Element Delete(stack) ::= if(IsEmpty(stack)) return else remove and return the item on the top of the stack.

Stack CreateStack(max_stack_size) ::= #define MAX_STACK_SIZE 100 /* Maximum Stack Size */ typedef struct { int key; /* other fields */ } element ; element stack[MAX_STACK_SIZE] ; int top = -1; Boolean IsEmpty(stack) ::= top < 0; Boolean IsFull(stack) ::= top >= MAX_STACK_SIZE - 1;

void add(int. top, element item) { /. add an item to the global stack void add(int *top, element item) { /* add an item to the global stack */ if (*top >= MAX_STACK_SIZE-1) { stack_full( ); return; } stack[++*top] = item; } element delete(int *top) { /* return the top element from the stack */ if (*top == -1) { return stack_empty( ); /* returns and error key */ } return stack[(*top)--]; }

Linked list representation of Stacks When several stacks coexisted, there was no efficient way to represent them sequentially, i.e., by array. top element link ... NULL Linked Stack

Void addstack(int i, char *x) { nodeptr *temp; temp = (nodeptr *) malloc(sizeof(nodeptr)); strcpy(temp -> data, x); temp -> link = top[i]; top[i] = temp; }

Void deletestack(int i, char *x) { nodeptr *temp; if (top[i] == NULL) empty_stcack(); else { temp = top[i]; strcpy(x, temp -> data); top[i] = temp -> next; free(temp); }}

The Queue Abstract Data Type A queue is an ordered list in which all insertions take place at one end, called rear, and all deletions take place at the opposite end, called front. Given a queue Q = (a0, a1, ... , an-1), a0 is the front element, an-1 is the rear element, and ai+1 is behind ai, 0 <= i < n-1. First-In-First-Out(FIFO) A rear B C D front

structure Queue is objects: a finite ordered list with zero or more elements. functions: for all queue  Queue, item  element, max_ queue_ size  positive integer Queue CreateQ(max_queue_size) ::= create an empty queue whose maximum size is max_queue_size Boolean IsFullQ(queue, max_queue_size) ::= if(number of elements in queue == max_queue_size) return TRUE else return FALSE Queue AddQ(queue, item) ::= if (IsFullQ(queue)) queue_full else insert item at rear of queue and return queue Boolean IsEmptyQ(queue) ::= if (queue ==CreateQ(max_queue_size)) return TRUE else return FALSE Element DeleteQ(queue) ::= if (IsEmptyQ(queue)) return else remove and return the item at front of queue.

Queue CreateQueue(max_queue_size) ::= #define MAX_QUEUE_SIZE 100 /* Maximum queue size */ typedef struct { int key; /* other fields */ } element ; element queue[MAX_QUEUE_SIZE] ; int rear = -1; int front = -1; Boolean IsEmpty(queue) ::= front == rear; Boolean IsFull(queue) ::= rear == MAX_STACK_SIZE - 1;

void addq(int. rear, element item) { /. add an item to the queue void addq(int *rear, element item) { /* add an item to the queue */ if (*rear == MAX_QUEUE_SIZE-1) { queue_full( ); return; } queue [++*rear] = item; } element deleteq(int *front, int rear) { /* remove element at the front of the queue */ if ( *front == rear) { return queue_empty( ); /* return an error key */ } return queue [++ *front]; }

Linked list representation of Queues When several queues coexisted, there was no efficient way to represent them sequentially, i.e., by array. front rear element link ... NULL Linked Queue

void addqueue(int i, char *x) { nodeptr *temp; temp = (nodeptr *)malloc(nodeptr)); strcpy(temp -> data, x); temp -> link = NULL: if (front[i] == NULL) front[i]=temp; else rear[i]->link = temp; rear[i] = temp; }

void deletequeue(int i, char *x) { nodeptr *temp; if (front[i] == NULL) empty_queue(); else { temp = front[i]; front[i] = temp -> link; strcpy(x, temp->data); free(temp); }}

(Ex) Job scheduling based on a sequential queue front : one position back from the first element rear : current end front -1 1 rear 2 Q[0] J1 Q[1] J2 Q[2] J3 Q[3] Comments queue is empty Job 1 is added Job 2 is added Job 3 is added Job 1 is deleted Job 2 is deleted The queue gradually shifts to the right

Circular Queue front : one position counterclockwise from the first element rear : current end The queue is empty iff front = rear front = 0, rear = 0 Empty queue front = 0, rear = 3 Nonempty queue [0] [1] [2] [3] [4] [5] J1 J2 J3

Circular queue hold at most MAX_QUEUE_SIZE - 1 elements [1] [0] [2] [3] [4] [5] front = 0, rear = 5 J1 J2 J3 FULL QUEUE J4 J5 front = 4, rear = 3 J7 J8 J9 J6

void addq(int front, int. rear, element item) { / void addq(int front, int *rear, element item) { /* add an item to the queue */ *rear = (*rear +1) % MAX_QUEUE_SIZE; if (front == *rear) /* reset rear and print error */ { return; } queue[*rear] = item; }

element deleteq(int. front, int rear) { element item; / element deleteq(int* front, int rear) { element item; /* remove front element from the queue and put it in item */ if (*front == rear) /* queue_empty returns an error key */ { return queue_empty( ); } *front = (*front+1) % MAX_QUEUE_SIZE; return queue[*front]; }

A Mazing Problem Representation of the maze entrance 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 entrance exit

typedef struct { short int vert; short int horiz; } offsets; offsets move[8]; /* array of moves for each direction */ next_row = row + move[dir].vert ; next_col = col + move[dir].horiz ; #define MAX_STACK_SIZE 100 /* Maximum Stack Size */ short int row; short int col; short int dir; } element ; element stack[MAX_STACK_SIZE];

Table of moves Name N NE E SE S SW W NW Dir 1 2 3 4 5 6 7 1 2 3 4 5 6 7 move [dir].vert -1 move [dir].horiz

Analysis of path : Stack size : Since each position is visited no more than once, the stack needs to have only as many positions as there are zeros The worst case complexity of the algorithm is O(mp), where m and p are the number of rows and columns of the maze.

Evaluation of Expressions X = a / b - c + d * e - a * c a = 4, b = c = 2, d = e = 3 Interpretation 1: ((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1 Interpretation 2: (4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666… How to generate the machine instructions corresponding to a given expression? precedence rule + associative rule

*Figure 3.12: Precedence hierarchy for C (p.119)

*Figure 3.12: Precedence hierarchy for C (p.119)

*Figure 3.12: Precedence hierarchy for C (p.119) 1.The precedence column is taken from Harbison and Steele. 2.Postfix form 3.prefix form *Figure 3.12: Precedence hierarchy for C (p.119)

*Figure 3.13: Infix and postfix notation (p.120) user compiler Postfix: no parentheses, no precedence

*Figure 3.14: Postfix evaluation (p.120)

Goal: Translate infix expression into postfix expression Assumptions: operators: +, -, *, /, % operands: single digit integer #define MAX_STACK_SIZE 100 /* maximum stack size */ #define MAX_EXPR_SIZE 100 /* max size of expression */ typedef enum{1paran, rparen, plus, minus, times, divide, mod, eos, operand} precedence; int stack[MAX_STACK_SIZE]; /* global stack */ char expr[MAX_EXPR_SIZE]; /* input string */

*Program 3.9: Function to evaluate a postfix expression (p.122) int eval(void) { /* evaluate a postfix expression, expr, maintained as a global variable, ‘\0’ is the the end of the expression. The stack and top of the stack are global variables. get_token is used to return the token type and the character symbol. Operands are assumed to be single character digits */ precedence token; char symbol; int op1, op2; int n = 0; /* counter for the expression string */ int top = -1;

*Program 3.9: Function to evaluate a postfix expression (p.122) token = get_token(&symbol, &n); while (token != eos) { if (token == operand) add(&top, symbol-’0’); /* stack insert */ else { op2 = delete(&top); /* stack delete */ op1 = delete(&top); switch(token) { case plus: add(&top, op1+op2); break; case minus: add(&top, op1-op2); break; case times: add(&top, op1*op2); break; case divide: add(&top, op1/op2); break; case mod: add(&top, op1%op2); } } token = get_token (&symbol, &n); } return delete(&top); /* return result */ }

*Program 3.10: Function to get a token from the input string (p.123) precedence get_token(char *symbol, int *n) { /* get the next token, symbol is the character representation, which is returned, the token is represented by its enumerated value, which is returned in the function name */ *symbol =expr[(*n)++]; switch (*symbol) { case ‘(‘ : return lparen; case ’)’ : return rparen; case ‘+’: return plus; case ‘-’ : return minus; case ‘/’ : return divide; case ‘*’ : return times; case ‘%’ : return mod; case ‘\0‘ : return eos; default : return operand; /* no error checking, default is operand */ } }

Infix to Postfix Conversion (Intuitive Algorithm) (1) Fully parenthesize expression a / b – c + d * e – a * c --> ((((a / b) – c) + (d * e)) – (a * c)) (2) All operators replace their corresponding right parentheses. ((((a / b) – c) + (d * e)) – (a * c)) --> ((((a b)/ c) – (d e)*) +(a c)*) – (3) Delete all parentheses. ab/c – de*+ac* – two passes

*Figure 3.15: Translation of a+b*c to postfix (p.124) The orders of operands in infix and postfix are the same. a + b * c, * > +

* Figure 3.16: Translation of a*(b+c)*d to postfix (p.124) Example: a *1 (b +c) *2 d match ) *1 = *2

Rules (1) Operators are taken out of the stack as long as their in-stack precedence is higher than or equal to the incoming precedence of the new operator. (2) ( has low in-stack precedence, and high incoming precedence. ( ) + - * / % eos isp 0 19 12 12 13 13 13 0 icp 20 19 12 12 13 13 13 0

precedence stack[MAX_STACK_SIZE]; / precedence stack[MAX_STACK_SIZE]; /* isp and icp arrays -- index is value of precedence lparen, rparen, plus, minus, times, divide, mod, eos */ static int isp [ ] = {0, 19, 12, 12, 13, 13, 13, 0}; static int icp [ ] = {20, 19, 12, 12, 13, 13, 13, 0}; void postfix(void) { /* output the postfix of the expression. The expression string, the stack, and top are global */ char symbol; precedence token; int n = 0; int top = 0; /* place eos on stack * stack[0] = eos; isp: in-stack precedence icp: incoming precedence

for (token = get _token(&symbol, &n); token for (token = get _token(&symbol, &n); token != eos; token = get_token(&symbol, &n)) { if (token == operand) printf (“%c”, symbol); else if (token == rparen ) { /*unstack tokens until left parenthesis */ while (stack[top] != lparen) print_token(delete(&top)); delete(&top); /*discard the left parenthesis */ } else { /* remove and print symbols whose isp is greater than or equal to the current token’s icp */ while(isp[stack[top]] >= icp[token] ) print_token(delete(&top)); add(&top, token); } } while ((token = delete(&top)) != eos) print_token(token); print(“\n”);

*Figure 3.17: Infix and prefix expressions (p.127) (1) evaluation (2) transformation

숙제 계산기 프로그램 조건: 사용 가능 연산자: *,/, +, -, (, ), % 사용가능 피연산자: 정수 및 실수