#include <stdio.h>
#include <stdlib.h>
typedef struct data {
int a, b;
} Data ;
struct node {
Data info;
int priority;
struct node *link;
};
typedef struct node* Node;
void insert(Node header, int pr, Data el) {
Node cur = header;
Node tmp = malloc(sizeof(struct node));
tmp->info = el;
tmp->priority = pr;
//descending <=
while (cur->link != NULL && pr >= cur->link->priority)
cur = cur->link;
tmp->link = cur->link;
cur->link = tmp;
}
Node delete(Node header) {
Node tmp;
if (header->link == NULL)
printf("Empty priority queue");
else {
tmp = header->link;
header->link = tmp->link;
free(tmp);
return tmp;
}
}
void display(Node head) {
Node cur = head->link;
while (cur != NULL) {
printf("%d ", cur->priority);
cur = cur->link;
}
}
int main(int argc, const char *argv[])
{
Data d;
Node tmp;
Node head = malloc(sizeof(struct node));
head->link = NULL;
insert(head, 3, d);
insert(head, 2, d);
insert(head, 1, d);
while (head->link != NULL) {
tmp = delete(head);
printf("%d ", tmp->priority);
}
return 0;
}
Вывод это 1 2 3. но при удалении я освободил память (free (tmp)) и затем возвратил tmp.почему он все еще печатает в основной функции.используя компилятор gcc