EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Linked List in Data Structure

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » Data Structures Tutorial » Linked List in Data Structure

Linked List in Data Structure

Introduction to Linked List in Data Structure

A linked list is a type of data structure that stores data in the form of a list of nodes. Each node has two parts. The first part stores the data element and the second part stores the reference to the next node. A linked list is a compelling data structure and helps in effective and efficient memory management.

How to Perform Operations on Linked List?

Operations like insertion and deletion can be efficiently performed for a linked list. Both insertions, as well as deletion, can be done from the beginning as well as the ending. We shall see how insertion and deletion happen for both cases through programs. Let’s go through the following programming code implemented in C language to understand linked list insertion and deletion operations.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
int d;
struct node *next;
};
struct node *start = NULL;
void insertbegin(int);
void insertend(int);
void display_list();
void delete_begin();
void delete_end();
int c = 0;
void main ()
{
int i, d;
for (;;) {
printf("\n1. Beginning Insert.\n");
printf("\n2. End Insert.\n");
printf("\n3. Display linked list.\n");
printf("\n4. Beginning Delete.\n");
printf("\n5. End Delete.\n");
printf("\n6. Exit\n");
scanf("%d", &i);
if (i == 1)
{
printf("\nEnter an element for beginning insertion: ");
scanf("%d", &d);
insert_begin(d);
}
else if (i == 2)
{
printf("\nEnter an element for ending insertion: ");
scanf("%d", &d);
insert_end(d);
}
else if (i == 3)
display_list();
else if (i == 4)
delete_begin();
else if (i == 5)
delete_end();
else if (i == 6)
break;
else
printf("\nInvalid input.");
}
getch();
}
void insert_begin(int x)
{
struct node *t;
t = (struct node*)malloc(sizeof(struct node));
t -> d = x;
c++;
if (start == NULL) {
start = t;
start -> next = NULL;
return;
}
t -> next = start;
start = t;
}
void insert_end(int x)
{
struct node *t, *temp;
t = (struct node*)malloc(sizeof(struct node));
t -> d = x;
c++;
if (start == NULL)
{
start = t;
start -> next = NULL;
return;
}
temp = start;
while (temp->next != NULL)
temp = temp->next;
temp -> next = t;
t -> next = NULL;
}
void display_list()
{
struct node *t;
t = start;
if (t == NULL)
{
printf("There is no element in the linked list.\n");
return;
}
printf("There are %d elements in linked list.\n", c);
while (t->next != NULL)
{
printf("%d ", t->d);
t = t->next;
}
printf("%d ", t->d);
}
void delete_begin()
{
struct node *t;
int k;
if (start == NULL)
{
printf("There's no element in the list.\n");
return;
}
k = start -> d;
t = start -> next;
free(start);
start = t;
c--;
printf("%d deleted from the beginning of the linked list.\n", k);
}
void delete_end()
{
struct node *t, *v;
int k;
if (start == NULL)
{
printf("Linked list is empty.\n");
return;
}
c--;
if (start->next == NULL)
{
k = start->d;
free(start);
start = NULL;
printf("%d deleted from the end of the linked list.\n", k);
return;
}
t = start;
while (t->next != NULL) {
v = t;
t = t->next;
}
k = t -> d;
v -> next = NULL;
free(t);
printf("%d deleted from end successfully.\n", k);
}

When we execute the program, we get options, as shown in the screenshot below. We have to enter the requisite option number for performing that operation.

Output:

Linked List in Data Structure in1

Code Explanation: We implemented the above code and executed it to check how it works. Going through the code, we can find that the program asks the user to enter one of the six choices viz. first inserting an element at the beginning of the linked list, second inserting an element at the end of the linked list, third displaying the linked list, fourth deleting an element from the beginning of the linked list, fifth deleting an element from the end of the linked list, and lastly to exit from the program. The infinite loop continues, till the user keeps passing proper input.

In case of exit the program breaks. As can be seen, the program makes heavy use of pointers which is the most important element of a linked list. We verified the above programming code and validated if it works well through a series of executions. The various inputs and the results returned are shown by the following screenshots. Let’s go through each of them,

Let’s begin by entering certain elements at the beginning of the linked list.

Popular Course in this category
Sale
All in One Data Science Bundle (360+ Courses, 50+ projects)360+ Online Courses | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (3,220 ratings)
Course Price

View Course

Related Courses
Oracle DBA Database Management System Training (2 Courses)SQL Training Program (7 Courses, 8+ Projects)

Input 1:

So, we entered 1 as the option. It refers to inserting an element at the beginning of the linked list. We passed 23 as the input which successfully got inserted in the beginning.

Linked List in Data Structure in2

Input 2:

