Data Structures – Week #3

Slides:



Advertisements
Similar presentations
§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.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
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.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
 Balancing Symbols 3. Applications
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Infix, Postfix, Prefix.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Stack Applications.
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.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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)
CSC 211 Data Structures Lecture 23
Infix to postfix conversion Scan the Infix expression left to right If the character x is an operand  Output the character into the Postfix Expression.
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.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
CHP-3 STACKS.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Review Use of Stack Introduction Stack in our life Stack Operations
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.
Lecture No.06 Data Structures Dr. Sohail Aslam
CC 215 Data Structures Stack ADT
Stacks Chapter 7 introduces the stack data type.
Objectives In this lesson, you will learn to: Define stacks
Stacks.
STACKS.
Stacks Stack: restricted variant of list
Stacks.
Stacks Chapter 4.
Data Structures – Week #3
Stack application: postponing data usage
Algorithms and Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I
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.
Data Structures – Week #5
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Stacks.
Stack.
Infix to postfix conversion
Data Structures – Week #4
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Data Structures – Week #3 Stacks

Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation of Stacks Stack Applications April 30, 2019 Borahan Tümer, Ph.D.

Stacks (Yığınlar) A stack is a list of data with the restriction that data can be retrieved from or inserted to the “top” of the list. By “top” we mean a pointer pointing to the element that is last added to the list. A stack is a last-in-first-out (LIFO) structure. April 30, 2019 Borahan Tümer, Ph.D.

Operations on Stacks Two basic operations related to stacks: Push (Put data to the top of the stack) Pop (Retrieve data from the top of the stack) April 30, 2019 Borahan Tümer, Ph.D.

Array Implementation of Stacks Stacks can be implemented by arrays. During the execution, the stack can grow by push operations, or shrink by pop operations within this array. One end of the array is the bottom and insertions and deletions (removals) are made from the other end. We also need another field that, at each point, keeps track of the current position of the top of the stack. April 30, 2019 Borahan Tümer, Ph.D.

Sample C Implementation #define stackSize …; struct dataType { … } typedef struct dataType myType; struct stackType { int top; myType items[stackSize]; typedef struct stackType stackType; stackType stack; stack structure April 30, 2019 Borahan Tümer, Ph.D.

Sample C Implementation… isEmpty() //Initialize Stack (i.e., set value of top to -1) stack.top=-1; int isEmpty(stackType s) { if (s.top == -1) return 1; //meaning true else return 0; //meaning false } April 30, 2019 Borahan Tümer, Ph.D.

