Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
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.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
The N-Queens Problem lab01.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
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.
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.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
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.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Topic 15 Implementing and Using Stacks
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Data Structure Dr. Mohamed Khafagy. Stacks Stack: what is it? ADT Applications Implementation(s)
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
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.
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.
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.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Data Structures: Stacks Queues 1. 2 Stack ADT: What is a Stack? a stack is a varying-length, collection of homogeneous elements Insertion and Deletion.
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.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CHP-3 STACKS.
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.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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 and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
ADT Stack What Is a Stack? Standard Template Library Stack Class
Data Structures Using C++ 2E
Homework 4 questions???.
C++ Plus Data Structures
Stacks Chapter 7 introduces the stack data type.
Algorithms and Data Structures
Stack.
Using a Stack Chapter 6 introduces the stack data type.
Chapter 5 ADTs Stack and Queue.
C++ Plus Data Structures
Using a Stack Chapter 6 introduces the stack data type.
Stacks.
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Stacks Data structure Elements added, removed from one end only
CSCS-200 Data Structure and Algorithms
Presentation transcript:

Stacks CS 3358 – Data Structures

What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). The last element to be added is the first to be removed (LIFO: Last In, First Out).

Stack Specification Definitions: (provided by the user) – MAX_ITEMS: Max number of items that might be on the stack – ItemType: Data type of the items on the stack Operations – makeEmpty – bool isEmpty – bool isFull – push (ItemType newItem) – pop ()

push (ItemType newItem) Function: Adds newItem to the top of the stack. Preconditions: Stack has been initialized and is not full. Postconditions: newItem is at the top of the stack.

ItemType pop () Function: Removes topItem from stack and returns it. Preconditions: Stack has been initialized and is not empty. Postconditions: Top element has been removed from stack and item is a copy of the removed element.

Stack overflow The condition resulting from trying to push an element onto a full stack. if(!stack.isFull()) stack.push(item); Stack underflow The condition resulting from trying to pop an empty stack. if(!stack.isEmpty()) stack.pop();

Example: Convert number bases Use the stack to remember – Let’s convert to binary

Example: Postfix expressions Postfix notation is another way of writing arithmetic expressions. In postfix notation, the operator is written after the two operands. infix: 2+5 postfix: Expressions are evaluated from left to right. Precedence rules and parentheses are never needed!!

Example: postfix expressions (cont.)

Postfix expressions: Algorithm using stacks (cont.)

Postfix expressions: Algorithm using stacks WHILE more input items exist Get an item IF item is an operand stack.Push(item) ELSE stack.Pop(operand2) stack.Pop(operand1) Compute result stack.Push(result) stack.Pop(result)

The N-Queens Problem Suppose you have 8 chess queens......and a chess board

The N-Queens Problem Can the queens be placed on the board so that no two queens are attacking each other ?

The N-Queens Problem Two queens are not allowed in the same row...

The N-Queens Problem Two queens are not allowed in the same row, or in the same column...

The N-Queens Problem Two queens are not allowed in the same row, or in the same column, or along the same diagonal.

The N-Queens Problem The number of queens, and the size of the board can vary. N Queens N rows N columns

The N-Queens Problem We will write a program which tries to find a way to place N queens on an N x N chess board.

How the program works The program uses a stack to keep track of where each queen is placed.

How the program works Each time the program decides to place a queen on the board, the position of the new queen is stored in a record which is placed in the stack. ROW 1, COL 1

How the program works We also have an integer variable to keep track of how many rows have been filled so far. ROW 1, COL 1 1 filled

How the program works Each time we try to place a new queen in the next row, we start by placing the queen in the first column... ROW 1, COL 1 1 filled ROW 2, COL 1

How the program works...if there is a conflict with another queen, then we shift the new queen to the next column. ROW 1, COL 1 1 filled ROW 2, COL 2

How the program works If another conflict occurs, the queen is shifted rightward again. ROW 1, COL 1 1 filled ROW 2, COL 3

How the program works When there are no conflicts, we stop and add one to the value of filled. ROW 1, COL 1 2 filled ROW 2, COL 3

