单向循环加头有序链表的构造

typedef struct Snode
{
    int data;
    struct Snode * next;
}Snode,*ptr;
#define MAX 999
#define End_elm 0

准备定义如上

构造函数如下:

ptr creatlinkC()
//输入数据是以End_elm为输入结束标记的元素序列
//返回值为所构造链表的首指针
{
    ptr head,f,s,p;
    int x;
    head=(ptr)malloc(sizeof(Snode));//申请表头节点
    head->data=MAX;                 //设置监督元
    head->next=head;                //设置循环链表
    scanf("%d",&x);
    while(x!=End_elm)               //输出结束标志
    {
        p=(ptr)malloc(sizeof(Snode));
        p->data=x;
        f=head;                     //f,s一步一趋的寻找有序位置
        s=f->next;
        while(x>f->data)            //因为有监督元,所以开始一定小于
        {
            f=s;
            s=s->next;
        }
        f->next=p;                  //将新节点插入有序位置
        p->next=s;
        scanf("%d",&x);             //循环条件
    }
    return (head);
}

猜你喜欢

转载自blog.csdn.net/qq_41995348/article/details/80559438