Pop Operation int pop(stackType *sptr, myType *node) { if ( isEmpty(*sptr) ) { printf("stack empty"); return 0; //failure } *node = sptr->items[sptr->top]; sptr->top-- ; //or *node = sptr->items[sptr->top--]; return 1; //success April 30, 2019 Borahan Tümer, Ph.D.

Sample C Implementation… pop() int pop(stackType *sptr, myType *node) { if ( isEmpty(*sptr) ) { printf("stack empty"); return 0; //failure } *node = sptr->items[sptr->top--]; return 1; //success April 30, 2019 Borahan Tümer, Ph.D.

Push Operation int push(stackType *sptr, myType *node, int n){ if ( sptr->top >= n ) { printf("stack full"); return 0; //failure } ++(sptr->top); //or sptr->content[++(sptr->top)]=node; sptr->content[ (sptr->top)]=node; return 1; //success April 30, 2019 Borahan Tümer, Ph.D.

Sample C Implementation… push() int push(stackType *sptr, myType *node, int n){ if ( sptr->top >= n ) { printf("stack full"); return 0; //failure } sptr->items[++(sptr->top)]=*node; return 1; //success April 30, 2019 Borahan Tümer, Ph.D.

Linked List Implementation of Stacks //Declaration of a stack node struct StackNode { int data; struct StackNode *next; } typedef struct StackNode StackNode; typedef StackNode * StackNodePtr; … April 30, 2019 Borahan Tümer, Ph.D.

Linked List Implementation of Stacks StackNodePtr NodePtr, top; … NodePtr = malloc(sizeof(StackNode)); top = NodePtr; NodePtr->data=2; // or top->data=2 NodePtr->next=NULL; // or top->next=NULL; Push(&top,&NodePtr); //Nodeptr is an output variable!!! Pop(&top); April 30, 2019 Borahan Tümer, Ph.D.

Push and Pop Functions Void Push (StackNodePtr *TopPtr, StackNodePtr *NewNodePtr) { *NewNodePtr = malloc(sizeof(StackNode)); // NewNodePtr to pass to invoking function!!! (*NewNodePtr)->data=5; (*NewNodePtr)->next = *TopPtr; *TopPtr = *NewNodePtr; } Void Pop(StackNodePtr *TopPtr) { StackNodePtr TempPtr; TempPtr= *TopPtr; *TopPtr = *TopPtr->next; free(TempPtr); // or you may return TempPtr!!! April 30, 2019 Borahan Tümer, Ph.D.

Linked List Implementation of Stacks Void Push (StackNodePtr *TopPtr, StackNodePtr *NewNodePtr) { *NewNodePtr = malloc(sizeof(StackNode)); (*NewNodePtr)->data=5; (*NewNodePtr)->next =NULL; (*NewNodePtr)->next = *TopPtr; *TopPtr = *NewNodePtr; } NodePtr = malloc(sizeof(StackNode)); top = NodePtr; NodePtr->data=2; // or top->data=2 NodePtr->next=NULL;// or top->next=NULL; Push(&top,&NodePtr); April 30, 2019 Borahan Tümer, Ph.D.

Stack Applications Three uses of stacks Symbol matching in compiler design Return address storage in function invocations Evaluation of arithmetic expressions and cross-conversion into infix, prefix and postfix versions April 30, 2019 Borahan Tümer, Ph.D.

Symbol Matching in Compiler Design Algorithm: 1. Create an empty stack. 2. Read tokens until EOF. Ignore all tokens other than symbols. 3. If token is an opening symbol, push it onto the stack. 4. If token is a closing symbol and stack empty, report an error. 5. Else pop the stack. If symbol popped and opening symbol do not match report an error 6. If EOF and stack not empty, 7. Else, the symbols are balanced. April 30, 2019 Borahan Tümer, Ph.D.

Symbol Matching Example: int pop(Stack *sptr, myType *node) { if ( isEmpty(*sptr) ) { printf("stack empty"); return 0; //failure } *node = sptr->items[sptr->top--]; return 1; //success April 30, 2019 Borahan Tümer, Ph.D.

Use of Stacks in Function Invocation During a function invocation (function call) Each argument value is copied to a local variable called “a dummy variable.” Any possible attempt to change the argument changes the dummy variable, not its counterpart in the caller. Memory space is allocated for local and dummy variables of the called function. Control is transferred to the called. Before this, return address of the caller must also be saved. This is the point where a system stack is used. April 30, 2019 Borahan Tümer, Ph.D.

Use of Stacks in Function Invocation Returning to the caller, three actions are taken: Return address is retrieved. Data area from the called is cleaned up. Finally, control returns to the caller. Any returned value is also stored in known registers. April 30, 2019 Borahan Tümer, Ph.D.

A Function Call Example Program Counter … main(…) { n11 … n12 … n13 call f2(…); r1 … n14 … } … f2(…) { n24 … n25 … n26 call f3(…); r2 … n27 … } … f3(…) { n37 … n38 … n39 call f4(…); r3 … } n41 n42 n39 n37 n26 n43 n38 n27 n14 r0 r1 n25 r2 r3 n24 n13 n12 n11 Stack Pointer s1 s3 sEmpty s2 s0 System Stack … f4(…) { n41 … n42 … n43 … } s3 s2 s1 s0 April 30, 2019 Borahan Tümer, Ph.D.

Infix, Postfix and Prefix Formats of Arithmetic Expressions The name of the format of arithmetic expression states the location of the operator. Infix: operator is between the operands (L op R) Postfix: operator is after the operands (L R op) Prefix: operator is before the operands (op L R) April 30, 2019 Borahan Tümer, Ph.D.

Examples to Infix, Postfix and Prefix Formats A+B AB+ +AB A/(B+C) ABC+/ /A+BC A/B+C AB/C+ +/ABC A-B*C+D/(E+F) ABC*-DEF+/+ +-A*BC/D+EF A*((B+C)/(D-E)+F)-G/(H-I) ABC+DE-/F+*GHI-/- -*A+/+BC-DEF/G-HI April 30, 2019 Borahan Tümer, Ph.D.

Rules to watch during Cross-conversions Associative Rules 1) + and - associate left to right 2) * and / associate left to right 3) Exponentiation operator (^ or **) associates from right to left. Priorities and Precedence Rules 1) + and - have the same priority 2) * and / have the same priority 3) (* and /) precede (+ and -) April 30, 2019 Borahan Tümer, Ph.D.

Algorithm for InfixPostfix Conversion Initialize an operator stack While not EOArithmeticExpression Do Get next token case token of ‘(’: Push; //assume the lowest precedence for ‘(’ ‘)’: Pop and place token in the incomplete postfix expression until a left parenthesis is encountered; If no left parenthesis return with failure an operator: If empty stack or token has a higher precedence than the top stack element, push token and go to 2.i Else pop and place in the incomplete postfix expression and go to c an operand: place token in the incomplete postfix expression If EOArithmeticExpression Pop and place token in the incomplete postfix expression until stack is empty April 30, 2019 Borahan Tümer, Ph.D.

Evaluation of Arithmetic Expressions Initialize an operand stack While not EOArithmeticExpression Do Get next token; Case token of an operand: push; an operator: if the last token was an operator, return with failure; pop twice; evaluate expression; push result; April 30, 2019 Borahan Tümer, Ph.D.

Evaluation of Arithmetic Expressions Example: 9 8 8 6 - / 2*1+- = ? Token Stack Content Operation 9 None 8 9 8 9 8 8 6 9 8 8 6 - 9 8 2 8-6=2 / 9 4 8/2=4 2 9 4 2 none * 4*2=8 1 9 8 1 + 9 9 8+1=9 9-9 April 30, 2019 Borahan Tümer, Ph.D.