Data Structures (INE2011)

Similar documents
CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008

Stacks and Queues. Introduction to Data Structures Kyuseok Shim SoEECS, SNU.

Template Functions and Class Templates

COMP6771 Advanced C++ Programming

Stacks and Their Applications

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Chapter 18: Stacks And Queues

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

STACKS AND QUEUES. Problem Solving with Computers-II

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Chapter 18: Stacks And Queues

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Stack and Queue. Stack:

Stacks and their Applications

csci 210: Data Structures Stacks and Queues

Data Structures and Algorithms(2)

Data Structures And Algorithms

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

CMPT 225. Lecture 9 Stack

CS24 Week 4 Lecture 2

September 19,

C++ Exception Handling 1

Name CPTR246 Spring '17 (100 total points) Exam 3

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

CMSC 132: Object-Oriented Programming II. Stack and Queue

ADTs Stack and Queue. Outline


Exceptions. CandC++ 7. Exceptions Templates. Throwing exceptions. Conveying information

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 7. Exceptions Templates 2/1. Throwing exceptions 14 }

Unit 4: Stacks and Queues

An Introduction to Queues With Examples in C++

ITI Introduction to Computing II

C and C++ 7. Exceptions Templates. Alan Mycroft

Chapter 5. ADTs Stack and Queue

.:: UNIT 4 ::. STACK AND QUEUE

03/29/2004. A dispenser has three essential features: adding, removing, accessing. Dispensers

Propedéutico de Programación

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

4.3 Stacks, Queues, and Linked Lists

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS302 - Data Structures using C++

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Chapter 3. Stack & Queue 1

4/27/2014. Templates II. Warmup Write the templated Swap function. Class Templates CMSC 202

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Design Patterns in C++

1 P age DS & OOPS / UNIT II

Data Structure. Recitation VII

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Object Oriented Software Design - II

Programming Abstractions

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

4.1 Ordered Lists Revisited

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

ADT Sorted List Operations

CMSC 341 Lecture 7. Announcements. Proj 2 up Project Preview tonight and tomorrow

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Data Structures G5029

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Lecture Data Structure Stack

LECTURE 11 TREE TRAVERSALS

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Object Oriented Programming COP3330 / CGS5409

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

ADTs: Stacks and Queues

Chapter 12 - Templates

CSC 222: Computer Programming II. Spring 2004

CS350: Data Structures Stacks

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Basic Data Structures

[CS302-Data Structures] Homework 2: Stacks

Lecture 3: Stacks & Queues

CS197c: Programming in C++

Computer Systems Lecture 9

3.1 Arrays Representation and basic operations

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

C++ Templates. David Camp

BBM 201 DATA STRUCTURES

Queue with Array Implementation

Basic Data Structures 1 / 24

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Data Structures. Jia-Liang Lu. Slides based on the course notes of C.A. Shaffer

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Data Structures and Algorithms

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Announcements Queues. Queues

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Abstract Data Types 1

Programming II Test 3 Revision 1 CS 2613 Autumn 2003

CMSC 341. Deques, Stacks and Queues 9/22/04 1

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Data Structures (INE2011)

Transcription:

Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam Lecture 4 1

Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push A0120C0C 00000194 00000001 2

Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push 2. Push 70C00A14 A0120C0C 00000194 3

Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push 2. Push 70C00A14 3. Push 00000005 A0120C0C 70C00A14 4

Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push 2. Push 70C00A14 3. Push 00000005 4. Pop 00000005 70C00A14 5

Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push 2. Push 70C00A14 3. Push 00000005 4. Pop 00000005 70C00A14 6

Stacks and Local Variables in A Function #include <iostream> int add2 (int i32val1, int i32val2) { int i32out = 0; i32out = i32val1 + i32val2; return i32out; } i32out 00000005 00000005 00000005 70C00A14 00000010 7

Stacks and A Function Memory map #include <iostream> int add2 (int i32val1, int i32val2) { int i32out = 0; i32out = i32val1 + i32val2; return i32out; } main( ) add2( ) 00000005 int main ( ) { int i32result = 0; 70C00A14 i32result = add2(3, 5); std::cout << The result is << i32result; } 8

Class Stack template<class T> class Stack { public: Stack(int stackcapacity = 10); ~Stack() {delete [] stack;} bool IsEmpty() const; T& Top() const; void Push(const T& item); void Pop(); private: T *stack; // array for stack elements int ; // position of element int capacity; // capacity of stack array }; template<class T> Stack<T>::Stack(int stackcapacity) :capacity(stackcapacity) { if (capacity < 1) throw Stack capacity must be > 0 ; stack = new T[capacity]; = -1; } template<class T> inline bool Stack<T>::IsEmpty() const {return == -1} template<class T> inline T& Stack<T>::Top() const { if (IsEmpty()) throw Stack is empty ; return stack[]; } 9

Class Stack template<class T> void Stack<T>::Push(const T& x) {// Add x to the stack. if ( == capacity - 1) {ChangeSize1D(stack, capacity, 2*capacity); capacity *= 2; } // add at stack stack[++] = x; } void Stack<T>::Pop() { if (IsEmpty()) throw Stack is empty. Cannot delete. ; stack[--].~t(); // destructor for T } 0 1 2 3 4 a b c d 0 1 2 3 4 a b c d e 0 1 2 3 4 a b c d e push pop 10

Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add A0120C0C 00000194 00000001 front, rear 11

Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add A0120C0C 00000194 2. Add 70C00A14 rear front 12

Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add 2. Add 70C00A14 3. Add 00000005 A0120C0C 70C00A14 rear front 13

Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add 2. Add 70C00A14 3. Add 00000005 4. Delete 00000005 70C00A14 rear front 14

Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add 2. Add 70C00A14 3. Add 00000005 4. Delete 00000005 70C00A14 rear front 15

Linear Queues Linear queues 0 1 2 3 4 a b c d Newest element Oldest element front push rear pop 0 1 2 3 4 5 6 7 8 9 a b c d e 0 1 2 3 4 5 a b c d e front rear front rear 16

Circular Queues Circular queues [0] [1] [2] [3] [4] [5] [0] [1] [2] [3] [4] [5] a b c d f c d e front rear rear front [2] [3] [2] [3] b c c [1] a [0] d [5] [4] [1] [0] f d e [5] [4] 17

Circular Queues Empty queue Full queue [0] [1] [2] [3] [4] [5] [0] [1] [2] [3] [4] f a b c d [5] e front = rear front = rear [2] [3] [2] [3] b c [1] [0] [5] [4] [1] a [0] f d e [5] [4] 18