How the program works Let's look at the third row. The first position we try has a conflict... ROW 1, COL 1 2 filled ROW 2, COL 3 ROW 3, COL 1

How the program works...so we shift to column 2. But another conflict arises... ROW 1, COL 1 2 filled ROW 2, COL 3 ROW 3, COL 2

How the program works...and we shift to the third column. Yet another conflict arises... ROW 1, COL 1 2 filled ROW 2, COL 3 ROW 3, COL 3

How the program works...and we shift to column 4. There's still a conflict in column 4, so we try to shift rightward again... ROW 1, COL 1 2 filled ROW 2, COL 3 ROW 3, COL 4

How the program works...but there's nowhere else to go. ROW 1, COL 1 2 filled ROW 2, COL 3 ROW 3, COL 4

How the program works When we run out of room in a row: pop the stack, reduce filled by 1 and continue working on the previous row. ROW 1, COL 1 1 filled ROW 2, COL 3

How the program works Now we continue working on row 2, shifting the queen to the right. ROW 1, COL 1 1 filled ROW 2, COL 4

How the program works This position has no conflicts, so we can increase filled by 1, and move to row 3. ROW 1, COL 1 2 filled ROW 2, COL 4

How the program works In row 3, we start again at the first column. ROW 1, COL 1 2 filled ROW 2, COL 4 ROW 3, COL 1

Pseudocode for N-Queens  Initialize a stack where we can keep track of our decisions.  Place the first queen, pushing its position onto the stack and setting filled to 0.  repeat these steps – if there are no conflicts with the queens... – else if there is a conflict and there is room to shift the current queen rightward... – else if there is a conflict and there is no room to shift the current queen rightward...

Pseudocode for N-Queens  repeat these steps – if there are no conflicts with the queens... Increase filled by 1. If filled is now N, then the algorithm is done. Otherwise, move to the next row and place a queen in the first column.

Pseudocode for N-Queens  repeat these steps – if there are no conflicts with the queens... – else if there is a conflict and there is room to shift the current queen rightward... Move the current queen rightward, adjusting the record on top of the stack to indicate the new position.

Pseudocode for N-Queens  repeat these steps – if there are no conflicts with the queens... – else if there is a conflict and there is room to shift the current queen rightward... – else if there is a conflict and there is no room to shift the current queen rightward... Backtrack! Keep popping the stack, and reducing filled by 1, until you reach a row where the queen can be shifted rightward. Shift this queen right.

Pseudocode for N-Queens  repeat these steps – if there are no conflicts with the queens... – else if there is a conflict and there is room to shift the current queen rightward... – else if there is a conflict and there is no room to shift the current queen rightward... Backtrack! Keep popping the stack, and reducing filled by 1, until you reach a row where the queen can be shifted rightward. Shift this queen right.

 Stacks have many applications.  The application which we have shown is called backtracking.  The key to backtracking: Each choice is recorded in a stack.  When you run out of choices for the current decision, you pop the stack, and continue trying different choices for the previous decision. Summary of N-Queens

Example using templates // Client code StackType myStack; StackType yourStack; StackType anotherStack; myStack.Push(35); yourStack.Push(584.39);

Implementing stacks using templates template class StackType { public: StackType(); void makeEmpty(); bool isEmpty() const; bool isFull() const; void push(const ItemType &); ItemType pop(); private: int top; ItemType items[MAX_ITEMS]; };

Implementing stacks using templates Templates allow the compiler to generate multiple versions of a class type or a function by allowing parameterized types. It is similar to passing a parameter to a function (we pass a data type to a class !!)

Implementing stacks using dynamic array allocation template class StackType { public: StackType(int); ~StackType(); void makeEmpty(); bool isEmpty() const; bool isFull() const; void push(const ItemType &); ItemType pop(); private: int top; int maxStack; ItemType *items; };

Let’s implement a simple stack Using arrays as memory – Stacks grow “up” – Make sure to test copy constructor! Using a linked structure – Always insert at head