CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2003
    Location
    Thailand
    Posts
    2

    how to implement linked list without using pointer in C

    how to implement linked list without using pointer in C Language ?

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158
    You could use an array to store the elements in. This would mean to take care of the array size when inserting elements, the array might have to grow.
    Operations like inserting/deleting from the middle of this 'list' would then require to shift all elements behind the insertion/deletion position. That is not very efficient, but would work.

  3. #3
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    I don't think its possible with out using one pointer...

  4. #4
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178
    You couldnt make a linked list without using a pointer. Any array solution would not be a linked list and not have the same properties of a linked list. The whole strength of a linked list relies upon the fact that it is pointer based and truly a dynamic structure.
    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  5. #5
    Join Date
    Aug 2002
    Posts
    58
    Your question is strange... As has been pointed out, a linked list by definition uses pointers - so what is it exactly that you're trying to accomplish and why?

    Regards,
    Bassman

  6. #6
    Join Date
    Mar 2002
    Location
    Israel
    Posts
    187
    Use of linked list is possible with offsets instead of pointers. However, if You writing a client side library I would move such data to the server side when ever possible. For example, kernel-mode driver usualy operate in constant address space of OS kernel; service/environment processes operate in constant address space too.
    In case You plan only client-side library, but need to control resources common to several processes, try to do the follow:
    Code:
    /* the assumtion here is that all memory blocks are taken from 
        contiguous memory pool (shared memory, memory mapped file,
        shared data segment etc.)
    */
    typedef struct listNode
    {
        char validMemoryBlock;
        int offsetToTheNextNode;
    #ifdef __DOUBLE_LINKED_LIST__
        int offsetToThePrevtNode;
    #endif
    };
    
    listNode* __cdecl nextNode(listNode* node)
    {
        if( !(node->validMemoryBlock) )
        {
            setError(ERROR_INVALID_MEMORY_ACCESS);
            return NULL;
        }
         /* additional parameter validation here  */
         return (listNode*) ( ((int)node) + node->offsetToTheNextNode );
    }
    In the future computer's weight will not exceed 1.5 ton.
    (Popular Mechanics, 1949)

  7. #7
    I have created an array implementation of a linked list as an attempt to implement my own intermediate memory heap. Here's some pseudo-code:

    Code:
    typedef struct{
     int isvalid;
     int index
     int data;
     int next; // index to next
     int prev; // index to prev
    } listitem;
    
    
    typedef struct{
     int head;
     listitem *list;
     int numitems;
     int maxitems;
    } list;
    
    list *createlist(){
     malloc list;
     list->numitems=NUMITEMS;
     list->maxitems=MAXITEMS;
     calloc NUMITEMS*sizeof(listitem)
     list->head=NULL;
     return list;
    }
    
    destroylist(list){
     free list->list
     free list
    }
    
    listitem *insert(list,item){
     listitem *place
     if(numitems==0){
      place=list->maxitems
     place->index=0
      set your data;
      set next and prev to NULL
      set isvalid to TRUE
      list->numitems++
      set head to 0
      return place
     }
     for(i=0;each item up to maxitems;i++){
      if(item is valid)
       break
     }
     if(i==maxitems){
      resize list to fit more items
      set maxitems to new size
     }
     place=&list[i]
     place->index=i
     set your data
     next=list[head]
     while(next!=NULL&&(data in next)<=(your data)){
      prev=next
      next=next->next
     }
     place->next=next
     place=prev=prev
     if(prev)
      prev->next=place
     else
      head=i
     if(next)
      next->prev=place
     numitems++
     return place
    }
    
    delete(list,item){
     if(!item)return
     if(item->prev)
      item->prev->next=NULL
     if(list[head]==item)
      head=(item->next)?item->next:0
     item->next=null
     item->prev=null
     item->isvalid=0
     list->numitems--
     return 0
    }

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449
    But it uses pointers:
    Code:
    listitem *list;
    So far we haven't gotten any word from the OP as to why he/she needs to do this.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    You could use a reference i guess.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured