DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Doubly Linked List in Data Structures and Algorithms
  • Queue in Data Structures and Algorithms
  • Troubleshooting Memory Leaks With Heap Profilers
  • Geo-Zoning Through Driving Distance Using K-Medoids Algorithm

Trending

  • How to Practice TDD With Kotlin
  • DGS GraphQL and Spring Boot
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Ethical AI in Agile
  1. DZone
  2. Data Engineering
  3. Data
  4. Linked List in Data Structures and Algorithms

Linked List in Data Structures and Algorithms

This blog delves deeper into the implementation of linked lists, their relevance to data structures, and how they are applied in real-world scenarios.

By 
Amrutha TESR user avatar
Amrutha TESR
·
Jul. 18, 23 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
4.6K Views

Join the DZone community and get the full member experience.

Join For Free

In the previous blog, we looked into different types of linear data structures. A linked list is also one of them. The program for linked lists seems to be lengthy, but once we are aware of the concept, It's easy to implement it. Linked lists are unlike stack and queues as it stores a pointer pointing to the address. For example, while we are listening to music on Spotify, we can create our song playlist and can play a song from both starting and end of the list. These music players are implemented using a linked list. 

In this blog, we further discuss linked list implementation, how it is related to data structures, and its real-time applications. 

What Is a Linked List?

A linked list is a linear data structure that includes a sequence of nodes connected. Here, a node contains two fields, i.e., data stored at that particular address and the pointer, which contains the address of the next node in the memory. We use dynamic memory allocation here instead of arrays, as its size is modifiable during the runtime.

Linked List

Here we can see the first node is pointing toward the second node, the second node points third node, and so on. The head always points to the first node; for any operations, the head node changes accordingly. The last node has no node to point and contains a NULL value.

Let's now see how we can implement a single linked list using c.

We can perform various operations on a single linked list, and they are:

1. Adding Node at the Front

To add the node at the front, first, create a dynamic memory and allocate the value to the new node. Then point the new node toward the head, and store the new node's address in the head. In this way, we can add the node at the front.

C
 
void ins_beg(){
  
      struct node *newnode = (struct node*)malloc(sizeof(struct node));
      // allocate memory to new node.
      newnode->data=item; // store value in data.
  
      newnode->next=head; // store address of head in next
  
      head = newnode;


   }


2. Deleting Node at the Front

To delete the node from the front, we have to store the 2nd node's address in the head.

C
 
void del_beg(){
      struct node *newnode;
         newnode = head;
         head= newnode->next;
  
         free(newnode); // free the node using free()

}


3. Adding Node at the End

It is similar to adding a node at the front, but we have to point the last node to the new node and store the value. The new node inserted at the end contains a NULL  value in the address node.

C
 
void ins_end(){
      // allocate memory for new node.

      temp = head;
       while(temp->next !=NULL)
        {
             temp = temp->next;
        }
       // traverse till you reach the last node.
       temp -> next = newnode;
       newnode-> next = NULL;  // NULL 
      }


4. Deleting Node at the End

To delete a node at the end, we must traverse the list and reach the end. We will use a pointer pointing toward the last 2nd node. Then free the last node.

C
 
id del_end(){
     temp = head;
  
     while(temp !=0)
     {
         temp = temp->next;  // traverse till the end
     }                        
     ptr1 = temp;     // ptr1 points the last 2nd node
     temp = ptr1->next;
     ptr1->next = NULL;
     free(temp);
}


5. Searching

We use it for searching for an element from the list.

C
 
void search(){
   struct node *temp;
   temp = head;

   while(temp != NULL){
       if(temp->data == key){
            //found
          }
       else
          {
           //not found
          }
        temp = temp->next;
   }
}


We can create many other operations using a linked list.

Now, let's see the implementation of singly linked lists using the c program:

C
 
#include<stdio.h>
#include<stdlib.h>

