Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Ceng-112 Data Structures I Chapter 5 Queues.
Stacks Chapter 11.
COSC 2006 Chapter 7 Stacks III
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.
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Chapter 3 Stacks.
Data Structures & Algorithms
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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.
Objectives of these slides:
Object Oriented Data Structures
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
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.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
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,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.
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.
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.
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.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
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 & Algorithms
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
Ceng-112 Data Structures ISerap ATAY, Ph. D. 1 Chapter 3 – Part 2 Linear Lists.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
CHP-3 STACKS.
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.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Stacks Chapter 3 Objectives Upon completion you will be able to
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Stacks Chapter 5.
Stacks and Queues Chapter 4.
Homework 4 questions???.
Objectives In this lesson, you will learn to: Define stacks
Stacks.
STACKS.
Stacks Stack: restricted variant of list
Stacks Chapter 4.
Data Structures – Week #3
Stack application: postponing data usage
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Stack.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stack.
Data Structures – Week #3
LINEAR DATA STRUCTURES
Presentation transcript:

Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks

Ceng-112 Data Structures ITurgut Kalfaoglu 2 Figure 3-2 Linear Lists Operations are; 1.Insertion 2.Deletion 3.Retrieval 4.Traversal (exception for restristed lists).

Ceng-112 Data Structures ITurgut Kalfaoglu 3 Figure 4-1 Stacks A stack is a Last in First out (LIFO) data structure in which all insertions and deletions are restricted to one end called top.

Ceng-112 Data Structures ITurgut Kalfaoglu 4 Figure 4-2 Stack Basic Operations Overflow?

Ceng-112 Data Structures ITurgut Kalfaoglu 5 Figure 4-3 Stack Basic Operations Underflow?

Ceng-112 Data Structures ITurgut Kalfaoglu 6 Figure 4-4 Stack Basic Operations

Ceng-112 Data Structures ITurgut Kalfaoglu 7 Figure 4-5

Ceng-112 Data Structures ITurgut Kalfaoglu 8 Figure 4-6 Stack Data Structure

Ceng-112 Data Structures ITurgut Kalfaoglu 9 Figure 4-7 Stack Data Structure

Ceng-112 Data Structures ITurgut Kalfaoglu 10 Figure 4-8, Part I Stack Operations

Ceng-112 Data Structures ITurgut Kalfaoglu 11 Figure 4-8, Part II Stack Operations

Ceng-112 Data Structures ITurgut Kalfaoglu 12 Create Stack algorithm createStack Allocates memory for a stack head node from dynamic memory and returns its address to the caller. Pre Nothing Post Head node allocated or error returned Return pointer to head node or null pointer if no memory 1.if (memory available) 1.allocate (stackPtr) 2.stackPtr  count = 0 3.stackPtr  top = null 2.else 1.stackPtr = null 3.return stackPtr end createStack

Ceng-112 Data Structures ITurgut Kalfaoglu 13 Figure 4-9 Push Stack

Ceng-112 Data Structures ITurgut Kalfaoglu 14 Push Stack algorithm pushStack(val stack, val data ) Insert (push) one item into the stack. Pre stack is a pointer to the stack head structure. data contains data to be pushed into stack. Post data have been pushed in stack. 1.if (stack full) 1.success = false 2.else 1.allocate (newPtr) 2.newPtr  data= data 3.newPtr  next= stack  top 4.stack  top=newPtr 5.stack  count = stack  count+1 6.Success=true 3.return success end pushStack

Ceng-112 Data Structures ITurgut Kalfaoglu 15 Figure 4-10 Pop Stack

Ceng-112 Data Structures ITurgut Kalfaoglu 16 Pop Stack algorithm popStack(val stack, val dataOut ) Pops the item on the top of the stack and returns it to the user. Pre stack is a pointer to the stack head structure. dataOut is a reference variable to receive the data. Post data have been returned to the calling algorithm. 1.if (stack empty) 1.success = false 2.else 1.dltPtr= stack  top 2.dataOut = stack  top  data 3.stack  top = stack  top  next 4.stack  count = stack  count – 1 5.recycle(dltPtr) 6.success=true 3.return success end popStack

