2
列表看起來真的很難。我想找到文件中最小的元素:1 5 8 6 4 8 6 48 9
。它是1,我想刪除那個1
。我可以找到最小的元素,但不能刪除它。我找到最小的元素地方,但沒有價值。我試圖從網上覆制刪除功能,但是我不能理解它,因爲我對C真的很陌生。它寫了一個錯誤,指向不完整類型。請幫忙。發佈整個代碼,因爲它應該更方便理解。c鏈表刪除最小元素
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedList {
int value;
struct linkedList *next;
} linkedList, head;
linkedList *readList(linkedList *head) {
FILE *dataFile;
dataFile = fopen("duom.txt", "r");
if (dataFile == NULL) {
printf("Nepasisekė atidaryti failo\n");
} else {
printf("Duomenų failą pavyko atidaryti\n");
}
while (!feof (dataFile))
if (head == NULL) {
head = malloc(sizeof(linkedList));
fscanf(dataFile, "%d", &head->value);
head->next = NULL;
} else {
struct linkedList *current = head;
struct linkedList *temp = malloc(sizeof(linkedList));
while (current->next != NULL) {
current = current->next;
}
fscanf(dataFile, "%d", &temp->value);
current->next = temp;
temp->next = NULL;
}
return head;
}
void search(linkedList *head, int *lowest) {
int a[100];
int i = 0;
int minimum;
int b = 0;
linkedList *current = head;
while (current != NULL) {
a[i] = current->value;
current = current->next;
i++;
}
b = i;
i = 0;
minimum = a[0];
while (b > 0) {
if (minimum > a[i]) {
minimum = a[i];
lowest = i;
}
i++;
b--;
}
}
void deleteNode(struct node **head_ref, int key) {
struct node* temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return;
prev->next = temp->next;
free(temp);
}
void printList(linkedList *head) {
linkedList *current = head;
while (current != NULL) {
printf("%d->", current->value);
current = current->next;
}
printf("NULL\n");
return;
}
int main() {
linkedList *A = NULL;
A = readList(A);
search(A);
head = head->next;
minimum = head->value;
headk->next = head->next;
free(head);
printList(A);
return 0;
}
「後整個代碼」。對不起,這不是Stackoverflow的工作原理。我們不是在這裏爲你完成你的代碼。 「它寫入一個錯誤,解除引用不完整的類型」。它是什麼」?編譯器?該程序? IDE?請提供正在發生的* exact *錯誤。 – kaylum
'搜索(A);'不同的簽名。 – BLUEPIXY
感謝批評,將改善和做得更好 – AdomasArabella