我有一個在unix平臺上創建的雙鏈表的程序,它只能找到。我只是複製粘貼代碼到我的Mac上的日食。對於一些奇怪的原因代碼運行正常,但無論何時添加或刪除或根本什麼做它總是顯示列表中的所有指標爲0我的雙鏈表中的所有內容都變成了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");
}
}
它會打印出
Index [0]: 0
Index [1]: 0
Index [2]: 0
它的工作原理罰款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;
}
get方法:
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;
}
}
你傳遞一個指向常整型:(無效*)1,可能不處理跨編譯器的方式相同。 – nicomp
你可以[編輯]你的問題,並添加'node'和函數'get'的定義? – usr2564301
獲得(l,i)的回報是什麼?我的猜測是,基於體系結構,get()返回的數據類型與%d期望的內容不兼容。例如,如果%d期望一個32位整數並且返回一個64位整數。 – DBug