CSCE 3110 Data Structures & Algorithm Analysis

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
A stack is a data linear data structure in which addition of new element or deletion of an existing element always takes place at the same end. This.
Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element.
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 CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
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.
Data Structure Dr. Mohamed Khafagy. Stacks Stack: what is it? ADT Applications Implementation(s)
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
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.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
 STACK STACK  STACK OPERATIONS STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  USES OF STACK USES OF STACK  THE TOWER.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
STACK Data Structure
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Stacks
Stacks II David Lillis School of Computer Science and Informatics
Stacks.
Lists, Stacks and Queues in C
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Stacks and Queues Chapter 4.
Data Structures and Algorithms
Abstract Data Types and Stacks
Stacks.
Hassan Khosravi / Geoffrey Tien
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
Stacks.
CSCE 3100 Data Structures and Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
STACKS.
Applications of Recursion
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
CSCI2100 Data Structures Tutorial 7
Stacks and Queues The Stack abstract data type
CSCE 3110 Data Structures & Algorithm Analysis
Stacks.
Programming Linked Lists.
CSCE 3110 Data Structures and Algorithm Analysis
Data Structures and Algorithms
Stacks: Implemented using Linked Lists
Abstract Data Type Abstract Data Type as a design tool
Stacks Data structure Elements added, removed from one end only
CSCE 3110 Data Structures and Algorithm Analysis
ECE 103 Engineering Programming Chapter 62 Stack Implementation
Stacks CS-240 Dick Steflik.
CSCE 3110 Data Structures and Algorithm Analysis
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack.
Python: Stacks and Queues (as an Array)
Abstract Data Types Stacks CSCI 240
Presentation transcript:

CSCE 3110 Data Structures & Algorithm Analysis Stacks Reading: Chap.3 Weiss

Stacks Stack: what is it? ADT Applications Implementation(s)

What is a stack? Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT = LIFO It means: the last element inserted is the first one to be removed Example Which is the first element to pick up?

Last In First Out B A C B A D C B A E D C B A top D C B A top top top

Stack Applications Real life Pile of books Plate trays More applications related to computer science Program execution stack (read more from your text) Evaluating expressions

Stack ADT objects: a finite ordered list with zero or more elements. methods: for all stack  Stack, item  element, max_stack_size  positive integer Stack createS(max_stack_size) ::= create an empty stack whose maximum size is max_stack_size Boolean isFull(stack, max_stack_size) ::= if (number of elements in stack == max_stack_size) return TRUE else return FALSE Stack push(stack, item) ::= if (IsFull(stack)) stack_full else insert item into top of stack and return

Stack ADT (cont’d) Boolean isEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUE else return FALSE Element pop(stack) ::= if(IsEmpty(stack)) return else remove and return the item on the top of the stack.

Array-based Stack Implementation Allocate an array of some size (pre-defined) Maximum N elements in stack Bottom stack element stored at element 0 last index in the array is the top Increment top when one element is pushed, decrement after pop

Stack Implementation: CreateS, isEmpty, isFull Stack createS(max_stack_size) ::= #define MAX_STACK_SIZE 100 /* maximum stack size */ typedef struct { int key; /* other fields */ } element; element stack[MAX_STACK_SIZE]; int top = -1; Boolean isEmpty(Stack) ::= top< 0; Boolean isFull(Stack) ::= top >= MAX_STACK_SIZE-1;

Push void push(int *top, element item) { /* add an item to the global stack */ if (*top >= MAX_STACK_SIZE-1) { stack_full( ); return; } stack[++*top] = item; }

Pop element pop(int *top) { /* return the top element from the stack */ if (*top == -1) return stack_empty( ); /* returns and error key */ return stack[(*top)--]; }

List-based Stack Implementation: Push void push(pnode top, element item) { /* add an element to the top of the stack */ pnode temp = (pnode) malloc (sizeof (node)); if (IS_FULL(temp)) { fprintf(stderr, “ The memory is full\n”); exit(1); } temp->item = item; temp->next= top; top= temp; }

Pop element pop(pnode top) { /* delete an element from the stack */ pnode temp = top; element item; if (IS_EMPTY(temp)) { fprintf(stderr, “The stack is empty\n”); exit(1); } item = temp->item; top = temp->next; free(temp); return item; }

Algorithm Analysis push O(?) pop O(?) isEmpty O(?) isFull O(?) What if top is stored at the beginning of the array?

A Legend The Towers of Hanoi In the great temple of Brahma in Benares, on a brass plate under the dome that marks the center of the world, there are 64 disks of pure gold that the priests carry one at a time between these diamond needles according to Brahma's immutable law: No disk may be placed on a smaller disk. In the begging of the world all 64 disks formed the Tower of Brahma on one needle. Now, however, the process of transfer of the tower from one needle to another is in mid course. When the last disk is finally in place, once again forming the Tower of Brahma but on a different needle, then will come the end of the world and all will turn to dust.

The Towers of Hanoi A Stack-based Application GIVEN: three poles a set of discs on the first pole, discs of different sizes, the smallest discs at the top GOAL: move all the discs from the left pole to the right one. CONDITIONS: only one disc may be moved at a time. A disc can be placed either on an empty pole or on top of a larger disc.

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi – Recursive Solution void hanoi (int discs, Stack fromPole, Stack toPole, Stack aux) { Disc d; if( discs >= 1) { hanoi(discs-1, fromPole, aux, toPole); d = fromPole.pop(); toPole.push(d); hanoi(discs-1,aux, toPole, fromPole); }

Is the End of the World Approaching? Problem complexity 2n 64 gold discs Given 1 move a second