We need a C file to add our code to it. After opening the console terminal application, write the “touch” instruction on it and press the “Enter” button from the keyboard to make a C file as follows:
Now the “linklist.c” file must have been created in the home directory. Open it up by using any editor installed on your system. We have GNU nano editor configured on our Ubuntu 20.04. Therefore, we have been using it to open the file as follows:
The headers have been added at the top to make the code working, e.g., standard input-output stream. Created a structure type node having integer type variables to tell us about the node, and a structure type pointer has been used to store the address of the next node.
The main method has been initialized and declared a variable. Some of the print statements have been used here to ease a user using the link list. Users will add several nodes for a linked list, and this number will be stored in the variable “x”. The function “List node()” has been called here and is used to form a Linked list. The variable value will be passed to this method by calling it here, and another display message will be shown. After the execution of the “Listnode()” method, the control has been passed to the “show()” method.
The method “Listnode()” has been defined here. It creates a linked list and assigns it a memory by the “malloc” method. If the first node of the list is empty, it will display a message that no memory has been assigned yet.
Otherwise, the system may ask you to enter the data for the first node. The user-added data will be stored. The “for” loop has been utilized here to create the next first node. If the first node is still Null, it displays a message. Else, it will ask the user to enter the data for nodes until the “for” loop ends.
The method “show()” has been used here to display the data of the linked list on the terminal display.
Compilation and execution of file “linklist.c” has given us the expected output. The user added several nodes for the formation of the linked list. Then, the user has added data in 5 nodes, and the system displays the data.
This example is quite similar and easy as well. After opening the same file, define the two functions after headers. The struct type node has been created as per the output image below. The first node has been declared NULL.
The main method has been defined. The menu system has been displayed on the screen by using the printf statements. Three options have been provided to create, display the linked list or simply quit the menu. The system has asked the user to enter their option choice by tapping 1, 2, or 3. The switch statement has been utilized here to jump over to the option entered by the user, e.g., create a linked list, display a linked list, or exit.
After the main method ends, we have implemented the “create()” method upon choice by the user as option 1. The create method has firstly checked if the first node is empty or NULL. If the node is NULL, it displays that memory has not to be assigned. If this is not the case, the system asks the user for data to enter at run time.
The method show() has been declared and implemented at last to display the node elements/values added by the user in the above creation method.
In the end, the code has been compiled and executed. The menu has been displayed. The user added his choice by tapping 1, 2, and 3 one after another to create, display a list, and then exit the menu.
Our article contains two simple and easy-to-implement examples in Ubuntu 20.04 to Create a linked list in C language. This article contains the displays of data for a linked list along with the insertions. We have elaborated two sample examples for the formation of a linked list in the C programming language.