带头结点的循环链表

带头节点的循环链表-c语言实现

 

#include
#include
#include
typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct  LNode *next;

}LNode;
LNode *create_tail(LNode *tail);
LNode *create_node(ElemType data);
void Insert_byHead(LNode *tail, ElemType data);
int EmptyList(LNode *tail);
void Print_List(LNode *tail);
int Len_Get(LNode *tail);
void Insert_Node(LNode *tail, ElemType data);
void Delete_Node(LNode *tail, int pos);
void List_Reverse(LNode* tail);
int Find_Node(LNode* tail, ElemType data);
void Clear_List(LNode* tail);
LNode *create_tail()//创建尾结点
{
    LNode *tail = (LNode *)malloc(sizeof(LNode));
    if (!tail)return;
    tail->data = NULL;
    tail->next = tail;
    return tail;
}
LNode *create_node(ElemType data)//创建结点
{
    LNode *newnode = (LNode*)malloc(sizeof(LNode));
    if (!newnode)return;
    newnode->data = data;
    newnode->next = NULL;
    return newnode;
}
void Insert_bytail(LNode *tail, ElemType data)//头插法链接循环列表
{
    LNode *newnode = create_node(data);
    newnode->next = tail->next;
    tail->next = newnode;
}
int EmptyList(LNode *tail)
{
    if (tail->next == NULL)return 1;
    return 0;
}
void Print_List(LNode *tail)//遍历打印
{
    if (EmptyList(tail))return;
    LNode *p = tail->next;
    while (p != tail)
    {
        printf("%d", p->data);
        p = p->next;
    }
}
int Len_Get(LNode *tail)//遍历获取长度
{
    int len = 0;
    LNode *p = tail->next;
    while (p != tail)
    {
        p = p->next;
        len++;
    }
    return len;
}
void Insert_Node(LNode *tail, ElemType data, int pos)//插入第几个结点后
{
    if (EmptyList(tail))return;
    if (pos > Len_Get(tail))
    {
        printf("error");
        return;
    }
    LNode *newnode = create_node(data);
    LNode *p = tail;
    LNode *tmp;
    int i = 0;
    while (i != pos)
    {
        p = p->next;
        i++;
    }
    tmp = p->next;
    p->next = newnode;
    newnode->next = tmp;
}
void Delete_Node(LNode *tail, int pos)//删除结点
{
    if (EmptyList(tail))return;
    if (pos > Len_Get(tail))
    {
        printf("error");
        return;
    }
    LNode *p = tail;
    LNode *tmp;
    int i = 1;
    while (i != pos)
    {
        p = p->next;
        i++;
    }
    tmp = p->next;
    p->next = tmp->next;
    free(tmp);
}

void Clear_List(LNode* tail)//链表清空
{
    LNode *p, *tmp;
    p = tail->next;
    while (p != tail)
    {
        tmp = p;
        p = p->next;
        free(tmp);
    }
    tail->next = NULL;
}
int Find_Node(LNode* tail, ElemType data)//链表根据查找的元素查询位置
{
    if (EmptyList(tail))return;
    int pos = 1;
    LNode *p = tail->next;
    while (p!=tail)
    {
        if (p->data == data)
        {
            return pos;
        }
        p = p->next;
        pos++;
    }

}
void main()
{
    LNode *L = create_tail();
    Insert_bytail(L, 5);
    Insert_bytail(L, 7);
    Insert_bytail(L, 9);
    Insert_bytail(L, 6);
    Insert_bytail(L, 4);
    //Insert_Node(L, 5, 1);
    //Delete_Node(L, 4);
    //Clear_List(L);
    Print_List(L);
    //printf("%d", Find_Node(L, 4));
    system("pause");
}

 

转载于:https://www.cnblogs.com/ljh-blog/p/10817004.html

你可能感兴趣的:(带头结点的循环链表)