Hello, Linear List is a linear data structure which keeps the every data separately. Arrays are static but Linked Lists has dynamic structure. We may have difficulty to add a data to the array but if we use linked lists, we can add the data where ever we want. Number of the data is not sure in the Linked Lists, we can add data or delete data whenever we want. Node is the location of the data in the linked list. Whenever we add a data this data called a node. If we in the last node, Last node next must be the show NULL value. There can be some disadvantages to use linked list instead of the using array. In the array, We can reach the data by pointing the location of the data but in the linked list we should route the all nodes to find the data. In this article, I will try to make Linked List application and in the application, I will try to add and delete data to the Linked List.

I will try to make this Linked List example using by the C programming language. After the creating our project, I add the neccessary libraries to the project. Like stdlib.h, thanks to this library, we can arrange the size of the memory using the malloc function. Now, I created a **Node** structure and in this structure, I keep the data and the point of the next node. In the main function, I created a **root** node and i arranged the memory size of the this root node using by the **malloc**. If there will not be a new data, **node’s next** must show the **NULL**. For that reason we make the root’s next equal to the NULL.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> #include<stdlib.h> typedef struct node{ int data; node *next; }; int main(){ node *root; root = (node *)malloc(sizeof(node)); root->data = 50; root->next = NULL; printf("%d",root->data); } |

In the function, I am controlling the some conditions. If the node is NULL, i created a new node. The other condition is make the decision, if the number big add to the right, if data small add left. All the nodes connected to the each other and the last node always show the NULL.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
node *insertItem(node *newnode,int newdata){ node *iter = newnode; // If the LinkedList Empty we added a new Item if(newnode == NULL){ // LinkList Boş İse newnode = (node *)malloc(sizeof(node)); newnode->next = NULL; newnode->data = newdata; return newnode; } //If the data smaller than the newdata we added the left side else if(newnode->data < newdata){ while(iter->next != NULL && iter->next->data < newdata){ iter = iter->next; } node *temp = (node *)malloc(sizeof(node)); temp->next = iter->next; iter->next = temp; temp->data = newdata; return newnode; } //If the data bigger than the newdata we added the right side else{ node *temp = (node *)malloc(sizeof(node)); temp->data = newdata; temp->next = newnode; return temp; } } |

I do not want to write printf all the time. For that reason, I write a function which shows the data of the linked list.

1 2 3 4 5 6 7 |
void showNodes(node *n){ // Until the item equal NULL we show the datas while(n != NULL){ printf("%d ",n->data); n = n->next; } } |

These are the codes of the Main function;

1 2 3 4 5 6 7 8 9 10 11 |
int main(){ node *root; root = NULL; root = insertItem(root,100); root = insertItem(root,10); root = insertItem(root,1060); root = insertItem(root,5); root = insertItem(root,50); root = insertItem(root,60); showNodes(root); } |

After the making adding process, we should write the deleting process. This function return a node. In the function, we will get the deleted data and then we will make the deleting process. These are the codes for the deleting process;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
node *deleteItem(node *deletedNode,int value){ node *temp; node *iter = deletedNode; if(deletedNode->data == value){ temp = deletedNode; deleteNode = deletedNode->next; free(temp); // This is the function delete item return deletedNode; } while(iter->next != NULL && iter->next->data != value){ iter = iter->next; } if(iter->next == NULL){ printf("Number NOT Found"); return deletedNode; } temp = iter->next; iter->next = iter->next->next; free(temp); return deletedNode; } |

Main Function;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int main(){ node *root; root = NULL; root = insertItem(root,100); root = insertItem(root,10); root = insertItem(root,1060); root = insertItem(root,5); root = insertItem(root,50); root = insertItem(root,60); showNodes(root); root = deleteItem(root,5); root = deleteItem(root,60); root = deleteItem(root, 1060); showNodes(root); } |

You can ask your questions via e-mail or comments.

## Be First to Comment