Thread: linkedlist concept please!

  1. #1
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218

    linkedlist concept please!

    Hello everyone;

    I would apreciate,
    if you kindly point out the clues and answer to my question.

    Linkedlists are the only subject i never realy touched while
    reading C and doing various programming, for the last
    few years.

    last couple of days i have read on it extensively and can
    figure out the algorithm fine.

    but i cant figure out the concept behind it in "its usage
    in database programming" whats the point of this dynamicaly
    allocated structures related to each other through these
    pointer nodes either to add or delete a member, which
    in actuality can be done through a simple "random access file"

    procedure of adding a new record to a file or deleting one
    permanently.

    then again if linkedlist is good just for the life of the program.
    then how does it attribute to a data base system.

    can u provide me with an example(not an bank account again)
    of utilizing a linkedlist in a real world environment.

    at the cost of being repetitive and boring, i would like
    to know why cant we replace linkedlists by using
    rendom access files?what r the differences.?


    cheers!

    *EDIT;
    by the way i would understand its usage in a game environment.
    it makes sense(dynamic environment). but other fields?
    Last edited by SAMSAM; 03-15-2003 at 11:13 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can u provide me with an example(not an bank account again) of utilizing a linkedlist in a real world environment.
    Memory managers are typically created using linked lists. Linked lists are helpful in creating more complex data structures such as hash tables, which are then used for all kinds of applications including data bases.

    >i would like to know why cant we replace linkedlists by using rendom access files?
    Because random access files when used portably are very complicated and difficult to work with, not to mention the performance of file based input/output is one of the big bottlenecks in programming. Linked lists are considerably faster, simpler, and easier to work with in general over random access files.

    >but other fields?
    Pick a field and I can guarantee you will find a linked list in just about every nontrivial application.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    Thx Prelude.
    "ok can u explain to me"linkedlist for dummies 2nd effort made"

    samsam

    "then again if linkedlist is good just for the life of the program.
    then how does it attribute to a data base system."

    then what confuses me is the fact that in all of the books
    i have reviewed, its like linkedlists utilized , yet no effort is being
    made into actually save it to a file or what.

    if its made for a one time use.then where this data is being saved?what use ?
    like this code that add banking info to the end of the list.



    Code:
    
    Name: BANK1.C
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    
    void addNewAccount(void);
    
    struct account {
        int number;
        char lastname[15];
        char firstname[15];
        float balance;
        struct account *next;
        };
    struct account *first,*current,*new;
    int anum = 0;
    
    void main()
    {
        char ch;
    
    /* Initialize all the pointers */
    
        first = (struct account *)NULL;
        current = (struct account *)NULL;
        new = (struct account *)NULL;
    
        do
        {
            puts("\nA - Add a new account");
            puts("Q - Quit this program\n");
            printf("\tYour choice:");
            ch = toupper(getch());
            switch(ch)
            {
                case 'A':
                    puts("Add new account\n");
                    addNewAccount();
                    break;
                case 'Q':
                    puts("Quit\n");
                default:
                    break;
            }
        }
        while(ch != 'Q');
    }
    
    void addNewAccount(void)
    {
        char buffer[64];
    
        new = (struct account *)malloc(sizeof(struct account));
    
    /* Check to see if this is the first record
     * If so, then initialize all the pointers to this,
     * first structure in the database
     */
    
        if(first==(struct account *)NULL)
            first = current = new;
    
    /* Otherwise, you must find the end of the structure list
     * (Easily spotted by the NULL pointer) and add on the
     * new structure you just allocated memory for
     */
    
        else
        {
            current = first;        //make the first record the current one
                                    //and loop through all records:
    
            while(current->next != (struct account *)NULL)
                current = current->next;
    
                                    //the last record is found
            current->next = new;    //save the address of the new record
            current = new;          //and make the current record the new one
        }
    
    /* Now, you just fill in the new structure */
    
        anum++;
        printf("             Account number: %5i\n",anum);
        current->number = anum;
    
        printf(" Enter customer's last name: ");
        gets(current->lastname);
    
        printf("Enter customer's first name: ");
        gets(current->firstname);
    
        printf("      Enter account balance: $");
        current->balance = atof(gets(buffer));
    
    /* Finally, cap the new record with a NULL pointer
     * so that you know it's the last record:
     */
    
        current->next = (struct account *)NULL;
    }

    Ok we are adding more acc numbers to this banking database

    but when the program ends everything is lost.

    your answer may be:
    ok then dumb samsam, we simply open a file . and read and
    write into it .

    which comes to my point that we still have to create that
    file first and read and write into it.

    i hate myself when im like lost.


    cheers!

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You make a couple of functions. one to build a list from a file and another to dump a list to a file. Obviously when you store a list in a file you only store the nodes data members as the pointers if stored would mean doodle squat when read back in again.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something about a linkedlist program..
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 10-17-2007, 01:14 PM
  2. What is array of LinkedList
    By enggabhinandan in forum C Programming
    Replies: 6
    Last Post: 10-22-2006, 03:12 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. linkedlist guidance
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-16-2001, 06:32 AM