tailieunhanh - Faculty of Computer Science and Engineering Department of Computer Science LAB SESSION 1

OBJECTIVE The objectives of Lab 1 are (1) to introduce on how to represent a linked list as an OO class; (2) to implement some basic operations on linked list and (3) to use an preimplemented class of linked list; 2. EXAMPLE This section gives an example on how to represent a linked list as a class, as well as implement some basic operations of the linked list. The way to use an implemented linked list is also introduced. Listing 1 gives an example of a class representing a node in a linked list. As stated in the lectures, a. | Faculty of Computer Science and Engineering Department of Computer Science LAB SESSION 1 BASIC OPERATIONS ON LINKED LIST 1. OBJECTIVE The objectives of Lab 1 are 1 to introduce on how to represent a linked list as an OO class 2 to implement some basic operations on linked list and 3 to use an preimplemented class of linked list 2. EXAMPLE This section gives an example on how to represent a linked list as a class as well as implement some basic operations of the linked list. The way to use an implemented linked list is also introduced. Listing 1 gives an example of a class representing a node in a linked list. As stated in the lectures a node in a linked list consists of two parts data and the link to the next node. class Node public Node next Listing 1 Listing 2 illustrates a simple way to create a linked list using the Node class. The list consisted of 3 elements 2 3 5 . We use the pointer pHead to keep track of the first element of the list meanwhile count reflecting the number of list elements. We also use a temporary pointer pTemp for our work. To check that our code runs correctly we use a loop to print out all of elements data. void main declaration Node pHead NULL int count 0 Node pTemp NULL make the list pTemp new Node count pTemp- data 5 pTemp- next NULL pHead pTemp the list now is 5 pTemp new Node count Page 1 5 Faculty of Computer Science and Engineering Department of Computer Science pTemp- data 3 pTemp- next pHead pHead pTemp the list now is 2 3 pTemp new Node count pTemp- data 2 pTemp- next pHead pHead pTemp the list now is 2 3 5 print them out while pTemp NULL cout pTemp- data pTemp pTemp- next Listing 2 In that way our program looks ill-organized. In Listing 3 we then improve it by combining pHead count into a class named List. Then the operation of inserting a new element into the list will be implemented as the method addFirst. We also implement method display to print out the list elements. Note that we use constructor to initialize the head of .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.