#include <stdio.h>
#include <stdlib.h>
typedef struct PEOPLE
{
char *name;
int age;
}People;
typedef struct Node_
{
int value;
struct Node_ *pre;
struct Node_ *next;
}Node, *NodeList;
Node *create_node(int value)
{
Node * p = (Node*)malloc(sizeof(Node));
if (p == NULL)
{
return NULL;
}
p->pre = p->next = p;
p->value = value;
return p;
}
NodeList create_node_list()
{
NodeList list = (NodeList)malloc(sizeof(Node));
if (!list)
{
return NULL;
}
list->pre = list->next = list;
list->value = -1;
return list;
}
int list_is_empty(NodeList list)
{
if (!list)
{
return -1;
}
if (list->pre == list->next)
{
return 0;
}
return -1;
}
void print_list(NodeList list)
{
if (!list)
{
return;
}
int flag = list_is_empty(list);
if (flag == 0)
{
printf("链表为空!\n");
return;
}
list = list->next;
while(list->value != -1)
{
printf("--> : %d ", list->value);
list = list->next;
}
printf("\n");
}
void insert_node_from_head(NodeList list, int value)
{
Node * node = create_node(value);
list->next->pre = node;
node->next = list->next;
list->next = node;
node->pre = list;
printf("头部插入: %d 完成。\n", value);
}
void insert_node_from_tail(NodeList list, int value)
{
Node *node = create_node(value);
while(list->next->value != -1)
{
list = list->next;
}
node->next = list->next;
list->next = node;
node->pre = list;
printf("尾部插入: %d 完成。\n", value);
}
int delete_node_by_value(NodeList list, int value)
{
list = list->next;
NodeList pre, next, del_node;
while(list->value != -1)
{
if (list->value == value)
{
del_node = list;
pre = list->pre;
next = list->next;
next->pre = pre;
pre->next = next;
printf("删除成功!\n");
return 0;
}
list = list->next;
}
printf("数据为[%d]的元素不存在!\n", value);
return -1;
}
int insert_node_from_pos(NodeList list, int pos, int value)
{
if (NULL == list)
{
return -1;
}
Node * node = create_node(value);
list = list->next;
NodeList current_node = NULL;
while(list->value != -1)
{
if (list->value == pos)
{
printf("找到了数据为[%d]的节点。\n", value);
current_node = list;
node->pre = current_node;
current_node->next->pre = node;
node->next = current_node->next;
current_node->next = node;
printf("在节点 [%d]后插入新节点[%d]\n", pos, value);
return 0;
}
list = list->next;
}
}
int set_value_at_pos(NodeList list, int pos, int value)
{
if (NULL == list)
{
return -1;
}
list = list->next;
while(list->value != -1)
{
if (list->value == pos)
{
list->value = value;
printf("将结点为 [%d] 的值修改为 [%d].\n", pos, value);
return 0;
}
list = list->next;
}
printf("数据为[%d]的元素不存在!\n", value);
return -1;
}
int main(int argc, char const *argv[])
{
NodeList list = create_node_list();
insert_node_from_head(list, -1);
int i = 0;
for (i; i < 5; i++)
{
insert_node_from_head(list, i*2);
insert_node_from_tail(list, i*3);
}
print_list(list);
delete_node_by_value(list, 0);
print_list(list);
insert_node_from_pos(list, 9, 999);
print_list(list);
set_value_at_pos(list, 999, 123456);
print_list(list);
return 0;
}