You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.2 KiB
58 lines
1.2 KiB
#include "list.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
Node * ListCreateNode(int *data){
|
|
Node *new = (Node *) malloc(sizeof(Node));
|
|
new->data = *data;
|
|
return new;
|
|
}
|
|
|
|
void ListPrintForward(List *list){
|
|
Node *current = list->head;
|
|
|
|
printf("\n[");
|
|
while(current != 0){
|
|
printf("%d,", current->data);
|
|
current = current->next;
|
|
}
|
|
printf("]\n");
|
|
}
|
|
|
|
void ListInsertFront(List *list, int data){
|
|
Node *new = ListCreateNode(&data);
|
|
|
|
if(list->head == 0){
|
|
list->head = new;
|
|
list->tail = new;
|
|
}
|
|
else if(list->head == list->tail){
|
|
list->head = new;
|
|
list->head->next = list->tail;
|
|
list->tail->prev = list->head;
|
|
}
|
|
else{
|
|
list->head->prev = new;
|
|
new->next = list->head;
|
|
list->head = new;
|
|
}
|
|
}
|
|
|
|
void ListInsertBack(List *list, int data){
|
|
Node *new = ListCreateNode(&data);
|
|
|
|
if(list->head == 0){
|
|
list->head = new;
|
|
list->tail = new;
|
|
}
|
|
else if(list->head == list->tail){
|
|
list->tail = new;
|
|
list->head->next = list->tail;
|
|
list->tail->prev = list->head;
|
|
}
|
|
else{
|
|
list->tail->next = new;
|
|
new->prev = list->tail;
|
|
list->tail = new;
|
|
}
|
|
} |