CSC 172 DATA STRUCTURES.

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.
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.
Prefix, Postfix, Infix Notation
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)
COSC 2006 Chapter 7 Stacks III
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:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
 Balancing Symbols 3. Applications
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
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.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Stacks, Queues & Deques CSC212.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Topic 15 Implementing and Using Stacks
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
Stack Applications.
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.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
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.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
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.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
 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.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
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 – review A Last-In First-Out (LIFO) structure Basic Operations:
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stacks Chapter 7 introduces the stack data type.
Objectives In this lesson, you will learn to: Define stacks
Stacks Stack: restricted variant of list
Data Structures – Week #3
Stack application: postponing data usage
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Infix to Postfix Conversion
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Queues Lecture 30 Fri, Apr 2, /3/2019 Queues.
Infix to postfix conversion
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
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:

CSC 172 DATA STRUCTURES

A TALE OF TWO STRUCTURES

STACK – LIFO QUEUE - FIFO

#define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double f) { if (sp < MAXVAL) val[sp++] = f; else printf(“error: stack full \n”); } double pop (void) { if (sp > 0) return val[--sp]; else { printf(“error: stack empty\n”); return 0.0; } }

#define MAXVAL 100 double val[MAXVAL]; void enqueue(double f) { #define MAXVAL 100 double val[MAXVAL]; void enqueue(double f) { } double dequeue (void) { }

#define MAXVAL 100 int head = 0 ; tail = 0; double val[MAXVAL]; void enqueue(double f) { if (((head + 1) % MAXVAL) != tail) { val[head] = f; head = (head + 1) % MAXVAL; } else printf(“error: queue full\n”); } double dequeue (void) { if (tail != head) { int temp = tail; tail = (tail + 1 ) % MAXVAL; return val[temp]; } else printf(“error: queue empty\n”); return 0.0 ; }

Stack & Queue with Linked List? What does “push” look like? What does “pop” look like? Queue What kind of linked list? What does enqueue look like? What does dequeus look like?

A useful stack algorithm Postfix evaluation We can rewrite the infix expression 1+2 As the postfix expression 1 2 + “Think” like a computer “load value ‘1’ into accumulator “load value ‘2’ into register A Add value in register A to value in accumulator How about 1+2+3+4 ? How about 2*3+4? How about 2+3*4?

How to implement? Can you write method that evaluates postfix expressions? double postfixeval(Object[] items) Where objects in items[] are either Double Character

Postfix evaluation using a stack Make an empty stack Read tokens until EOF If operand push onto stack If operator Pop two stack values Perform binary operation Push result At EOF, pop final result

Trace by hand 1 2 3 4 + + + 2 3 * 4 + 2 3 4 * +

Infix to postfix 1 + 2 * 3 == 7 (because multiplication has higher precedence) 10 – 4 – 3 == 3 (because subtraction proceeds left to right)

Infix to postfix 4 ^ 3 ^ 2 == 262144 != 4096 Generally, Rather than:

Precedence A few simple rules: ( ) > ^ > * / > + - ( ) > ^ > * / > + - Subtraction associates left-to-right Exponentiation associates right to left

Infix Evaluation 1 – 2 – 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 2 == -8 (1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) ) Could you write a program to evaluate stuff like this?

Postfix If we expressed (1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) ) As 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - Then, we could use the postfix stack evaluator

Postfix evaluation using a stack Make an empty stack Read tokens until EOF If operand push onto stack If operator Pop two stack values Perform binary operation Push result At EOF, pop final result

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2 1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 4 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 5 4 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 1024 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 3 1024 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 3072 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 6 3072 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 18432 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 7 18432 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2 7 18432 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2 7 18432 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 4 7 18432 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2041 18432 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 7 -1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - -8

But how to go from infix to postfix? Could you write a program to do it? What data structures would you use Stack Queue How about a simple case just using “+” 1+ 2 + 7 + 4 1 2 7 4 + + + Operands send on to output? Operator push on stack? Pop ‘em all at the end?

More complex 2 ^ 5 – 1 == 2 5 ^ 1 – Modify the simple rule? If you are an operator, pop first, then push yourself? 1 + 2 + 7 + 4 1 2 + 7 + 4 + ok

Even more complex 3 * 2 ^ 5 - 1 3 2 5 ^ * 1 – If you are an operator: 3 * 2 ^ 5 - 1 3 2 5 ^ * 1 – If you are an operator: Pop if the top of the stack is higher precedence than

Infix to postfix Stack Algorithm Operands : Send to queue Close parenthesis: Pop stack & send to queue until you find an open parenthesis Operators: Pop all stack symbols and send to queue until a symbol of lower precedence (or a right-associative symbol of equal precedence) appears. Push operator EOF: pop all remaining stack symbols and send to queue

1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7

1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 1

1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 - 1

1 – 2 ^ 3 ^ 3 – ( 4 + 5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