Minimalna wartość w liście jednokierunkowej

0

Dzień dobry napisałem taki kod i nie wiem dlaczego ale nie znajduje mi minimum czy ktoś umiałby pomóc ?

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

 struct node {// lista jednukierunkowa ma wskaznik tylko na 1 element
    int data;
    struct node* next;

};

void printList(struct node*n) {// wyswietlanie listy
    if(n==NULL)
        printf("lista jest pusta");
    else
    while (n != NULL) {
        printf("%d ", n->data);
        n = n->next;
    }
}
    void push(struct node** head_ref,int newdata){//dodawanie na poczatek
        struct node* newnode=(struct node*)malloc(sizeof(struct node));
        newnode->data=newdata;
        newnode->next=(*head_ref);// nowy node jako head
        (*head_ref)=newnode;//przemieszcznie sie tego nodea
    }
    void after(struct node*prevnode,int newdata){// dodawanie na srodek
    struct node*newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=newdata;
    newnode->next=prevnode->next;
    prevnode->next=newnode;


}
    void koniec(struct node** head_ref, int newdata){// dodawanie na koniec
    struct node* newnode=(struct node*)malloc(sizeof(struct node));
    struct node *last=*head_ref;
    newnode->data=newdata;
    newnode->next=NULL;
   if(*head_ref==NULL){
       (*head_ref)=newnode;
       return;
   }else
   {
    while(last->next!=NULL)
        last=last->next;
    last->next=newnode;
    return;
   }


}
int Min(struct node**head_ref)
{

    int min = INT_MAX;
    while ((*head_ref)!= NULL) {

        if (min > (*head_ref)->data) {
            min = (*head_ref)->data;
            (*head_ref) = (*head_ref)->next;
        }
    }
    return min ;

}
int Max(struct node**head_ref)
{
    int max = INT_MIN;
    while ((*head_ref)!= NULL) {
        if (max < (*head_ref)->data)
            max = (*head_ref)->data;
        (*head_ref) = (*head_ref)->next;
    }
    return max;
}


bool szukaj(struct node**head_ref, int x)
{
    struct node* current = (*head_ref); // wskazuje na obecny
    while (current != NULL) {
        if (current->data == x)
            return true;
        current = current->next;
    }
    return false;
}
int main() {

    struct node* head = NULL;
    struct node* drugi=NULL;
    struct node* trzeci=NULL;

    head=(struct node*)malloc(sizeof(struct node));
    drugi=(struct node*)malloc(sizeof(struct node));
    trzeci=(struct node*)malloc(sizeof(struct node));

    head->data=9;
    head->next=drugi;

    drugi->data=2;
    drugi->next=trzeci;

    trzeci->data=3;
    trzeci->next=NULL;
    push(&head,12);
    after(drugi,15);
    koniec(&head,31);
    push(&head,142);
    push(&head,122);
    push(&head,142);



    printList(head);
printf("\n");
    szukaj(&head, 185) ? printf("Jest\n") : printf("Nie ma\n");

    int maksim=Max(&head);
    printf("Max %d\n", maksim);
    int minimum=Min(&head);
    printf("Min %d\n", minimum);






    return 0;

}
0

Problemem jest brak zasad formatowania kodu.
Ja nigdy nie napiszę:

if(Condition)
  DoSomeThing();

tylko:

if(Condition) DoSomeThing();

lub:

if(Condition)
{
  DoSomeThing();
}

Porównaj Min z Max a zobaczysz w czym problem.

0
_13th_Dragon napisał(a):

Problemem jest brak zasad formatowania kodu.
Ja nigdy nie napiszę:

if(Condition)
  DoSomeThing();

tylko:

if(Condition) DoSomeThing();

lub:

if(Condition)
{
  DoSomeThing();
}

Porównaj Min z Max a zobaczysz w czym problem.

Ma Pan rację, ale nawet po zmianie nie wyświetla wartości minimalnej.

5

Weź debugger i przeleć krok po kroku, patrząc co się dzieje. Kod, w takiej postaci, w jakiej go tutaj wrzuciłeś, nie nadaje się do czytania i analizy…

3

Z tego co widzę, ten kod jest skażony C++. Kastowanie wartości zwracanej z malloc nie jest potrzebne.
Co do błędu:

int Min(struct node** head_ref)
{
    int min = INT_MAX;
    while ((*head_ref) != NULL) {
        if (min > (*head_ref)->data) {
            min = (*head_ref)->data;
            (*head_ref) = (*head_ref)->next; // to jest tutaj 
        } // i tutaj
    }
    return min;
}

I podobny problem w Max, co ma wpływ na wywołanie Min.

Zresztą argument jest też przekombinowany.

Polecam też połatać wycieki pamięci, to błąd też się znajdzie: https://godbolt.org/z/n8cdjvbvb (twój oryginalny kod tylko z clang format + flagi dla kompilatora)

1 użytkowników online, w tym zalogowanych: 0, gości: 1