Files
dslab/7.c
Amneesh Singh c460e95304 exp 3,5,6,7: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-27 19:40:13 +05:30

67 lines
922 B
C

#include <stdio.h>
#include <stdlib.h>
typedef struct Node node;
struct Node {
int id;
struct Node *next;
};
typedef struct Queue queue;
struct Queue {
node *head;
node *tail;
};
int peek(queue *q) { return q->head->id; }
void insert(queue *q, int id) {
node *new = malloc(sizeof(node));
new->id = id;
new->next = NULL;
if (q->tail)
q->tail->next = new;
else
q->head = new;
q->tail = new;
}
void delete (queue *q) {
if (!q->head)
return;
node *next = q->head->next;
free(q->head);
q->head = next;
if (!q->head)
q->tail = NULL;
}
void display(queue *q) {
node *cur = q->head;
while (cur != NULL) {
printf("%d ", cur->id);
cur = cur->next;
}
printf("\n");
}
int main() {
queue *q = malloc(sizeof(queue));
insert(q, 56);
insert(q, 3);
insert(q, 99);
delete(q);
display(q);
// free
while (q->head != NULL)
delete(q);
free(q);
}