213 lines
3.7 KiB
C
213 lines
3.7 KiB
C
#include "llist.h"
|
|
#include <stdio.h>
|
|
|
|
List *create_list(void *data)
|
|
{
|
|
ListNode *new_node;
|
|
|
|
List *new_list = (List *) malloc(sizeof(List));
|
|
*new_list = (ListNode *) malloc(sizeof(ListNode));
|
|
|
|
new_node = *new_list;
|
|
new_node->data = data;
|
|
new_node->next = NULL;
|
|
|
|
return new_list;
|
|
}
|
|
|
|
List *list_remove(List *list, uint32_t position)
|
|
{
|
|
ListNode *head;
|
|
ListNode *current;
|
|
ListNode *tmp;
|
|
|
|
if (list == NULL || *list == NULL)
|
|
{
|
|
return list;
|
|
}
|
|
|
|
if (position == 0)
|
|
{
|
|
head = *list;
|
|
head = head->next;
|
|
*list = head;
|
|
return list;
|
|
}
|
|
|
|
if (position >= list_size(list))
|
|
{
|
|
current = *list;
|
|
while(current->next)
|
|
{
|
|
tmp = current;
|
|
current=current->next;
|
|
}
|
|
|
|
tmp->next = NULL;
|
|
free(current);
|
|
|
|
return list;
|
|
}
|
|
|
|
current = *list;
|
|
tmp = current;
|
|
while (current && position > 0)
|
|
{
|
|
tmp = current;
|
|
current = current->next;
|
|
position--;
|
|
}
|
|
|
|
tmp->next = current->next;
|
|
free(current);
|
|
|
|
return list;
|
|
}
|
|
|
|
List *list_add(List *list, void *data, uint32_t position)
|
|
{
|
|
ListNode *new_node;
|
|
ListNode *head;
|
|
ListNode *current;
|
|
|
|
if (list == NULL || *list == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
head = *list;
|
|
|
|
if (head == NULL)
|
|
{
|
|
head->data = data;
|
|
head->next = NULL;
|
|
return list;
|
|
}
|
|
|
|
new_node = (ListNode*) malloc(sizeof(ListNode));
|
|
new_node->data = data;
|
|
|
|
if (position == 0)
|
|
{
|
|
new_node->next = *list;
|
|
*list = new_node;
|
|
return list;
|
|
}
|
|
|
|
if (position >= list_size(list))
|
|
{
|
|
current = *list;
|
|
while (current->next)
|
|
{
|
|
current = current->next;
|
|
}
|
|
current->next = new_node;
|
|
return list;
|
|
}
|
|
|
|
/* add at any position */
|
|
current = *list;
|
|
while (current && position-- >1)
|
|
{
|
|
current = current->next;
|
|
}
|
|
|
|
ListNode *tmp = current->next;
|
|
current->next = new_node;
|
|
new_node->next = tmp;
|
|
|
|
return list;
|
|
}
|
|
|
|
void print_list(List *list)
|
|
{
|
|
ListNode *current;
|
|
uint32_t index = 0;
|
|
|
|
if (list == NULL || *list == NULL)
|
|
return;
|
|
|
|
current = *list;
|
|
|
|
while (current)
|
|
{
|
|
printf("At %d value = %p\n", index, current);
|
|
index++;
|
|
current = current->next;
|
|
}
|
|
}
|
|
|
|
void list_delete(List *list)
|
|
{
|
|
ListNode *current;
|
|
ListNode *next;
|
|
|
|
if (list == NULL || *list == NULL)
|
|
return;
|
|
|
|
current = *list;
|
|
while (current->next)
|
|
{
|
|
next = current->next;
|
|
free(current);
|
|
current = next;
|
|
}
|
|
|
|
free(list);
|
|
}
|
|
|
|
uint32_t list_size(List *list)
|
|
{
|
|
uint32_t size = 0;
|
|
ListNode *current;
|
|
|
|
if (list == NULL || *list == NULL)
|
|
return size;
|
|
|
|
current = *list;
|
|
while (current)
|
|
{
|
|
size++;
|
|
current = current->next;
|
|
}
|
|
|
|
return size;
|
|
}
|
|
|
|
void print_size_and_list(List *list)
|
|
{
|
|
printf("size = %d\n", list_size(list));
|
|
print_list(list);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
void *new_data = (void *) malloc(sizeof(void));
|
|
void *new_data2 = (void *) malloc(sizeof(void));
|
|
void *new_data3 = (void *) malloc(sizeof(void));
|
|
void *new_data4 = (void *) malloc(sizeof(void));
|
|
|
|
List *new_list = create_list(new_data);
|
|
print_size_and_list(new_list);
|
|
|
|
list_add(new_list, new_data2, 0);
|
|
print_size_and_list(new_list);
|
|
|
|
list_add(new_list, new_data3, 3);
|
|
print_size_and_list(new_list);
|
|
|
|
list_add(new_list, new_data4, 2);
|
|
print_size_and_list(new_list);
|
|
|
|
list_remove(new_list, 2);
|
|
print_size_and_list(new_list);
|
|
|
|
list_delete(new_list);
|
|
new_list = NULL;
|
|
|
|
print_size_and_list(new_list);
|
|
|
|
return 0;
|
|
}
|
|
|