#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node* pre;
struct Node* next;
}Node;
Node* initlist()
{
Node* head=(Node*)malloc(sizeof(Node));
head->data=0;
head->next=head;
head->pre=head;
}
void head_insert(Node* head,int data)
{
Node* node=(Node*)malloc(sizeof(Node));
node->data=data;
if(head->data==0)
{
node->pre=head;
node->next=head->next;
head->next=node;
head->pre=node;
head->data++;
}
else
{
node->next=head->next;
node->pre=head;
head->next->pre=node;
head->next=node;
head->data++;
}
}
void tail_list(Node* head,int data)
{
Node* node=(Node*)malloc(sizeof(Node));
Node* n=(Node*)malloc(sizeof(Node));
n->data=data;
node=head;
while(node->next!=head)
{
node=node->next;
}
n->pre=node;
n->next=head;
node->next=n;
head->pre=n;
head->data++;
}
void delete_list(Node* head,int data)
{
Node* node=(Node*)malloc(sizeof(Node));
node=head->next;
while(node!=head)
{
if(node->data==data)
{
node->pre->next=node->next;
node->next->pre=node->pre;
free(node);
head->data--;
}
node=node->next;
}
}
void print_list(Node* head)
{
Node* node=(Node*)malloc(sizeof(Node));
node=head->next;
while(node!=head)
{
printf("%d ",node->data);
node=node->next;
}
}
int main()
{
Node* list=initlist();
head_insert(list,1);
head_insert(list,2);
head_insert(list,3);
tail_list(list,4);
tail_list(list,5);
print_list(list);
printf("\n");
delete_list(list,2);
print_list(list);
return 0;
}
双向循环链表
最新推荐文章于 2024-07-23 18:51:15 发布