循环链表
对循环链表的操作
1、Initialization();
2、Create_loop_list(head);//创建链表
3、Delete_loop_list(head,300);//指定删除
4、Head_delete_loop(head);//头删
5、Tail_delete_loop(head);//尾删
6、Head_insert_loop(head,900);//头插
7、Tail_insert_loop(head,800);//尾插
8、Add_loop_list(head,200,111);//指定插入 200后插入111
9、Length_loop_list(head)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
struct student
{
int date;
struct student *next;
};
student *Initialization()
{
student *head=(student*)malloc(sizeof(student));
assert(head);
if(head==NULL)
{
printf("申请内存失败!");
return head;
}
head->date=0;
head->next=head;
return head;
}
void Print_loop_list(student *head)//打印
{
student *p=head->next;
while(p!=head)
{
printf("%d ",p->date);
p=p->next;
}
}
void Create_loop_list(student *head)//创建列表
{
student *p=head;
int arr[7]={100,200,300,400,500,600,700};
for(int i=0;i<7;i++)
{
student *new_node=(student*)malloc(sizeof(student));
assert(new_node);
new_node->date=arr[i];
p->next=new_node;
new_node->next=head;
p=new_node;
}
printf("创建链表:");
Print_loop_list(head);//打印
printf("\n");
}
void Delete_loop_list(student *head,int num)//指定删除
{
student *p=head,*pre=head->next;
while(pre->next!=head)
{
p=p->next;
pre=pre->next;
if(pre->date==num)
{
break;
}
}
p->next=pre->next;
printf("指定删除:");
Print_loop_list(head);//打印
printf("\n");
}
void Head_delete_loop(student *head)//头删
{
head->next=head->next->next;
printf("头删链表:");
Print_loop_list(head);//打印
printf("\n");
}
void Tail_delete_loop(student *head) //尾删
{
student *p=head,*pre=head->next;
while(pre->next!=head)
{
p=p->next;
pre=pre->next;
}
p->next=head;
printf("尾删链表:");
Print_loop_list(head);//打印
printf("\n");
}
void Head_insert_loop(student *head,int num)//头插
{
student *p=head;
student *New_head=(student *)malloc(sizeof(student));
assert(New_head);
New_head->next=p->next;
p->next=New_head;
New_head->date=num;
printf("头插链表:");
Print_loop_list(head);//打印
printf("\n");
}
void Tail_insert_loop(student *head,int num)//尾插
{
student *p=head;
student *New_head=(student *)malloc(sizeof(student));
assert(New_head);
while(p->next!=head)
{
p=p->next;
}
p->next=New_head;
New_head->next=head;
New_head->date=num;
printf("尾插链表:");
Print_loop_list(head);//打印
printf("\n");
}
void Add_loop_list(student *head,int num1,int num2)//指定插入
{
student *p=head;
student *New_head=(student *)malloc(sizeof(student));
assert(New_head);
while(p->next!=head)
{
p=p->next;
if(p->date==num1)
{
break;
}
}
New_head->next=p->next;
p->next=New_head;
New_head->date=num2;
printf("指定插入:");
Print_loop_list(head);//打印
printf("\n");
}
int Length_loop_list(student *head)//链表长度
{
student *p=head;
int length=0;
while(p->next!=head)
{
p=p->next;
length++;
}
return length;
}
int main()
{
student *head=Initialization();
Create_loop_list(head);//创建链表
Delete_loop_list(head,300);//指定删除
Head_delete_loop(head);//头删
Tail_delete_loop(head);//尾删
Head_insert_loop(head,900);//头插
Tail_insert_loop(head,800);//尾插
Add_loop_list(head,200,111);//指定插入 200后插入111
printf("链接长度:%d\n",Length_loop_list(head));//链表长度
return 0;
}
运行结果