02.09.2019
Posted by 
Contoh Linked List C Rating: 7,8/10 4672 reviews

Circular linked list Ilustrasi Struktur Linked List: Pada gambar diatas kita memiliki 4 buah Struktur node, yang masih-masing menyimpan data dengan nilai 10, 20, 30 dan 40.

A linked list is linear data structure which is made up of a set of nodes. In addition to the data, each node also contains a pointer to the next node in the list. If the next node pointer is NULL it means that node is the last node in the list. A linked list also uses a local variable called the head thats points to the top of the list. If the head pointer points to NULL then it means the list is empty. In this article we will discuss how to insert an element(node) in a linked with the help of a C program.

There are three possibilities to insert/add an element to a linked list. Here are those three possiblities and the steps involved:

Inserting a node to the top of a list

This is probably the easiest and fastest method of adding an element to a linked list. Here are the steps.

  • Create the new node, lets say N.
  • Set the nodes data field (N→data = data).
  • Set the next pointer of new node to head pointer (N→next = head).
  • Set the head to point to the new node (head = N).

Inserting a node at the end of a list

To insert an element at the bottom of a list, you need to traverse up to the last element of the list and then,

  • Create the new node, lets say N.
  • Set the nodes data field (N→data = data).
  • Set the next pointer of new node to NULL (N→next = NULL).
  • Set the last element to point to the new node (last_node→next = N).

Inserting before or after a node

To insert an element before or after a particular node you need to traverse up to that node(to insert after) or up to the previous node(to insert before)and then,

  • Create the new node, lets say N.
  • Set the nodes data field (N→data = data).
  • Set the next pointer of new node to current nodes next (N→next = current_node→next).
  • Set the current node to point to the new node (current_node→next = N).

Here is a C Program to perform the following operations on a singly linked list.

  • Insert an element at the top of a list.
  • Insert an element at the bottom of a list.
  • Insert an element after the specified element in a list.
  • Insert an element before the specified element in a list.
  • Display all elements in the list.

This program also displays a menu for the users to make a selection. Here is the full source code.

Source Code

Sample Output

************************************ * Linked list operations: * * 1. Insert at the top of list * * 2. Insert at bottom of list * * 3. Insert after an element * * 4. Insert before an element * * 5. Show all elements * * 6. Quit * ************************************ Choose an option [1-5] : 4 Enter a number to insert : 4 Before which number do you want to insert : 5 Number 4 is now inserted before 5 in the list Press any key to continue... Contoh Penerapan Linked List Dalam Bahasa C sedikit lebih Rumit daripada PASCAL. Pelajari pelan2 saja...
#include <stdio.h>
#include <stdlib.h>
struct NODE {
int number;
struct NODE *next;
};
int search_value(struct NODE *llist, int num);
void append_node(struct NODE *llist, int num);
void display_list(struct NODE *llist);
void delete_node(struct NODE *llist, int num);
int main(void) {
int num = 0;
int input = 1;
int retval = 0;
struct NODE *llist;
llist = (struct NODE *)malloc(sizeof(struct NODE));
llist->number = 0;
llist->next = NULL;
while(input != 0) {
printf('n-- Menu Selection --n');
printf('0) Quitn');
printf('1) Insertn');
printf('2) Deleten');
printf('3) Searchn');
printf('4) Displayn');
scanf('%d', &input);
switch(input) {
case 0:
default:
printf('Goodbye ...n');
input = 0;
break;
case 1:

Linked List C Pointer


printf('Your choice: `Insertion'n');
printf('Enter the value which should be inserted: ');
scanf('%d', &num);
append_node(llist, num);
break;
case 2:
printf('Your choice: `Deletion'n');
printf('Enter the value which should be deleted: ');
scanf('%d', &num);
delete_node(llist, num);
break;
case 3:
printf('Your choice: `Search'n');
printf('Enter the value you want to find: ');
scanf('%d', &num);
if((retval = search_value(llist, num)) -1)
printf('Value `%d' not foundn', num);
else
printf('Value `%d' located at position `%d'n', num, retval);
break;
case 4:
printf('You choice: `Display'n');
display_list(llist);
break;
} /* switch */
} /* while */
free(llist);
return(0);
}
void display_list(struct NODE *llist) {
while(llist->next != NULL) {

Contoh Linked List C ++ Code

printf('%d ', llist->number);
llist = llist->next;
}
printf('%d', llist->number);
}
void append_node(struct NODE *llist, int num) {
while(llist->next != NULL)
List llist = llist->next;
llist->next = (struct NODE *)malloc(sizeof(struct NODE));
llist->next->number = num;
llist->next->next = NULL;
}
void delete_node(struct NODE *llist, int num) {
struct NODE *temp;
temp = (struct NODE *)malloc(sizeof(struct NODE));
if(llist->number num) {
temp = llist->next;
free(llist);
llist = temp;
} else {
while(llist->next->number != num)
llist = llist->next;
temp = llist->next->next;
free(llist->next);
llist->next = temp;
}
}
int search_value(struct NODE *llist, int num) {
int retval = -1;
int i = 1;
while(llist->next != NULL) {
if(llist->next->number num)
return i;
else
i++;
llist = llist->next;
}
return retval;
}
Tampilan nya:
My Ancestors Are Smiling At Me Imperials