Linked List in Data Structures

Ratings:
(4)
Views:640
Banner-Img
  • Share this blog:

Linked List

 

The term “list” refers to a linear collection of data items. One way to store such data is by means at “array” . The array implementation of list hap certain draw backs there are

  1. Memory storage space is wasted, very often the list is much stories than the array size declared.
  2. List cannot grow in its size beyond the size at the declared array i.e required during paragraph execution.
  3. Operations like insertion and declaration as a specified location in  a list requires a lot of movement of data, Therefore leading to inefficient and time containing algorithm
  4. Another way of storing a list in memory is by means of linked list. Each element in the list contain a field, called a link fields, Which contains the address of the next elements in the list need not occupy adjacent space in memory. Thus will make it easier to insert and delete element in the list.

   

Advantages of linked list over arrays:

  1. Linked list is a dynamic structure where as array is a static structure. That is the size of the array is fixed, it cannot be increased or decreased during execution of a program.
  2. In portion and deletion of nodes require no data movement
  3. Joining two arrays is very tedious process because these is lot of data movement, where of joining two linked list is very easy.

Types of linked list :

These are 3 types.

  1. Singly linked list
  2. Doubly linked list
  3. Circular linked list

 

Singly linked list:

  • A singly linked list or one way list is a linear collection of data elements, called nodes
  • Here each node is dividing into two fields.
  • The first filed containing the information of the element, and the second field contains the address in the next node in the list.
  • The first filed is called “Data field ” and the second field is called “Link field”

  30  

  • The left part represents information part of the node the right part represents the link field of the node.
  • The pointer to the last node contains a special value called “NULL” pointer.
  • Linked list also contains a pointer variable, called HEAD, which points to the first node of the list

  31  

Inserting at an element in list:     

  1. Inserting at the beginning of the list :
  • Suppose a node N is to be inserted into the list at beginning as Shan in the fig. The simplest strategy for an item at the beginning at the list ip to:
  1. Allocate space for a new node.
  2. Copy the item to be inserted into it.
  3. Make the new nodes next pointer point to the current head of the list
  4. Make the HEAD of the list point to the newly allocated node.
  1. cur -> Link = Head
  2. Hand = Cur;

  32    

Insertion After a give node :

To insert a node in the linked list we need to specify the element to be inserted and the element in the list after which the element has to be inserted.

  1. Search for the element after which the insertion is to be done.
  2. Create a memory space for element to be inserted.
  3. Assign the data value its data field.
  4. Assign is a pointer to pointer of the element searched at step 2
  5. Assign pointer of the element searched at step 2to this new element started.

   

  1. cur    - >  Link      = ptr -> Link;
  2. ptr     - > Link       =cur;

  33  

Deletion Operation :

Deleting Beginning Node:

  1. Store the first “link field” in the “HEAD” pointer
  2. Disappear the memory space held by the deleted element

Head=Head - > Link free(otr); 34  

Deleting a specified node:

 

  1. Search for the element with key value same as the element to be deleted.
  2. Its pointer to next element be saved and late assigned to next element of predecessor of the element searched at step.
  3. Dispose the memory space held by the deleted element.

ptr1=ptr àlink; ptr à link= ptr1 àlink; free(ptr1);   35  

Linked List Program

Implementation of singly linked list # include <studio.h> #include <conio.h> #include<stdlib.h> struct Node { int Data; struct Node *link; }; struct Node*Head; void createList() { int else; struct Node*ptr,*cur; Head=NULL; printf(“Enter Data for the Node -1): “); scanf(“%d”,&ele); while(ele!=-1) { cur=(struct Node*)malloc (size of (structNode)); cur 7 Data=ele; cur 7Link=NULL; if (Head==NULL) Head=cur; else ptr 7link =cur; ptr=cur; printf(“Enter Data For The Node -1):”); scanf(“%d”,&ele); } } void Insertion() { int ele,x; char ch; struct Node*ptr,*cur; ptr=Head; cur=(structNode *) malloc (size of (structNode)); printf(“Enter Data for the New Node:”); scanf(“%d”,&x); cur 7 Data=x; cur 7Link=NULL; printf(“Do you want to insert first(y\n:”); fflush(stdin); scanf(“%c”,&ch); if((ch==’y’) || (ch==’n’)) { cur 7 link =Head; Head =cur; } else { printf(“After which element you want to Insert:”); scanf(“%d”,&ele); while(ptr!=NULL) { if(ptr 7 Data ==ele) { cur 7 Link =ptr à Link; ptr 7 Link= cur; break; } else { ptr =ptr 7Link; } } } } void Deletion () { int ele; char ch; struct Node*ptr,*ptr1; ptr=Head; printf(“Do you want to Delete First (Y/n):”); scanf(“%c”,&ch); if(ch==’y’|| ch==’n’) { Head=Head 7Link; free(ptr) } else { printf(“ which element you want to delete:”); scanf(“%d”, &ele); while(ptr!=NULL) { if(ptr7 Link àData==ele) { ptr1=ptr 7Link; } } } } void Display() { struct node*ptr; ptr=Head; if(ptr==NULL) { printf(“List is Empty \n”); } else { while(ptr!=NULL) { printf(“%5d”,ptr Data); ptr=ptr 7Link; } } } void main() { int choice; createlist() printf(“1.INSERTION \n”); printf(“2.DELETION  \n”); printf(“3.DISPLAY  \n”); printf(“4.EXIT \n”); do { printf(“Enter your choice:”); scanf(“%d”,&choice); switch (choice) { { case1: insertion (); break; case2: deletion (); break; case3: Display(); break; case4: exit(0); } } while (choice<=4); getch(); }    

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox