2016-12-19 2 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

"Post весь код". Извините, это не так, как работает Stackoverflow. Мы не здесь, чтобы заполнить ваш код для вас. «Он пишет ошибку, которая разыменовывает неполный тип». Что это"? Компилятор? Программа? IDE? Пожалуйста, укажите * точную * ошибку, которая происходит. – 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; 
} 
Смежные вопросы