76
3.c
Normal file
76
3.c
Normal 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);
|
||||
}
|
Reference in New Issue
Block a user