0% found this document useful (0 votes)
72 views16 pages

Chapter 2-Edited

The document discusses lists and linked lists. It defines lists as a collection of elements of the same type, and linked lists as lists where each element points to the next in the sequence. It describes the basic operations of linked lists as creating a new list, creating new nodes, checking if a list is full or empty, inserting nodes, and deleting nodes. Insertion and deletion operations in linked lists are more efficient than arrays since they take constant time regardless of list size.

Uploaded by

Suriey Tafar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views16 pages

Chapter 2-Edited

The document discusses lists and linked lists. It defines lists as a collection of elements of the same type, and linked lists as lists where each element points to the next in the sequence. It describes the basic operations of linked lists as creating a new list, creating new nodes, checking if a list is full or empty, inserting nodes, and deleting nodes. Insertion and deletion operations in linked lists are more efficient than arrays since they take constant time regardless of list size.

Uploaded by

Suriey Tafar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

LIST AND LINKED LIST

2.0

INTRODUCTION In this unit you will learn about the implementation of list on how you will create list, node, checking list, add node and delete node in list and analyze the effectiveness in using linear linked list.

2.1

LIST AND LINKED LIST List is one of the group data collection, element, and component or in the same type of object. List usually in one record. A linked list is like a clothesline on which the data structures hang sequentially. There is a head pointer addressing the first element of the list, and each element points at a successor element, with the last element having a link value NULL. (Kelley & Pohl (1992:448)

In file list.h: #include<iostream.h> typedef char DATA; /* we will use char in examples */ struct linked_list { DATA Struct linked_list *next; }; typedef struct linked_list ELEMENT; typedef ELEMENT *LINK; Example 2.1 Is An Example Of Linked List.

LIST AND LINKED LIST

Example one of the group data collection, element, component or in the same type of object. (Figure 2.1 The Explanation of Example of List). Mammal Group Bird Group

Amphibious Group

Reptile Group Fish Group Insects Group

Figure : 2.1 Group of animals that have many types of small groups. The operations that can be done with this list are: a. Insertion b. Deletion Insert more items in the list. Delete one item from the list. This operation involve the process of identifing the location of item and then, delete it. c. Retrieval d. Traversal After that, the items below it have to be removed above. Identify the item from the list and present it. All the items will be listed systematically. This operation needs one repetition algorithm . Table 2.1 : Table The Operation of List

LIST AND LINKED LIST

2.2 LINEAR LINKED LIST OPERATION AND THE EFFECTIVENESS 2.2.1 LIST OPERATIONS The basic operations on linear linked list include the following: i. ii. iii. iv. v. Create new linked list Create new node Check linked list : full, null Insert node to linked list Delete node in linked list

2.2.2

INSERTION One of the most useful properties of lists is that insertion takes a fixed amount of time once the position in the list is found. In contrast, if one wished to place a value in a large array, retaining all other array values in the same sequential order, the insertion would take, on average, time proportional to the length of the array. The values of all elements of the array that came after the newly inserted value would have to be moved over one element (Kelley & Pohl (1992:453) Let us illustrate insertion into a list by having two adjacent elements pointed at by p1 and p2 and inserting between them an element pointed at by q.

Before insertion : P1 A C

NULL

LIST AND LINKED LIST

After insertion P1 A p2 C

/ * Inserting an elemet in a linked list * / #include list.h void insert (LINK p1, LINK p2, LINK q) { p1 ->next =q; /* insertion * q ->next =p2; }

2.2.3

DELETING Deleting an element is very simple in linear linked list. The predecessor of the element to be element has its link member assigned the address of the successor to the deleted to be deleted has its link member assigned the address of the successor to be the deleted element. Let us first illustrate graphically the situation before deletion.

p A B C

The next line of code q = p ->next; causes q to point at the element that we want to delete

LIST AND LINKED LIST

p A

q B C

Now consider the statement p ->next =q -> next; p After this statement is executed q

LIST AND LINKED LIST

SENARAI DAN SENARAI BERPAUT

DEFINISI: Senarai adalah satu kumpulan koleksi data, elemen, komponen atau objek yang sama jenis SENARAI BERJUJUKAN Mengimplementasikan operasi senarai secara tatasusun.

Senarai boleh diimplementasi secara tatasusun yang mempunyai item yang berjujukan. Jujukan tersebut adalah berdasarkan kepada susunan berikut, item yang pertama

dimasukkan akan berada pada kedudukan pertama dalam tatasusun, begitulah turutan kedudukan item seterusnya. Rajah menunjukkan ilustrasi cara data dimasukkan ke dalam senarai

Senarai

P0

P1

P2

P3

.................

Pn

Tatasusun

X [ 0] X[1] X[2] X[3] .. X[Maks]

Mencipta senarai

Mencipta senarai akan melibatkan proses menentukan bilangan maksima bagi item yang hendak digunakan dalam senarai. Proses yang seterusnya ialah mengenal pasti jenis-jenis item yang diperlukan

Awalan (input) Bilangan item Jenis-jenis item

Tugas (operation) Mencipta satu senarai kosong.

Kembali (output) Senarai tercipta

LIST AND LINKED LIST

Menyemak senarai

Proses menyemak senarai adalah untuk mengenal pasti samaada senarai tersebut kosong atau penuh. Proses ini terbahagi kepada dua cara : a. Proses menentukan senarai kosong atau tidak.

input Menerima satu senarai

operation Mengenal pasti samada item pertama wujud atau tidak.

output Jika terdapat item pertama senarai tidak kosong begitulah sebaliknya

b. input Menerima satu senarai

Proses menentukan senarai penuh atau tidak

operasi Mengenalpasti samada item output terakhir wujud Jika terdapat item terakhir atau tidak. senarai tersebut penuh begitulah sebaliknya

Menambah item-item dalam senarai


Proses menambah item melibatkan penerimaan item yang baru. Item terakhir dalam senarai dikenalpasti kedudukannya untuk proses menambah item dalam senarai output Senarai yang telah dikemaskinikan (item baru ditambah)

input Menerima satu senarai

operation Pastikan senarai tidak penuh Terima item baru Tentukan kedudukan item terakhir. Masukan item baru

Menghapuskan ahli dalam senarai.

Menghapuskan ahli dalam senarai ialah proses menghapuskan item yang terdapat di dalam satu senarai. Senarai yang diterima perlu disemak untuk menentukan senarai tidak kosong.

LIST AND LINKED LIST


Item yang hendak dihapuskan perlu dikenalpasti. Item tersebut di hapuskan dan senarai di kemaskini.

input Menerima satu operation senarai Pastikan senarai tidak kosong Kenalpasti item Item disemak dalam senarai untuk menentukan kedudukannya Proses hapus dan anjak item dilaksanakan

output Senarai yang telah dikemaskinikan (item telah dihapuskan)

SENARAI BERPAUT ( LINKED LIST ) Senarai berpaut adalah sejenis struktur data di mana setiap itemnya mempunyai hubungkait antara item yang lain dalam sesuatu senarai. Segala operasi yang hendak dilaksanakan ke atas sesuatu item perlulah mengambilkira item bersebelahannya. Senarai berpaut adalah satu kumpulan item yang dinamik di mana saiznya akan bertambah dan berkurang bergantung kepada jumlah itemnya. CONTOH: Senarai berpaut boleh dikaitkan dengan kepala keretapi dan gerabak-gerabaknya. Gerabakgerabak tersebut membawa muatan yang berlain (data), setiap gerabaknya disambung kepada gerabak/kepala keretapi yang lain (pepaut) . lihat rajah:

Ia mempunyai kelebihan dari segi fleksibilitinya jika dibandingkan dgn perwakilan struktur data bersebelahan. Dengan struktur data yang dinamik, (data boleh dibuang atau ditambah pada manamana bahagian dalam senarai), setiap item diberikan ruang memori apabila ditambah pada senarai. Lazimnya setiap nod pada senarai ini mempunyai 2 elemen ( medan ):

A
8

LIST AND LINKED LIST

item yg disimpan dlm senarai (data) (pepaut) kpd item yg seterusnya dlm senarai

dan penuding

Nod terakhir dalam senarai adalah NULL pointer utk menentukan ia adalah penamat atau ekor kpd sesuatu senarai. Perbezaan antara timbunan dan senarai berpaut ialah item pada timbunan hanya boleh ditambah pada top of stack tetapi pada senarai berpaut ia boleh ditambah pada manamana tempat di dalam senarai berpaut. Pembolehubah yang mewakili senarai adalah penuding kepada nod kepala Berikut ialah contoh struktur senarai berpaut:

a
Operasi senarai berpaut: Mencipta senarai

NULL

Proses mencipta senarai berpaut cuma memerlukan kita mengistiharkan satu jenis data kompleks iaitu struct. Jenis-jenis item yang perlu dimasukan harus diketahui terlebih dahulu.

Awalan (input) Jenis-jenis item

Tugas (operation) Mencipta satu senarai kosong.

Kembali (output) Senarai tercipta

Mencipta nod dalam senarai berpaut

Proses mencipta nod perlu diimplimentasi dengan menggunakan Untuk tujuan mencipta nod secara dinamik, malloc() perlu digunakan dalam Proses ini perlu disebabkan untuk tujuan menambah satu item baru, item

penunjuk.yang dapat menyediakan ruang ingatan untuk nod secara dinamik.

aturcara ini.

baru tersebut perlu dipautkan kepada item lama dalam senarai, proses pepautan adalah melalui nod

Awalan (input) Menerima satu senarai

Tugas (operation) Kembali (output) Mencipta satu nod baru Satu senarai yang boleh menerima item baru. 9

LIST AND LINKED LIST

Menyemak senarai berpaut

Proses menyemak senarai untuk mengenal pasti samaada senarai tersebut Proses ini melibatkan pengujian pembolehubah dalam senarai berpaut

kosong.

samada ianya kosong (null) atau tidak

Awalan (input) Menerima satu Tugas (operation) Mengenal pasti samada Senarai berpaut pembolehubah Null wujud atau tidak.

Kembali (output) Jika Null, senarai berpaut kosong

Menambah nod dalam senarai berpaut Menambah nod ke dalam senarai berpaut boleh terdiri daripada menambah di bahagian:

Hujung senarai. Tengah senarai

Hujung senarai. Penambahan nod pada hujung perlu disemak adakah pembolehubah tersebut terdapat Null atau tidak. Kalau pembolehubah Null bermaksud senarai kosong, item ditambah pada lokasi pertama

Penambahan item pada senarai kosong


DATA X

NULL

DATA

Sebelum proses penambahan

Selepas proses penambahan

10

LIST AND LINKED LIST

Penambahan item pada senarai tidak kosong


DATA 4 X

DATA 1

DATA 2

DATA 3

Sebelum proses penambahan


DATA 1 DATA 2 DATA 3 DATA 4 X

Selepas proses penambahan

Sebelum proses penambahan

Penambahan item diantara Lokasi dikenalpasti dan


DATA 4 X Nod diputuskan

DATA 1

DATA 2

DATA 3

Selepas proses penambahan


DATA 1 DATA 4 DATA 2 DATA 3 X

11

LIST AND LINKED LIST

Menghapuskan item dalam senarai.

Menghapuskan item bermaksud item yang tidak diperlukan dibuang dari Proses menghapuskan item terdapat tiga jenis, iaitu: Menghapuskan item tunggal. Menghapuskan item di tengah senarai. Menghapuskan item dihujung. Senarai yang diterima perlu disemak untuk menentukan senarai tidak Item yang hendak dihapuskan perlu dikenalpasti. Item tersebut di hapuskan dan di kemaskini

senarai.

a. b. c.

kosong.

Menghapuskan item tunggal. Item dalam senarai diuji untuk mengenal pasti pembolehubah X wujud, item berada dipenghujung senarai. Pepaut pada item diputuskan dan di sambung pada pembolehubah NULL.

DATA

S
DATA X NULL

Selepas proses penghapusan ebelum proses penghapusan

12

LIST AND LINKED LIST

Menghapuskan item di tengah senarai. Item sebelum dan selepas dikenalpasti. Pepaut pada item sebelum dan selepas diputuskan dan di sambung

DATA 1

DATA 2

proses penghapusan
DATA 2

DATA 3

DATA 4

DATA 1

DATA 3

DATA 4

Menghapuskan item di hujung senarai. Item dalam senarai diuji untuk mengenal pasti pembolehubah X wujud, item berada dipenghujung senarai Pepaut pada item diputuskan dan di sambung pada pembolehubah X

DATA 1

Sebelum proses 3 DATA 2 DATA

DATA 4

proses
DATA 1 DATA 2 DATA 3

DATA 4 X

13

LIST AND LINKED LIST

IMLPEMENTASI SENARAI BERPAUT a) pengisytiharan struktur data: typedef char UNSUR; typedef struct nod { UNSUR data ; struct nod *next; } kita takrifkan pula pembolehubah berjenis struktur di atas untuk digunakan dalam aturcara kita nanti sebagai: NOD *senarai; struct nod *pnod; b) cipta senarai terpaut kosong void CiptaSenarai (NOD **sList) { *sList = NULL; } contoh panggilan kepada fungsi ini adalah seperti berikut: CiptaSenarai (&senarai)

