Thread: free() problem

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by audinue View Post
    I'm a little bit stupid here King Mir, sorry. I still don't get it. -_-"?
    It would help if you specified the part you didn't understand.

    You can also try google. to find more explanations and even java applets demonstrating circular queues.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    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.

  3. #18
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    King Mir: It is ok if we using circular buffer. But how to solve when the array is full? *confuse*

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by audinue View Post
    King Mir: It is ok if we using circular buffer. But how to solve when the array is full? *confuse*
    You have to create a new bigger array. Then you copy each element over. This is most easily done by dequeuing one element at a time, and queuing it in the bigger buffer queue. Or you can just copy the everything from the first element to the end of the buffer first, then every thing from the start of the buffer to the last index, not including the last index itself.

    Pictorially you want to do this:
    Old buffer
    ---|-- < start index
    ---|-- < one past the end index.
    DEFABC

    New buffer:
    |----------- < start index
    ------|----- < one past the end index.
    ABCDEFXXXXXX
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #20
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    A little bit more complicated I think, but it's OK...

    Thank you very much King Mir

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