Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University."— Presentation transcript:

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

2 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

3 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

4 Slide 4 Data Structure Using Template Class (Cont.) A Node class Implementation: template class Node { public: Node(): next(NULL){} Node(const T &i, Node *n): info(i), next(n) {} T getInfo() const {return info;} Node * getNext() const {return next;} void setInfo(T i) {info=i;} void setNext(Node *n) {next=n;} ~Node() {}

5 Slide 5 Data Structure Using Template Class (Cont.) A Node class Implementation: (Cont.) private: T info; Node *next; };

6 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) == != *

7 Slide 7 Iterator (Cont.) Iterator Implementation: class Iterator { public: Iterator(): current(NULL) {} Iterator(Node * nd): current(nd) {} Iterator operator++(){current=current->getNext(); return *this;}; // prefix form // postfix form Iterator 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() {}

8 Slide 8 Iterator (Cont.) Iterator Implementation: (Cont.) private: Node *current; };

9 Slide 9 Linked List Linked nodes using Iterator: template class List { public: List(): head(NULL), count(0) {} void insertInfo(const T &i); void deleteInfo(const T &i); T getCount() const {return count;} Iterator begin() {return Iterator (head);} Iterator end() {return Iterator ();} ~List() {}

10 Slide 10 Linked List (Cont.) Linked nodes using Iterator: (Cont.) template private: Node *head; int count; };

11 Slide 11 Linked List (Cont.) Linked nodes using Iterator: (Cont.) Operations: Insert item: template void List ::insertInfo(const T &i) Delete item: template void List ::deleteInfo(const T &i)

12 Slide 12 Linked List (Cont.) Example: list.cpp

13 Slide 13 Data Structure - stack Stack: insert (push) and delete (pop) at the same place (top).

14 Slide 14 Stack Implementation Write a program that implements a stack of integers using a linked list. template class Stack { public: Stack(): top(NULL) {} void push(const T &i); T pop(); Iterator begin() {return Iterator (top);} Iterator end() {return Iterator ();} bool isEmpty() const; ~Stack();

15 Slide 15 Stack Implementation (Cont.) Write a program that implements a stack of integers using a linked list. (Cont.) private: Node *top; };

16 Slide 16 Stack Implementation (Cont.) Stack operations: Push Pop isEmpty

17 Slide 17 Stack Implementation (Cont.) Stack operations: (Cont.) template void Stack ::push(const T &i) { Node *newnode=new Node ; newnode->setInfo(i); newnode->setNext(top); top=newnode; }

18 Slide 18 Stack Implementation (Cont.) Stack operations: (Cont.) template T Stack ::pop() { if (isEmpty()) { cout <<" Error: Stack is empty, cannot pop out.\n"; exit(1);} T item=top->getInfo(); Node *deleted=top; top=top->getNext(); delete deleted; return item; }

19 Slide 19 Stack Implementation (Cont.) Stack operations: (Cont.) template bool Stack ::isEmpty() const { return top==NULL; }

20 Slide 20 Stack Implementation (Cont.) Example: stack.cpp

21 Slide 21 Data Structure - Queue Insert (enqueue) at the tail and Delete (dequeue) at the head.

22 Slide 22 Example - Queue Write a program that implements a queue of integers using a linked list. template class Queue { public: Queue(): head(NULL), tail(NULL) {} void enqueue(const T &i); T dequeue(); Iterator begin() {return Iterator (head);} Iterator end() {return Iterator ();} bool isEmpty() const; ~Queue();

23 Slide 23 Example – Queue (Cont.) Write a program that implements a queue of integers using a linked list. (Cont.) private: Node *head, *tail; };

24 Slide 24 Example – Queue (Cont.) Queue operations: enqueue dequeue isEmpty

