else
{
pre = list->head;
while(pre->next != node)
pre = pre->next;
node_delete(node, pre);
}
return list;
}
nodetype * list_add(listtype *list, int data)
{
nodetype *cur, *temp;
for (cur = list->head;
cur != NULL && cur->next != NULL;
cur = cur->next)
;
temp = (nodetype *)malloc(sizeof(nodetype));
temp->data = data;
temp->next = NULL;
if (list->length == 0)
list->head = temp;
else
cur->next = temp;
list->length++;
return temp;
}
void error(int err)
{
switch(err)
{
case 1:
printf("RAM error!\n");
break;
case 2:
printf("Length error!\n");
break;
case 3:
printf("Location error!\n");
break;
default:
printf("Unknown error!\n");
}
exit(EXIT_FAILURE);
}
以下是一些功能的测试:
test.c:
[cpp]
#include <stdio.h>
#include "list.h"
int main(void)
{
listtype *list;
int length, location, data;
list = list_create();
for (;;)
{
printf("Please input the length of the list:");
scanf("%d", &length);
if (length <= 0)
break;
list_input(list, length);
list_print(list);
}
for (;;)
{
printf("Please input the data you want to insert:");
scanf("%d", &data);
printf("The location:");
scanf("%d", &location);
if (location < 0)
break;
list_insert(list, location, data);
list_print(list);
}
for(;;)
{
printf("Please input the data you want to delete:");
scanf("%d", &data);
if(data == 0)
break;
list_delete(list, data);
list_print(list);
}
return 0;
}