linked list using C Data Structure developerIndian.com

Updated:13/08/2023 by Computer Hope

Go Back



A linked list is a linear data structure in which elements are not stored at contiguous memory locations ,which is not similar to arrays. Instead, each element (or node) in a linked list is a separate object or Item and contains a reference (or pointer) to the next node in the sequence.
The list can be defined as an abstract data type in which the elements are stored in an ordered manner for easier and efficient retrieval of the elements.

  • Node: Each element in a linked list is represented by a node. Each node contains two fields: the data field to store the actual value or payload, and the next field to store a reference (pointer) to the next node in the sequence.
  • Head: The head of a linked list refers to the first node in the list. It serves as the entry point for accessing the elements of the list.
  • Dynamic size: Linked lists can grow or shrink in size dynamically during run time.
  • Efficient insertion and deletion: Insertion and deletion of elements can be done in constant time (O(1)) at the beginning or end of the list.
  • Optimising at run time : Unlike arrays, linked lists do not require contiguous memory allocation, making them more flexible for memory management.
//WAP to Implement Circular Link List
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct  node
	{
	int no;
	struct node * next;
	};
struct node *start=NULL,*start1=NULL;
void append( )
     {struct node *p=start , *n;
      n = ( struct node * ) malloc( sizeof(struct node ));
      printf("\n enter no : ");
      scanf("%d", &n->no );
      n->next = NULL;
      if( start == NULL )
	{start = n;
	 start->next=start;
	 return ;
	}
      while( p -> next != start)
	{ p=p->next ; }
      p->next = n ;
      n->next=start;
      }
void traverse( struct node *q)
     {
       struct node *r=q;
       do
	{ printf("  %d", q->no );
	  q=q->next ;
	}while(q!=r);
     }
void tra_f_node( struct node *p)
     {int val;
      if ( p == NULL)
	{
	printf("\n empty link node ");
	return;
	}
       printf(" \n Enter No from Which you want to search :");
       scanf("%d",&val);
       do
	{ if(val==p->no)
	    {
	    traverse(p);
	    return;
	    }
	p=p->next ;
	}while(p!=start);
       printf(" %d Not Found ");
      }
void search( )
     {
     struct node *p = start ;
     int val;
     if ( p == NULL)
	{ printf("\n empty link node ");
	  return;
	}
     printf("\n enter element to be searched :");
     scanf("%d" , &val);
     do
       { if ( val == p->no )
	    { printf("\n found ");
	      return ;
	     }
	 p = p->next ;
       }while(p!=start);
      printf("\n element not found ");
      }
void del_all()
     {
     struct node *p;
     do{
	p=start;
	start=start->next;
	free(p);
	}while(p!=start);
     }
void main( )
	{
	 int ch;
	 do
	 {
	 clrscr();
	 printf("\n 1 append          :");
	 printf("\n 2 traverse        :");
	 printf("\n 3 search          :");
	 printf("\n Remaining cases same as LINK LIST ");
	 printf("\n 0 Exit ");
	 printf("\n enter your choice :");
	 scanf("%d",&ch);
	 switch ( ch )
	    { case 1 :append();
		      break ;
	      case 2 :tra_f_node(start);
		      break ;
	      case 3 :search();
		      break ;
	      case 0  :del_all();
		       break;
	      }
	    printf("\n Press Any Key To Continue...");
	    getch();
	    }while( ch != 0 );
	  }



        

Type of Linked List

Conclusion

Pointers and structure can be used to create a linked list in the C programming language. struct LinkedList{struct LinkedList *next; }; int data; Each node in the list is created using the definition given above. The element is stored in the data field, and the location of the next node is stored in a pointer that follows.
In this article ,basic features of a singly linked list in C, including node creation, insertion, printing, and memory deallocation