65
Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Embed Size (px)

Citation preview

Page 1: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Chapter 19 C++ Data Structures

By C. Shing

ITEC Dept

Radford University

Page 2: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 2

Objectives Understand how to use template structures

for linked list, stack, queue, binary search tree Understand how to use STL

(Standard Template Library)

for vector, list, stack, queue and tree Understand how to use algorithms

Page 3: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 3

Data Structure Using Template Class A Node class:

Member data: Info field Next field: link to another Node

Member functions: Accessors:

getInfo getNext

Mutators: setInfo setNext

Page 4: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 4

Data Structure Using Template Class (Cont.)

A Node class Implementation:template <class T>class Node {public:

Node(): next(NULL){}Node(const T &i, Node<T> *n): info(i), next(n) {}T getInfo() const {return info;}Node<T>* getNext() const {return next;}void setInfo(T i) {info=i;}void setNext(Node<T> *n) {next=n;}~Node() {}

Page 5: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 5

Data Structure Using Template Class (Cont.)

A Node class Implementation: (Cont.)

private:

T info;

Node<T> *next;

};

Page 6: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 6

Iterator Iterator is a class that allows you to

cycle through any element in a data structure I contains

Member data Pointer to the current node: current

Member functions ++ (prefix and postfix) == != *

Page 7: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 7

Iterator (Cont.) Iterator Implementation:class Iterator {public:

Iterator(): current(NULL) {}Iterator(Node<T> * nd): current(nd) {}Iterator operator++(){current=current->getNext(); return *this;}; // prefix form// postfix formIterator operator++(int){Iterator old=current; current=current->getNext(); return old;}bool operator == (const Iterator &rs) const {return current==rs.current;}bool operator != (const Iterator &rs) const {return current!=rs.current;}const T operator *() const {return current->getInfo();}~Iterator() {}

Page 8: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 8

Iterator (Cont.) Iterator Implementation: (Cont.)

private:

Node<T> *current;

};

Page 9: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 9

Linked List Linked nodes using Iterator:template <class T>class List {public:

List(): head(NULL), count(0) {}void insertInfo(const T &i);void deleteInfo(const T &i);T getCount() const {return count;}Iterator<T> begin() {return Iterator<T>(head);}Iterator<T> end() {return Iterator<T>();}~List() {}

Page 10: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 10

Linked List (Cont.) Linked nodes using Iterator: (Cont.)

template <class T>

private:

Node<T> *head;

int count;

};

Page 11: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 11

Linked List (Cont.) Linked nodes using Iterator: (Cont.)

Operations: Insert item:

template <class T>

void List<T>::insertInfo(const T &i) Delete item:

template <class T>

void List<T>::deleteInfo(const T &i)

Page 12: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 12

Linked List (Cont.) Example:

list.cpp

Page 13: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 13

Data Structure - stack Stack: insert (push) and delete (pop)

at the same place (top).

Page 14: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 14

Stack Implementation Write a program that implements a stack of

integers using a linked list.

template <class T>class Stack {public:

Stack(): top(NULL) {}void push(const T &i);T pop();Iterator<T> begin() {return Iterator<T>(top);}Iterator<T> end() {return Iterator<T>();}bool isEmpty() const;~Stack();

Page 15: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 15

Stack Implementation (Cont.) Write a program that implements a stack of

integers using a linked list. (Cont.)

private:

Node<T> *top;

};

Page 16: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 16

Stack Implementation (Cont.) Stack operations:

Push Pop isEmpty

Page 17: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 17

Stack Implementation (Cont.) Stack operations: (Cont.)template <class T>void Stack<T>::push(const T &i){

Node<T> *newnode=new Node<T>;newnode->setInfo(i);newnode->setNext(top);top=newnode;

}

Page 18: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 18

Stack Implementation (Cont.) Stack operations: (Cont.)template <class T>T Stack<T>::pop(){

if (isEmpty()) {cout <<" Error: Stack is empty, cannot pop out.\n";exit(1);}

T item=top->getInfo();Node<T> *deleted=top;top=top->getNext();delete deleted;return item;

}

Page 19: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 19

Stack Implementation (Cont.) Stack operations: (Cont.)

template <class T>

bool Stack<T>::isEmpty() const

{

return top==NULL;

}

Page 20: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 20

Stack Implementation (Cont.) Example:

stack.cpp

Page 21: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 21

Data Structure - Queue Insert (enqueue) at the tail and

Delete (dequeue) at the head.

Page 22: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 22

Example - Queue Write a program that implements a queue of

integers using a linked list.template <class T>class Queue {public:

Queue(): head(NULL), tail(NULL) {}void enqueue(const T &i);T dequeue();Iterator<T> begin() {return Iterator<T>(head);}Iterator<T> end() {return Iterator<T>();}bool isEmpty() const;~Queue();

Page 23: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 23

Example – Queue (Cont.) Write a program that implements a queue of

integers using a linked list. (Cont.)

private:

Node<T> *head, *tail;

};

Page 24: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 24

Example – Queue (Cont.) Queue operations:

enqueue dequeue isEmpty

Page 25: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 25

Example – Queue (Cont.) Queue operations Implementation:template <class T>void Queue<T>::enqueue(const T &i){

if (isEmpty()) head=tail=new Node<T>(i, NULL);else{

tail->setNext(new Node<T> (i, NULL));tail=tail->getNext();

}}

Page 26: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 26

Example – Queue (Cont.) Queue operations Implementation: (Cont.)template <class T>T Queue<T>::dequeue(){

if (isEmpty()) {cout <<" Error: Stack is empty, cannot pop out.\n";

exit(1);}T item=head->getInfo();Node<T> *deleted=head;head=head->getNext();delete deleted;return item;

}

Page 27: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 27

Example – Queue (Cont.) Queue operations Implementation:

template <class T>

bool Queue<T>::isEmpty() const

{

return (head==NULL) && (tail==NULL);

}

Page 28: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 28

Queue Implementation Example:

queue.cpp

Page 29: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 29

Data Structure – Binary Tree Each (parent) node has at most 2 children Binary search tree:

right child node value > parent node value > left

child value

Page 30: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 30

Example – Binary search tree Write a program that implements a

binary search tree of integers using a linked list.

Page 31: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 31

Example – Binary search tree (Cont.) First define a TreeNode:

Member data: Info field Left field: link to left TreeNode Right field: link to right TreeNode

Member functions: Accessors:

getInfo getLeft getRight

Mutators: setInfo setLeft setRight

Page 32: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 32

Example – Binary search tree (Cont.) TreeNode implementation:template <class T>class TreeNode {public:

TreeNode(): left(NULL), right(NULL){}TreeNode(const T &i, TreeNode<T> *l,TreeNode<T> *r):

info(i), left(l), right(r) {}T getInfo() const {return info;}TreeNode<T>* &getLeft() {return left;}TreeNode<T>* &getRight() {return right;}void setInfo(T i) {info=i;}void setLeft(TreeNode<T> *l) {left=l;}void setRight(TreeNode<T> *r) {right=r;}~TreeNode() {}

Page 33: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 33

Example – Binary search tree (Cont.)

TreeNode implementation: (Cont.)

private:

T info;

TreeNode<T> *left;

TreeNode<T> *right;

};

Page 34: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 34

Example – Binary search tree (Cont.)

Binary Search Tree operations: Insert Delete isEmpty Inorder traversal

Page 35: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 35

Example – Binary search tree (Cont.) Binary Search Tree specification:template <class T>class BST {public:

BST(): root(NULL) {}void insert (T i);void deleteInfo (T i);void inOrder() const;bool isEmpty() const {return root==NULL;}~BST();

Page 36: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 36

Example – Binary search tree (Cont.) Binary Search Tree specification: (Cont.)private:

void insert(TreeNode<T>*&r, T i);void deleteInfo(TreeNode<T>*&r, T i);void deleteInfo(TreeNode<T>*&r);void getPredecessor(TreeNode<T>*&r, T &i);void deleteTree(TreeNode<T> *r);void inOrder(TreeNode<T> *r) const;TreeNode<T> *root;

};

Page 37: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 37

Example – Binary search tree (Cont.)

Binary Search Tree Implementation:

template <class T>

void BST<T>::insert(T i)

{

insert(root,i);

}

Page 38: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 38

Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.)template <class T>void BST<T>::insert(TreeNode<T>* &r, T i){

if (r==NULL) r=new TreeNode<T>(i, NULL, NULL);else {

if (i> r->getInfo()) {insert(r->getRight(),i); }

else {insert(r->getLeft(),i);}

}}

Page 39: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 39

Example – Binary search tree (Cont.)

Binary Search Tree Implementation: (Cont.)

template <class T>

void BST<T>::deleteInfo(T i)

{

deleteInfo (root,i);

}

Page 40: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 40

Example – Binary search tree (Cont.) Binary Search Tree Implementation: (cont.)template <class T>void BST<T>::deleteInfo (TreeNode<T>* &r, T i){

if (i > r->getInfo()) {deleteInfo (r->getRight(),i);

}else if (i < r->getInfo()) {

deleteInfo (r->getLeft(),i);}else

deleteInfo(r);}

Page 41: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 41

Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.)template <class T>void BST<T>::deleteInfo(TreeNode<T>*&r){

T item;TreeNode<T> *temp=r;if (r->getLeft() == NULL) {

r=r->getRight();delete temp;

}else if (r->getRight() == NULL) {

r=r->getLeft();delete temp;

} else {

getPredecessor(r->getLeft(), item);r->setInfo(item);deleteInfo (r->getLeft(), item);

}}

Page 42: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 42

Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.)template <class T>void BST<T>::getPredecessor

(TreeNode<T>*&r, T &item){

while (r->getRight() != NULL)r=r->getRight();item=r->getInfo();

}

Page 43: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 43

Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.)template <class T>void BST<T>::deleteTree(TreeNode<T> *r){

if (r != NULL) {

deleteTree(r->getRight());deleteTree(r->getLeft());delete r;

}}

Page 44: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 44

Example – Binary search tree (Cont.)

Binary Search Tree Implementation: (Cont.)

template <class T>

void BST<T>::inOrder() const

{

inOrder(root);

}

Page 45: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 45

Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.)template <class T>void BST<T>::inOrder(TreeNode<T> *r) const{

if (r != NULL){

inOrder(r->getLeft());cout<< "item "<<r->getInfo()<<'\n';inOrder(r->getRight());

}}

Page 46: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 46

Example – Binary search tree (Cont.)

Binary Search Tree Implementation: (Cont.)

template <class T>

BST<T>::~BST()

{

deleteTree(root);

}

Page 47: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 47

Binary search tree Implementation Example:

tree.cpp

Page 48: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 48

Standard Template Library STL: library that contains useful template

data structures Container adapter: implemented on top of

deque template class stack queque

Container: use iterator to traverse items Sequential container:

vector list

Associative container: set map

Page 49: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 49

STL - stack Put #include <stack> Has member functions:

size empty top push pop

Page 50: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 50

STL – stack (Cont.) Example:

stack.cpp

Page 51: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 51

STL - queue Put #include < queue > Has member functions:

size empty front back push pop

Page 52: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 52

STL – queue (Cont.) Example:

queue.cpp

Page 53: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 53

STL – sequential container Has member functions:

size begin: for iterator end: for iterator rbegin: for reverse iterator rend: for reverse iterator push_front push_back front insert clear erase

Page 54: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 54

STL - vector Put #include < vector > Self-grown array

Page 55: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 55

STL – vector (Cont.) Example:

vector.cpp

vector_data.txt

vector_iterator.cpp

Page 56: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 56

STL – list Put #include < list> Example:

list_iterator.cpp

Page 57: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 57

STL – deque Put #include < deque > Example:

deque_iterator.cpp

Page 58: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 58

STL – associative container Associate a key with a value Has member functions:

size empty find insert erase

Page 59: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 59

STL - set Put #include < set > Item can be duplicated

Page 60: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 60

STL – set (Cont.) Example:

set_iterator.cpp

set_data.txt

Page 61: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 61

Generic Algorithm Basic template functions in STL Put #include < algorithm >

Page 62: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 62

Generic Algorithm (Cont.) Sorting algorithm: has function

binary_search Set algorithm: has template functions

include set_union set_intersection set_difference set_symmetric_difference

Page 63: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 63

Sort Algorithm Example:

vectorsort_iterator.cpp

vector_data.txt

Page 64: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 64

Set Algorithm Example:

setop_iterator.cpp

setop_data.txt

Page 65: Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 65

References Deitel & Deitel: C How to Program, 4th ed.,

Chapter 21, Prentice Hall Deitel & Deitel: C++ How to Program, 4th ed.,

Chapter 21, Prentice Hall