Sunteți pe pagina 1din 8

Definition: a collection of components, called nodes.

Each node contains the address of the next node.

Chapter 5
Linked-Lists

Every node in a linked list has two components:


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

Data Structure Using C++

Set of operations: Linked Lists


Node Structure

Point to the first node

Basic operations on the Linked Lists


The address of the first node in the linked list stored in the pointer head. Each node has two components:

To store information To store an address of the next node

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

Data Structure Using C++

Data Structure Using C++

Create a node and declare pointers


Declaration a node
struct noteType { int info; noteType *link; }

Steps to create a node, insert and delete data to / from linked list

infor link

Declaration a pointer noteType *head, *p,*q, *newNode;

Data Structure Using C++

Data Structure Using C++

Insert data in to a linked list


Assume: the list is exist as shown in the diagram:

Insert data in to a linked list


To insert int 50 between node 65 and 34

Create a new node: newNode = new nodeType; assert(newNode !=NULL); newNode->info = 50;

Why newNode & nodeType?

pointer p point to node 65

struct noteType { int info; noteType *link } noteType *head, *p,*q, *newNode;
7 Data Structure Using C++ 8

Data Structure Using C++

Insert data in to a linked list


newNode->link = p->link; p->link=newNode;

Insert data in to a linked list


Important!!!!:

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

Data Structure Using C++

Data Structure Using C++

10

Insert data in to a linked list


To insert int 50 between node 65 and 34

Insert data in to a linked list


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;

atau p->link =newNode; newNode->link=q

Data Structure Using C++

11

Data Structure Using C++

12

Insert data in to a linked list

Delete a node from a linked list


Node to be deleted is 34 q = p->link;
2. Should use two pointers: 1. To point to the previous node to be deleted, p To point to the node to be deleted, q

After the following statement executes: P->link = newNode;

p->link = q->link; q delete q;

newNode->link=q;

?
13

head

45

65

34

45

p q
Struktur Data Menggunakan C++ 14

Data Structure Using C++

Traversing a linked list


First Current 14

Traversing a linked list


First Current 14

To display data in a linked list


16 18 20

To search & display data 16 in a linked list


16 18 20

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) {

while (current !=NULL) {

cout << current -> infor << ->; // display data

if (search_data == current -> infor) pointed by the current pointer.


current = current -> next; <<set current-> infor cout // current pointer }

<< ->;

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

18 16 Struktur Data Menggunakan C++20

Stop!!!

15

Struktur Data Menggunakan C++

16

Traversing a linked list


First Current 14

Building a Linked List

12

16

18

20

Two types: 1) forwards linked list 2) backwards linked list

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

Delete an item from a linked list


2 15 8 24 34

newNode
Data Structure Using C++ 17 Struktur Data Menggunakan C++ 18

Building a Linked List (Forward)


Input data: 2 15 8 24 34 Need three pointers : Pointer first point to the first node Pointer last point to the last node Pointer newNode to point to the new node

Building a Linked List (Forward)

Assume:

Input data: 2 15 8 24 34

Create new node Pointers :


nodeType *first, *last, *newNode; int num;

Steps / algorithm [Forward] :


struct noteType { int info; noteType *link }

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)

Initialize: (List is empty) first=NULL; last=NULL;

Create a new node: newNode = new nodeType; assert(newNode !=NULL); newNode->info = 2;

Refer to page 285 - 288


Struktur Data Menggunakan C++ 19 Data Structure Using C++ 20

Building a Linked List (Forward)


Algorithm: 1. cin >> num; 2. newNode = new nodeType; 3. assert(newNode!=NULL) 4. newNode->info =num; 5. newNode->link =NULL; 6. if (first==NULL) // list is empty { 6a. first=newNode; 6b. last=newNode; } 7. else { 7a. last->link=newNode; // List is not empty 7b. last=newNode; }

Building a Linked List (Forward)

If the list is empty :


first=NULL; last=NULL;

first

last

If the following statements execute:


cin >> num; newNode = new newType; assert(newNode!=NULL) newNode->info =num; //num=2 newNode->link =NULL

2
newNode

Data Structure Using C++

21

Struktur Data Menggunakan C++

22

Building a Linked List (Forward)


List is empty

Building a Linked List (Forward)


List is NOT empty

If list is empty (first==NULL)


first=newNode; last=newNode;

If repeat statements 1 7b diulangi and num = 15 :


first last

15
newNode

first 2
newNode

Because first is NOT NULL, statements 7a and 7b are executed


first

7a last->link=newNode; 7b. last=newNode;

2
newNode

15

last
Data Structure Using C++ 23

last

Struktur Data Menggunakan C++

24

Building a Linked List (Forward)

Building a Linked List (Backward)

If statements 1 7b are repeated : The following input : 8 24 34

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

It is your responsibility!!! Read page 288 - 289

first

15

24
last

34
newNode
25 Struktur Data Menggunakan C++ 26

Struktur Data Menggunakan C++

Linked List as ADT


Double linked list

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

struct NodeType { int info; NodeType *next; NodeType *prev; }


Struktur Data Menggunakan C++ 28

Circular Linked List

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]

Use struct to declare data structure


Your program will be verified next week. Your understanding is the first priority.

Data Structure Using C++

29

Data structure C++

30

S-ar putea să vă placă și