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

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Lists: An internal look
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Data Structures: A Pseudocode Approach with C
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
Chapter 4.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
CS Data Structures Chapter 4 Lists.
1 Linked Lists It’s a conspiracy!. 2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Data Structures Using C++ 2E
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Memory Management CS Chakrabarti Variable storage thus far  Never used global variables  All variables allocated inside functions, passed.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
1 CSC241: Object Oriented Programming Lecture No 05.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
LINKED LISTS.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Pointers and Dynamic Arrays
C++ Programming:. Program Design Including
Pointers and Linked Lists
CSC212 Data Structure - Section BC
Pointers and Linked Lists
Linked Lists Chapter 6 Section 6.4 – 6.6
Lists CS 3358.
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists.
CSCE 210 Data Structures and Algorithms
Chapter 4 Linked Lists
The Bag and Sequence Classes with Linked Lists
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 16-2 Linked Structures
Pointers and Linked Lists
Pointers, Dynamic Data, and Reference Types
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
CC 215 Data Structures Pointers (review and examples)
Linked Lists Chapter 4.
Chapter 16 Linked Structures
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
SPL – PS3 C++ Classes.
Data Structures & Programming
Presentation transcript:

Prof. I. J. Chung Data Structure #4 Professor I. J. Chung

Prof. 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

Prof. 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; };

Prof. 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( → ).

Prof. 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

Prof. 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

Prof. 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)

Prof. 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 default value

Prof. 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; }

Prof. 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); head_pter

Prof. 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.

Prof. 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.

Prof. 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);} head_ptr … 210 head_ptr … 1 entry

Prof. 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

Prof. 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

Prof. 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

Prof. 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

Prof. 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 }

Prof. 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

Prof. 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; }

Prof. 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

Prof. 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

Prof. 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 ____________

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

Prof. 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?

Prof. 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

Prof. 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; }