Stos nie działa

0

a dokładnie funkcja print bo robi się w nieskończoność


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

typedef struct list {
    int data;
    struct list* next;
} list_element;

int push_front(list_element* head, int n)
{
    list_element* tmp = (list_element*)malloc(sizeof(list_element));
    if (tmp == NULL) {
        puts("no memory");
        return 0;
    }
    tmp->data = n;
    tmp->next = head;
    head = tmp;
    return 1;
}
void print(list_element* head)
{
    printf("%d", head->data);
    print(head->next);
    if (head == NULL)
        return;
}

int main()
{
    list_element* first = NULL;

    int choice;
    int end_program;

    while (end_program != 0) {

        scanf("%d", &choice);

        switch (choice) {
        case 1:
            puts("Give me a number to add:");
            scanf("%d", &choice);
            push_front(first, choice);
            break;
        case 2:
            print(first);
            break;
        default:
            puts("Blad!");
            break;
        }
        puts("you continue?:");
        scanf("%d", &end_program);
    }

    return 0;
}


Format! Style:

C++ online code formatter © 2014 by KrzaQ

Powered by vibe.d, the D language and clang-format
0
void print(list_element* head)
{
    printf("%d", head->data);
    print(head->next);
    if (head == NULL)
        return;
}

Ta pętla nigdy się nie skończy, gdyż nigdy nie dochodzi do sprawdzenia warunku. Zanim dochodzi do sprawdzenia funkcja wywołuje samą siebie.
Umieść warunek na początku funkcji:

void print(list_element* head)
{
    if (head == NULL)
        return;
    printf("%d", head->data);
    print(head->next);
}
1
    tmp->next = head;
    head = tmp;

Hmmmm

0

aha, aha, no nie wiem dlaczgeo moja lista jest pusta, po dodaniu do niej liczb

0

tera działa, first = tmp;
no ale, dlaczego przecież jest przekazane przez wskażnik, więc powinno oddziaływać na first i go zmieniać nie rozumiem

0

Przekazujesz do funkcji kopię wskaźnika i tę kopię modyfikujesz. Musisz przekazać wskaźnik do first:

int push_front(list_element** head, int n)
{
    list_element* tmp = (list_element*)malloc(sizeof(list_element));
    if (tmp == NULL) {
        puts("no memory");
        return 0;
    }
    tmp->data = n;
    tmp->next = *head;
    *head = tmp;
    return 1;
}

push_front(&first, choice);
0

ale przecież first jest już adresem nie? to mam przekazać adres adresu

0

Ta gwiazdka oznacza jedynie tyle, że zmienna przechowuje adres. Przypisanie wartości do zmiennej nie modyfikuje danej wskazywanej przez ten adres a jedynie ten adres. Musisz przekazać adres wskaźnika.

0

omg teraz to jest pomieszane

2
int a;        // zmienna przechowuje liczbę
int* b;       // zmienna przechowuje adres
int* c = &a;  // zmienna c przechowuje adres zmiennej a
int** d = &b; // zmienna d przechowuje adres zmiennej b
*c = 5;       // zapisujemy wartość w zmiennej a
*d = NULL;    // zapisujemy adres w zmiennej b
0
void print(list_element** head)
{   if (*head == NULL)
        return;
    printf("\t%d", (*head)->data);
    print((*head)->next);

}

wywala mi bład podczas wypisywania wartośći

0

Błąd wynika z priorytetów operatorów. Operator -> ma większy priorytet od operatora *. *head musisz umieścić w nawiasie.

0

push back nie działa (zadanie -dodaje element na koniec listy)
dodatkwoo wpadłem na szlony pomysł jak usunąć całą listę, jest w case 8, nie wiem czy działa,się pytam

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

typedef struct list {
    int data;
    struct list* next;
} list_element;

int push_front(list_element** head, int n)
{
    list_element* tmp = (list_element*)malloc(sizeof(list_element));
    if (tmp == NULL) {
        puts("no memory");
        return 0;
    }
    tmp->data = n;
    tmp->next = *head;
    *head = tmp;
    return 1;
}
void push_back(list_element** head, int n)
{

    if (*head == NULL) {
        list_element* tmp = (list_element*)malloc(sizeof(list_element));
        if (tmp == NULL) {
            puts("no memory");
            return;
        }
        tmp->data = n;
        tmp->next = (*head)->next;
        (*head)->next = tmp;
        return;
    }
    else
        push_back(&(*head)->next, n);
}

void print(list_element** head)
{
    if (*head == NULL)
        return;
    printf("\t%d", (*head)->data);
    print(&(*head)->next);
}

void size(list_element* head)
{
    int amount = 0;
    for (; head != NULL; head = head->next)
        ++amount;
    printf("%d", amount);
}
void pop_front(list_element** head)
{
    list_element* tmp;
    tmp = *head;
    *head = (*tmp).next;
    free(tmp);
}

/*void pop_back(list_element** head)
{for(; ; head=(*head)->next)
    if((head=(*head)->next) ==NULL)
        free((*head)->next);


}
*/

int main()
{
    list_element* first = NULL;

    int choice;
    int end_program;

    while (end_program != 0) {

        scanf("%d", &choice);

        switch (choice) {
        case 1:
            puts("Give me a number to add:");
            scanf("%d", &choice);
            push_front(&first, choice);
            break;
        case 2:
            print(&first);
            break;
        case 3:
            size(first);
            break;
        case 4:
            pop_front(&first);
            break;
        case 5:
            puts("Give me a number to add:");
            scanf("%d", &choice);
            push_back(&first, choice);
            break;
        case 6:
            //  pop_back(&first);
            break;
        case 8:
            free(first);
            first = NULL;
            break;
        default:
            puts("Blad!");
            break;
        }
        puts("\nyou continue?:");
        scanf("%d", &end_program);
    }

    return 0;
}