Likewise, we inserted another element which is 44 the beginning of the linked list as shown by the screenshot below.

Linked List in Data Structure in3

Input 3:

We entered option 2, which refers to inserting an element at the end of the linked list. We inserted 89 at the end of the linked list as can be seen in the below screenshot.

Linked List in Data Structure in4

Input 4:

Similarly, we inserted another element, 57 at the end of the linked list as shown by the following screenshot.

Linked List in Data Structure in5

Input 5:

Again, we inserted another element at the beginning of the linked list. This time we inserted 77 as can be seen in the below screenshot.

Linked List in Data Structure in6

Input 6:

Now, we chose option 3, which is for displaying the linked list. As can be seen below, the linked list now has five elements.

Linked List in Data Structure in7

Input 7:

We will now delete some elements. This time option 4 was chosen and 77 stored at the beginning got deleted from the list.

in8

Input 8:

Similarly, we deleted another element from the beginning of the linked list.

 in9

Input 9:

Now, we chose option 5 to delete an element from the end of the linked list.

10

Input 10:

Finally, we displayed the linked list. As can be seen below, the list now contains only two elements.

11

Conclusion – Linked List in Data Structure

Linked list, as a data structure, is a very complex concept. The use of the concept happens in large-scale applications where memory usage is important. There are various ways in which the linked list can be used, and the context often governs the implementation.

Recommended Articles

This is a guide to Linked List in Data Structure. Here we discuss a basic concept, how to perform operations in the linked list, and insertion and deletion operation. You may also look at the following articles to learn more-

  1. Arrays in Data Structure
  2. Phases of Data Analysis Process
  3. Types of Data Structures
  4. Data Lake Architecture

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Data Structures Tutorial
  • Basics
    • Linked List Advantages
    • What is Data Structure
    • Heap Data Structure
    • Types of Trees in Data Structure
    • AVL Tree in Data Structure
    • B Tree in Data Structure
    • B+ Tree in Data Structure
    • DFS Algorithm
    • BFS Algorithm
    • Arrays in Data Structure
    • Graph in Data Structure
    • Graph Representation
    • Breadth First Search
    • Depth Limited Search
    • Hashing in Data Structure
    • Searching in Data Structure
    • Linear Search in Data Structure
    • Linked List in Data Structure
    • Doubly linked list in Data Structure
    • Circular Linked List in Data Structure
    • Pointers in Data Structure
    • Types of Graph in Data Structure
    • Bubble Sort in Data Structure
    • Quick Sort in Data Structure
    • Bitonic Sort
    • Merge Sort in Data Structure
    • Selection Sort in Data Structure
    • Insertion Sort in Data Structure
    • Radix Sort in Data Structure
    • Stack in Data Structure
    • Queue in Data Structure
    • Priority Queue in Data Structure
    • Asymptotic Analysis
    • Tree Traversal in Data Structure
    • Tree Traversal Techniques
    • Trie Data Structure
    • Splay Tree in Data Structure
    • Spanning Tree Algorithm
    • Sparse Matrix in Data Structure
    • Radix Sort Algorithm
    • Counting Sort Algorithm
    • Skip List Data Structure
    • Linked List Algorithm
    • Linked List Types
    • Inorder Traversal of Binary Tree
    • Kruskals Algorithm
    • Prims Algorithm
    • BFS VS DFS
    • BCNF
    • Skip List
    • Hash Table?in Data Structure
    • Data Structure Interview Questions
    • Data Structures & Algorithms Interview
    • AVL Tree Deletion
    • B+ Tree Deletion
    • Decision Tree Advantages and Disadvantages
    • Data Architect Skills
    • Data Architecture Principles
    • Data Engineer Jobs
    • Data Engineer Roadmap
    • Fundamentals of Data Structure
    • Circular queue in Data Structure
    • Spanning Tree in Data Structure
    • Tree traversal types
    • Deque in Data structure
    • Shell Sort in Data Structure
    • Heap sort in data structure
    • Heap data structure C++
    • Heap data structure in Java
    • Binary Search Tree Types
    • Binary Tree in Data Structure
    • Binary Tree Types
    • Binary search tree in data structure
    • Binary Search Tree Advantages
    • Binary Search Tree Properties
    • Binary Search in Data Structure
    • Binary Tree Deletion
    • Sparse Matrix Multiplication
    • Preorder Traversal of Binary Tree
    • Postorder traversal
    • Decision Tree Hyperparameters
    • PostOrder Traversal without Recursion
    • AVL Tree Rotation
    • Avro File Format
    • Decision Tree Types
    • Binomial heap
    • Confluence Jira Integration
    • Timm Sort
    • Depth First Search

Related Courses

All in One Data Science Course

Oracle DBA Course

SQL Certification Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Independence Day Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More