The linked list can do the following:
- Create a new linked list
- Add new elements
- Traversing a linked list
- Print linked list
The basic functions that correspond to the above operations are defined below.
Create a new linked list
After the new list is created, there are no elements, we want to allocate the node in memory for the data, and then insert the node into the list. Since this list has only one node, the next node it points to is null.
/*defining a function to create a new list. The return type of the function is struct node, because we'll return the head nodethe parameters would be the node, passe D from the main function. The second parameter would be the data element.*/structNode * CreateList (structNode *head,intNumber ) { //Creating a temporary node for our initialization purpose//The below line allocates a space in memory so can hold the data in a node. structNode *temp = (structNode *)malloc(sizeof(structnode)); //assigning the number to dataTemp--Data =Number ; //assigning the next of this node to NULL, as a new list is formed.Temp--Next =NULL; //Now since we had passed head as the parameter, we need to return it.Head =temp; returnhead;}
Add new elements
Adding new elements to the list is added by default to the tail of the list. We'll discuss what we've added to the middle or the beginning of the list in future content.
To add an element, there are two things:
- The list already exists so that we simply traverse the entire list and add the new element to the end of the chain.
- List does not exist, we can use the above function to create a linked list.
Traversing a linked list
Traversing the list is simple, Lian Peu head always points to the first element of the list. If we do this:
Head = head Next;
Head moves a node to the end of the chain.
The end of the list is denoted by null. So all we have to do is execute this statement until NULL is encountered, which means that the traversal is complete.
/*Defining a case if we need to element in the Linked List.here 2 cases can arise:--the Linked List can be empty, Then we need to create a new list-the Linked list exists and we need to add the element*/structNode * AddElement (structNode *head,intNumber ) { if(Head = =NULL) Head= CreateList (head, number);//we can directly call the above function Else { //Now the is a case when we need to add an element to an existing list. //Creating A new node and assigning values structNode * temp = (structnode*)malloc(sizeof(structnode)); Temp-Data =Number ; TempNext =NULL; //Now we had created the node but, we need to insert it at the right place. //a Linked List terminates when we encounter A NULL//Let us traverse the List using another temporary variable. //We 'll point it to the start structNode * Temp2 =Head; //the limiting condition of the while loop "until Temp2 ' s NEXT are not equal to NULL//This would happen the last node of the list. while(Temp2-Next! =NULL) { //We need to go to the next nodeTemp2 = Temp2Next; } //Now Temp2 points at the last node of the list. //We can add the node at this position now.Temp2Next = temp;//The number is added to the end of the List. } returnHead//because we only need the HEAD pointer for a Linked List.}
Print linked list
Similar to traversing a list, the difference is that we are printing elements while moving toward the end of the chain.
/*A function to print the Linked list.the return type of this function is void, as we do not need to return Anythin G.we just need the HEAD as the parameter, to traverse the Linked List.*/voidPrintlist (structNode *head) { //The terminating point of a Linked List was defined when we encounter a NULL while(Head! =NULL) {printf ("%d",head->data); //Now we need to move to the next elementHead = head->Next; }}
The complete code that includes the main function is given:
#include <stdio.h>#include<stdlib.h>//creating the basic structure of a node of a Linked Liststructnode{intdata; structNode *next;};/*defining a function to create a new list. The return type of the function is struct node, because we'll return the head nodethe parameters would be the node, passe D from the main function. The second parameter would be the data element.*/structNode * CreateList (structNode *head,intNumber ) { //Creating a temporary node for our initialization purpose//The below line allocates a space in memory so can hold the data in a node. structNode *temp = (structNode *)malloc(sizeof(structnode)); //assigning the number to dataTemp--Data =Number ; //assigning the next of this node to NULL, as a new list is formed.Temp--Next =NULL; //Now since we had passed head as the parameter, we need to return it.Head =temp; returnhead;}/*Defining a case if we need to element in the Linked List.here 2 cases can arise:--the Linked List can be empty, Then we need to create a new list-the Linked list exists and we need to add the element*/structNode * AddElement (structNode *head,intNumber ) { if(Head = =NULL) Head= CreateList (head, number);//we can directly call the above function Else { //Now the is a case when we need to add an element to an existing list. //Creating A new node and assigning values structNode * temp = (structnode*)malloc(sizeof(structnode)); Temp-Data =Number ; TempNext =NULL; //Now we had created the node but, we need to insert it at the right place. //a Linked List terminates when we encounter A NULL//Let us traverse the List using another temporary variable. //We 'll point it to the start structNode * Temp2 =Head; //the limiting condition of the while loop "until Temp2 ' s NEXT are not equal to NULL//This would happen the last node of the list. while(Temp2-Next! =NULL) { //We need to go to the next nodeTemp2 = Temp2Next; } //Now Temp2 points at the last node of the list. //We can add the node at this position now.Temp2Next = temp;//The number is added to the end of the List. } returnHead//because we only need the HEAD pointer for a Linked List.}/*A function to print the Linked list.the return type of this function is void, as we do not need to return Anythin G.we just need the HEAD as the parameter, to traverse the Linked List.*/voidPrintlist (structNode *head) { //The terminating point of a Linked List was defined when we encounter a NULL while(Head! =NULL) {printf ("%d",head->data); //Now we need to move to the next elementHead = head->Next; }}//defining the MAIN FUNCTIONintMainvoid){ structNode * Listhead =NULL; Listhead= CreateList (Listhead, the);//creating a new List with starting number//Adding a few number to the listListhead = AddElement (Listhead, to); Listhead= AddElement (Listhead,98); Listhead= AddElement (Listhead, -); //finally printing our Linked Listprintlist (Listhead); return 0;}
View Code
Basic operation of the linked list (basic Operations on a Linked List)