Hello š
As promised before, here I go with a tutorial on creating a singly linked list using simple c programming. I wanted to start tutorials on embedded C programming but just to have a touch and feel of C programs I go with few programming posts from now.
Basically you need the follow basic knowledge for creating a linked list.
1)What are structures? how are the accessed in C?
2)Pointers, a Very basics like what are pointers? how to use them to access structure members??
3)Dynamic memory allocation, what it is?? how is it done???
Anyways, before I start about the singly linked list tutorial,Lets go through the basic we need for understanding the concept of the linked list
Lets start with structure,
Structure is basically a collection of one or more variable. For example, college maintains details of student like Name, University No, Gender, your dept etc etc.
Here Name is a string or array of characters, University no is a integer etc Like wise if we want to a group with differ variable of different data types we define a structure.
Sample of the structure template
struct s_details
{
int USN;
char Gender;
};
The struct is the keyword to define a structure template. Once the structure template is defined the memory is not assigned, but the after structure variables the memory is assigned. Sample struct templates and the way the variables can be defined is shown below.
struct s_details
{
int USN;
char Gender;
}student1; Ā /*student1 is a variable that is defined just after the structure template before ;*/
OR
struct s_details
{
int USN;
char Gender;
};
main()
{
struct s_details student1; /*here its defined as a local variables like others*/
The student1 is a variable of type struct s_details and USN,Gender are the members of the struct s_details structure.
once the structure is defined we should be able to access the structure members, we can access The structure member in two ways
1)By using the structure variable.
2)By using pointer to the structure.
Lets have a look at the two type in a very quick way and look for creating a singly linked list.
1)By using structure variable.
Once the variable of type struct s_details is declared, here the variable is student1
we can use the operator “.” to access the member.
Example1 : I wanna assign the value 27 for the member USN.
student1.USN=27;
Ā /*this assign’s the value 27 to the student1’s member USN.*/
Example2 : Now If i wanna assign ‘M’ to the member Gender.
student1.Gender='M';/*
this assign’s M character to the student1’s member Gender.*/
2)By using Pointer to the structure.
Like the way we define a variable of type struct s_details
, we define a pointer to the structure.
struct s_details
{
int USN;
char Gender;
}student1,*temp; Ā /* (*temp) is a pointer of type struct s_details*/
As a basic, after declaring a pointer we need to tell the pointer about the address it has to point, That is done by the following.
<code>main()
{
temp=&student1; //gives the address of the student1 to the pointer temp</code>
Now u can access the structure members using this operator “->“.
Example1 : As the example before, I wanna assign the value 47 for the member USN.
temp->USN=42; /* assigns the value 47 to the struct member USN */
Example2 : If I want to assign the F to the Gender member.
temp->Gender=’F’ /*assigns ‘F’ character to the Member Gender*/
Finally these basic can drive you and will help you to understand the Linked List Code and logic.
Now, Coming to the linked list Concept its just a way Where in the memory is being allocated on During Run time of the program. Instead of you Declaring a variable and theĀ compilerĀ reserving the Memory location during compiling time, The memory is allocated During run time of the program. This isĀ referredĀ as the “DYNAMIC MEMORY ALLOCATION”.
The following Diagrams should give you a brief Logic on creating a Linked list in C.
The structure that is considered for linked list has the following
So as you can see on your right, The whole structure is from now termed as NODE, a terminology usually used in the linked list process. So the structure template is as follow
Struct Node
{
int data; // this is the data
struct node *addr; /* this is the pointer that points to the next location*/
};
So the node is ready for programming it as the Linked list.
what is the basic logic.??
The figure blow helps you to understand it better.
The Node contains a Data which is for our reference to check whether the programming is working or not, The ADDR in the node points to the next node’s Address. Similarly the next node points to the other one.But the First node is taken as a Reference and is referred as Header.
Now the dynamic memory allocation is done my malloc function in the program. So just for your information malloc is a function that is being called for memory allocation during the run time. And the end of linked List always made null.
With this details I give a sample Code which creates a linked list of 10 nodes.
/********************************************************************************************************************************
The program creates a linked list of 10 nodes, The program is run on gcc compiler!!
*********************************************************************************************************************************/
#include<stdio.h>
#include<stdlib.h> //headerfile to use malloc() function.
struct node
{
int a;
struct node *p;
}hdr;
main()
{
struct node *present,*previous;
int i;
previous=&hdr;
for(i=0;i<10;i++)
{
// present pointer points at the memory allocated by malloc.
present=malloc(sizeof (struct node));
//new address is assigned to the header (at i=0).
previous->p=present;
// the value is given to the data.
present->a=i;
// control is passed.
previous=present;
printf("the adress created is %p\n",present);
printf("the value present in the allocated memory is %d\n",present->a);
if(i==9)
{
// indicates the end of linked list.
present->p=NULL;
}
}
}
Thanks for reading My post. Hope it helped atleast some of you guys. I believe in sharpening the blade before I get into the cutting process, so Have focused on basics more.
The UP-Coming Tutorials will be … Adding a Node to linked list… deleting a node… Circular linked list..
Then will be on the MSP430 Board and basic of the embedded OS and Services Operated by the Operating system. Thanks again and the blog will be updated Regularly from now.