Share this post on:

Now lets see that how we can Create a Linked List in C Language. A List can be created using the insertion. First time we will have to insert into an empty list, and then we will keep on inserting nodes at the end of the list. The case of insertion in an empty list reduces to the case of insertion in the beginning, so far inserting the first node we will call addatbeg(), and then for inserting of all other nodes we will call addatend().

Example:

struct node *create_list(struct node *start)

{

int i,n,data;

printf(“Enter the number of nodes : “);

scanf(“%d”,&n);

start=NULL;

if(n=0);

return start;

printf(“Enter the element to be inserted: “);

scanf(addatbeg(start,data);

for(i=2;i<=n;i++)

{

printf(“Enter the element to be inserted : “);

scanf(“%d”,&data);

start=addatend(start,data);

}

return start;

}

Deletion in a Linked List

For deletion of any node, the pointer are rearranged so that this node is logically removed from the list. To physically remove the node and return the memory occupied by it to the pool of available memory we will use the function free(). We will take a pointer variable tmp which will point to the node being deleted so that after the pointer have been altered we will still have address of that node in tmp to free it. There can be four cases while deleting an element from a linked list.

  1. Deletion of first node.
  2. Deletion of the only node.
  3. Deletion in between the list.
  4. Deletion at the end.

In all the cases, at the end we should call free(tmp) to physically remove node T from the memory.

Deletion of first node

Before deletion :

Node T is the first node

start points to node T

Link of node T points to node P.

After deletion :

Node P is the first node

start points to node P.

Since the node to be deleted is the first node, tmp will be assigned the address of first node.

tmp=start;

So now tmp points to the first node, which has to be deleted. since start points to the first node of linked list, start->link will point to the second node of linked list. After deletion of first node, the second node(node p) would become the first one, so start should be assigned the address of the node P as-

start=start->link;

After this statement, start points to node P so now it is the first node of the list.

Deletion of the only node

if there is only node in the list and we have to delete it, then after deletion the list would become empty and start would have NULL value.

Before deletion :

T is the only node

start points to T

link of T is NULL.

After deletion :

start is NULL

tmp=start;

start=NULL;

Deletion in between the list nodes

Before deletion

Link of P points to node T

Link of T points to node Q

After deletion

Node Q is after node P

Link of P points to node Q

Suppose node T is to be deleted and pointer tmp points to it and we have pointers p and q which points to nodes P and Q respectively. For deletion of node T we will just link the predecessor to T(node P) to successor of T(node Q).

p=>link=q;

The address of q is stored in tmp->link so instead of q we can write tmp->link in the above statement.

p->link=tmp->link;

The value to be deleted is in the node T and we need a pointer to its predecessor which is node P, so as in addbefore() our condition for searching will be if(p->link->info == data). here data is the element to be deleted.

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *