Thread: What is a linked list in programming ?

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    128

    What is a linked list in programming ?

    I need help to understand linked list in c programming. I have been seen some example on the internet. But I don't understand the use of list in programming.

    A linked list is a simple way to store some unknown number of elements.

    I want to know when we need to use linked list in program. What the information can we get from linked list program

    It would be very useful for me if you can explain by giving example

    let suppose I have this data

    Name number address
    smith 123 Brazil
    john 658 UK
    Sendra 589 china
    Dav 789 india

    How to make list program for above data ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    You said it yourself,
    "A linked list is a simple way to store some unknown number of elements."

    > let suppose I have this data
    Let's suppose you start with just a simple integer so you can get some understanding.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    128
    generally I know the meaning of list. Number of elements or items. I am asking for program.How to make list program for my example ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    We don't deliver programs on demand.
    There are plenty of examples to use and adapt.
    Show some actual effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    A linked list is a simple way to store some unknown number of elements.
    arrays have elements, I think would qualify as a strut or class, maybe even an object. the list part is when you have more that one of the same type struts or class linked together to make a list of them that is where the term "linked list" comes into play.
    Code:
    struct LinkedList{
        int data; // <-- member 
        size_t something; // <-- member
        char *c; // <-- member
        struct LinkedList *next; //<-- another same struct to keep track of it in the list.
     };
    then you just make a bunch of them and tie strings to them using pointers, therefore making them into a "linked list' .
    Code:
    (struct)tail->struct->struct->struct->(struct)head

    but if
    this is for a class work, I'd think basic data types and how to deal with them would have been covered first, then advance data types, ie, structs, then how and what one can do with them next, ie link them together and do stuff with them. and pointers would have been covered somewhere before that too.

    so my question is, if this is homework where you sleeping the entire time then just happened to wake up when he gave out this assignment?

    ever thought of asking google this question?

    How to make (a linked) list ( in c )
    Linked Lists in C Tutorial - Cprogramming.com
    Last edited by userxbw; 10-20-2017 at 12:41 PM.

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    128
    Quote Originally Posted by Salem View Post
    We don't deliver programs on demand.
    There are plenty of examples to use and adapt.
    Show some actual effort.
    I am not demanding for program. I was showing my example. I think I have chosen wrong topic. Before going to linked list. I should learn structure. I will back on clearing some basics

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    128
    Quote Originally Posted by userxbw View Post

    but if
    this is for a class work, I'd think basic data types and how to deal with them would have been covered first, then advance data types, ie, structs, then how and what one can do with them next, ie link them together and do stuff with them. and pointers would have been covered somewhere before that too.

    so my question is, if this is homework where you sleeping the entire time then just happened to wake up when he gave out this assignment?

    ever thought of asking google this question?
    Thanks for your explanations This is not homework or Class assignment. This is just simple example for basic understanding. I will study on structure and then come back on this topics.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    > I am not demanding for program.
    Oh really, so what was this then in post #3?
    > I am asking for program.How to make list program for my example ?

    Your answer is you start with
    Code:
    struct list {
      int data;
      struct list *next;
    };
    to make sure you understand the basics, then you move on to

    Code:
    struct list {
      char Name[30];
      int number;
      char address[50];
      struct list *next;
    };
    You learn the most by actually doing things and getting stuck - which is why we ask you to post code in the first place.

    Simply dumping an answer serves nobody's interests in the long term.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2017
    Posts
    128
    Quote Originally Posted by Salem View Post
    > I am not demanding for program.
    Oh really, so what was this then in post #3?
    .
    I have already accepted my mistake. Again I am really sorry. I apologize for my mistake. I took that just for example. I wanted to change it in program. But I have realized that I don't have good understanding of structure. That's why I didn't write any further program.. I am still confuse on structure. I thought to start new thread on structure. I stopped and thought. I felt that after spending some time I should do it. because I have presented myself in wrong way. I am reading and I will back soon with code

  10. #10
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    92
    AFTER your reading, you might play a bit with this code:

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct MYLIST
    {
        char Name[30];
        int number;
        char address[50];
        struct MYLIST *next;
    } MYLIST;
    
    void main()
    {
        MYLIST *startPtr, *p1, *p2;
        int cntr = 0, maxNr=100;
    
        // ---- make a list with maxNr elements ----
        startPtr = p1 = p2 = NULL;
        while(cntr < maxNr)
        {
            p1 = malloc(sizeof(MYLIST));
            if(p1 != NULL)
            {
                if(startPtr == NULL)
                    startPtr = p2 = p1;
                else
                    p2->next = p1;
    
                sprintf(p1->Name, "Some name %d", cntr);
                p1->number = cntr++;
                strcpy(p1->address, "Some address");
                p1->next = NULL; // Last element in list must (!) point to NULL
    
                p2 = p1;
            }
            else
        break;
        }
    
        // ---- show all names in the list ----
        p1 = startPtr;
        while(p1 != NULL)
        {
            printf("%s\n", p1->Name);
            p1 = p1->next;
        }
    
        // ---- clean-up !; free() all elements of the list ----
        p1 = p2 = startPtr;
        while(p1 != NULL)  // clean-up !; free() all elements of the list
        {
            p2 = p1->next;
            free(p1);
            p1 = p2;
        }
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    But then again, there's always someone to come along and dump an answer.....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by abhi143 View Post
    I have already accepted my mistake. Again I am really sorry. I apologize for my mistake. I took that just for example. I wanted to change it in program. But I have realized that I don't have good understanding of structure. That's why I didn't write any further program.. I am still confuse on structure. I thought to start new thread on structure. I stopped and thought. I felt that after spending some time I should do it. because I have presented myself in wrong way. I am reading and I will back soon with code
    it is called struc for the very simple reason of the definition of the word , "structure"

    the arrangement of and relations between the parts or elements of something complex.
    hence a complex means of storing different data types into one 'box" for safe keeping, while still giving the program a means to access same said data types, one at a time or more while keeping them all self contained within the same box at all times. therefore one can now create a more complex "data type". If one was to try and define a car using data types and not putting them all into a box then it gets messy. we have to take a few characteristics of a car then use data types to describe a car. tires, doors, color; just using three.
    Code:
    int main()
    {
    int tires = 4;
    int doors = 2;
    char * color = "white";
    // if you wanted two cars then you will run 
    // into problems with your variable names. 
    return 0 ;
    }
    to access that and keep it all together if more than one car can get really confusing to keep track of. using a struc or a complex data type it is easier to keep track of , change its values within it, and even create more than one car, record for a person , but if you have two people now what?

    Without having somewhere to keep all of the data separate but still using the same variable names, you will be having to come up with creative names for each time you create a new car so your variables will not cause conflict with each other, then you have to keep track of everything in what it is called,it too can get really lengthy having to type a new name for each one.
    Code:
    char name1[20], name2[20],name3[20],name4[20], ...
    char address1[30],address2[30],address3[30],address4[30],...
    if I need a 100 names, and addresses or more even. That is a lot to keep track of that way. let lone all of the writing one will have to do.

    just stick it in a structure
    Code:
    struct list {  
    
       char Name[30];
       int number;
      char address[50];
      struct list *next;
    };

    then after that program is written without a struc and you get a new name and you only have it written for 5000 names and address, then you get name number 5001. your program is useless.


    the example left in here demonstrates how to elevate that problem. using a struct. Now using a data type and its same variable name,
    It can be used multiple times to keep multiple different names distinctly for each different person along with whatever else you want to keep on them, all using the same data types with their same variable name and not over writing that same variable with a different name each time you access it to put another persons name inside of it.

    this way it keeps everything separate but together at the same time by using the same data type and their same variable names keeping multiple different values within it, while with the use of linking it together to keep track of them all so you can find them and work with them. hence the linked list was then invented to deal with that side of it.
    Last edited by userxbw; 10-21-2017 at 12:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-08-2014, 12:13 AM
  2. Replies: 2
    Last Post: 09-15-2013, 01:16 PM
  3. C programming - Doubly linked list
    By pghpens91 in forum C Programming
    Replies: 5
    Last Post: 07-12-2012, 07:56 PM
  4. C programming-Linked list problem
    By male4329 in forum C Programming
    Replies: 18
    Last Post: 06-02-2005, 02:05 AM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread