Chapter 5 Stack (part 1).

Slides:



Advertisements
Similar presentations
Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental.
Advertisements

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks, Queues, and Deques
Data Structures & Algorithms
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.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
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.
30-Jun-15 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.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Object Oriented Data Structures
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures & Algorithms
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
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.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Review Array Array Elements Accessing array elements
Stacks and Queues Chapter 4.
Chapter 6: The Stack Abstract Data Type
CC 215 Data Structures Stack ADT
Stacks.
Chapter 6: The Stack Abstract Data Type
Queues Queues Queues.
Cinda Heeren / Geoffrey Tien
Stacks.
Stack and Queue APURBO DATTA.
Visit for more Learning Resources
Stacks and Queues.
Building Java Programs
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Data Structures and Database Applications Stacks in C#
Stacks, Queues, and Deques
Stack Implementations
Abstract Data Type Abstract Data Type as a design tool
Pointers & Dynamic Data Structures
Stacks CS-240 Dick Steflik.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks, Queues, and Deques
CS210- Lecture 3 Jun 6, 2005 Announcements
Stacks.
CMPT 225 Lecture 7 – Stack.
Stack Implementations
Presentation transcript:

Chapter 5 Stack (part 1)

What is Stack? A stack is a data structure with the property that only the top element of the stack is accessible A stack is a list of homogeneous elements In a stack, addition and deletion of elements occurs only at one end, called the top of the stack A stack is a Last In First Out (LIFO) data structure

Last In First Out (LIFO) algorithm In LIFO data structure, top element of a stack is the last element to be added to the stack Elements are added and removed from one end known as top The item that is added last will be removed first.

Add and remove element in stack occurs at the top of the stack Adding element Remove element

Example of stack: Stack of coins Stack of books Stack of trays Stack of boxes

Stack Operations Assume that there are 5 boxes to be added into a stack namely A, B, C, D and E. Initially, the stack is empty.

Stack Operations (cont..)

Stack Specification The Java API includes a Stack class as part of the package java.util. The ADT specification is quite simple and there are only five (5) operations ina ddition to the constructor.

The Stack ADT Description: This ADT describes a dynamic stack whose element type is left unspecified. Dynamic means the size is not fixed. Properties: Stack is LIFO data structure. All accesses to elements are done from the element referenced by top. All stack operations refer to the topmost element in the stack. Insertion / Addition of new elements are done “above” the top element.

The Stack ADT Attributes: size: the number of elements in the stack. top: the topmost element of the stack. (the top is set to null when the stack is empty)

The Stack ADT (cont..) Operations Stack( ) : the constructor Pre-condition : None Task : to initialize the stack attributes Post-condition: Size is zero Top refers to null Returns : Nothing

The Stack ADT (cont..) Example: Start with an empty stack The bottom of the stack is at index 0 and the topmost element is at index size ( ) -1. Coding example: Stack name = new Stack(); // this statement will create a new stack named name and initally the stack is empty. So, the value of top is null.

The Stack ADT (cont..) Operations push (data_type element ) Pre-condition : None Task : to insert / add a new element onto the top of the stack Post-condition: Element is placed on top of the stack Size is incremented by 1 Top refers to the element pushed Returns : Nothing

EXAMPLE Add an object to the top of the stack name.push(“Ali”); name.push(“Siti”); Top Ali [0] name Siti [1] Ali [0] name

The Stack ADT (cont..) Operations pop ( ) Pre-condition : isEmpty() is false Task : remove and return the element at top Post-condition: The top element id no longer in the stack Size is decremented by 1 Top refers to the element below the previous topmost element or null if the stack is empty Returns : the element removed

Example Remove an object from the top of the stack name.pop(); This statement will return “Sti”. After Before Siti [1] Ali [0] name Ali [0] name

REMOVE ALL OBJECTS FROM THE STACK It can be done by repeatedly calling pop() until the stack is empty while (!name.isEmpty() { System.out.println(name.pop()); } After the loop is completely iterated, the stack is now empty.

The Stack ADT peek ( ) Pre-condition : isEmpty() is false Task : return the element at top Post-condition : The stack is unchanged Returns : the element referenced by top

Example To retrieve the object from the top of the stack (without removing the object) and display the object: System.out.println (name.peek());

The Stack ADT size ( ) Pre-condition : none Task : determine teh number of elements in the stack Post-condition : the stack is unchanged Returns : the number of elements in the stack

The Stack ADT isEmpty ( ) Pre-condition : none Task : determine if the stack has any elements in it Post-condition : the stack is unchanged Returns : true if the stack is empty (size = 0), false if the stack is not empty

Example To determine whether a stack is empty (or how many elements are in it) if (!name.isEmpty()) System.out.println(“Size=“+ name.size()); else System.out.println(“The stack is empty”);