TAILIEUCHUNG - Faculty of Computer Science and Engineering Department of Computer ScienceLAB SESSION 1 BASIC

Tham khảo tài liệu 'faculty of computer science and engineering department of computer sciencelab session 1 basic', khoa học xã hội, thư viện thông tin phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 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 7 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
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.