| Start |
|---|
| A doubly linked list An example of code showing how nodes can be inserted at a doubly linked list, how the list can easily be reversed, and how it can be printed in reverse. Note that sometimes, storing a pointer to the last node is useful (it is more efficient to simply be able to jump straight to the end of the list than to need to iterate through to the end): In which case, updating it upon changes to the list is needed. Sometimes, a key is also used to identify elements. It is simply a member of the Node structure: The key is then used when any tasks are performed on a specific element, like deleting elements. |
| Inserting a node at the beginning of a singly linked list The code below will prompt for numbers and continue to add them to the beginning of a linked list. Explanation for the Insertion of NodesIn order to understand how we add nodes at the beginning, let's take a look at possible scenarios:
The line Now, we want to set our head node pointer to point to our current node. This is done with
With Which, obviously needs to be altered since This is done with |
| Inserting a node at the nth position So far, we have looked at inserting a node at the beginning of a singly linked list (opens new window). However, most of the times you will want to be able to insert nodes elsewhere as well. The code written below shows how it is possible to write an |
| Reversing a linked list You can also perform this task recursively, but I have chosen in this example to use an iterative approach. This task is useful if you are inserting all of your nodes at the beginning of a linked list (opens new window). Here is an example: Explanation for the Reverse List MethodWe start the Basically, the concept of reversing the linked list here is that we actually reverse the links themselves. Each node's next member will become the node before it, like so: Where each number represents a node. This list would become: Finally, the head should point to the 5th node instead, and each node should point to the node previous of it. Node 1 should point to However, there is one small problem with this method. If we break the link to the next node and change it to the previous node, we will not be able to traverse to the next node in the list since the link to it is gone. The solution to this problem is to simply store the next element in a variable ( RemarksThe C language does not define a linked list data structure. If you are using C and need a linked list, you either need to use a linked list from an existing library (such as GLib) or write your own linked list interface. This topic shows examples for linked lists and double linked lists that can be used as a starting point for writing your own linked lists. Singly linked listThe list contains nodes which are composed of one link called next. Data structureDoubly linked listThe list contains nodes which are composed of two links called previous and next. The links are normally referencing to a node with the same structure. Data structureTopoligesLinear or openCircular or ringProceduresBindBind two nodes together.
Making circularly linked listMaking linearly linked listInsertionLets assume a empty list always contains one node instead of NULL. Then insertion procedures do not have to take NULL into consideration. |
Read Article
https://develop.horje.com/learn/1434/reference