#include<stdio.h>
#include<stdlib.h>
struct list
{
struct list *pr;
int data;
struct list *next;
};
struct list* Create()
{
struct list *head,*pnew,*pre;
int i;
int number = 5;
head = (struct list*)malloc(sizeof(struct list));
head->data = 0;
head->next = NULL;
pre = head;
for(i=1;i<number;i++)
{
pnew = (struct list*)malloc(sizeof(struct list));
pnew->pr = pre;
pnew->data = i;
pnew->next = NULL;
pre->next = pnew;
pre = pre->next;
}
pnew->next = head;
head->pr = pnew;
return head;
}
struct list* Insert(struct list *head,int pos)
{
struct list *pnew,*end;
int i;
pnew = (struct list*)malloc(sizeof(struct list));
pnew->data = 100;
if(pos==0)
{
head->pr->next = pnew;
pnew->next = head;
head->pr = pnew;
pnew->pr = head->pr;
head = pnew;
}
else
{
end = head;
if(pos>=2)
{
for(i=0;i<(pos-1);i++)
{
end = end->next;
if(end->next==NULL)
break;
}
}
pnew->pr = end;
pnew->next = end->next;
if(end->next==NULL)
pnew->next = NULL;
else
end->next->pr = pnew;
end->next = pnew;
}
return head;
}
struct list* Delete(struct list *head,int pos)
{
struct list *obj;
int i;
if(pos==0)
{
obj = head;
head->pr->next = head->next;
head->next->pr = head->pr;
head = head->next;
}
else
{
obj = head->next;
if(pos>=2)
{
for(i=0;i<(pos-1);i++)
obj = obj->next;
}
obj->pr->next = obj->next;
obj->next->pr = obj->pr;
if(obj==head)
head = head->next;//如果被删除的是head,那么新head为原head下一个链节
}
free(obj);
return head;
}
int Check(struct list *head,int pos)
{
int i;
if(pos>=1)
{
for(i=0;i<pos;i++)
head = head->next;
}
return head->data;
}
void Modify(struct list *head,int pos,int dat)
{
int i;
if(pos>=1)
{
for(i=0;i<pos;i++)
head = head->next;
}
head->data = dat;
}
void Print(struct list *head)//只打印有效数据
{
struct list *temp;
temp = head;
printf("%d\n",temp->data);
do
{
temp = temp->next;
printf("%d\n",temp->data);
}while(temp->next!=head);//如果再次循环到head处,则停止输出
}
void Print_all(struct list *head)//这个是打印全部数据,也就是一个无限循环
{
printf("%d\n",head->data);
head = head->next;
}
void main()
{
struct list *head;
int data;
head = Create();
data = Check(head,0);
printf("%d\n",data);
}
C语言双向环形链表
最新推荐文章于 2022-03-08 11:02:17 发布