Data Structures and Algorithms C++ Implementation



Similar documents
Stacks. Linear data structures

DATA STRUCTURE - STACK

Linked Lists Linked Lists, Queues, and Stacks

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Data Structures Using C++ 2E. Chapter 5 Linked Lists

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

CS 2412 Data Structures. Chapter 2 Stacks and recursion

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Chapter Objectives. Chapter 7. Stacks. Various Types of Stacks LIFO. Empty Stack. Stacks

St S a t ck a ck nd Qu Q eue 1

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

DATA STRUCTURE - QUEUE

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

CSE 211: Data Structures Lecture Notes VII

Software Engineering Techniques

Data Structures and Algorithms Stacks and Queues

Common Data Structures

Module 2 Stacks and Queues: Abstract Data Types

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Basic Data Structures and Algorithms

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Analysis of a Search Algorithm

Tail call elimination. Michel Schinz

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

7.1 Our Current Model

Chapter 3: Restricted Structures Page 1

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Universidad Carlos III de Madrid

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

DATA STRUCTURES USING C

Sequential Data Structures

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

GSM. Global System for Mobile Communications, Security in mobile phones. System used all over the world. Sikkerhed04, Aften Trusler

Practical Session 4 Java Collections

Data Structure with C

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Data Structures and Algorithms Lists

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

STACKS,QUEUES, AND LINKED LISTS

Parallelization: Binary Tree Traversal

Stacks. Data Structures and Data Types. Collections

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Introduction to Data Structures

Data Structures and Data Manipulation

Data Structures and Algorithms V Otávio Braga

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Data Structures and Algorithms

Introduction to Data Structures and Algorithms

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Lecture 2: Data Structures Steven Skiena. skiena

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Cpt S 223. School of EECS, WSU

Algorithms and Data Structures

Lecture Notes on Stacks & Queues

Application of Stacks: Postfix Expressions Calculator (cont d.)

10CS35: Data Structures Using C

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Adapter, Bridge, and Façade

Comp151. Definitions & Declarations

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Binary Search Trees (BST)

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Data Structures Using C++

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

G. H. RAISONI COLLEGE OF ENGG NAGPUR-16 Session DEPARTMENT CSE Semester IV SUBJECT DSPD

Introduction to Stacks

Data Structures and Algorithms(5)

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

PES Institute of Technology-BSC QUESTION BANK

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Abstract Data Types. Chapter 2

Software Vulnerabilities

Data Structures. Level 6 C Module Descriptor

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Code Refactoring and Defects

D06 PROGRAMMING with JAVA

Computer Programming using C

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Tutorial on C Language Programming

Lecture 12 Doubly Linked Lists (with Recursion)

Transcription:

BK TP.HCM Ho Chi Minh City University of Technology Faculty of Computer Science and Engineering BK TP.HCM Data Structures and Algorithms C++ Implementation Huỳnh Tấn Đạt Email: htdat@cse.hcmut.edu.vn Home Page: http://www.cse.hcmut.edu.vn/~htdat/

Basic stack operations Linked-list implementation Stack applications Array implementation Stacks Slide 2

Linear List Concepts General list: no restrictions on where can be inserted/deleted, and on which operations can be used on the list Restricted list: can be inserted/deleted and operations are performed only at the ends of the list Slide 3

Stack All insertions and deletions are restricted to one end called the top Last-In First-Out (LIFO) structure Slide 4

Basic Stack Operations Data Push Top Top Stack Stack Slide 5

Basic Stack Operations Overflow Data Top Stack Slide 6

Basic Stack Operations Pop Data Top Top Stack Stack Slide 7

Basic Stack Operations Underflow Top Stack Slide 8

Basic Stack Operations Stack Top Data Top Top Stack Stack Slide 9

Linked-List Implementation Top Stack structure top 5 Conceptual Physical Slide 10

Linked-List Implementation Stack structure count top stack count <integer> top <node pointer> end stack Stack node structure node <Type> <node pointer> end node Slide 11

Linked-List Implementation template <class ItemType> struct Node { ItemType ; Node<ItemType> *; }; template <class List_ItemType> class Stack { public: Stack(); ~Stack(); Slide 12

Linked-List Implementation void Push(List_ItemType In); int Pop(List_ItemType &Out); int GetStackTop(List_ItemType &Out); void Clear(); int IsEmpty(); int GetSize(); Stack<List_ItemType>* Clone(); void Print2Console(); private: Node<List_ItemType>* top; int count; }; Slide 13

