Presentation is loading. Please wait.

Presentation is loading. Please wait.

2016-06-051Prof. I. J. Chung Data Structure #4 Professor I. J. Chung.

Similar presentations


Presentation on theme: "2016-06-051Prof. I. J. Chung Data Structure #4 Professor I. J. Chung."— Presentation transcript:

1 2016-06-051Prof. I. J. Chung Data Structure #4 Professor I. J. Chung

2 2016-06-052Prof. I. J. Chung Data Structure Note : 3 steps for implementation of class in C++ (class in C++ ≡ struct in C ≡ record in Pascal) 1)declare methods in the public section of the class definition 2)declare data fields in the private section of the class definition 3)define the implementation of the instructions following the class definition

3 2016-06-053Prof. I. J. Chung Data Structure Example class Date (of C++) {public : … private : int day; int month; int yr;}; struct Date (of C) {int day; int month; int yr; };

4 2016-06-054Prof. I. J. Chung Data Structure Difference of class of C++ and struct of C struct (of C) provides no privacy : data fields of the structure can be inspected and modified freely anywhere. Note : accessing fields (members) of structures (or classes) members (fields) of a structure (or class) are accessed by the methods : methods are expressed by the dot operation ( ㆍ ) and the arrow operation( → ).

5 2016-06-055Prof. I. J. Chung Data Structure Pointer variable and Dynamic variable pointer : memory address of some identifier / variable Note : pointer could be used to the dynamic variables. Dynamic Variable : variables with the following properties D.V is not declared unlike the ordinary variables `D.V can be used in the program body, but D.V. is not included in the declaration part. D.V. is created in the execution of program

6 2016-06-056Prof. I. J. Chung Data Structure Creation of dynamic variable in C++ : new op. float *p; p is just declared but it points to nothing p = new float; p points to newly allocated a variable of float data type *p = 10.0; the new float variable contains the value 10.0 Note : delete operator : freeing or releasing the memory space P P 10.0 P

7 2016-06-057Prof. I. J. Chung Data Structure node constructor initializes both the data and link Syntax node (const value_type & data = value_type(), const node* init_link = NULL); 1)Assume that value_type was defined before as the int data type such as class node struct node { public : typedef int value_type; { value_type data; private : value_type data; node *link;}; node *link;}; 2) The first parameter of node construct : default value for the data is “ data = value_type “ default value : 0 (#) false (bool) second parameter of node construct : link defined as int default arg. created by the value_type (old C++ : X, new ANSI C++ : OK)

8 2016-06-058Prof. I. J. Chung Data Structure Example P = new node;q = new node(1.0)r = new node(2.0,P) // 0 argument:// 1 argument.//2 argument data field = 0, // data field = 1.0// … link field = Ω link field = Ω P 0 q 1.0 2.0 default value

9 2016-06-059Prof. I. J. Chung Data Structure Example struct node { int data; node *link; }; int main () { node *p; p = new node; p->link = NULL; p ->data = 1; }

10 2016-06-0510Prof. I. J. Chung Data Structure Example node* head_pter =new node(1.0, NULL); head_pter = new node(2.0, head_pter); head_pter = new node(3.0, head_pter); head_pter = new node(4.0, head_pter); 4.03.02.01.0 head_pter

11 2016-06-0511Prof. I. J. Chung Data Structure pgm1 function which computes the length of a list size_t list_length(const node* head_ptr) //precond : head_ptr is the head pointer of a LL //postcond : returned value is the # node in the LL {const node *cursor; size_t answer; answer = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor → link()) ++answer; return answer; } Note : head_ptr is used to access the nodes of the list, but the function doesn ’ t change anything in the list.

12 2016-06-0512Prof. I. J. Chung Data Structure Note : A function which operates a linked list has parameter(s) that are pointers to nodes in that list. If the function does not change the linked list, then the parameter should be declared as const node * Example : size_t list_length(const node* head_ptr) //precond : head_ptr is the head pointer of a LL //postcond : returned value is the # node in the LL {const node *cursor; size_t answer; answer = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor → link()) ++answer; return answer; } Note : head_ptr is used to access the nodes of the list, but the function doesn ’ t change anything in the list.

13 2016-06-0513Prof. I. J. Chung Data Structure pgm2 function program to insert a new node with some data at the head position of a list void list_head_insert(node*& head_ptr, const node :: value_type& entry) {head_ptr = new node(entry, head_ptr);} 1 210 head_ptr … 210 head_ptr … 1 entry

14 2016-06-0514Prof. I. J. Chung Data Structure program which inserts a node in the middle of list A BC … A B C … void insert_a_node(node* privious_ptr, const node :: value_type&entry) previous_ptr insert_ptr

15 2016-06-0515Prof. I. J. Chung void list_insert(node* previous_ptr, const node::value_type& entry) // see the parameter rules in next page {node *insert_ptr; insert_ptr = new node; insert_ptr->set_data(entry); insert_ptr->set_link(previous_ptr->link()); previous_ptr->set_link(insert_ptr); } program which inserts a node in list

16 2016-06-0516Prof. I. J. Chung Note : When a function accesses to a linked list, use a node* parameter with the following rules : 1)Use a pointer to a constant node const node* if the function accesses to the linked list and the function does not change the linked list 2) Use a value parameter node* if the function changes the list but does not need to make the pointer point to a new node 3) Use a reference parameter node*& if the function accesses to the linked list and makes the pointer point to a new node parameters for linked list

17 2016-06-0517Prof. I. J. Chung void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr) { head_ptr = NULL; tail_ptr = NULL; if (source_ptr == NULL) return; // list = empty? // make the head node for the newly created list list_head_insert(head_ptr, source_ptr->data()); source_ptr = source_ptr->link(); // copy the remaining nodes to the newly created list while (source_ptr != NULL) { list_insert(tail_ptr, source_ptr->data()); tail_ptr = tail_ptr->link(); souce_ptr = source_ptr->link(); } } Program which copies a linked list

18 2016-06-0518Prof. I. J. Chung program which deletes a head node from a linked list void remove_head (node*& head_ptr) { node *remove_ptr; remove_ptr = head_ptr; head_ptr = head_ptr→link(); //now head_ptr points to the 2 nd node delete remove_ptr; // return the space to the heap space }

19 2016-06-0519Prof. I. J. Chung delete a non-head node void remove_nonhead(node* previous_ptr) { node *remove_ptr; remove_ptr = previous_ptr→link() previous_ptr→set_link(remove_ptr→link()); delete remove_ptr; } … A BC … … A C … previous_ptr remove_ptr

20 2016-06-0520Prof. I. J. Chung Search node with a specific data(key) value node* list_search(node* head_ptr, const node::value_type& target) { // Precond : head_ptr points the head node of a linked list // Postcond : Returned value is a pointer to the first node // which has the specified key (target) value in its data field; // If the linked list has no such node, // NULL pointer is returned for (cursor = head_ptr; cursor != NULL; cursor = cursor → link()) if (cursor->data() == target) return cursor; return NULL; }

21 2016-06-0521Prof. I. J. Chung Data Structure doubly linked list a linked list where every node has 2 link fields, LLINK to its predecessor and RLINK to its successor empty doubly LL info LLINLK RLINK … … P (head) (+) :easy to traverse the list in both direction (-) : (1) extra memory cost due to LLINK (2) extra execution time due to change both the LL & RL

22 2016-06-0522Prof. I. J. Chung Data Structure class doubly_ll { public : typedef value_type; //member function to set the data & LL, RL voidset_data(const volue_type & new_data) voidset_llink(node* new_llink) voidset_rlink(node* new_rlink) …. private: value_typedata_field; node *llink; node *rlink; }; ˜˜˜˜˜˜˜˜˜˜ fill the d.t. you want e.g. int, double, char, float, … etc

23 2016-06-0523Prof. I. J. Chung Sequential list or Linked list? 1) If frequent access to nodes in the list, use____________ 2) If frequent resizing is needed, use ______________ 3) If frequent insertion into the middle of the list, use ________ 4) If frequent deletion from the middle of the list, use ________ 5) If operations occur at a cursor, use _________________ 6) If operations occur at a 2-way cursor, use ____________ 7) If the number of nodes(items) is large, use ____________

24 2016-06-0524Prof. I. J. Chung Memory Management Static Memory Management : all memory needs are determined before program execution and declared by defining the

25 2016-06-0525Prof. I. J. Chung Programming Assignment Polynomial Addition Write a program which adds two polynomials : each term of a polynomial has coefficient and exponent. Question : How can you represent a polynomial in computer? i.e. How can you represent each term of a polynomial in computer?

26 2016-06-0526Prof. I. J. Chung Programming Assignment Answer : Represent each term of a polynomial using the list. class node { public : … private : float coef; // coeffient of a term int exp; // exponent of a term node *link; // link field}; Example Let P(x) = Q(x) = Explain in class with diagrams

27 2016-06-0527Prof. I. J. Chung PA (continued) Case 1 : if (p->exp == q->exp) { s->coef = p->coef + q->coef; s->exp = p->exp; p = p->link; q = q-link;} Note : You have to check whether s->coef == 0 or not. If so, what will you do? Case 2 : if (p->exp > q->exp) { s->coef = p->coef; s->exp = p->exp; p = p->link; } Case 3 : if (q->exp == p->exp) { s->coef = q->coef; s->exp = q->exp; q = q->link; }


Download ppt "2016-06-051Prof. I. J. Chung Data Structure #4 Professor I. J. Chung."

Similar presentations


Ads by Google