数据结构 3 双链表
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct Node{
int data;
struct Node* pre;
struct Node* next;
}Node;
Node* InitaNode()
{
Node* Head = (Node*)malloc(sizeof(Node));
Head->data = 0;
Head->next = NULL;
Head->pre = NULL;
return Head;
}
void HeadInsert(Node* list, int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->pre = list;
node->next = list->next;
list->data++;
if(list->next)
{
list->next->pre = node;
}
list->next = node;
}
void TailInsert(Node* list, int data)
{
Node* CurrentNode = list->next;
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
while(CurrentNode->next != NULL)
{
CurrentNode = CurrentNode->next;
}
node->next = CurrentNode->next;
CurrentNode->next = node;
node->pre = CurrentNode;
list->data++;
}
int DeleteNum(Node* list, int data)
{
Node* CurentNode = list->next;
while(CurentNode)
{
if(CurentNode->data == data)
{
CurentNode->pre->next = CurentNode->next;
if(CurentNode->next)
CurentNode->next->pre = CurentNode->pre;
list->data--;
free(CurentNode);
return TRUE;
}
CurentNode = CurentNode->next;
}
return FALSE;
}
void PrintList(Node* list)
{
Node* node = list->next;
while(node)
{
printf("%d->", node->data);
node = node->next;
}
printf("NULL\n");
}
void main(void)
{
Node* HeadNode = InitaNode();
for(int i = 0; i < 6; i++)
{
HeadInsert(HeadNode, i);
}
for(int i = 1; i < 6; i++)
{
TailInsert(HeadNode, i);
}
DeleteNum(HeadNode,1);
DeleteNum(HeadNode,0);
PrintList(HeadNode);
}