循环链表
struct listnode{
int data;
struct listnode *next;
};
读入生成一个循环链表
Nodeptr list_insert(Nodeptr head,int n)
{
int a,b;
Nodeptr p=head,q=NULL;
for(int i=0;i<n;i++)
{
scanf("%d",&a);
q=(Nodeptr)malloc(sizeof(Node));
q->data=b;
q->next=NULL;
if(head==NULL)
head=p=q;
else{
p->next=q;
p=p->next;
}
}
p->next=head;
return head;
}
根据data的值查找
Nodeptr list_search(Nodeptr head,int num)
{
Nodeptr p=head;
if(p==NULL)
return NULL;
Nodeptr p0=NULL,q=NULL;
int min=0x7fffff;
if(p->data==num)
return p;
if(p->data>num)
{
min=p->data;
q=p;
}
p=p->next;
while(p!=head){
if(p->data==num)
return p;
else{
if((p->data>num) && (p->data)<min)
{
min=p->data;
q=p;
}
p=p->next;
}
}
return q;
}
查找循环链表指定节点的前驱节点
Nodeptr list_findpre(Nodeptr head,Nodeptr q)
{
Nodeptr p=head;
while(p->next!=q)
{
p=p->next;
}
return p;
}