Data Structures & Algorithms

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
The unorganized person’s data structure
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.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
EC-211 DATA STRUCTURES LECTURE 5. 2 Stack Data Structure Stack of Books Stack of Notes Stack of Gifts.
Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)
Topic 15 Implementing and Using Stacks
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using 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.
The Stack Alexander G. Chefranov CMPE-231 Spring 2012.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures and Algorithms
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
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.
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)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
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.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
A grammar for arithmetic expressions involving the four basic operators and parenthesized expressions. Parenthesized expressions have the highest precedence.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
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 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
CHP-3 STACKS.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
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 No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
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.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
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.
Revised based on textbook author’s notes.
Stacks.
Algorithms and Data Structures
Stack.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks.
Introduction to C++ Programming
Abstract Data Type Abstract Data Type as a design tool
Stacks.
Topic 15 Implementing and Using Stacks
Stack.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Data Structures & Algorithms Stacks

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 at one end called the TOP of the stack

Stack The definition of stack provides for the insertion and deletion of items, so that a stack is a dynamic, constantly changing object. How does a stack change?

Stack New items may be put on top of the stack In which case the top of the stack moves upward to correspond to the new highest element Items which are at the top of the stack may be removed In which case the top of the stack moves downward to correspond to the new highest element Which way is up??? We must decide which end of the stack is designated as its top

Stack: More Specifically A common data structure in computing. Data items are "popped" and "pushed" (retrieved and stored) from the top of the stack. Stacks normally have a maximum size. It is an error to push items onto a full stack, or pop items off an empty stack. LIFO: Last In First Out

Applications Parsing of Algebraic Expression… Banking Transaction View You view the last transaction first Inventory Systems like issuing students the Multi-meters… you will be issued the most recently returned item likely

Insertion and Deletion in Stack C is the current top element of the stack. If any new items are added to the stack, they are placed on top of C If any new items are deleted C is deleted first C B A

Insertion and Deletion in Stack push D push E pop pop D C B A E D C B A D C B A C B A B A C B A

Abstraction of a Stack Now, let's think about a stack in an abstract way i.e., it doesn't hold any particular kind of thing (like books) and we aren't restricting ourselves to any particular programming language or any particular implementation of a stack. Stacks hold objects, usually all of the same type. Most stacks support just the simple set of operations we introduced above; and thus, the main property of a stack is that objects go on and come off of the top of the stack.

Operation on Stack A Stack should have the following methods: push(item) //Push an item onto the stack pop( ) //Pop an item off the stack isEmpty() //Return true if stack is empty top( ) //Return value of top item Because we think of stacks in terms of the physical analogy, we usually draw them vertically (so the top is really on top). There are several ways to implement a Stack in C.

Implementing a Stack with an Array Let's think about how to implement this stack in the C programming language. An ordered collection of items in C is array. First, if we want to store letters, we can use type char. Next, since a stack usually holds a bunch of items with the same type (e.g., char), we can use an array to hold the contents of the stack. Now, consider how we'll use this array of characters, call it contents, to hold the contents of the stack. At some point we'll have to decide how big this array is; keep in mind that a normal array has a fixed size. Let's choose the array to be of size 4 for now. So, an array getting A, then B, will look like: A B [0] [1] [2] [3]

Array… Stacks (Cont.) How do we know that which element to pop when user asks a pop operation.. As not all the spaces of array are filled. We need another variable (usually int) to keep track of last pushed index. So for each push we add one to top and for each pop we deduct one from top. A B [0] [1] [2] [3] TOP 1

Array Stack Pictorial View say Array Name is CharStacK Push(CharStack,’C’) Push(charStack, ‘D’) Pop(charStack) A B [0] [1] [2] [3] 1 A B C [0] [1] [2] [3] 2 A B C D [0] [1] [2] [3] 3 A B C [0] [1] [2] [3] 2

Push Concerns Pop Concerns Solution What if when the stack is full? What if the stack is empty? Solution Before any push check if the stack is already filled or not. If it is filled then generate error message e.g., Stack Overflow Before pop check if stack is not already empty

A Sample Program (Push Pop using Array) #include<stdio.h> #include<conio.h> #include<stdlib.h> #include <iostream.h> #define length 10 int top=-1; int stack[length]; void main() { int ch; do{ cout << endl << "1.push"; cout << endl << "2.pop"; cout << endl << "3.exit"; cout << endl << "enter choice"; cin >> ch; switch(ch) { case 1: push(); listStack(); break; case 2: cout << "data poped= " << pop(); case 3:exit(0); } } while(1); getch();

A Sample Program (Push Pop using Array) void push() { int data; if(top+1==length) cout << "stack overflow\n Cannot enter new data"; return; } top++; cout << "enter the data "; cin >> data; stack[top]=data; int pop() { int tempVar; if(top==-1)//we can also make isEmpty() cout << "stack is underflow (Empty)"; return(-1); } tempVar=stack[top]; top--; return(tempVar); void listStack() cout << endl << "The stack is" << endl ; for(int i=top;i>=0;i--) cout << stack[i] << endl;

Stack in Problem Solving Consider a mathematical expression that includes several sets of nested parenthesis, e.g. ( x + (y – (a +b)) ) We want to ensure that parenthesis are nested correctly and the expression is valid Validation There is an equal number of right and left parentheses Every right parenthesis is preceded by a matching left parenthesis

Expressions (A+B) * C Valid A + B) Invalid (A+B] Invalid

Rules Each left parentheses is the opening scope. Each right parenthesis is a closing scope. The number of left parentheses encountered whose matching right parentheses have not been encountered is nesting depth at that point. Parentheses count = 0 at the end means that no scopes have been left open and left and right parentheses exactly match. The parentheses count at any time should never become negative. IF negative then its error.

Expressions ( ( A + B ) Error 1 2 2 2 2 1 ( A * B ) 1 1 1 1 0 A*B( 0 0 0 1 Error Right Error

Parsing Parenthesis Let us change the problem slightly Three different kinds of scope delimiters exist e.g. { x + (y – [a +b]) } The scope ender must be of same type as scope opener It is necessary to keep track of not only the count of scope but also the types A stack may be used to keep track of the types of scopes encountered

Stacks in Expression Validation Whenever a scope opener is encountered it is pushed into the stack Whenever a scope ender is encountered the stack is examined If the stack is empty the scope ender does not have a matching opener and string is invalid If stack is not empty we pop an item and check if it corresponds to scope ender If match occurs we continue, at the end of the string the stack must be empty

Pusedo-Code Valid = true S = the empty stack // array of chars say char s[100] While (we have not read the entire string) { read the next symbol (symb) of the string; if (symb == ‘(‘ || symb == ‘[‘ || symb == ‘{‘) push (s, symb); if (symb == ‘)‘ || symb == ‘]‘ || symb == ‘}‘) if (empty(s)) valid = false; else { I = pop (s); if ( I is not the matching opener of symb) } // end of else } //end while If (valid) cout << “Valid String” << endl; Else cout << “not a valid string”

{ x + (y – [a +b]) } { ( { [ ( { ( { { 1 2 3 4 5 6 7