exp 3,5,6,7: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-12-27 19:40:13 +05:30
parent 8fa27b556e
commit c460e95304
9 changed files with 266 additions and 3 deletions

76
3.c Normal file
View File

@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Employee employee;
struct Employee {
int eid;
struct Employee *next;
struct Employee *prev;
};
typedef struct Employees employees;
struct Employees {
uint len;
struct Employee *head;
struct Employee *tail;
};
employees *init() {
employees *list = malloc(sizeof(employees));
list->len = 0;
list->head = NULL;
list->tail = NULL;
return list;
}
void insert(employees *list, uint eid) {
employee *new = malloc(sizeof(employee));
new->eid = eid;
new->prev = NULL;
new->next = list->head;
if (list->head)
list->head->prev = new;
else
list->tail = new;
list->head = new;
list->len++;
}
void delete (employees *list) {
employee *prev = list->tail->prev;
free(list->tail);
if (prev) {
prev->next = NULL;
if (prev->prev == NULL)
list->head = prev;
}
list->tail = prev;
list->len--;
}
int main() {
employees *list = init();
insert(list, 4);
insert(list, 20);
insert(list, 300);
insert(list, 44);
delete (list);
employee *cur = list->head;
while (cur != NULL) {
printf("%d ", cur->eid);
cur = cur->next;
}
// free
while (list->tail != NULL)
delete (list);
free(list);
}