0

Formatuj kod należycie - nie da się go zrozumieć.

1
void push_back(list_element** head, int n)
{
    list_element* tmp = (list_element*)malloc(sizeof(list_element));
    if (tmp == NULL) 
    {
        puts("no memory");
        return ;
    }

    tmp->data = n;
    tmp->next = NULL;
 
    if(*head == NULL)
    {
        *head = tmp;
    }
    else 
    {
        list_element** last = &(*head)->next;
        while(*last != NULL)
            last = &(*last)->next;
        *last = tmp;
    }
 }

Powinieneś jednak dodać do kodu zmienną, która przechowywałaby adres ostatniego węzła, aby czas dodawania na końcu listy był rzędu O(1) a więc taki sam, jak czas dodawania elementu na początku listy.

0

Dobra poradzilem sobie z tym, jeśli ktoś ma pomysł jak napisać funkcję insert_before, dodającą element przed określonym elementem to będzie fajnie(w kodzie jest funkcja którą zrobiłem dodającą element po określonym elemencie-insert_after)

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

typedef struct list {
    int data;
    struct list* next;
} list_element;

int push_front(list_element** head, int n)
{
    list_element* tmp = (list_element*)malloc(sizeof(list_element));
    if (tmp == NULL) {
        puts("no memory");
        return 0;
    }
    tmp->data = n;
    tmp->next = *head;
    *head = tmp;
    return 1;
}

void print(list_element** head)
{
    if (*head == NULL)
        return;
    printf("\t%d", (*head)->data);
    print(&(*head)->next);
}

void size(list_element* head)
{
    int amount = 0;
    for (; head != NULL; head = head->next)
        ++amount;
    printf("%d", amount);
}
void pop_front(list_element** head)
{
    list_element* tmp;
    tmp = *head;
    *head = (*tmp).next;
    free(tmp);
}
void push_back(list_element** head, int n)
{
    list_element* tmp = (list_element*)malloc(sizeof(list_element));
    if (tmp == NULL) {
        puts("no memory");
        return;
    }

    tmp->data = n;
    tmp->next = NULL;

    if (*head == NULL) {
        *head = tmp;
    }
    else {
        list_element** last = &(*head)->next;
        while (*last != NULL)
            last = &(*last)->next;
        *last = tmp;
    }
}

void pop_back(list_element** head)
{
    list_element* tmp = *head;
    list_element* preavious = NULL;
    while (tmp->next) {
        preavious = tmp;
        tmp = tmp->next;
    }
    if (preavious == NULL) {
        free(head);
        head = NULL;
    }
    else {
        preavious->next = NULL;
        free(tmp);
    }
}

void insert_after(list_element* head, int n)
{
    int choice, i;
    printf("\nafter element(1,2..):");
    scanf("%d", &choice);

    for (i = 1; i < choice; ++i) {
        head = head->next;
    }
    list_element* tmp = (list_element*)malloc(sizeof(list_element));
    tmp->data = n;
    tmp->next = head->next;
    head->next = tmp;
}

void options()
{
    puts("1:push_front");
    puts("2:print list");
    puts("3:size list");
    puts("4:pop_front");
    puts("5:push_back");
    puts("6:pop_back");
    puts("7:insert_after");
    puts("8:clear list");
    puts("0:end program");
}

int main()
{
    list_element* first = NULL;

    int choice;
    int end_program;

    while (end_program != 0) {
        options();
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            printf("\nGive me a number to add:");
            scanf("%d", &choice);
            push_front(&first, choice);
            break;
        case 2:
            print(&first);
            break;
        case 3:
            size(first);
            break;
        case 4:
            pop_front(&first);
            break;
        case 5:
            printf("\nGive me a number to add:");
            scanf("%d", &choice);
            push_back(&first, choice);
            break;
        case 6:
            pop_back(&first);
            break;
        case 7:
            printf("\nGive me a number to add:");
            scanf("%d", &choice);
            insert_after(first, choice);
            break;
        case 8:
            free(first);
            first = NULL;
            break;
        default:
            puts("Blad!");
            break;
        }
        printf("\nyou continue?(0 - end program):");
        scanf("%d", &end_program);
    }

    return 0;
}

0
void insert_after(list_element** head, int n)
{
    if(*head ==NULL) {
        puts("no element");
        return;
    }
    int choice, i;
    printf("\nafter element(1,2..):");
    scanf("%d", &choice);

    for (i = 1; i < choice; ++i) {
        *head = (*head)->next;
    }
    list_element* tmp = (list_element*)malloc(sizeof(list_element));
    tmp->data = n;
    tmp->next = (*head)->next;
    (*head)->next = tmp;
}

```dobra jest, NIECH MI KTOS powie dlaczego bez ** ta poprzednia wersja działała poprawnie
0

Mam pytanie, co się dzieje gdy w casie 8 wykonuje instrukcje free(first) ?

0

popatrzalem do internetu i jest to zrobione tak


void UsunWszystkie(Wezel *&poczatek) 
{ Wezel *tmp; 
 while (poczatek != NULL) 
 { tmp = poczatek; 
 poczatek = tmp->nastepny; 
 free(tmp); 
 }
} ale to wynika z tego że tmp przypisujemy początek, no troche to jakieś dziwne, bo usuwamy pamięc tmp, przechodziy dalej, i dlaczego niby miało się ususnać poczatek, przecież tmp przchowuje adres, poczatku, ale nie ma tam ** , nie wiem 

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