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;
}