Thread: free() problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by audinue View Post
    eXeCuTeR: Would you like to give me some advice how to get element by index using linked list??
    Well, this is an option:
    (there's no checking whether index is bigger than the number of elements of the linked list, you can make one easily though)
    Code:
    typedef struct
    {
    int data;
    struct node *next;
    } node;
    
    
    node *getElementByIndex(node *myList, int index)
    {
     node *pos = myList; // pointing to the first element
     while(index >= 0)
     {
      pos = pos->next;
      --index;
     }
     return pos;
    }
    Last edited by eXeCuTeR; 07-19-2008 at 04:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lots of freeware
    By major_small in forum General Discussions
    Replies: 60
    Last Post: 02-14-2017, 03:00 AM
  2. Program that displays amount of free memory
    By trancedeejay in forum Linux Programming
    Replies: 3
    Last Post: 01-13-2006, 01:27 PM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  5. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM