Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct List * list_create (void) {
- struct List* temp;
- temp = (struct List*) calloc(1, sizeof(struct List));
- temp->head = NULL;
- return temp;
- }
- void list_add_first (struct List * list, Data x) {
- struct Node* temp;
- temp = (struct Node*) calloc(1, sizeof(struct Node));
- temp->next = list->head;
- list->head = temp;
- list->head->val = x;
- }
- void list_add_last (struct List * list, Data x) {
- struct Node* temp;
- struct Node* last = list->head;
- temp = (struct Node*) calloc(1, sizeof(struct Node));
- temp->val = x;
- temp->next = NULL;
- while(last->next != NULL)
- last = last->next;
- last->next = temp;
- }
- Data list_remove_first (struct List * list) {
- Data val;
- struct Node* temp;
- val = list->head->val;
- temp = list->head;
- list->head = list->head->next;
- free(temp);
- return val;
- }
- Data list_remove_last (struct List * list) {
- Data val;
- struct Node* temp = list->head;
- while(temp->next != NULL) {
- temp = temp->next;
- }
- val = temp->val;
- free(temp);
- return val;
- }
- Data list_get_first (struct List * list) {
- return list->head->val;
- }
- Data list_get_last (struct List * list) {
- struct Node* temp = list->head;
- while (temp->next != NULL) {
- temp = temp->next;
- }
- return temp->val;
- }
- void list_print (struct List * list) {
- struct Node* tree = list->head;
- if (list->head == NULL) {
- printf("Empty list\n");
- return ;
- }
- while (tree->next != NULL && tree != NULL) {
- printf("%d ", tree->val);
- tree = tree->next;
- }
- printf("%d \n", tree->val);
- }
- int list_size(struct List * list) {
- struct Node* tree = list->head;
- unsigned int size = 1;
- if (tree == NULL) { ///empty list
- return 0;
- }
- while (tree->next != NULL) {
- size++;
- tree = tree->next;
- }
- return size;
- }
- void list_clear(struct List * list) {
- struct Node* base = list->head;
- struct Node* step = NULL;
- if (base == NULL) /// empty already
- return ;
- while (base->next != NULL) {
- step = base->next;
- free(base);
- base = step;
- step = base->next;
- }
- free(base);
- }
- void list_destroy (struct List * list) {
- list_clear(list);
- free(list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement