Table of Contents

Linked list in C: Linked list data structure is very useful and has many advantages. New element can be inserted at the beginning or at the end in a constant time (in doubly linked lists). Memory is utilized efficiently as it is allocated when we add new element to a list and we can increase a list size as desired till all of computer memory is exhausted. Linked lists are useful when size of a list is unknown and changes frequently. Linked list data structure uses node which is represented below. It contains data to be stored and pointer to next node.

 #include <stdio.h>
#include <stdlib.h>
 
struct node {
   int data;
   struct node *next;
};
 
struct node *start = NULL;
void insert_at_begin(int);
void insert_at_end(int);
void traverse();
void delete_from_begin();
void delete_from_end();
int count = 0;
 
int main () {
   int input, data;
   
   for (;;) {
      printf("1. Insert an element at beginning of linked list.\n");
      printf("2. Insert an element at end of linked list.\n");
      printf("3. Traverse linked list.\n");
      printf("4. Delete element from beginning.\n");
      printf("5. Delete element from end.\n");
      printf("6. Exit\n");
     
      scanf("%d", &input);
     
      if (input == 1) {
         printf("Enter value of element\n");
         scanf("%d", &data);
         insert_at_begin(data);
      }
      else if (input == 2) {
         printf("Enter value of element\n");
         scanf("%d", &data);
         insert_at_end(data);
      }
      else if (input == 3)
         traverse();
      else if (input == 4)
         delete_from_begin();  
      else if (input == 5)
         delete_from_end();
      else if (input == 6)
         break;
      else
         printf("Please enter valid input.\n");      
   }
   
   return 0;
}
 
void insert_at_begin(int x) {
   struct node *t;
   
   t = (struct node*)malloc(sizeof(struct node));
   count++;
     
   if (start == NULL) {
      start = t;
      start->data = x;
      start->next = NULL;
      return;
   }
   
   t->data = x;
   t->next = start;
   start = t;
}
 
void insert_at_end(int x) {
   struct node *t, *temp;
   
   t = (struct node*)malloc(sizeof(struct node));
   count++;
   
   if (start == NULL) {
      start = t;
      start->data = x;
      start->next = NULL;
      return;
   }
   
   temp = start;
   
   while (temp->next != NULL)
      temp = temp->next;  
   
   temp->next = t;
   t->data    = x;
   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);
}
 
void delete_from_begin() {
   struct node *t;
   int n;
   
   if (start == NULL) {
      printf("Linked list is already empty.\n");
      return;
   }
   
   n = start->data;
   t = start->next;
   free(start);
   start = t;
   count--;
   
   printf("%d deleted from beginning successfully.\n", n);
}
 
void delete_from_end() {
   struct node *t, *u;
   int n;
     
   if (start == NULL) {
      printf("Linked list is already 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);
} 

Arrays should be used when size of data to be stored is known in advance and does not changes frequently. Disadvantage of linked list is that we can not access every element in constant time as in array. Understanding linked lists will help you to learn tree data structure.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest
Share on whatsapp
WhatsApp
Share on email
Email
Md. Nurnobi Hosen
Md. Nurnobi Hosen

Student of Computer Science and Engineering at Daffodil International University,Main Campus ,Dhanmondi.
Web and App Developer also expart of wordpress customization

All Blog Posts »
Subscribe
Notify of
guest
1202 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Going Here
Going Here
December 4, 2019 1:06 AM

Hiya, I am really glad I have found this info. Nowadays bloggers publish just about gossips and internet and this is actually irritating. A good site with interesting content, this is what I need. Thank you for keeping this web site, I’ll be visiting it. Do you do newsletters? Can not find it.

learn more
learn more
December 4, 2019 1:44 AM

Hiya, I am really glad I have found this info. Nowadays bloggers publish just about gossips and internet and this is actually irritating. A good site with interesting content, this is what I need. Thank you for keeping this web site, I’ll be visiting it. Do you do newsletters? Can not find it.

Homepage
Homepage
December 4, 2019 11:59 AM

Great write-up, I¡¦m regular visitor of one¡¦s blog, maintain up the excellent operate, and It is going to be a regular visitor for a long time.

Go Here
Go Here
December 4, 2019 12:36 PM

I will immediately snatch your rss as I can not find your e-mail subscription hyperlink or newsletter service. Do you’ve any? Please permit me recognise in order that I may just subscribe. Thanks.

Website
Website
December 5, 2019 11:35 AM

As I web-site possessor I believe the content matter here is rattling fantastic , appreciate it for your hard work. You should keep it up forever! Best of luck.

Going Here
Going Here
December 5, 2019 12:09 PM

I am constantly searching online for ideas that can facilitate me. Thanks!

Click Here
Click Here
December 5, 2019 12:10 PM

You are a very clever person!

view source
view source
December 5, 2019 12:37 PM

Valuable information. Fortunate me I discovered your website by chance, and I’m shocked why this twist of fate did not took place earlier! I bookmarked it.

Read This
Read This
December 5, 2019 1:13 PM

You completed a few nice points there. I did a search on the theme and found nearly all persons will consent with your blog.

Read More Here
Read More Here
December 5, 2019 1:27 PM

I want to express my appreciation to the writer just for bailing me out of this type of setting. After looking through the world wide web and getting views that were not beneficial, I assumed my entire life was well over. Existing without the presence of solutions to the difficulties you have solved all through your entire write-up is a crucial case, and ones that might have negatively damaged my entire career if I hadn’t come across your blog. Your own personal mastery and kindness in dealing with all areas was tremendous. I don’t know what I would’ve done if… Read more »

Homepage
Homepage
December 5, 2019 2:01 PM

I simply wanted to say thanks again. I’m not certain the things I would’ve carried out in the absence of these points provided by you regarding such field. It was before a real daunting difficulty in my view, nevertheless considering the very skilled style you treated the issue took me to jump with fulfillment. Extremely grateful for the advice and then expect you comprehend what a great job that you’re putting in training the rest all through your site. Most likely you’ve never met all of us.

Click Here
Click Here
December 5, 2019 2:11 PM

Hi there, I found your site via Google at the same time as looking for a similar subject, your site came up, it appears great. I have bookmarked it in my google bookmarks.

Home Page
Home Page
December 5, 2019 2:37 PM

magnificent submit, very informative. I wonder why the opposite specialists of this sector do not understand this. You must proceed your writing. I’m confident, you have a great readers’ base already!

click here
click here
December 5, 2019 2:46 PM

hello!,I like your writing very much! percentage we be in contact extra about your post on AOL? I require a specialist on this area to solve my problem. Maybe that’s you! Having a look forward to peer you.

more info
more info
December 5, 2019 3:10 PM

I have to get across my admiration for your generosity supporting persons who really want assistance with the area. Your very own dedication to getting the message all over has been extraordinarily functional and has in every case enabled ladies like me to reach their endeavors. Your new helpful publication signifies a great deal to me and far more to my colleagues. With thanks; from each one of us.

Clicking Here
Clicking Here
December 5, 2019 3:23 PM

Thanks for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our area library but I think I learned more from this post. I’m very glad to see such great info being shared freely out there.

Homepage
Homepage
December 5, 2019 3:58 PM

I truly wanted to post a brief comment to be able to thank you for some of the lovely pointers you are sharing at this site. My incredibly long internet search has at the end of the day been honored with excellent facts and techniques to share with my good friends. I ‘d express that most of us site visitors are really lucky to exist in a wonderful site with so many special people with insightful tips and hints. I feel rather blessed to have come across your site and look forward to so many more entertaining moments reading here.… Read more »

Read More Here
Read More Here
December 5, 2019 4:36 PM

Hi there, You have done an incredible job. I will definitely digg it and personally suggest to my friends. I am sure they’ll be benefited from this site.

Visit Website
Visit Website
December 5, 2019 5:09 PM

Normally I do not learn post on blogs, however I wish to say that this write-up very forced me to take a look at and do so! Your writing style has been amazed me. Thanks, quite great article.

Get More Info
Get More Info
December 5, 2019 6:07 PM

I’m still learning from you, but I’m trying to reach my goals. I absolutely enjoy reading all that is posted on your blog.Keep the stories coming. I liked it!

Read This
Read This
December 5, 2019 6:10 PM

Very good written information. It will be valuable to anybody who employess it, as well as yours truly :). Keep up the good work – for sure i will check out more posts.

Discover More
Discover More
December 5, 2019 6:41 PM

I am not sure where you are getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for excellent information I was looking for this information for my mission.

Find Out More
Find Out More
December 5, 2019 6:45 PM

I’m so happy to read this. This is the kind of manual that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this greatest doc.

view source
view source
December 5, 2019 6:56 PM

magnificent submit, very informative. I wonder why the opposite specialists of this sector do not understand this. You must proceed your writing. I’m confident, you have a great readers’ base already!

Home Page
Home Page
December 5, 2019 7:01 PM

Fantastic goods from you, man. I’ve understand your stuff previous to and you are just extremely fantastic. I really like what you have acquired here, really like what you are stating and the way in which you say it. You make it entertaining and you still take care of to keep it sensible. I can’t wait to read far more from you. This is really a tremendous site.

Visit Website
Visit Website
December 5, 2019 7:29 PM

I think other site proprietors should take this website as an model, very clean and great user genial style and design, let alone the content. You’re an expert in this topic!

visit here
visit here
December 5, 2019 7:35 PM

Thank you for all your work on this site. Gloria enjoys carrying out investigation and it’s really easy to understand why. All of us hear all about the lively mode you convey vital things through this website and cause response from others about this point then our own child is without a doubt learning a lot. Enjoy the remaining portion of the new year. You’re the one carrying out a powerful job.

get more info
get more info
December 5, 2019 7:44 PM

I think other site proprietors should take this website as an model, very clean and great user genial style and design, let alone the content. You’re an expert in this topic!

Read More Here
Read More Here
December 5, 2019 7:52 PM

Wonderful paintings! This is the kind of info that are meant to be shared around the web. Shame on the search engines for no longer positioning this submit upper! Come on over and discuss with my website . Thank you =)

Visit This Link
Visit This Link
December 5, 2019 8:17 PM

You are a very clever person!

Web Site
Web Site
December 5, 2019 8:28 PM

Wow, fantastic blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is magnificent, as well as the content!

visit
visit
December 5, 2019 8:32 PM

Very good written information. It will be valuable to anybody who employess it, as well as yours truly :). Keep up the good work – for sure i will check out more posts.

get more info
get more info
December 5, 2019 8:45 PM

Hi, Neat post. There is an issue with your website in web explorer, would test this¡K IE still is the market chief and a huge element of folks will pass over your great writing due to this problem.

Read This
Read This
December 5, 2019 9:05 PM

Definitely, what a splendid blog and illuminating posts, I surely will bookmark your site.All the Best!

Find Out More
Find Out More
December 5, 2019 9:20 PM

Hi, Neat post. There is an issue with your website in web explorer, would test this¡K IE still is the market chief and a huge element of folks will pass over your great writing due to this problem.

website
website
December 5, 2019 9:21 PM

hey there and thank you for your info – I have definitely picked up anything new from right here. I did however expertise a few technical issues using this website, as I experienced to reload the web site lots of times previous to I could get it to load correctly. I had been wondering if your web host is OK? Not that I am complaining, but slow loading instances times will very frequently affect your placement in google and could damage your high quality score if ads and marketing with Adwords. Anyway I’m adding this RSS to my email and… Read more »

Visit This Link
Visit This Link
December 5, 2019 9:38 PM

I was recommended this website by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my trouble. You are incredible! Thanks!

learn more
learn more
December 5, 2019 10:08 PM

Valuable information. Fortunate me I discovered your website by chance, and I’m shocked why this twist of fate did not took place earlier! I bookmarked it.

more info
more info
December 5, 2019 10:14 PM

Good article and right to the point. I don’t know if this is really the best place to ask but do you folks have any thoughts on where to employ some professional writers? Thanks in advance 🙂

Click This Link
Click This Link
December 5, 2019 10:30 PM

Thank you so much for giving everyone an extraordinarily special opportunity to read critical reviews from this site. It’s always very enjoyable and jam-packed with amusement for me and my office colleagues to search your web site a minimum of thrice in a week to see the newest tips you have. And definitely, we are at all times amazed with all the wonderful tactics you give. Selected 1 areas in this post are particularly the finest we have all had.

visit here
visit here
December 5, 2019 10:41 PM

I think other site proprietors should take this website as an model, very clean and great user genial style and design, let alone the content. You’re an expert in this topic!

Web Site
Web Site
December 5, 2019 10:55 PM

certainly like your web site however you need to test the spelling on quite a few of your posts. Many of them are rife with spelling issues and I in finding it very troublesome to inform the truth then again I¡¦ll certainly come again again.

learn more
learn more
December 5, 2019 11:07 PM

Wow, marvelous weblog layout! How lengthy have you been blogging for? you made blogging glance easy. The overall look of your website is fantastic, as well as the content!

website
website
December 5, 2019 11:23 PM

Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate?

Discover More
Discover More
December 5, 2019 11:29 PM

whoah this weblog is excellent i like reading your posts. Keep up the great paintings! You know, a lot of people are looking around for this information, you can aid them greatly.

Get More Info
Get More Info
December 5, 2019 11:59 PM

I have been surfing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my opinion, if all web owners and bloggers made good content as you did, the internet will be much more useful than ever before.

Click This Link
Click This Link
December 6, 2019 12:15 AM

Hi, Neat post. There is an issue with your website in web explorer, would test this¡K IE still is the market chief and a huge element of folks will pass over your great writing due to this problem.

learn more
learn more
December 6, 2019 12:51 AM

Good article and right to the point. I don’t know if this is really the best place to ask but do you folks have any thoughts on where to employ some professional writers? Thanks in advance 🙂

Going Here
Going Here
December 7, 2019 9:43 AM

Very nice post. I just stumbled upon your blog and wanted to say that I’ve really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

Find Out More
Find Out More
December 7, 2019 9:45 AM

I have read some just right stuff here. Certainly value bookmarking for revisiting. I surprise how much attempt you place to make this type of excellent informative website.

1202
0
Would love your thoughts, please comment.x
()
x