struct node{
     int data;
     struct node* next;
};
struct node *head;
void ins_beg();
void ins_end();
void del_beg();
void del_end();
void display();
int search();
void main(){
     int choice=0;
     while(choice!=7)
     {
         printf("enter choice:\n");
         printf("\n1.ins_beg\n2.ins_end\n3.del_beg\n4.del_end\n5.display\n6.search\n7.exit");
         scanf("%d", &choice);

         switch(choice)
         {
             case 1:
                ins_beg();
                break;
             case 2:
                ins_end();
                break;
             case 3:
                del_beg();
                break;
             case 4:
                del_end();
                break;
             case 5:
                display();
                break;
             case 6:
                search();
                break;
             case 7:
                printf("exiting...\n");
                break;
             default:
             printf("unknown choice\n");
        }
    }
}
void ins_beg(){
      int item;
      struct node *newnode;
      newnode = (struct node*)malloc(sizeof(struct node));
      printf("enter item:");
      scanf("%d", &item);


        newnode->data=item;
        newnode->next=head;
        head = newnode;
        printf("Inserted\n");

    }
void ins_end(){
      struct node *temp,*newnode;
      int item;
      newnode= (struct node*)malloc(sizeof(struct node));
      printf("enter item");
      scanf("%d", &item);
      newnode->data = item;
      if(head==NULL)
          {
              newnode->next =NULL;
              head = newnode;
              printf("inserted");
          }
      else
          {
              temp = head;
              while(temp->next !=NULL)
              {
                  temp = temp->next;
              }

              temp -> next = newnode;
              newnode-> next = NULL;
              printf("inserted");
          }
      }
void del_beg(){
      struct node *newnode;
      if(head == NULL)
         printf("empty\n");
      else
      {
         newnode = head;
         head= newnode->next;
         free(newnode);
         printf("deleted\n");
      }
}
void del_end(){
     struct node *temp,*ptr1;
     temp = head;

     if(head==NULL)
     {
         free(head);
         printf("deleted");
     }
     else{
     while(temp !=0)
     {
         temp = temp->next;
     }
     ptr1 = temp;
     temp = ptr1->next;
     ptr1->next = NULL;
     free(temp);
     printf("deleted");
}
}
void display(){
   struct node *newnode;
   newnode = head;
   if(newnode==NULL)
      printf("empty");
   else
   {
      printf("values are:\n");
       while(newnode!=NULL)
       {
           printf("%d", newnode->data);
            newnode = newnode ->next;
       }
   }
}
int search()
{
    struct node *ptr;
    int item,flag;
    ptr = head;
    if(ptr == NULL)
    {
        printf("\nEmpty List\n");
    }
    else
    {
        printf("\nEnter item which you want to search?\n");
        scanf("%d",&item);
        while (ptr!=NULL)
        {
            if(ptr->data == item)
            {
                printf("Found");
                flag= 1;
            }
            else
            {
                flag=2;
            }
            ptr = ptr -> next;
        }
        if(flag==2)
        {
           printf(" not  found\n");
        }
    }
}


The output for the program is as follows:

Till now, we discussed the implementation of a linked list. But where do we use it?? 

Applications of Linked Lists

  • Play Next Track: The pointer to the next node makes it easy to start the next track when a track is over. 
  • Forward: Linked lists have a pointer that points toward the next node; it is easy to implement skip-forward functionality. We can also implement skip backward using a doubly linked list which we'll see in the next blog.

Conclusion

This blog gives us insight into the usage of a single linked list and its implementation. We can further learn about double-linked lists and circular-linked lists. I hope you enjoyed reading this blog. Please do like and comment on your views on today's topic. Happy learning!!

Do check my previous blogs on data structures, System integration using APIs:

  • Stack in Data Structures

  • Queue in Data Structures and Algorithms 

  • Build RAML-Based API Specification Using MuleSoft Platform

  • Publishing API to Anypoint Exchange Using MuleSoft Platform

API Data structure Algorithm Data (computing) Memory (storage engine) Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Doubly Linked List in Data Structures and Algorithms
  • Queue in Data Structures and Algorithms
  • Troubleshooting Memory Leaks With Heap Profilers
  • Geo-Zoning Through Driving Distance Using K-Medoids Algorithm

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!