how to implement linked list without using pointer in C Language ?
Printable View
how to implement linked list without using pointer in C Language ?
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.
I don't think its possible with out using one pointer...
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.
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
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 );
}
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
}
But it uses pointers:
So far we haven't gotten any word from the OP as to why he/she needs to do this.Code:listitem *list;
Regards,
Paul McKenzie
You could use a reference i guess.