C 我的双链接列表中的所有内容都变为0

C 我的双链接列表中的所有内容都变为0,c,linked-list,C,Linked List,我有一个在unix平台上创建的用于双链接列表的程序,它只需查找即可工作。我只是将代码复制粘贴到mac上的eclipse上。出于某些奇怪的原因,代码运行良好,但无论何时添加或删除或执行任何操作,列表中的所有索引都始终显示为0 int main() { list l = create_list(); prepend (&l, (void*)1); prepend (&l, (void*)2); prepend (&l, (void*)55);

我有一个在unix平台上创建的用于双链接列表的程序,它只需查找即可工作。我只是将代码复制粘贴到mac上的eclipse上。出于某些奇怪的原因,代码运行良好,但无论何时添加或删除或执行任何操作,列表中的所有索引都始终显示为0

int main()
{
    list l = create_list();
    prepend (&l, (void*)1);
    prepend (&l, (void*)2);
    prepend (&l, (void*)55);
return 0;
}

void display_list(list l)
{
int i;
for(i=0;i<size(l);i++)
{
printf("Index [%d]: ",i);
printf("%d",get(l,i));
printf("\n");
}

}
它在unix上运行得很好,所以我不认为是它的方法,但我不知道它是怎么回事

预结束方法:

int prepend (list* l, void* item)
{
 int result = 0;
 if (l !=NULL)
 {
 node* temp = malloc(sizeof(node));
 if (temp != NULL)
 {
 result = 1;
 temp -> item = item;
 if (l-> front == NULL)
 {
 temp -> next = NULL;
 temp -> prev = NULL;
 l -> front = temp;
 l -> rear = temp;
 }
 else
 {
   temp -> next = l -> front;
   temp -> prev = l -> rear;
    l -> front= temp;
 }
 l -> size++;
 }
}
return result;
}
获取方法:

void* get (list l, int location)
{
void* item =NULL;
if(1<=location && location<+ size(l))
{
node* temp = l.front;
int i;
for(i=1;i<location; i++)
 temp = temp -> next;
 item= temp -> item;
}
}
void*get(列表l,int位置)
{
void*item=NULL;

如果(1首先,您似乎有点不一致。如果列表中已有内容,则将temp->prev设置为l->rear,这将生成一个循环双链接列表。但是如果列表为空,则添加新元素并将其next/prev设置为NULL,从而使列表成为一个非循环双链接列表。我假设您想要生成一个循环双链接列表应收账款清单

问题是您没有更新旧的l->front的prev字段和l->rear的下一个字段。这应该是您的前置函数:

int prepend (list* l, void* item)
{
  int result = 0;
  if (l !=NULL)
  {
    node* temp = malloc(sizeof(node));
    if (temp != NULL)
    {
      result = 1;
      temp -> item = item;
      if (l-> front == NULL)
      {
        temp -> next = temp;
        temp -> prev = temp;
        l -> front = temp;
        l -> rear = temp;
      }
      else
      {
        l -> front -> prev = temp;
        l -> rear -> next = temp;
        temp -> next = l -> front;
        temp -> prev = l -> rear;
        l -> front= temp;
      }
      l -> size++;
    }
  }
  return result;
}
如果您想制作一个非循环列表,那么这将是您的代码:

int prepend (list* l, void* item)
{
  int result = 0;
  if (l !=NULL)
  {
    node* temp = malloc(sizeof(node));
    if (temp != NULL)
    {
      result = 1;
      temp -> item = item;
      if (l-> front == NULL)
      {
        temp -> next = NULL;
        temp -> prev = NULL;
        l -> front = temp;
        l -> rear = temp;
      }
      else
      {
        l -> front -> prev = temp;
        temp -> next = l -> front;
        temp -> prev = NULL;
        l -> front= temp;
      }
      l -> size++;
    }
  }
  return result;
}
除此之外,对您的问题给出的注释也适用:我想
get(l,I)
将返回
item
字段,这是一个指针。如果您的平台是64位的,但您尝试将指针打印为int(可能是32位),那么您将遇到问题…将只打印指针的一半。

2问题:

get()被定义为返回void*,但没有return语句,因此实际返回值是函数return位置堆栈上的任何数据

您正在使用%d打印一个void*%d假定为32位整数。void*在x86上是32位,在x86-64上是64位,因此当您尝试在x86-64上打印它时,%d只查看上半部分,最重要的32位,我猜可能都是零


因此,要修复它,请修复get的返回类型以返回int(并实际返回某些内容),或者将printf更改为使用%p,这表示要打印的数据是指针。

您正在传递指向常量整数的指针:(void*)1,这在编译器中可能不会以相同的方式处理。您能否提出问题并添加
节点
和函数
get
的定义?get(l,i)返回什么?我猜基于体系结构,get()返回的数据类型与%d所期望的不兼容。例如,如果%d期望一个32位整数,get返回一个64位整数。@DelmerNicholson
(void*)1
不是一个指向常量整数的指针,而是指向以实现定义的方式从
1
转换而来的某个对象(可能没有任何对象)。@MikeCat-“实现定义”--…这就是我的观点。我认为我的64位平台是个问题,我应该如何解决?我使用了你对我的方法的更正,它仍然没有解决它,而不是使用
printf(“%d”,get(l,I));
尝试使用
printf(“%ld”,get(l,I));
。另外,我不太明白为什么你的项目是空的*。你正在传入(void*)1等等,它们不是真正的指针…原因是什么?如果您的项类型是int、long int或int64_t(只需包含stdint.h以获得各种整数类型;long int只能保证至少与int一样长——尽管实际上通常是64位的),您会更好吗?
int prepend (list* l, void* item)
{
  int result = 0;
  if (l !=NULL)
  {
    node* temp = malloc(sizeof(node));
    if (temp != NULL)
    {
      result = 1;
      temp -> item = item;
      if (l-> front == NULL)
      {
        temp -> next = NULL;
        temp -> prev = NULL;
        l -> front = temp;
        l -> rear = temp;
      }
      else
      {
        l -> front -> prev = temp;
        temp -> next = l -> front;
        temp -> prev = NULL;
        l -> front= temp;
      }
      l -> size++;
    }
  }
  return result;
}