c) cipta nod baru NOD *NodBaru(UNSUR unsurdata) { NOD *Nnod; Nnod = (NOD *) malloc (sizeof(NOD)); If(Nnod != NULL) { Nnod ->data = unsurdata; Nnod ->next = NULL; } return Nnod; }

14

LIST AND LINKED LIST

d) tentukan senarai terpaut kosong BOOL SenaraiKosong(Nod *sList) { return (sList == NULL); } e) selit item baru pada senarai i. selit nod di awal senarai terpaut andaikan nod temp ingin diselit di awal senarai setkan temp.next = sList setkan sList = temp

ii. selit nod di tengah senarai terpaut andaikan temp hendak diselit di tengah senarai sList, iaitu selepas nod yang ditunjukkan oleh pnod setkan temp->next =pnpd->next setkan pnod ->next =temp iii. selit nod dihujung senarai terpaut andaikan temp ingin diselit di hujung senarai setkan temp->next = pnod->next setkan pnod->next = temp bebaskan temp dengan fungsi free(temp) f) hapuskan nod pada senarai void HapusSenarai(NOD **sList, NOD *pnod) { NOD *temp; if(SenaraiKosong(*sList)) { cout<<Ralat hapus: senarai kosong; return; } if(pnod == NULL) { temp = *sList; *sList = temp ->nextPtr; } else { temp = pnod ->nextPtr; pnod->nextPtr = temp->nextPtr; } }

15

LIST AND LINKED LIST

16

You might also like