#include
#include
struct clist
{
int num;
struct clist *next;
};
typedef struct clist node;
typedef node *clink;
void Printlist(clink head)
{
clink ptr;
if(head==NULL)
{
printf("\n");
}
else
{
ptr=head;
do
{
ptr=ptr->next;
printf("[%d]",ptr->num);
}while(ptr!=head);
printf("\n");
}
}
clink Insertnode(clink head,clink ptr,int x)
{
clink newnode;
newnode=(clink)malloc(sizeof(node));
if(!newnode)
{
return NULL;
}
newnode->num=x;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
if(ptr==NULL)
{
newnode->next=head->next;
head->next=newnode;
}
else
{
newnode->next=head->next->next;
head->next->next=newnode;
}
}
return head;
}
clink Findnode(clink head,int x)
{
clink ptr,ptr1;
ptr1=NULL;
if(head==NULL)
{
return NULL;
}
else
{
ptr=head;
do
{
if(ptr->num==x)
{
ptr1=ptr;
break;
}
ptr=ptr->next;
}while(ptr!=head);
return ptr1;
}
}
clink Deletnode(clink head,clink ptr)
{
clink ptr1;
ptr1=head;
if(head->next==head)
{
head=NULL;
}
else
{
while(ptr1->next!=ptr)
{
ptr1=ptr1->next;
}
ptr1->next=ptr1->next->next;
if(ptr==head)
{
head=ptr1;
}
}
return head;
}
void Freelist(clink head)
{
clink ptr,ptr1;
ptr1=head;
do
{
ptr=head->next;
head=head->next->next;
free(ptr);
}while(ptr!=ptr1);
}
void main()
{
int arry[6]={9,7,3,4,5,6};
int i,num;
clink head,ptr;
head=NULL;
head=Insertnode(head,head,arry[0]);
if(!head)
{
printf("内存分配错误\n");
exit(1);
}
printf("创建第一节点-->");
Printlist(head);
head=Insertnode(head,NULL,arry[1]);
printf("插入第一节点钱-->");
Printlist(head);
for(i=2;i<6;i++)
{
head=Insertnode(head,head,arry[i]);
printf("插入节点之后");
Printlist(head);
}
while(1)
{
printf("请输入查询值-->");
scanf("%d",&num);
if(num<0)
{
exit(1);
}
else
{
ptr=Findnode(head,num);
if(ptr==NULL)
{
printf("找不到相应的编号\n");
continue;
}
head=Deletnode(head,ptr);
printf("删除后的链表-->");
Printlist(head);
}
}
Freelist(head);
}