|
-
July 4th, 2003, 03:49 AM
#1
how to implement linked list without using pointer in C
how to implement linked list without using pointer in C Language ?
-
July 4th, 2003, 10:46 AM
#2
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.
-
July 4th, 2003, 11:46 AM
#3
I don't think its possible with out using one pointer...
-
July 4th, 2003, 12:25 PM
#4
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.
-
July 4th, 2003, 12:30 PM
#5
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
-
July 4th, 2003, 01:43 PM
#6
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)
-
July 4th, 2003, 06:03 PM
#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
}
-
July 4th, 2003, 07:03 PM
#8
But it uses pointers:
So far we haven't gotten any word from the OP as to why he/she needs to do this.
Regards,
Paul McKenzie
-
July 6th, 2003, 08:33 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|