Linked list

This is a discussion on Linked list within the C Programming forums, part of the General Programming Boards category; Hello. A few days ago, i created a program, something like a database with students. I used a struct Code: ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    11

    Linked list

    Hello.
    A few days ago, i created a program, something like a database with students. I used a struct
    Code:
      Struct student{
               int code;
               char name[50];
                float grade;
                .
                .
                .
     }Student[1000];
    In this program's menu the user choose to enter student's name or grade...print struct, or save the info in a file so he can load it another time.
    Now i'm supposed to create the same program but using a linked list instead of the Student[1000] array.(Insert student,Search student,Delete student.......)
    I know some things about lists but i don;t know how to combine it with the struct and i'm a little confused, Could you help me on how to begin or something. (Or if you Know something similar so i have an example)
    Thanks...

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    A linked list is made up of nodes of some type of object where each node has some sort of reference or way of obtaining the next node.

    In C, this can be realized by structs and pointers. You already have your struct, so you're halfway there in terms of the data structure. All you need next is a pointer variable inside your struct that points to the next element. The type of this pointer is that of the struct itself. So therefore, you would need this variable inside the struct (named whatever you like, but next is a good name):

    Code:
    struct student *next;
    When you have to insert, search, or delete, think of how you would do it with an array. Where you would go through each element of an array, you can now go through each element of this linked list.

    You can find tutorials and references on linked lists all over the place. This is the one off of this site: http://www.cprogramming.com/tutorial/c/lesson15.html

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    11
    1st:
    Code:
    struct something{
             .
             .
             .
    }*What;
    What does this "*What" means, if i place it there??
    2nd:
    My struct should be like this:
    Code:
    struct student{
    	int code;
    	char *name;
    	float *vathmos;
    	struct student *next;
    };
    ???

    3rd:
    i'm calling an equation, is this right? : insert_list(struct student**head,int code,char *name,char *vathmos);
    Last edited by Zuko; 06-10-2007 at 07:03 AM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    1. '* What' is a pointer to an instance of the structure.
    2. Probably
    3. Probably

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    You should perhaps look this good tutorial that wiki has.

    Linked List

    ssharish2005

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    i'm calling an equation, is this right? : insert_list(struct student**head,int code,char *name,char *vathmos);
    Probably not
    1. const char* name sounds better
    2. why parameter is char* vathmos if the member is float *vathmos;
    3. Why at all you need a pointer to float in the struct - is it dynamically created array of floats?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21