#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
typedef struct lint
{
int data;
struct lint *prev;
struct lint *next;
} lint, *plist;
void stamp(plist head);
//创建头节点
plist LOCK(void)
{
plist head = malloc(sizeof(lint));
if (head == NULL)
{
printf("head errer\n");
return NULL;
}
head->data = 0;
head->prev = head;
head->next = head;
return head;
}
//创建新节点
plist Her_a(int data)
{
plist new = malloc(sizeof(lint));
if (new == NULL)
{
printf("new erreo\n");
}
new->data = data;
new->prev = NULL;
new->next = NULL;
return new;
}
//尾部插入
int tail(plist head, int data)
{
plist newnode = Her_a(data);
plist p = head->next;
while (p->next != head)
{
p = p->next;
}
p->next = newnode;
newnode->prev = p;
newnode->next = head;
head->prev = newnode;
return 0;
}
//循环插入
void Wak_a(plist head)
{
for (int i = 1; i <= SIZE ; i++)
{
tail(head, i);
}
stamp(head);
}
// 打印
void stamp(plist head)
{
plist p = head->next;
printf("-head\n");
while (p != head)
{
printf("-%d", p->data);
p = p->next;
}
printf("\n");
}
void insert_tail_list(plist head, plist p);
// 交换
void exchange(plist head)
{
plist tmp = head ->prev;
plist p = head->next;
plist q = NULL;
while (p != head)
{
q = p->next;
printf("111\n");
p->prev->next = p->next;
p->next->prev = p->prev;
p -> next = p -> prev =NULL;
p->next = head -> next;
p->prev = head;
head->next ->prev = p;
head->next = p;
p = q;
}
}
void insert_tail_list(plist head, plist p)
{
// printf("head -> prev -> data %d\n", head -> prev -> data);
p -> prev = head;
p -> next = head -> next;
head -> next -> prev = p;
head -> next = p;
}
//
// }
void del_list(plist p)
{
p -> prev -> next = p -> next;
p -> next -> prev = p -> prev;
p -> next = p -> prev = NULL;
}
void sort(plist head)
{
if(head -> next == head)
{
return;
}
// tmp是标记位
plist tmp = head -> prev;
// p是头结点下一个
plist p = head -> next;
plist q = NULL;
while(p != head)
{
// q是p的下一个
q = p -> next;
del_list(p); // 删除
insert_tail_list(head, p); // 头插
p = q;
}
}
int main(int argc, char const *argv[])
{
plist myhead = LOCK();
Wak_a(myhead);
exchange(myhead);
stamp(myhead);
return 0;
}
双向循环的链表的逆序
最新推荐文章于 2024-02-25 21:51:14 发布