pertemuan ke 2 - Linked List - 2101659403 - Rasya Hanifah Hanan


Linked list
  • Is a linear collection of data elements. These data elements are called node.
  •        Is a data structure which is turn can be used to implement other data structure.
  •            It acts as a building blocks to implement data structures. Such as, stocks, queue and other variations.

Single Linked list
  • A singly linked list is the simplest type of linked list in which every node contains some        data and pointer to the next node of some data.
  •  A singly linked list allows traversal of data only in one way.

Single Linked list : ‘Insert’

              Case 1 : the new node is inserted at the beginning

              Case 2 : the new node is inserted at the end

              Case 3 : the new node is inserted after a given node

              Case 4 : the new node is inserted before a given node

d             
 
      Single Linked list : ‘Delete’
  •       To delete a value, first we should find the location of node which store the value        we want to delete, remove it and connect the remaining linked list.

  •       Supposed the value we want to remove it is x and assuming x is exist in linked list         and it is unique.


               There are two conditions we should pay attention to :

         a.     If x is in a head node or
                 


         b.     x is not in a head node


Double Linked list :
     Double linked list dibentuk dengan menyusun sejumlah elemen sehingga pointer next menunjuk ke elemen yang mengikutinya dan pointer back menunjuk ke elemen yang mendahulunya. 

A. Linked list dengan head :
               a) Menggunakan 1 pointer head
                           b) Head selalu menunjuk node pertama

TNode *head;
'deklarasi pointer head'

head = NULL;
setelah mendeklarasi pointer head, head harus dibuat bernilai null

     int isEmpty( ) {
             if (head == NULL)  
   return 1;
     else 
    return 0; 
}

1. Contoh program :

void pushDepan (int databaru)
{
TNode *baru; 
baru = new TNode; 
baru -> data = databaru; 
baru -> next = NULL; 
baru -> prev = NULL; 
if (isEmpty( ) == 1)
      {
head=baru;
head->next=NULL;
head->prev=NULL;
}
     }

  else
  { 
           baru->next=head;
              head->prev=baru;
           head=baru;
  }
     printf(”data masuk”);

void pushBelakang (int databaru)

{
TNode *baru,*bantu;  

//digunakan untuk mengetahui data terbelakang 

baru = new TNode; 
baru->data = databaru; 
baru->next = NULL; 
baru->prev = NULL;
if (isEmpty( ) == 1)
{
head=baru;
head->next = NULL;
head->prev = NULL;
}

else
{ 
bantu=head; 
while(bantu->next!=NULL)
{ 
bantu=bantu->next;
} 
bantu->next = baru; 
baru->prev = bantu;
}
                    printf(”data masuk”);

void viewAll( )
{
 TNode *bantu; 
bantu = head;
if (isEmpty( ) == 0)
{ 
while(bantu!=NULL)
{
                              printf(“%i “,bantu->data); 
bantu=bantu->next;
} 
printf(“\n”);
}
}
else
{
printf(“Masih Kosong”);
}

void popBelakang( )
{
TNode *hapus;
int d;
if (isEmpty( ) == 0)
{
if(head->next != NULL)
{
hapus = head; 
while(hapus->next!=NULL)
{
hapus = hapus->next;
}
d = hapus->data;
hapus->prev->next = NULL;
delete hapus;
}
else
{
d = head->data;
head = NULL;
}
 printf(“%i terhapus”,d);
 }
else
{
            printf(“Masih Kosong”);
}
}

A. Linked list dengan head dan tail :
a) Menggunakan 2 pointer head and tail
b) Head selalu menunjuk node pertama dan tail selalu menunjuk node terakhir 

TNode *head, *tail;
'deklarasi pointer head dan tail'

head = NULL;
tail = NULL;
'setelah kita mendeklarasikan pointer head dan tail, kita tidak bisa langsung mendeklarasikan node yang dituju, maka pointer head dan tail harus bernilai null terlebih dahulu'

int isEmpty( ) {
             if (tail == NULL)
   return 1;
     else 
    return 0; 
}


Komentar