2016-12-19 89 views
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; 
} 
+3

「後整個代碼」。對不起,這不是Stackoverflow的工作原理。我們不是在這裏爲你完成你的代碼。 「它寫入一個錯誤,解除引用不完整的類型」。它是什麼」?編譯器?該程序? IDE?請提供正在發生的* exact *錯誤。 – kaylum

+1

'搜索(A);'不同的簽名。 – BLUEPIXY

+1

感謝批評,將改善和做得更好 – AdomasArabella

回答

1

請嘗試,如果這個程序可以幫助你。

#include <stdio.h> 
#include <stdlib.h> 
#include <limits.h> 

struct node { 
    int data; 
    struct node *next; 
}; 

void push(struct node **head_ref, int new_data) { 
    struct node *new_node = (struct node *) malloc(sizeof(struct node)); 
    new_node->data = new_data; 
    new_node->next = (*head_ref); 
    (*head_ref) = new_node; 
} 

void deleteNode(struct node **head_ref, int key) { 
    struct node *temp = *head_ref, *prev; 
    if (temp != NULL && temp->data == key) { 
     *head_ref = temp->next; 
     free(temp); 
     return; 
    } 
    while (temp != NULL && temp->data != key) { 
     prev = temp; 
     temp = temp->next; 
    } 
    if (temp == NULL) return; 
    prev->next = temp->next; 
    free(temp); 
} 

void printList(struct node *node) { 
    while (node != NULL) { 
     printf(" %d ", node->data); 
     node = node->next; 
    } 
} 

void min(struct node **q) { 
    struct node *r; 
    int min = INT_MAX;; 
    r = *q; 
    while (r != NULL) { 
     if (r->data < min) { 
      min = r->data; 
     } 
     r = r->next; 
    } 
    printf("The min is %d", min); 
    deleteNode(q, min); 
    printf("\n"); 
} 

int main() { 
    struct node *head = NULL; 
    FILE *file = fopen("duom.txt", "r"); 
    int i = 0; 
    fscanf(file, "%d", &i); 
    while (!feof(file)) { 
     push(&head, i); 
     fscanf(file, "%d", &i); 
    } 
    fclose(file); 
    puts("Created Linked List: "); 
    printList(head); 
    min(&head); 
    puts("\nLinked List after Deletion of minimum: "); 
    printList(head); 
    return 0; 
} 

文件duom.txt

1 5 8 6 4 8 6 48 9 

測試

./a.out 
Created Linked List: 
9 48 6 8 4 6 8 5 1 The min is 1 

Linked List after Deletion of minimum: 
9 48 6 8 4 6 8 5 ⏎    
2

這樣

#include <stdio.h> 
#include <stdlib.h> 
#include <limits.h> 

typedef struct linkedList{ 
    int value; 
    struct linkedList *next; 
} linkedList, node; 

linkedList *readList(void){ 
    FILE *dataFile; 
    dataFile = fopen("duom.txt", "r"); 
    if(dataFile == NULL) { 
     perror("file open"); 
     return NULL; 
    } 

    int v; 
    node head = {0, NULL}, *curr = &head; 
    while (1 == fscanf(dataFile, "%d", &v)){ 
     node *new_node = malloc(sizeof(node)); 
     if(new_node == NULL){ 
      perror("malloc"); 
      break; 
     } 
     new_node->value = v; 
     new_node->next = NULL; 
     curr = curr->next = new_node; 
    } 
    fclose(dataFile); 
    return head.next; 
} 

int searchMin(linkedList *head){ 
    if(head == NULL){ 
     fprintf(stderr, "%s: The list MUST NOT be NULL.\n", __func__); 
     return INT_MIN; 
    } 
    int min = head->value; 
    node *p = head->next; 
    while(p){ 
     if(p->value < min) 
      min = p->value; 
     p = p->next; 
    } 
    return min; 
} 

void deleteNode(node **head_ref, int key){ 
    node *curr = *head_ref, *prev = NULL; 

    while (curr != NULL && curr->value != key){ 
     prev = curr; 
     curr = curr->next; 
    } 

    if (curr == NULL) return;//not found 
    if(prev) 
     prev->next = curr->next; 
    else 
     *head_ref = curr->next; 
    free(curr); 
} 

void printList(linkedList *head){ 
    node *current = head; 

    while (current != NULL) { 
     printf("%d->", current->value); 
     current = current -> next; 
    } 
    puts("NULL"); 
} 

void freeList(linkedList *list){ 
    while(list){ 
     node *temp = list; 
     list = list->next; 
     free(temp); 
    } 
} 

int main(void){ 
    linkedList *A = readList(); 
    int min = searchMin(A); 
    printList(A); 
    deleteNode(&A, min); 
    printList(A); 
    freeList(A); 

    return 0; 
}