CC 215 Data Structures Stack ADT

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
The unorganized person’s data structure
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
CMPT 225 Stacks.
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 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
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 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
1 Stacks (Walls & Mirrors - Chapter 6). 2 Overview The ADT Stack Array Implementation of a Stack Linked-List Implementation of a Stack Application Domain:
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
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.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Lecture 6: Stacks 1. 2 The Abstract Data Type Specifications of an abstract data type for a particular problem –Can emerge during the design of the problem’s.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
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.
6/12/20161 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Stacks The Stack ADT stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) scheme. The last item placed.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Abstract Data Types (ADTs)
Chapter 7 Queues.
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
Stacks and Queues Chapter 4.
CC 215 Data Structures Queue ADT
Data Abstraction: The Walls
Abstract Data Types (ADTs)
Stacks.
Stack and Queue APURBO DATTA.
Data Structures – Week #3
Chapter 4 Linked Lists
Chapter 5 ADTs Stack and Queue.
Pointers and Linked Lists
Linked Lists Chapter 4.
Chapter 6 Stacks.
Queues.
C++ Plus Data Structures
Chapter 4 Linked Lists.
Chapter 4 Linked Lists.
Data Abstraction: The Walls
Stacks CS-240 Dick Steflik.
Abstract Data Types (ADTs)
Data Structures – Week #3
Data Structures and Algorithms
Chapters 6 and 7 Stacks.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

CC 215 Data Structures Stack ADT AASTMT Engineering and Technology College CC 215 Data Structures Stack ADT Lecture 4 Dr. Manal Helal - Fall 2014

Overview Reading Learning Objectives Section 3.6 The Stack ADT: The Stack Model, Implementation of Stacks, Applications Learning Objectives Define stacks as abstract data type. Demonstrate how to implement stacks as arrays and linked data structures. Explain, with code examples, the algorithms for initializing, pushing, popping, and checking for a full/empty stack.

ADT Stack A stack Last-in, first-out (LIFO) property Analogy The last item placed on the stack will be the first item removed Analogy A stack of dishes in a cafeteria Stack of cafeteria dishes

ADT Stack ADT stack operations Create an empty stack Destroy a stack Determine whether a stack is empty Add a new item Remove the item that was added most recently Retrieve the item that was added most recently A program can use a stack independently of the stack’s implementation

ADT Stack // stack operations: bool isEmpty(); // Determines whether a stack is empty. // Precondition: None. // Postcondition: Returns true if the // stack is empty; otherwise returns false.

ADT Stack bool push(StackItemType newItem); // Adds an item to the top of a stack. // Precondition: newItem is the item to // be added. // Postcondition: If the insertion is // successful, newItem is on the top of // the stack.

ADT Stack bool pop(); // Removes the top of a stack. // Precondition: None. // Postcondition: If the stack is not // empty, the item that was added most // recently is removed. However, if the // stack is empty, deletion is impossible.

ADT Stack bool pop(StackItemType& stackTop); // Retrieves and removes the top of a stack // Precondition: None. // Postcondition: If the stack is not empty, // stackTop contains the item that was added // most recently and the item is removed. // However, if the stack is empty, deletion // is impossible and stackTop is unchanged.

ADT Stack bool getTop(StackItemType& stackTop); // Retrieves the top of a stack. // Precondition: None. // Postcondition: If the stack is not empty, // stackTop contains the item that was added // most recently. However, if the stack is // empty, the operation fails and stackTop // is unchanged. The stack is unchanged.

