int temp;
nodetype *cur, *next;
if (num <= 0)
error(2);
cur = list->head;
if (cur == NULL)
{
scanf("%d", &temp);
cur = (nodetype *)malloc(sizeof(nodetype));
cur->data = temp;
num--;
list->length = 1;
list->head = cur;
} else{
while (cur->next != NULL)
cur = cur->next;
}
while (num--)
{
scanf("%d", &temp);
next = (nodetype *)malloc(sizeof(nodetype));
next->data = temp;
cur->next = next;
cur = next;
list->length++;
}
cur->next = NULL;
return list;
}
listtype * list_insert(listtype *list, int location, int data)
{
nodetype *temp, *cur;
cur = list->head;
if (location > 0 && location <= list->length)
{
while(--location)
cur = cur->next;
node_insert(cur, data);
}
else if(location == 0)
{
temp = (nodetype *)malloc(sizeof(nodetype));
temp->data = data;
temp->next = list->head;
list->head = temp;
}
else
{
error(3);
}
list->length++;
return list;
}
nodetype * node_delete(nodetype *cur, nodetype *pre)
{
pre->next = cur->next;
free(cur);
return pre;
}
int list_delete(listtype *list, int data)
{
nodetype *cur, *pre;
for (cur = list->head, pre = NULL;
cur->data != data && cur != NULL;
pre = cur, cur = cur->next)
;
if (cur == NULL)
{
printf("Can't find the data!\n");
return 0;
}
else if(cur != list->head)
{
node_delete(cur, pre);
}
else if(cur == list->head)
{
list->head = cur->next;
free(cur);
}
list->length--;
return data;
}
listtype * list_delete_node(listtype *list, nodetype *node)
{
nodetype *pre;
if (node == list->head)
{
list->head = node->next;
free(node);
list->length--;
}