/* 循环链表内的结点插入 */
#include "stdio.h"
struct clist
{
int data;
struct clist *next;
};
typedef struct clist node;
typedef node *clink;
/*循环链表的输出*/
void printclist(clink head)
{
clink ptr;
ptr=head;
do
{
printf("[%d]",ptr->data);
ptr=ptr->next;
}while(ptr!=head&&head!=head->next);
printf("/n");
}
/*循环链表的节点插入*/
clink insertnode(clink head,clink ptr,int value)
{
clink new_node;
clink previous;
new_node=(clink)malloc(sizeof(node));
new_node->data=value;
new_node->next=NULL;
if(head==NULL)
{
new_node->next=new_node;
return new_node;
}
if(ptr==NULL)
{
/*第一种情况:插在第一个结点之前且成为链表开始。*/
new_node->next=head;
previous=head;
while(previous->next!=head)
previous=previous->next;
previous->next=new_node;
head=new_node;
}
else
{
/*第二中情况:插在节点之后*/
new_node->next=ptr->next;
ptr->next=new_node;
}
return head;
}
void main()
{
clink head=NULL;
int list[6]={1,2,3,4,5,6};
int i;
head=insertnode(head,head,list[0]);
printclist(head);
/*第一种情况:插在第一结点之前且成为链表开始*/
head=insertnode(head,NULL,list[1]);
printclist(head);
for(i=2;i<6;i++)
{
/*第二种情况:插在第一个结点后*/
head=insertnode(head,head,list[i]);
printclist(head);
}
}
循环链表内的结点插入
最新推荐文章于 2021-10-08 10:21:47 发布