Checking for Balanced Braces A stack can be used to verify whether a program contains balanced braces An example of balanced braces abc{defg{ijk}{l{mn}}op}qr An example of unbalanced braces abc{def}}{ghij{kl}m

Checking for Balanced Braces Requirements for balanced braces Each time you encounter a “}”, it matches an already encountered “{” When you reach the end of the string, you have matched each “{”

Checking for Balanced Braces Traces of the algorithm that checks for balanced braces

Checking for Balanced Braces aStack.createStack() balancedSoFar = true i = 0 while ( balancedSoFar and i < length of aString ){ ch = character at position i in aString ++i if ( ch is '{' ) // push an open brace aStack.push( '{' ) else if ( ch is '}' ) // close brace if ( !aStack.isEmpty() ) aStack.pop() // pop a matching open brace else // no matching open brace balancedSoFar = false // ignore all characters other than braces } if ( balancedSoFar and aStack.isEmpty() ) aString has balanced braces else aString does not have balanced braces

Implementations of the ADT Stack The ADT stack can be implemented using An array A linked list The ADT list

Implementations of the ADT Stack Implementation of the ADT stack that use a) an array; b) a linked list

An Array-Based Implementation of the ADT Stack Private data fields An array of items of type StackItemType The index top Compiler-generated destructor and copy constructor An array-based implementation

An Array-Based Implementation of the ADT Stack const int MAX_STACK = maximum-size-of-stack; typedef desired-type-of-stack-item StackItemType; class Stack{ public: Stack(); bool isEmpty(); bool push(StackItemType newItem); bool pop(); bool pop(StackItemType& stackTop); bool getTop(StackItemType& stackTop); private: // array of stack items StackItemType items[MAX_STACK]; // index to top of stack int top; };

An Array-Based Implementation of the ADT Stack // default constructor Stack() { top = -1; }

An Array-Based Implementation of the ADT Stack bool isEmpty() { return top < 0; }

An Array-Based Implementation of the ADT Stack bool push(StackItemType newItem){ // if stack has no more room for // another item if (top >= MAX_STACK-1) return false; else{ ++top; items[top] = newItem; return true; }

An Array-Based Implementation of the ADT Stack bool pop(){ if (isEmpty()) return false; // stack is not empty; pop top else { --top; return true; }

An Array-Based Implementation of the ADT Stack bool pop(StackItemType& stackTop){ if (isEmpty()) return false; // stack is not empty; retrieve top else { stackTop = items[top]; --top; return true; }

An Array-Based Implementation of the ADT Stack bool getTop(StackItemType& stackTop) const{ if (isEmpty()) return false; // stack is not empty; retrieve top else { stackTop = items[top]; return true; }

A Pointer-Based Implementation of the ADT Stack Required when the stack needs to grow and shrink dynamically top is a reference to the head of a linked list of items A copy constructor and destructor must be supplied A pointer-based implementation

A Pointer-Based Implementation of the ADT Stack typedef desired-type-of-stack-item StackItemType; class Stack{ public: Stack(); Stack(const Stack& aStack); ~Stack(); bool isEmpty() const; bool push(StackItemType newItem); bool pop(); bool pop(StackItemType& stackTop); bool getTop(StackItemType& stackTop) const; private: struct StackNode { // a node on the stack StackItemType item; // a data item on the stack StackNode *next; // pointer to next node }; StackNode *topPtr; // pointer to first node in the stack

A Pointer-Based Implementation of the ADT Stack // default constructor Stack() { topPtr = NULL; }

A Pointer-Based Implementation of the ADT Stack // copy constructor Stack(const Stack& aStack){ if (aStack.topPtr == NULL) topPtr = NULL; // original stack is empty else { // copy first node topPtr = new StackNode; topPtr->item = aStack.topPtr->item; // copy rest of stack StackNode *newPtr = topPtr; for (StackNode *origPtr = aStack.topPtr->next; origPtr != NULL; origPtr = origPtr->next){ newPtr->next = new StackNode; newPtr = newPtr->next; newPtr->item = origPtr->item; } newPtr->next = NULL;

A Pointer-Based Implementation of the ADT Stack // destructor ~Stack(){ // pop until stack is empty while (!isEmpty()) pop(); }

A Pointer-Based Implementation of the ADT Stack bool isEmpty() { return topPtr == NULL; }

A Pointer-Based Implementation of the ADT Stack bool push(StackItemType newItem) { // create a new node StackNode *newPtr = new StackNode; // set data portion of new node newPtr->item = newItem; // insert the new node newPtr->next = topPtr; topPtr = newPtr; return true; }

A Pointer-Based Implementation of the ADT Stack bool pop() { if (isEmpty()) return false; // stack is not empty; delete top else{ StackNode *temp = topPtr; topPtr = topPtr->next; // return deleted node to system temp->next = NULL; // safeguard delete temp; return true; }

A Pointer-Based Implementation of the ADT Stack bool pop(StackItemType& stackTop) { if (isEmpty()) return false; // not empty; retrieve and delete top else{ stackTop = topPtr->item; StackNode *temp = topPtr; topPtr = topPtr->next; // return deleted node to system temp->next = NULL; // safeguard delete temp; return true; }

A Pointer-Based Implementation of the ADT Stack bool getTop(StackItemType& stackTop) const { if (isEmpty()) return false; // stack is not empty; retrieve top else { stackTop = topPtr->item; return true; }