Lecture Programming in C++ - Chapter 16: Data structures and recursion. On completion of this chapter students will know how to: Create a linked list, create a stack, create a queue, create a binary tree, identify recursive functions.
Trang 1and Recursion
Trang 5node
Lesson 16.1
Trang 6Use two classes to create linked list
– Node class
Holds only the data All data members public because no function members
– Second used to manipulate nodes
One variable holds address of first object in list Another variable holds current node address Lesson 16.1
Trang 7type member1;
type member2;
… Node* next_node;};
Trang 8Address of node being manipulated Address of
first object
Trang 9Data structure created using linked list modelWith stack can perform only two fundamental operations
Trang 10Two classes create stack
– One class holds only data
Same form as linked list one member must be pointer variable
– Second class used to manipulate nodes
Data only pointer variables used to hold addresses
of nodes Nodes of interest for stack are head and tail Function members initialize, push and pop items Lesson 16.2
Trang 11Lesson 16.2
class Stack{
Trang 12Create empty stack by initializing a head and tail node
Trang 13Lesson 16.3
Trang 14Lesson 16.3
class Queue {
private:
Node* head; Node* tail;
public:
Queue ( );
void insert (int); int remov ( ); };
Trang 15Dequeue "doubleended queue"
– Nodes can be inserted at either end and removed from either end
Trang 17Nodes can also be called vertices or points
Connections between nodes called edges or arcs
Trang 18– Rooted tree
Tree with one node specified to be root Root traditionally shown at top of diagram
– Binary tree
Tree in which no node has more than two children Lesson 16.4
Trang 19type member;
Tree_node* left_child; Tree_node* right_child; };
Trang 20Within function body there is call to
function with identical name and signatureBasic action which is repeated until it
reaches the final iteration of the basic action
Lesson 16.5