Chapter 4 Stacks 1. 4.1 Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

Stacks, Queues, and Linked Lists
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.
Stacks Example: Stack of plates in cafeteria.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
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: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
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.
Topic 15 Implementing and Using 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.
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 7 Stacks Dr. Youssef Harrath
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures and Algorithms
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
CHAPTER 61 STACK, QUEUES, RECURSION. Introduction when one wants to restrict insertion and deletion so that they can take place only at the beginning.
Stacks, Queues & Recursion
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 18: Stacks and Queues
Stack Any Other Data Structure Array Linked List
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
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.
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.
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
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
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
Data Structures & Algorithm CS-102
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.
Review Use of Stack Introduction Stack in our life Stack Operations
Data Structures Using C++ 2E
Queues Chapter 4.
Stacks and Queues Chapter 4.
Lecture No.06 Data Structures Dr. Sohail Aslam
Objectives In this lesson, you will learn to: Define stacks
Stacks.
Queues Chapter 4.
Data Structures Array Based Stacks.
STACKS.
Stacks Stack: restricted variant of list
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Stack.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
STACK, QUEUES, RECURSION
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks Data structure Elements added, removed from one end only
Stack.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Chapter 4 Stacks 1

4.1 Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called LIFO. A stack is defined in terms of operations that change its status and operations that check this status.: 1. Clear() 2. isEmpty() 3. Push(el) 4. Pop() 5. topEl() 2

Stack is useful when data have to be stored and then retrieved in reverse order. An application example of the stack is in matching delimiters in a program. parentheses, square brackets, curly brackets, and comment delimiters. Ex: a= b + (c-d) * (e-f) The program could have nested delimitiers. 3

The delimiters matching algorithm in C++ 4

5

6

7

A stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element,but is characterized by only two fundamental operations: push and pop. Elements are removed from the stack in the reverse order to the order of their addition :therefore, the lower elements are those that have been on the stack the longest. 8

9

Basic Operations on a Stack InitializeStack: Initializes the stack to an empty state. DestroyStack: Removes all the elements from the stack, leaving the stack empty. IsEmptyStack: Checks whether the stack is empty. If empty, it returns true; otherwise, it returns false. IsFullStack: Checks whether the stack is full. If full, it returns true; otherwise, it returns false 10

Push: Add new element to the top of the stack The input consists of the stack and the new element. Prior to this operation, the stack must exist and must not be full Top: Returns the top element of the stack. Prior to this operation, the stack must exist and must not be empty. Pop: Removes the top element of the stack. Prior to this operation, the stack must exist and must not be empty. Basic Operations on a Stack 11

12 Operations : initialize destroy build from given data (set of elements) check if it is empty get the total size of the stack add an element to the top of the stack [PUSH] delete an element from the top of the stack [POP] get the data from the element at the top of the stack update the data of the top element print the data of the top element print the entire stack Infix, Postfix and Prefix

13 In stack, no search, no adding in arbitrary positions, no sorting, no access to anything beyond the top element.

14 Push Top Data Top Check for enough room, (no overflow) operation

15 Pop Top Data Top Check if empty, (no underflow) operation

16 Stack top Top Data Top Check if empty, (no underflow) operation

17 (a) Physical Top (a) Conceptual count Top Head Data nodes

18 Stack Linked List Implementation Stack head structure count top datanext Stack node structure Stack count top end stack node data next end node

19 Stack Algorithms Create stack algorithm createStack Initializes metadata for a stack. stack.head = null 2 stack.count = 0 3 Return end createStack ? count ? (a) Before Create top stack count 0 (b) After Create top stack

20 Push stack algorithm pushStack Insert (push) data into a new node in the liked list. Postdata have been pushed in stack Return true if successful, false if memory overflow 1 If (stack full) 1 successes = false 2 else 1 allocate (newptr) 2 newptr->data = data 3 newptr->next = stack.top 4 stack.top = newptr 5 stack.count = stack.count successes = true 3 end if 4 Return successes end pushStack

21 Pop stack algorithm popStack This algorithm pops the item on the top of the stack and returns it to the user Postdata have been returned to calling algorithm Return true if successful, false if underflow 1 If (stack empty) 1 successes = false 2 else 1 dptr= stack.top 2 dataout = stack.top->data 3 stack.top= stack.top->next 4 stack.count = stack.count – 1 5 Recycle (dptr) 6 successes = true 3 end if 4 return successes end popStack

22 Stack Top algorithm Stacktop This algorithm receives the data from the top of the stack without changing the stack. Postdata have been returned to calling algorithm Return true if data returned, false if underflow 1 If (stack empty) 1 successes = false 2 else 1 dataout = stack.top->data 2 successes = true 3 end if 4 return successes end Stacktop

23 Empty Stack algorithm emptyStack Determines if stack is empty and returns a Boolean. Postreturns stack status Return Boolean, true: stack empty, false: stack contains data 1 If (stack not empty) 1 result = false 2 else 1 result = true 3 end if 4 return result end emptyStack

24 Full Stack algorithm fullStack Determines if stack is full and returns a Boolean. Postreturns stack status Return Boolean, true: stack full, false: stack is empty 1 If (memory available) 1 result = false 2 else 1 result = true 3 end if 4 return result end fullStack

25 Stack Count algorithm Stackcount Returns the number of elements currently in stack. Postreturns stack count Return integer count of number of elements in stack 1 return (stack.count) end Stackcount

26 Destroy Stack algorithm destroyStack This algorithm releases all nodes back to dynamic memory 1 Loop (stack.top not null) 1 temp= stack.top 2 Stack.top= stack.top->next 3 recycle (temp) 2 end loop 3 Stack.count = 0 4 return end destroyStack

Infix, Postfix and Prefix 27 Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is easiest to demonstrate the differences by looking at examples of operators that take two operands. Infix notation: X + Y Operators are written in-between their operands. This is the usual way we write expressions. An expression such as A * ( B + C ) / D is usually taken to mean something like: "First add B and C together, then multiply the result by A, then divide by D to give the final answer." Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules. For example, the usual rules for associativity say that we perform operations from left to right, so the multiplication by A is assumed to come before the division by D. Similarly, the usual rules for precedence say that we perform multiplication and division before we perform addition and subtraction.

28 Postfix notation (also known as "Reverse Polish notation"): X Y + Operators are written after their operands. The infix expression given above is equivalent to A B C + * D / The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the "+" is to the left of the "*" in the example above, the addition must be performed before the multiplication. Operators act on values immediately to the left of them. For example, the "+" above uses the "B" and "C". We can add (totally unnecessary) brackets to make this explicit: ( (A (B C +) *) D /) Thus, the "*" uses the two values immediately preceding: "A", and the result of the addition. Similarly, the "/" uses the result of the multiplication and the "D".

29 Prefix notation (also known as "Polish notation"): + X Y Operators are written before their operands. The expressions given above are equivalent to / * A + B C D As for Postfix, operators are evaluated left-to-right and brackets are superfluous. Operators act on the two nearest values on the right. I have again added (totally unnecessary) brackets to make this clear: (/ (* A (+ B C) ) D) Although Prefix "operators are evaluated left-to-right", they use values to their right, and if these values themselves involve computations then this changes the order that the operators have to be evaluated in. In the example above, although the division is the first operator on the left, it acts on the result of the multiplication, and so the multiplication has to happen before the division (and similarly the addition has to happen before the multiplication).

30

31