Struktura danych

Otrzymałeś(aś) rozwiązanie do zamieszczonego zadania? - podziękuj autorowi rozwiązania! Kliknij
Robakks
Czasem tu bywam
Czasem tu bywam
Posty: 149
Rejestracja: 30 wrz 2012, 20:36
Podziękowania: 2 razy
Otrzymane podziękowania: 13 razy
Płeć:

Struktura danych

Post autor: Robakks »

Próbowałem napisać listę ale oto co wyszło

Kod: Zaznacz cały

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

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


int isEmpty(struct Node*);
void push(struct Node**,int);
int pop(struct Node**);


int main()
{
    int k;
    int val;
    struct Node * head;
    head=NULL;
    srand(time(NULL));
    for(k=1; k<=24; k++)
    {
        val=rand()%100;
        push(&head,val);
    }

    while(!isEmpty(head))
    {
        printf("%d \n",pop(&head));
    }
    while(!kbhit())
    {
    }

}

int isEmpty(struct Node* head)
{
    return (head==NULL);
}

void push(struct Node** head,int n)
{
    struct Node* newNode;
    newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data=n;
    newNode->next=(*head) ;
    (*head)=newNode;
}

int pop(struct Node** head)
{
    int val;
    struct Node* temp;
    if(!isEmpty(*head))
    {
        temp=(*head);
        val=temp->data;
        (*head)=(*head)->next;
        free(temp);
        return val;
    }
    else
    {
        printf("Struktura jest pusta\n");
        return;
    }
}
Przydałoby się dodać sprawdzanie dostępnej pamięci na węzeł
Nie udało mi się dodać funkcji aby struktura bardziej przypominała listę
ODPOWIEDZ