Dr. Bernard Chen Ph.D. University of Central Arkansas

Slides:



Advertisements
Similar presentations
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Advertisements

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 Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Stacks, Queues, and Deques. 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.
Chapter 16 Stack and Queues part2
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 4. Stacks Introduction Consider the 4 problems on pp (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks CHAPTER 7 MAY 21, 2015 Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Exam Review 5/28/15. Software Development (Review)  Make it work…then make it pretty can result in dangerous code  Better to take a unified, consistent.
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 first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
LINKED LISTS.
STACKS & QUEUES for CLASS XII ( C++).
Chapter 4 Stacks
Data Structures Using C, 2e
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Pointers and Linked Lists
Lectures linked lists Chapter 6 of textbook
12 C Data Structures.
Data Structure and Algorithms
Homework 4 questions???.
Chapter 4 Linked Lists.
Stacks and Queues.
Cinda Heeren / Geoffrey Tien
Stack and Queue APURBO DATTA.
Stacks Chapter 4.
Introduction to C++ Recursion
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Dynamically Allocated Memory
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Array Lists Chapter 6 Section 6.1 to 6.3
An Introduction to Pointers
Linked Lists.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Popping Items Off a Stack Lesson xx
Pointers and Linked Lists
Data structures.
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Stacks Data structure Elements added, removed from one end only
Pointers & Dynamic Data Structures
Stacks and Queues.
Stacks CS-240 Dick Steflik.
Chapter 9: Pointers and String
Dynamic allocation (continued)
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Dr. Bernard Chen Ph.D. University of Central Arkansas Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas

Introduction to Stacks Consider the following problems: Problem 1: For a poker game; on any turn, a player may discard a single card from his hand to the top of the pile, or he may retrieve the top card from the discard pile Is there an appropriate data type to model this discard pile???

Introduction to Stacks Is there an appropriate data type to model this parking lot???

Introduction to Stacks An algorithm converting 26 (11010) into base-two representation

Introduction to Stacks Each problem involves a collection of related data items: The basic operations are adding a card to and removing a card from the top of discard pile The basic operation are pushing a car onto the parking lot and removing the last car previously placed on the parking lot We notice that the remainders are generated in reverse order (right to left), therefore, they must be stored in some structure so they can later be displayed in the usual left-to-right order

Introduction to Stacks This type of last-in-first-out processing occurs in a wide variety of applications This last-in-first-out (LIFO) data structure is called a Stack Adding an item to a stack is referred to as pushing that item onto the stack Removing an item from the stack is referred to as popping the stack

Designing and Building a Stack class The basic functions are: Constructor: construct an empty stack Empty(): Examines whether the stack is empty or not Push(): Add a value at the top of the stack Top(): Read the value at the top of the stack Pop(): Remove the value at the top of the stack Display(): Displays all the elements in the stack

Selecting storage structures Two choices Select position 0 as top of the stack Select position 0 as bottom of the stack

Select position 0 as top of the stack Model with an array Let position 0 be top of stack Problem … consider pushing and popping Requires much shifting

Select position 0 as bottom of the stack A better approach is to let position 0 be the bottom of the stack Thus our design will include An array to hold the stack elements An integer to indicate the top of the stack

Implementation of the Operations Constructor: Create an array: (int) array[capacity] Set myTop = -1 Empty(): check if myTop == -1

Implementation of the Operations Push(int x): if array is not FULL (myTop < capacity-1) myTop++ store the value x in array[myTop] else output “out of space”

Implementation of the Operations Top(): If the stack is not empty return the value in array[myTop] else: output “no elements in the stack”

Implementation of the Operations Pop(): If the stack is not empty myTop -= 1 else: output “no elements in the stack”

Further Considerations What if static array initially allocated for stack is too small? Terminate execution? Replace with larger array! Creating a larger array Allocate larger array Use loop to copy elements into new array Delete old array

Linked Stacks Another alternative to allowing stacks to grow as needed Linked list stack needs only one data member Pointer myTop Nodes allocated (but not part of stack class)

Implementing Linked Stack Operations Constructor Simply assign null pointer to myTop Empty Check for myTop == null Top Return data to which myTop points

Implementing Linked Stack Operations Push Insertion at beginning of list node *newptr = new node; newptr->data = element; newptr->next = top; top = newptr;

Implementing Linked Stack Operations Pop Delete first node in the linked list ptr = myTop; myTop = myTop->next; delete ptr; Output Traverse the list for (ptr = myTop; ptr != 0; ptr = ptr->next) out << ptr->data << endl;

C/C++ Standard library The C standard library (also known as libc) is a now-standardized collection of header files and library routines used to implement common operations, such as input/output and string handling For example: #include <iostream>

Vector Vectors contain contiguous elements stored as an Dynamic array. All you have to do is include vector from library #include <vector>

Vector Functions

Designing and Building a Stack class The basic functions are: Constructor: construct an empty stack Empty(): Examines whether the stack is empty or not Push(): Add a value at the top of the stack Top(): Read the value at the top of the stack Pop(): Remove the value at the top of the stack Display(): Displays all the elements in the stack

Functions related to Stack Constructor: vector<int> L; Empty(): L.size() == 0? Push(): L.push_back(value); Top(): L.back(); Pop(): L.pop_back(); Display(): Write your own

A Small Example #include <iostream> #include <vector> using namespace std; int main() { vector<int> L; L.push_back(1); L.push_back(2); L.push_back(3); L.pop_back(); for(int i=L.size() ; i>=0 ; i--) cout << L[i] << " "; cout << L.back(); }

Use of Stack in Function calls Whenever a function begins execution, an activation record is created to store the current environment for that function Current environment includes the values of its parameters, contents of registers, the function’s return value, local variables address of the instruction to which execution is to return when the function finishes execution (If execution is interrupted by a call to another function)

Use of Stack in Function calls Functions may call other functions and thus interrupt their own execution, some data structure must be used to store these activation records so they can be recovered and the system can be reset when a function resumes execution It is the fact that the last function interrupted is the first one reactivated It suggests that a stack can be used to store these activation records A stack is the appropriate structure, and since it is manipulated during execution, it is called the run-time stack

Consider the following program segment int main(){ int a=3; f1(a); cout << endl; } Void f1(int x){ cout << f2(x+1); Int f2(int p){ int q=f3(p/2); return 2*q; Int f3(int n){ return n*n+1;

Run-time Stack OS denotes that when execution of main() is completed, it returns to the operating system

Use of Run-time Stack When a function is called … Copy of activation record pushed onto run-time stack Arguments copied into parameter spaces Control transferred to starting address of body of function