cirthix
02-12-2007, 12:08 AM
Ok, so I thought about how to implement a list for a while and came up with something:
if kept updated, doubly linked, indexed list can be treated *almost* as an array, if the functions are put together properly. however, I'm getting a whole ton of errors when I try to compile it. I know I'm really bending some rules with this one, but is there a way to force-ignore mismatching data types and stuff?
a quick explanation of the code:
insert takes the data, list, and place and makes a new cell, moves the pointer sof the cells that are supposed to be next to it, then updates the list's numbering
rewrite overwrites a cell's data with new data
remove switches the next and previous pointers around near the cell to be removed, then renumbers the list, then frees the memory
addr is really what makes this list implementation act like an array. it allows psuedo-random(though just obscured searching) access. it goes thought he list and returns the address of the proper cell
almost all of the manipulation of the elements is done through ponters. the logic should be correct. I'm pretty sure that the majority of the errors are coming from something to do with the typedefs, but i can't pinpoint what.
there's a simple driver that just prints a zero. once the list compiles properly, it can easily be edited to print the entered number. it's the simplest driver i could think of.
now that you know what it's *supposed* to do, here's the code:
typedef struct {int number;} data;
typedef struct {data stuff; int count; int* next; int* prev;} ent;
typedef struct {int entries; ent* top; ent* bottom;} list;
list* make();
void insert(list* a, data stuff, int position);
void rewrite(data stuff, int position);
void remove(int position, list* a);
ent* addr(list* a, position);
int main ()
{
int y;
list* x=make();
printf("enter a number");
scanf("%d", &y);
add(y, &x);
printf("%d\n", 0;
}
list* make()
{
return calloc(sizeof(list));
}
void insert(list* a, data stuff, int position)
{
ent* tmp, y, z;
int x=0;
if (position<=a.count+1 && position>0)
{
tmp=malloc(sizeof(ent));
y=addr(position);
z=*(y+sizeof(int)+sizeof(tmp));
*(tmp)=position;
*(tmp+sizeof(int)+2*(sizeof(tmp)))=stuff;
a.count++;
if(position>1)
{
*(z+sizeof(int))=tmp;
*(tmp+sizeof(int)+sizeof(tmp))=z;
}
else{a.back=tmp;}
if(position<a.count)
{
*(y+sizeof(int)+sizeof(tmp))=tmp;
*(tmp+sizeof(int))=y;
while (position<=a.count)
{
*(y)++;
y=*(y+sizeof(int));
position++;
}
else{a.back=tmp;}
}
else {printf("error\n");}
}
void rewrite(data stuff, int position)
{
int* useless;
*(addr(position)+sizeof(int)+2(sizeof(useless)))=s tuff;
}
void remove(int position, list* a)
{
ent* tmp=addr(position);
ent* tmp2=tmp;
if (position!=1)
{*(*(tmp+sizeof(int)+sizeof(tmp))+sizeof(int))=*(t mp+sizeof(int));}
else{a.back=*(a.back+sizeof (int));}
if (position!=a.count)
{*(*(tmp+sizeof(int))+sizeof(tmp)+sizeof(int))=*(t mp+sizeof(int)+sizeof(tmp));}
else{a.front=*(a.front+sizeof(int)+sizeof(tmp));}
a.count--;
while(position<a.count)
{
*tmp--;
tmp=*(tmp+sizeof(int));
position++;
}
*tmp--;
}
ent* addr(list* a, int position)
{
ent* tmp=a.back;
int x=0;
while (x<position)
{
tmp=*(tmp+sizeof(int));
x++;
}
return tmp;
}
cc errors:
list2.c:9: error: parse error before "position"
list2.c: In function `main':
list2.c:19: error: parse error before ';' token
list2.c: In function `insert':
list2.c:33: error: request for member `count' in something not a structure or union
list2.c:35: warning: assignment makes pointer from integer without a cast
list2.c:36: error: incompatible types in assignment
list2.c:37: error: invalid operands to binary +
list2.c:38: error: incompatible types in assignment
list2.c:39: error: incompatible types in assignment
list2.c:40: error: request for member `count' in something not a structure or union
list2.c:43: error: invalid operands to binary +
list2.c:46: error: request for member `back' in something not a structure or union
list2.c:48: error: request for member `count' in something not a structure or union
list2.c:50: error: invalid operands to binary +
list2.c:52: error: request for member `count' in something not a structure or union
list2.c:54: error: wrong type argument to increment
list2.c:55: error: invalid operands to binary +
list2.c:58: error: parse error before "else"
list2.c: In function `rewrite':
list2.c:68: error: called object is not a function
list2.c: In function `remove':
list2.c:76: error: invalid operands to binary +
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:78: error: request for member `count' in something not a structure or union
list2.c:79: error: invalid operands to binary +
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:81: error: request for member `count' in something not a structure or union
list2.c:82: error: request for member `count' in something not a structure or union
list2.c:85: error: incompatible types in assignment
list2.c: In function `addr':
list2.c:93: error: request for member `back' in something not a structure or union
list2.c:97: error: incompatible types in assignment
gcc errors:
list2.c:9: error: parse error before "position"
list2.c: In function `main':
list2.c:19: error: parse error before ';' token
list2.c: In function `insert':
list2.c:33: error: request for member `count' in something not a structure or union
list2.c:35: warning: assignment makes pointer from integer without a cast
list2.c:36: error: incompatible types in assignment
list2.c:37: error: invalid operands to binary +
list2.c:38: error: incompatible types in assignment
list2.c:39: error: incompatible types in assignment
list2.c:40: error: request for member `count' in something not a structure or union
list2.c:43: error: invalid operands to binary +
list2.c:46: error: request for member `back' in something not a structure or union
list2.c:48: error: request for member `count' in something not a structure or union
list2.c:50: error: invalid operands to binary +
list2.c:52: error: request for member `count' in something not a structure or union
list2.c:54: error: wrong type argument to increment
list2.c:55: error: invalid operands to binary +
list2.c:58: error: parse error before "else"
list2.c: In function `rewrite':
list2.c:68: error: called object is not a function
list2.c: In function `remove':
list2.c:76: error: invalid operands to binary +
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:78: error: request for member `count' in something not a structure or union
list2.c:79: error: invalid operands to binary +
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:81: error: request for member `count' in something not a structure or union
list2.c:82: error: request for member `count' in something not a structure or union
list2.c:85: error: incompatible types in assignment
list2.c: In function `addr':
list2.c:93: error: request for member `back' in something not a structure or union
list2.c:97: error: incompatible types in assignment
if kept updated, doubly linked, indexed list can be treated *almost* as an array, if the functions are put together properly. however, I'm getting a whole ton of errors when I try to compile it. I know I'm really bending some rules with this one, but is there a way to force-ignore mismatching data types and stuff?
a quick explanation of the code:
insert takes the data, list, and place and makes a new cell, moves the pointer sof the cells that are supposed to be next to it, then updates the list's numbering
rewrite overwrites a cell's data with new data
remove switches the next and previous pointers around near the cell to be removed, then renumbers the list, then frees the memory
addr is really what makes this list implementation act like an array. it allows psuedo-random(though just obscured searching) access. it goes thought he list and returns the address of the proper cell
almost all of the manipulation of the elements is done through ponters. the logic should be correct. I'm pretty sure that the majority of the errors are coming from something to do with the typedefs, but i can't pinpoint what.
there's a simple driver that just prints a zero. once the list compiles properly, it can easily be edited to print the entered number. it's the simplest driver i could think of.
now that you know what it's *supposed* to do, here's the code:
typedef struct {int number;} data;
typedef struct {data stuff; int count; int* next; int* prev;} ent;
typedef struct {int entries; ent* top; ent* bottom;} list;
list* make();
void insert(list* a, data stuff, int position);
void rewrite(data stuff, int position);
void remove(int position, list* a);
ent* addr(list* a, position);
int main ()
{
int y;
list* x=make();
printf("enter a number");
scanf("%d", &y);
add(y, &x);
printf("%d\n", 0;
}
list* make()
{
return calloc(sizeof(list));
}
void insert(list* a, data stuff, int position)
{
ent* tmp, y, z;
int x=0;
if (position<=a.count+1 && position>0)
{
tmp=malloc(sizeof(ent));
y=addr(position);
z=*(y+sizeof(int)+sizeof(tmp));
*(tmp)=position;
*(tmp+sizeof(int)+2*(sizeof(tmp)))=stuff;
a.count++;
if(position>1)
{
*(z+sizeof(int))=tmp;
*(tmp+sizeof(int)+sizeof(tmp))=z;
}
else{a.back=tmp;}
if(position<a.count)
{
*(y+sizeof(int)+sizeof(tmp))=tmp;
*(tmp+sizeof(int))=y;
while (position<=a.count)
{
*(y)++;
y=*(y+sizeof(int));
position++;
}
else{a.back=tmp;}
}
else {printf("error\n");}
}
void rewrite(data stuff, int position)
{
int* useless;
*(addr(position)+sizeof(int)+2(sizeof(useless)))=s tuff;
}
void remove(int position, list* a)
{
ent* tmp=addr(position);
ent* tmp2=tmp;
if (position!=1)
{*(*(tmp+sizeof(int)+sizeof(tmp))+sizeof(int))=*(t mp+sizeof(int));}
else{a.back=*(a.back+sizeof (int));}
if (position!=a.count)
{*(*(tmp+sizeof(int))+sizeof(tmp)+sizeof(int))=*(t mp+sizeof(int)+sizeof(tmp));}
else{a.front=*(a.front+sizeof(int)+sizeof(tmp));}
a.count--;
while(position<a.count)
{
*tmp--;
tmp=*(tmp+sizeof(int));
position++;
}
*tmp--;
}
ent* addr(list* a, int position)
{
ent* tmp=a.back;
int x=0;
while (x<position)
{
tmp=*(tmp+sizeof(int));
x++;
}
return tmp;
}
cc errors:
list2.c:9: error: parse error before "position"
list2.c: In function `main':
list2.c:19: error: parse error before ';' token
list2.c: In function `insert':
list2.c:33: error: request for member `count' in something not a structure or union
list2.c:35: warning: assignment makes pointer from integer without a cast
list2.c:36: error: incompatible types in assignment
list2.c:37: error: invalid operands to binary +
list2.c:38: error: incompatible types in assignment
list2.c:39: error: incompatible types in assignment
list2.c:40: error: request for member `count' in something not a structure or union
list2.c:43: error: invalid operands to binary +
list2.c:46: error: request for member `back' in something not a structure or union
list2.c:48: error: request for member `count' in something not a structure or union
list2.c:50: error: invalid operands to binary +
list2.c:52: error: request for member `count' in something not a structure or union
list2.c:54: error: wrong type argument to increment
list2.c:55: error: invalid operands to binary +
list2.c:58: error: parse error before "else"
list2.c: In function `rewrite':
list2.c:68: error: called object is not a function
list2.c: In function `remove':
list2.c:76: error: invalid operands to binary +
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:78: error: request for member `count' in something not a structure or union
list2.c:79: error: invalid operands to binary +
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:81: error: request for member `count' in something not a structure or union
list2.c:82: error: request for member `count' in something not a structure or union
list2.c:85: error: incompatible types in assignment
list2.c: In function `addr':
list2.c:93: error: request for member `back' in something not a structure or union
list2.c:97: error: incompatible types in assignment
gcc errors:
list2.c:9: error: parse error before "position"
list2.c: In function `main':
list2.c:19: error: parse error before ';' token
list2.c: In function `insert':
list2.c:33: error: request for member `count' in something not a structure or union
list2.c:35: warning: assignment makes pointer from integer without a cast
list2.c:36: error: incompatible types in assignment
list2.c:37: error: invalid operands to binary +
list2.c:38: error: incompatible types in assignment
list2.c:39: error: incompatible types in assignment
list2.c:40: error: request for member `count' in something not a structure or union
list2.c:43: error: invalid operands to binary +
list2.c:46: error: request for member `back' in something not a structure or union
list2.c:48: error: request for member `count' in something not a structure or union
list2.c:50: error: invalid operands to binary +
list2.c:52: error: request for member `count' in something not a structure or union
list2.c:54: error: wrong type argument to increment
list2.c:55: error: invalid operands to binary +
list2.c:58: error: parse error before "else"
list2.c: In function `rewrite':
list2.c:68: error: called object is not a function
list2.c: In function `remove':
list2.c:76: error: invalid operands to binary +
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:78: error: request for member `count' in something not a structure or union
list2.c:79: error: invalid operands to binary +
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:81: error: request for member `count' in something not a structure or union
list2.c:82: error: request for member `count' in something not a structure or union
list2.c:85: error: incompatible types in assignment
list2.c: In function `addr':
list2.c:93: error: request for member `back' in something not a structure or union
list2.c:97: error: incompatible types in assignment