Ceng-112 Data Structures ITurgut Kalfaoglu 17 Destroy Stack algorithm destroyStack(val stack ) This algorithm releases all nodes back to the dynamic memory. Pre stack is a pointer to the stack head structure. Post stack empty and all nodes recycled 1.if (stack not empty) 1.loop 1.temp = stack  top 2.stack  top = stack  top  link 3.recycled(temp) 2.recycled (stack) 3.return null pointer end destroyStack

Ceng-112 Data Structures ITurgut Kalfaoglu 18 Stack Applications We can be classified stack applications into four categories: 1.Reversing data. 2.Parsing data. 3.Postponing data usage. 4.Backtracking steps.

Ceng-112 Data Structures ITurgut Kalfaoglu 19 Stack Applications Reversing Data Push Pop

Ceng-112 Data Structures ITurgut Kalfaoglu 20 Figure 4-11 Stack Applications Parsing Data Breaks the data into independent pieces for further processing. source program Compiler machine language

Ceng-112 Data Structures ITurgut Kalfaoglu 21 Stack Applications Parsing Data 3.Pop ) 2.Push ( 1.Push ( 3.Pop ) 2.Pop ) 1.Push (

Ceng-112 Data Structures ITurgut Kalfaoglu 22 Stack Applications Posponement Arithmetic expression can be represent three different formats: 1.Prefix+ a b 2.Infixa + b 3.Postfixa b + Arithmetic precedence; multiply and divide before add and subtract!

Ceng-112 Data Structures ITurgut Kalfaoglu 23 Stack Applications Posponement A + B * C Place the paranthesis ( A + ( B * C)) Replace it in postfix format (A(BC*)+) Remove all paranthesis ABC*+ Implementation by computer is too hard!

Ceng-112 Data Structures ITurgut Kalfaoglu 24 Figure 4-12, Part I Stack Applications Posponement * and / operators have higher priority than the operator at the top of the stack.

Ceng-112 Data Structures ITurgut Kalfaoglu 25 Figure 4-12, Part II Stack Applications Posponement

Ceng-112 Data Structures ITurgut Kalfaoglu 26 Figure 4-13 Stack Applications Evaluation of Postfix Expression

Ceng-112 Data Structures ITurgut Kalfaoglu 27 Excercise Change the following infix expression to postfix expression using the algoritmic method (a stack). a+b*c-d Solution: abc*+d-

Ceng-112 Data Structures ITurgut Kalfaoglu 28 Infix String : a+b*c-d The first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack. Next character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack. The next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack. Next character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be : End result : * Infix String : a+b*c-d * Postfix String : abc*+d-

Ceng-112 Data Structures ITurgut Kalfaoglu 29 Figure 4-14 Stack Applications BackTracking

Ceng-112 Data Structures ITurgut Kalfaoglu 30 Figure 4-15

Ceng-112 Data Structures ITurgut Kalfaoglu 31 Figure 4-16 Stack Applications BackTracking

Ceng-112 Data Structures ITurgut Kalfaoglu 32 Figure 4-17

Ceng-112 Data Structures ITurgut Kalfaoglu 33 Figure 4-20 Array Implementation of Stacks stack stackAry count stackMax top end stack

Ceng-112 Data Structures ITurgut Kalfaoglu 34 Hw-5 Write a program that accepts parentheses and brackets characters, one per line on standard input. Use a stack to determine whether pairs of characters are matching or not. Therefore complete the body of below function. bool balanced(const char p[ ], size_t n) // Precondition: p[0]...p[n-1] contains n characters, each of which // is '(', ')', '{' or '}'. // Postcondition: The function returns true if the characters form a // sequence of correctly balanced parentheses with each '(' matching // a ')' and each '{' matching a '}'. Note that a sequence such as // ( { ) } is NOT balanced because when we draw lines to match the // parentheses to their partners, the lines cross each other. On the // other hand, ( { } ) amd { ( ) } are both balanced. Load your HW-5 to FTP site until 13 Apr. 07 at 09:00 am.

Ceng-112 Data Structures ITurgut Kalfaoglu 35 Figure 4-21 Exercises Projects – 23 page 211, 212

Ceng-112 Data Structures ITurgut Kalfaoglu 36 Figure 4-22 Exercises Projects – 24 page 212