簡而言之,我應該有“ 0 1 2 3 4 5 6 7 8 9 10 ”。當在 CodeBlocks 而不是 0 中執行時,我得到的是亂數。但是當使用在線編譯器編譯時,我得到了預期的輸出。哪個輸出可靠?在線或 CodeblockIDE 的?如果需要,可以使用鏈表練習的代碼:創建、輸入資料元素、在指定點洗掉并最終輸出結果。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node *next;
} SLNode;
void ListInitiate(SLNode **head) /* Initialization */
{
/* If there is memory space, apply the head node space and make the head pointer point to the head node */
if((*head = (SLNode *)malloc(sizeof(SLNode))) == NULL) exit(1);
(*head)->next = NULL; /* set the last of the list as Null */
}
int ListLength(SLNode *head) /* Get the size of the linked list */
{
SLNode *p = head;
int size = 1;
while(p->next != NULL) /*count with loop*/
{
p = p->next;
size ;
}
return size;
}
int ListInsert(SLNode *head, int i, DataType x)
/* Insert a node that contains the data element x before the node ai (0 ≤ i ≤ size) of the linked list with header */
{
SLNode *p, *q;
int j;
p = head; /* p points to the head*/
j = -1; /* the initial value of j is -1*/
while(p->next != NULL && j < i - 1)
/* Finally let pointer p point to data element ai-1 node */
{
p = p->next;
j ;
}
if(j != i - 1)
{
printf("Insert position parameter wrong!");
return 0;
}
/* q points to the new node*/
if((q = (SLNode *)malloc(sizeof(SLNode))) == NULL) exit(1);
q->data = x;
/* There is an error blow*/
q->next = p->next;
p->next = q;
return 1;
}
int ListDelete(SLNode *head, int i, DataType *x)
/* delete the node ai of the list with a header*/
/* put the data element of the node in x. If success, return 1; if fail, return 0*/
{
SLNode *p, *s;
int j;
p = head;
j = -1;
while(p->next != NULL && p->next->next!= NULL && j < i - 1)
/*Finally let pointer p point to data element ai-1 node */
{
p = p->next;
j ;
}
if(j!= i - 1)
{
printf("Insert position parameter wrong!");
return 0;
}
s = p->next; /*s points to ai*/
*x = s->data; /*Assign the data field value of the node pointed by pointer s to x */
p->next = s->next; /* delete ai*/
free(s); /* free the memory space of s */
return 1;
}
int ListGet(SLNode *head, int i, DataType *x)
/*The function of taking the data element ai is similar to deleting ai function, but do not delete the data element ai node*/
{
SLNode *p;
int j;
p = head;
j = 0;
while(p->next != NULL && j < i)
{
p = p->next;
j ;
}
if(j != i)
{
printf("The position of the parameter is wrong!");
return 0;
}
*x = p->data;
return 1;
}
void Destroy(SLNode **head)
{
SLNode *p, *p1;
p = *head;
while(p != NULL)
{
p1 = p;
p = p->next;
free(p1);
}
*head = NULL;
}
void main(void)
{
SLNode *head;
int i , x;
ListInitiate(&head);
for(i = 0; i < 10; i )
{
if(ListInsert(head, i, i 1) == 0) /*insert ten data elements*/
{
printf("Error!! \n");
return;
}
}
if(ListDelete(head, 4, &x) == 0) /* delete data element 5*/
{
printf("error! \n");
return;
}
for(i = 0; i < ListLength(head); i )
{
if(ListGet(head, i, &x) == 0) /* take out the element*/
{
printf("Error! \n");
return;
}
else printf("%d ", x); /* show data elements*/
}
Destroy(&head);
}
uj5u.com熱心網友回復:
問題在于代碼的邏輯。好的輸出是 Code::Blocks 之一。
問題出在insertNode函式內部。它的行為如下:它找到第 i-1 個節點并在它之后創建一個新節點。如果i = 0,則在您的第一個回圈的第一次迭代中找到第 0 個元素,之后不會有錯誤,因為j = i - 1 = -1. 永遠不要在您的代碼中編輯頭部的資料。此外,每次呼叫insertNode函式時,都要求新節點的值是它的位置 1。
所以你會有這個:
---------- ----- -----
| random | ---> | 1 | ---> | 2 | ---> etc.
---------- ----- -----
head 0th 1st
之后,在listGet函式中,您對串列的制作方式產生了誤解。例如,when will call listGet(head, 0, &x),p->next != NULL && j < i在第一次迭代時總是滿足條件,這意味著回傳的節點不是第 0 個節點,而是第一個節點。你應該改寫:
j = -1; // 0 become -1
while(p->next != NULL && j < i)
{
p = p->next;
j ;
}
此外,您必須更改主回圈的邊界。實際上,listLength回傳串列中元素的數量(此處為 10),但索引不是從 0 開始的 1 bt,這意味著您不能停在 10(因為沒有第 10 個節點)而是停在 9。因此你必須寫:
for(i = 0; i < ListLength(head) - 1; i )
{
if(ListGet(head, i, &x) == 0) /* take out the element*/
{
printf("Error! \n");
return 1;
}
else printf("%d ", x); /* show data elements*/
}
還有一點與此問題無關,但無論如何您都必須更正:您的破壞功能。
void Destroy(SLNode **head)
{
SLNode *p, *p1;
p = *head;
while(p != NULL)
{
p1 = p;
p = p->next;
/* make NULL assignment here */
p1->next = NULL;
free(p1);
}
/* head has been already freed in the above loop
* do not try to manipulate it */
// *head = NULL;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464200.html
上一篇:在C中釋放結構內的結構
