Contoh Linked List C

Posted on by  admin

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:

Struktur Data – Double Linked List dengan Bahasa C 30 December 2016 Comments Struktur Data, Tutorial C Mahir Koding – Double Linked List adalah salah satu contoh lain implementasi linked list selain single linked list yang telah kita bahas di tutorial sebelumnya.

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).
Contoh Linked List C

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.

Create linked list in c

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... Linked lists are a way to store data with structures so that the programmercan automatically create a new place to store data whenever necessary.Specifically, the programmer writes a struct definition that containsvariables holding information about something and that has a pointer to astruct of its same type (it has to be a pointer--otherwise, every time anelement was created, it would create a new element, infinitely). Each of these individual structs or classes in thelist is commonly known as a node or element of the list.

Contoh Linked List C A Linked List C++ Geeks



One way to visualize a linked list is as though it were a train. The programmer alwaysstores the first node of the list in a pointer he won't lose access to. Thiswould be the engine of the train. The pointer itself is the connector betweencars of the train. Every time the train adds a car, it uses the connectors toadd a new car. This is like a programmer using malloc to create a pointer to anew struct.

Contoh Single Linked List Circular

In memory a linked list is often described as looking like this: The representation isn't completely accurate in all of its details, but itwill suffice for our purposes. Each of the big blocks is a struct that has apointer to another one. Remember that the pointer only stores the memorylocation of something--it is not that thing itself--so the arrow points to the nextstruct. At the end of the list, there is nothing for the pointer to point to, soit does not point to anything; it should be a null pointer or a dummy node toprevent the node from accidentally pointing to a random location in memory (which isvery bad).
So far we know what the node struct should look like: This so far is not very useful for doing anything. It is necessary tounderstand how to traverse (go through) the linked list before it reallybecomes useful. This will allow us to store some data in the list and laterfind it without knowing exactly where it is located.
Think back to the train. Let's imagine a conductor who can only enter the trainthrough the first car and can walk through the train down the line as long asthe connector connects to another car. This is how the program will traversethe linked list. The conductor will be a pointer to node, and it will firstpoint to root, and then, if the root's pointer to the next node is pointing tosomething, the 'conductor' (not a technical term) will be set to point to thenext node. In this fashion, the list can be traversed. Now, as long as thereis a pointer to something, the traversal will continue. Once it reaches a nullpointer (or dummy node), meaning there are no more nodes (train cars) then itwill be at the end of the list, and a new node can subsequently be added if sodesired.
Here's what that looks like: That is the basic code for traversing a list. The if statement ensures thatthe memory was properly allocated before traversing the list. If thecondition in the if statement evaluates to true, then it is okay to try andaccess the node pointed to by conductor. The while loop will continue as longas there is another pointer in the next. Theconductor simply moves along. It changes what it points to by getting theaddress of conductor->next.
Finally, the code at the end can be used to add a new node to the end. Oncethe while loop as finished, the conductor will point to the last node in thearray. (Remember the conductor of the train will move on until there isnothing to move on to? It works the same way in the while loop.) Therefore,conductor->next is set to null, so it is okay to allocate a new area ofmemory for it to point to (if it weren't NULL, then storing something else inthe pointer would cause us to lose the memory that it pointed to). When weallocate the memory, we do a quick check to ensure that we're not out ofmemory, and then the conductor traverses one more element (like a trainconductor moving on to the newly added car) and makes sure that it has itspointer to next set to 0 so that the list has an end. The 0 functions like aperiod; it means there is no more beyond. Finally, the new node has its xvalue set. (It can be set through user input. I simply wrote in the '=42' asan example.)
To print a linked list, the traversal function is almost the same. In ourfirst example, it is necessary to ensure that the last element is printedafter the while loop terminates. (See if you can think of a better way beforereading the second code example.)
For example: The final output is necessary because the while loop will not run once itreaches the last node, but it will still be necessary to output the contentsof the next node. Consequently, the last output deals with this. We can avoidthis redundancy by allowing the conductor to walk off of the back of thetrain. Bad for the conductor (if it were a real person), but the code issimpler as it also allows us to remove the initial check for null (if root isnull, then conductor will be immediately set to null and the loop will neverbegin):
Previous: Accepting command-line arguments
Next: Recursion
Back to C Tutorial Index
Related articles
Learn how linked lists help with the Huffman encoding algorithm
Advertising Privacy policy Copyright © 2019 Cprogramming.com Contact About

Comments are closed.