25 Slide 25 Example – Queue (Cont.) Queue operations Implementation: template void Queue ::enqueue(const T &i) { if (isEmpty()) head=tail=new Node (i, NULL); else { tail->setNext(new Node (i, NULL)); tail=tail->getNext(); }

26 Slide 26 Example – Queue (Cont.) Queue operations Implementation: (Cont.) template T Queue ::dequeue() { if (isEmpty()) { cout <<" Error: Stack is empty, cannot pop out.\n"; exit(1);} T item=head->getInfo(); Node *deleted=head; head=head->getNext(); delete deleted; return item; }

27 Slide 27 Example – Queue (Cont.) Queue operations Implementation: template bool Queue ::isEmpty() const { return (head==NULL) && (tail==NULL); }

28 Slide 28 Queue Implementation Example: queue.cpp

29 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

30 Slide 30 Example – Binary search tree Write a program that implements a binary search tree of integers using a linked list.

31 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

32 Slide 32 Example – Binary search tree (Cont.) TreeNode implementation: template class TreeNode { public: TreeNode(): left(NULL), right(NULL){} TreeNode(const T &i, TreeNode *l,TreeNode *r): info(i), left(l), right(r) {} T getInfo() const {return info;} TreeNode * &getLeft() {return left;} TreeNode * &getRight() {return right;} void setInfo(T i) {info=i;} void setLeft(TreeNode *l) {left=l;} void setRight(TreeNode *r) {right=r;} ~TreeNode() {}

33 Slide 33 Example – Binary search tree (Cont.) TreeNode implementation: (Cont.) private: T info; TreeNode *left; TreeNode *right; };

34 Slide 34 Example – Binary search tree (Cont.) Binary Search Tree operations: Insert Delete isEmpty Inorder traversal

35 Slide 35 Example – Binary search tree (Cont.) Binary Search Tree specification: template class BST { public: BST(): root(NULL) {} void insert (T i); void deleteInfo (T i); void inOrder() const; bool isEmpty() const {return root==NULL;} ~BST();

36 Slide 36 Example – Binary search tree (Cont.) Binary Search Tree specification: (Cont.) private: void insert(TreeNode *&r, T i); void deleteInfo(TreeNode *&r, T i); void deleteInfo(TreeNode *&r); void getPredecessor(TreeNode *&r, T &i); void deleteTree(TreeNode *r); void inOrder(TreeNode *r) const; TreeNode *root; };

37 Slide 37 Example – Binary search tree (Cont.) Binary Search Tree Implementation: template void BST ::insert(T i) { insert(root,i); }

38 Slide 38 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::insert(TreeNode * &r, T i) { if (r==NULL) r=new TreeNode (i, NULL, NULL); else { if (i> r->getInfo()) { insert(r->getRight(),i); } else { insert(r->getLeft(),i);} }

39 Slide 39 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::deleteInfo(T i) { deleteInfo (root,i); }

40 Slide 40 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (cont.) template void BST ::deleteInfo (TreeNode * &r, T i) { if (i > r->getInfo()) { deleteInfo (r->getRight(),i); } else if (i getInfo()) { deleteInfo (r->getLeft(),i); } else deleteInfo(r); }

41 Slide 41 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::deleteInfo(TreeNode *&r) { T item; TreeNode *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); }

42 Slide 42 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::getPredecessor (TreeNode *&r, T &item) { while (r->getRight() != NULL) r=r->getRight(); item=r->getInfo(); }

43 Slide 43 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::deleteTree(TreeNode *r) { if (r != NULL) { deleteTree(r->getRight()); deleteTree(r->getLeft()); delete r; }

44 Slide 44 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::inOrder() const { inOrder(root); }

45 Slide 45 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::inOrder(TreeNode *r) const { if (r != NULL) { inOrder(r->getLeft()); cout getInfo()<<'\n'; inOrder(r->getRight()); }

46 Slide 46 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template BST ::~BST() { deleteTree(root); }

47 Slide 47 Binary search tree Implementation Example: tree.cpp

48 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

49 Slide 49 STL - stack Put #include Has member functions: size empty top push pop

50 Slide 50 STL – stack (Cont.) Example: stack.cpp

51 Slide 51 STL - queue Put #include Has member functions: size empty front back push pop

52 Slide 52 STL – queue (Cont.) Example: queue.cpp

53 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

54 Slide 54 STL - vector Put #include Self-grown array

55 Slide 55 STL – vector (Cont.) Example: vector.cpp vector_data.txt vector_iterator.cpp

56 Slide 56 STL – list Put #include Example: list_iterator.cpp

57 Slide 57 STL – deque Put #include Example: deque_iterator.cpp

58 Slide 58 STL – associative container Associate a key with a value Has member functions: size empty find insert erase

59 Slide 59 STL - set Put #include Item can be duplicated

60 Slide 60 STL – set (Cont.) Example: set_iterator.cpp set_data.txt

61 Slide 61 Generic Algorithm Basic template functions in STL Put #include

62 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

63 Slide 63 Sort Algorithm Example: vectorsort_iterator.cpp vector_data.txt

64 Slide 64 Set Algorithm Example: setop_iterator.cpp setop_data.txt

65 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


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

Similar presentations


Ads by Google