Documente Academic
Documente Profesional
Documente Cultură
Chapter 5
Linked-Lists
To store data / information To store address of the next node (called link)
The address of the first node in the list is stored a pointer called head or first.
Pointer: head / first
First Node
Data Structure Using C++ 1
The address of the first node in the linked list stored in the pointer head. Each node has two components:
Insert data to a linked list Search and identify the existing data in a linked list Delete data in a linked list
Remember: Pointer head / first always point to the first node of a linked list
Steps to create a node, insert and delete data to / from linked list
infor link
Create a new node: newNode = new nodeType; assert(newNode !=NULL); newNode->info = 50;
struct noteType { int info; noteType *link } noteType *head, *p,*q, *newNode;
7 Data Structure Using C++ 8
A sequence of statements is important What happen if the following sequence is executed. : p->link = newNode; newNode ->link = p -> link;
head
45
65
34
45
p
newNode
50
head
45
65
34
45
p
newNode
50
10
However, if two pointers are used, sequence is not important. Eg.: There pointers p and q.
Possible instructions / statements sequenceto insert new node between node pointed by pointer p and q. [!!!Sequence is no important if we use two pointers] newNode ->link =q; p->link = new Node;
11
12
newNode->link=q;
?
13
head
45
65
34
45
p q
Struktur Data Menggunakan C++ 14
12
12
Current = First; // set current pointer point to the first node while (current !=NULL) { cout << current -> infor << ->; // display data pointed by the current pointer. current = current -> next; // set current pointer point to the following node. } Idea is to set current pointer to point to each node to be display from the first node until to the end node.
Search_data = 16; Current = First; // set current pointer point to the first node while (current !=NULL) {
<< ->;
But how to move the current pointer from first node to the end Should use a pointer, generally called current pointer
currentpoint to the following node. current pointer point to the = current -> next; // set following node. }
Output:-
12
14
Stop!!!
15
16
12
16
18
20
To search & display data in a linked list Insert an item into a linked list
First
The different???
Input data: 2 15 8 24 34
Last
newNode
Data Structure Using C++ 17 Struktur Data Menggunakan C++ 18
Assume:
Input data: 2 15 8 24 34
Create new node If first node is NULL, the list is empty. Pointer first and and last point to newNode Else , set last point to the new node (last node)
first
last
2
newNode
21
22
15
newNode
first 2
newNode
2
newNode
15
last
Data Structure Using C++ 23
last
24
Pertimbangkan penyataan berikut: 1. cin >> num; 2. newNode = new newNode; 3. assert(newNode!=NULL) 4. newNode->info =num; 5. newNode->link =NULL; 6. if (first==NULL) { 6a. first=newNode; 6b. last=newNode; } 7. else { 7a. last->link=newNode; 7b. Last=newNode; }
first
15
24
last
34
newNode
25 Struktur Data Menggunakan C++ 26
A data type that specifies the logical properties without the implementation details Three (3) components: The name of ADT The set of values to the ADT The set of operations on the data
Basic operations on Linked Lists: Initialize the list Check whether the list is empty Output the list Find the length of the list Delete an item from the list . .
Struktur Data Menggunakan C++ 27
Lab Assignment 3
A linked list in which the last node point to the first node
Write a simple program to implement linked list. Your program should do the following tasks:
Insert data Edit data Delete data. [Data can be integer or string]
Your program will be verified next week. Your understanding is the first priority.
29
30