// 双向链表的创建、删除、插入、修改、查找
#include <stdio.h>
#include <stdlib.h>
typedef struct linklist
{
int data;
struct linklist *prev;
struct linklist *next;
} LinkList;
typedef LinkList Node;
LinkList *list_creat()
{
LinkList *head = NULL;
}
LinkList *list_delete(LinkList *head, int value)
{
Node *p = head;
Node *pre = NULL;
while (p)
{
if (p->data == value)
{
if (p == head)
{
head = head->next;
head->prev = NULL;
}
else if (p->next == NULL)
{
pre->next = NULL;
p->prev == NULL;
}
else
{
pre->next = p->next;
p->next->prev = pre;
}
Node *temp = p;
p = p->next;
free(temp);
}
else
{
pre = p;
p = p->next;
}
}
return head;
}
LinkList *list_insert(LinkList *head, int value)
{
Node *p = (Node *)malloc(sizeof(Node));
p->data = value;
p->next = NULL;
p->prev = NULL;
if (head == NULL)
{
head = p;
return head;
}
else
{
Node *cur = head;
Node *pre = NULL;
while (cur != NULL)
{
if (cur->data > value)
break;
pre = cur;
cur = cur->next;
}
if (cur == head)
{
p->next = head;
head->prev = p;
head = p;
}
else if (cur == NULL)
{
pre->next = p;
p->next = cur;
p->prev = pre;
}
else
{
pre->next = p;
p->next = cur;
p->prev = pre;
cur->prev = p;
}
}
return head;
}
LinkList *list_change(LinkList *head)
{
}
LinkList *list_find(LinkList *head)
{
}
int main(int argc, char *argv[])
{
LinkList *head = list_creat();
head = list_insert(head, 1);
head = list_insert(head, 2);
head = list_insert(head, 3);
head = list_insert(head, 4);
head = list_insert(head, 0);
head = list_insert(head, 2);
Node *temp = head;
while (temp->next != NULL)
temp = temp->next;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->prev;
}
printf("\n");
temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
head = list_delete(head, 0);
temp = head;
while (temp->next != NULL)
temp = temp->next;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->prev;
}
printf("\n");
temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
head = list_delete(head, 2);
temp = head;
while (temp->next != NULL)
temp = temp->next;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->prev;
}
printf("\n");
temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
head = list_delete(head, 4);
temp = head;
while (temp->next != NULL)
temp = temp->next;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->prev;
}
printf("\n");
temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
return 0;
}
双向链表的创建,有序插入 删除
最新推荐文章于 2022-02-23 00:21:44 发布