EDUCBA

EDUCBA

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

Linked List in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Linked List in C

LinkedList in c

Introduction to Linked List in C

As the name suggests linked list means linking lists together or we can say that a linked list is the sequence of data structures that are connected to each other via links. Linked list use pointer for its implementation in the data structure. It’s a linear data structure in which data is stored at different locations and linked using pointers. Linked list node has two parts one is the data part and the other is the address part which has many advantages in insertion and deletion of the element from a particular position without wasting any time because it saves memory space dynamically by changing size.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Let’s have a look at the syntax of representing a linked list in your code:

struct node {
Int data ;
struct node *next ;
} ;

In the above-linked list syntax struct is the mandatory keyword to be used because with help of structure we can create custom data structure and as it is a node so node keyword is used so we are creating a data structure and in node, we have two parts, one is integer data part while other is the pointer which we address to the next node in the list.

Basically, the linked list node has two parts:

  • A data part: It will contain the data of the user
  • A pointer part: It will always point to the next member of the linked list in code.

How Linked List work in C?

Now we will discuss the working of the linked list through C code with a brief explanation. Here is the C code to demonstrate the working of the linked list:

Code:

#include <stdio.h>
#include <stdlib.h>
struct node {
int data ;
struct node *next ;
};
struct node *start = NULL ;
void insert_begin(int) ;
void insert_end(int) ;
void traverse() ;
void delete_begin() ;
void delete_end() ;
int count = 0 ;
int main () {
int x, data ;
for (;;) {
printf("1. Want to insert an element at the beginning of linked list.\n") ;
printf("2. Want to insert an element at the end of linked list.\n") ;
printf("3. Want to traverse linked list.\n") ;
printf("4. Want to delete an element from beginning.\n") ;
printf("5. Want to delete an element from end.\n") ;
printf("6. Exit\n") ;
scanf("%d", &x) ;
if (x == 1) {
printf("Enter value of element\n") ;
scanf("%d", &data) ;
insert_begin(data) ;
}
else if (x == 2) {
printf("Enter value of element\n") ;
scanf("%d", &data) ;
insert_end(data) ;
}
else if (x == 3)
traverse() ;
else if (x == 4)
delete_begin() ;
else if (x == 5)
delete_end() ;
else if (x == 6)
break ;
else
printf("Please enter valid input.\n") ;
}
return 0 ;
}
void insert_begin(int i) {
struct node *t ;
t = (struct node*)malloc(sizeof(struct node)) ;
t -> data = i;
count++ ;
if (start == NULL) {
start = t ;
start->next = NULL ;
return ;
}
t->next = start ;
start = t ;
}
void insert_end(int i) {
struct node *t, *temp ;
t = (struct node*)malloc(sizeof(struct node));
t -> data = i;
count++ ;
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 traverse() {
struct node *t ;
t = start ;
if (t == NULL) {
printf("Linked list is empty.\n") ;
return ;
}
printf("There are %d elements in linked list.\n", count) ;
while (t->next != NULL) {
printf("%d\n", t->data) ;
t = t->next ;
}
printf("%d\n", t->data); // Print last node
}
void delete_begin() {
struct node *t ;
int n ;
if (start == NULL) {
printf("Linked list is empty.\n") ;
return ;
}
n = start->data ;
t = start->next ;
free(start) ;
start = t ;
count-- ;
printf("%d deleted from the beginning successfully.\n", n) ;
}
void delete_end() {
struct node *t, *u ;
int n;
if (start == NULL) {
printf("Linked list is empty.\n") ;
return ;
}
count-- ;
if (start->next == NULL) {
n = start->data ;
free(start) ;
start = NULL ;
printf("%d deleted from end successfully.\n", n) ;
return ;
}
t = start ;
while (t->next != NULL) {
u = t ;
t = t->next ;
}
n = t->data ;
u->next = NULL ;
free(t);
printf( "%d deleted from end successfully.\n ", n) ;
}

Output:

Linked List in C -1

Explanation:

In the above code, we have created a node and we have also created the address part of a node. We have added 5 main functionality of linked that help in performing all kinds of possible operations in our code. So we have declared insertion, deletion operations at the beginning as well as the end of the linked list. One more function is declared for traversing the list linked together.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

In the main class, we have declared two variables of integer data type as “I” and “data”. After declaring all the functionalities in beginning we will implement an if-else loop so that we can switch between the mentioned functionalities. For all the 5 functionalities we are implementing the logic in our code through an algorithm.

For insertion in the list at the beginning, we created a node t and in its data part, we named it as x. Therefore, if the start is null then start will be put in node t data part and the address part will point to the next part which is NULL. This process will insert the element at the beginning. In the same way, we have defined the logic for insertion and deletion at the start and end of the linked list in our code.

Conclusion

Memory utilization plays a crucial role in the complex as well as simpler codes. Therefore, with the help of Linked list memory utilization can be done easily and efficiently. Most useful when we don’t have an idea about the size of the list. It can grow and shrink accordingly.

Recommended Articles

This is a guide to Linked List in C. Here we discuss the introduction to Linked List in C, along with the syntax, how Linked List work and an example to implement. You can also go through our other related articles to learn more –

  1. LinkedIn Website
  2. LinkedIn Profile Page
  3. LinkedList in Java
  4. LinkedList in JavaScript

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

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

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

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

EDUCBA

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

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More