Create Stack Before After?? count top 0 count top (no stack) (empty stack) Slide 14

Create Stack Algorithm createstack (ref stack <meta>) Initializes meta for a stack Pre stack is structure for meta Post meta initialized 1 stack.count = 0 2 stack.top = null 3 return End createstack Slide 15

Linked-List Implementation template <class List_ItemType> Stack<List_ItemType>::Stack(){ this->top = NULL; this->count = 0; } template <class List_ItemType> Stack<List_ItemType>::~Stack(){ this->clear(); } Slide 16

Push Stack Before After pnew red pnew red stack stack 2 blue 3 blue count top count top green green Slide 17

Push Stack Algorithm pushstack (ref stack <meta>, val <Type>) Inserts (pushes) one item into the stack Pre stack is a meta structure to a valid stack contains to be pushed into stack Post have been pushed in stack Return true if successful; false if memory overflow Slide 18

Push Stack 1 if (stack full) 1 success = false 2 else 1 allocate (pnew) 2 pnew -> = 3 pnew -> = stack.top 4 stack.top = pnew 5 stack.count = stack.count + 1 6 success = true 3 return success End pushstack Before pnew red stack 2 blue count top green Slide 19

Push Stack template <class List_ItemType> void Stack<List_ItemType>::Push (List_ItemType value){ Node<List_ItemType>* pnew = new Node<List_ItemType>(); pnew-> = value; pnew-> = top; this->top = pnew; this->count++; } Slide 20

Pop Stack Before After dltptr red dltptr recycled stack stack 3 blue 2 blue count top count top green green Slide 21

Pop Stack Algorithm popstack (ref stack <meta>, ref Out <Type>) Pops the item on the top of the stack and returns it to caller Pre stack is a meta structure to a valid stack Out is to receive the popped Post have been returned to caller Return true if successful; false if underflow Slide 22

Pop Stack 1 if (stack empty) 1 success = false Before 2 else 1 dltptr = stack.top 2 Out = stack.top -> dltptr stack red 3 stack.top = stack.top -> 4 stack.count = stack.count - 1 5 recycle (dltptr) 3 count top blue 6 success = true 3 return success End popstack green Slide 23

Pop Stack template <class List_ItemType> int Stack<List_ItemType>::Pop(List_ItemType &Out){ if (count == 0) return 0; Node<List_ItemType>* dltptr = this->top; Out = this->top->; this->top = this->top->; this->count--; delete dltptr; return 1; } Slide 24

Stack Top Algorithm stacktop (val stack <meta>, ref Out <Type>) Retrieves the from the top of the stack without changing the stack Pre Post stack is a meta structure to a valid stack Out is to receive top stack have been returned to caller Return true if successful; false Slide 25

Stack Top 1 if (stack empty) 1 success = false 2 else 1 Out = stack.top -> 2 success = true 3 return success End stacktop Slide 26

Stack Top template <class List_ItemType> int Stack<List_ItemType>::GetStackTop (List_ItemType &Out){ if (count == 0) return 0; Out = this->top->; return 1; } Slide 27

Destroy Stack Algorithm destroystack (ref stack <meta>) Releases all nodes back to memory Pre stack is a meta structure to a valid stack Post stack empty and all nodes recycled 1 if (stack not empty) 1 loop (stack.top not null) 1 temp = stack.top 2 stack.top = stack.top -> 3 recycle (temp) 2 stack.count = 0 3 return End destroystack Slide 28

Destroy Stack template <class List_ItemType> void Stack<List_ItemType>::Clear() { Node<List_ItemType>* temp; while (this->top!= NULL){ temp = this->top; this->top = this->top->; delete temp; } this->count = 0; } Slide 29

Stack Empty template <class List_ItemType> int Stack<List_ItemType>::IsEmpty() { return (count == 0); } template <class List_ItemType> int Stack<List_ItemType>::GetSize() { return count; } Slide 30

Print a stack template <class List_ItemType> void Stack<List_ItemType>::Print2Console() { } Node<List_ItemType>* p; p = this->top; while (p!= NULL){ } printf("%d\t", p->); p = p->; printf("\n"); Slide 31

Using Stacks int main(int argc, char* argv[]){ Stack<int> *mystack = new Stack<int>(); int val; mystack->push(7); mystack->push(9); mystack->push(10); mystack->push(8); mystack->print2console(); mystack->pop(val); mystack->print2console(); delete mystack; return 0; } Slide 32

Exercises template <class List_ItemType> Stack<List_ItemType>* Stack<List_ItemType>::Clone() { //... } Slide 33