CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)

Slides:



Advertisements
Similar presentations
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
Advertisements

Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
Topic 3 Basic Data Structures continued CSE1303 Part A Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A4 – Basic Data Structures – Continued (Queues)
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)
CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.
Stacks.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A8 – Linked Stacks and Linked Queues.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Lists CSE1303 Part A Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
Object Oriented Data Structures
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
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,
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out.
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 STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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,
Infix to postfix conversion Scan the Infix expression left to right If the character x is an operand  Output the character into the Postfix Expression.
EENG212 Algorithms and Data Structures
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C Programming Lecture 11 : Structure. Data Type struct union enum typedef.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Computer Engineering Rabie A. Ramadan Lecture 6.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
STACK Data Structure
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
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.
Stacks Chapter 3 Objectives Upon completion you will be able to
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
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 CSCI 132, Spring 2016 Notes_ 5 Stacks.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
CSCE 3110 Data Structures & Algorithm Analysis
STACKS & QUEUES II Array Based Approach.
Stacks and Queues Chapter 4.
Basic Data Structures – Continued (Queues)
Stack and Queue APURBO DATTA.
CSC 172 DATA STRUCTURES.
CSCE 3110 Data Structures & Algorithm Analysis
Lectures on data structures and C 3ed CS(CSI33) By, CHANDRASHEKAR A.M.
CSCE 3110 Data Structures & Algorithm Analysis
Abstract Data Types and Stacks
ECE 103 Engineering Programming Chapter 62 Stack Implementation
template< class T > class Stack { public:
Stack.
Infix to postfix conversion
Abstract Data Types and Stacks
Presentation transcript:

CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)

2 Basic Data Structures Stacks Queues Lists

3 Overview of Stacks What is a Stack? Operations on a Stack –push, pop –initialize –status: empty, full Implementation of a Stack. Example: Reversing a sequence.

4 A Stack Top Items on the Stack

5 Push Top After Top Before

6 Pop Top Before After Top This comes off the stack

7 Operations Initialize the Stack. Pop an item off the top of the stack. Push an item onto the top of the stack. Is the Stack empty? Is the Stack full? Clear the Stack Determine Stack Size

8 Stack Properties Sequence of items, where insertions and deletions are done at the top. Main operations are pop and push. Last-In First Out (LIFO). Used when calling functions. Used when implementing recursion.

9 Implementation Top index : array (upside-down) int top; float entry[MAXSTACK];

10 #define MAXSTACK 20 struct StackRec { int top; float entry[MAXSTACK]; }; typedef struct StackRec Stack; Stack: entry: top:

11 #ifndef STACKH #define STACKH #include #define MAXSTACK 20 struct StackRec { int top; float entry[MAXSTACK]; }; typedef struct StackRec Stack; void intializeStack(Stack* stackPtr); bool stackEmpty(const Stack* stackPtr); bool stackFull(const Stack* stackPtr); void push(Stack* stackPtr, float item); float pop(Stack* stackPtr); #endif

12 #include #include “stack.h” void initializeStack(Stack* stackPtr) { stackPtr -> top = -1; } top: entry: Stack: addr of Stack stackPtr:

13 bool stackEmpty(const Stack* stackPtr) { if (stackPtr-> top < 0) { return true; } else { return false; } bool stackFull(const Stack* stackPtr) { if (stackPtr -> top >= MAXSTACK-1) { return true; } else { return false; }

14 void push(Stack* stackPtr, float item) { if (stackFull(stackPtr)) { fprintf(stderr, “Stack is full\n”); exit(1); } else { stackPtr-> top++; stackPtr-> entry[stackPtr-> top] = item; }

15 float pop(Stack* stackPtr) { float item; if (stackEmpty(stackPtr)) { fprintf(stderr, “Stack is empty\n”); exit(1); } else { item = stackPtr-> entry[stackPtr-> top]; stackPtr-> top--; } return item; }

16 Take a sequence: Then pop each number off the stack Reversing a Sequence Push onto a stack one number at a time Top

Push onto a stack one number at a time -1.5 Reversing a Sequence Top Then pop each number off the stack

Push onto a stack one number at a time Reversing a Sequence Top Then pop each number off the stack

Reversing a Sequence Top Then pop each number off the stack Push onto a stack one number at a time

Reversing a Sequence Top 6.7 Then pop each number off the stack Push onto a stack one number at a time

Reversing a Sequence Top Then pop each number off the stack Push onto a stack one number at a time

22 Reversing a Sequence Top Then pop each number off the stack Push onto a stack one number at a time

23 module reverse () { initialize the Stack loop{ input nextNumber if (end of input) then {exit loop} if (Stack is not full) then {push nextNumber onto Stack} } loop { if (Stack is empty) then {exit loop} pop nextNumber off the Stack output nextNumber }

24 #include #include “ stack.h ” int main() { Stack theStack; float next; initializeStack(&theStack); printf(“Enter number sequence: ”); while (scanf(“%f”, &next) != EOF) { if (!stackFull(&theStack)) { push(&theStack, next); } while (!stackEmpty(&theStack)) { next = pop(&theStack); printf(“%f”, next); } printf(“\n”); }

25 Revision Stack Operations on a Stack –push, pop –initialize –status: empty, full –others: clear, size Example: Reversing a sequence. Implementation.

26 Next Lecture Queue Main Operations Implementation Revision: Reading Kruse - Chapters – Deitel & Deitel – Chapter 12.5 Langsam - Chapter 2 Standish – Chapters 7.1 – 7.6 Preparation Next lecture: Queues Read 4.1 to 4.2 in Kruse et al. Deitel & Deitel 12.6