#include <stdio.h>
#include <stdlib.h>
typedef struct double_list
{
int data;
struct double_list *pre;
struct double_list *next;
}List;
List *initList()
{
List *node = (List *)malloc(sizeof(List));
node->data = 0;
node->pre = NULL;
node->next = NULL;
return node;
}
void headInsert(List *l, int data)
{
List *node = (List *)malloc(sizeof(List));
node->data = data;
node->next = l->next;
node->pre = l;
if(l->next)
{
l->next->pre = node;
l->next = node;
}
else
{
l->next = node;
}
}
void tailInsert(List *l, int data)
{
List *node = l;
List *new = (List *)malloc(sizeof(List));
new->data = data;
while (node->next)
{
node = node->next;
}
new->next = node->next;
node->next = new;
new->pre = node;
}
void deleteNode(List *l, int data)
{
List *node = l->next;
while(node)
{
if(node->data == data)
{
node->pre->next = node->next;
if(node->next)
{
node->next->pre = node->pre;
}
free(node);
break;
}
node = node->next;
}
}
void printList(List *l)
{
List *node = l->next;
while (node)
{
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main()
{
List *l = initList();
headInsert(l, 0);
headInsert(l, 1);
headInsert(l, 2);
headInsert(l, 3);
tailInsert(l, 4);
tailInsert(l, 5);
printList(l);
deleteNode(l, 2);
deleteNode(l, 5);
printList(l);
}
暂无评论