链表实现线性表操作(可直接放入头文件使用的函数)(C语言)

这里给大家一些可以直接放在头文件里使用的链表函数,需要用链表的时候直接调用就可以

以后会继续更新的~

//链表
typedef struct Node
{
    struct Node* next;
    int data;
    int num;
}NODE,*LIST,*LPNODE;
//创建链表
LIST CreateHead()
{
    LIST headNode = (LIST)malloc(sizeof(NODE));
    assert(headNode);
    headNode->next = NULL;
    return headNode;

};
//创建节点
LIST CreateNode(LIST headnode,int data)
{
    LIST newnode=(LIST)malloc(sizeof(NODE));
    newnode->data=data;
    newnode->next=NULL;
    return newnode;
};
//头插入节点
void inserthead(LIST headnode ,int data)
{
    LIST newnode=CreateNode(headnode,data);
    newnode->next=headnode->next;
    headnode->next=newnode;
    headnode->num++;
};
//尾插入节点
void insertback(LIST headnode ,int data)
{
    LIST newnode=CreateNode(headnode,data);
    LIST pmove=headnode;
    while(pmove->next!=NULL)
    {
        pmove=pmove->next;

    }
    pmove->next=newnode;
    headnode->num++;
}
//中间插入节点
void insertcenter(LIST headnode ,int data,int postdata)
{
    LIST newnode=CreateNode(headnode,data);
    LIST left=headnode;
    LIST right=headnode->next;
    while(left!=NULL&&right->data!=postdata)
    {
        left=right;
        right=right->next;
    }
    if(right==NULL)
    {
        printf("insert error");
    }else
    {
        left->next=newnode;
        newnode->next=right;
    }
    headnode->num++;
}
//头删除节点
void delhead(LIST headnode)
{
    LIST temp=headnode->next;
    if (temp == NULL)
    {
        printf("Node NULL!\n");
        return;
    }
    headnode->next=temp->next;
    free(temp);
    headnode->num--;
}
//尾删除节点
void delback(LIST headnode)
{
    LPNODE left = headnode;
    LPNODE right = headnode->next;
    while (left != NULL && right->next != NULL)
    {
        left = right;
        right = right->next;
    }
    if (right == NULL)
    {
        printf("Node NULL");
    }
    else
    {
        free(right);
        right = NULL;
        left->next = NULL;
    }
    headnode->num--;
}
//中间删除节点
void delcenter(LIST headnode,int postdata)
{
    LIST left=headnode;
    LIST right=headnode->next;
    while(left!=NULL&&right->data!=postdata)
    {
        left=right;
        right=right->next;

    }
    if(right==NULL)
    {
        printf("no find");
    }
    else
    {
        LIST temp=left->next;
        left->next=right;
        free(temp);
        temp = NULL;
    }
    headnode->num--;
}


//打印链表
void printList(LIST headNode)
{
    if (headNode == NULL)
    {
        printf("ERROR\n");
        return;
    }
    //NULL是没有next
    LPNODE pmove = headNode->next;
    while (pmove != NULL)
    {
        printf("%d\t", pmove->data);
        pmove = pmove->next;
    }
    printf("\n");
}
//对比两个链表
void listcat(LIST first, LIST second)
{
    LIST pmove = second->next;
    while (pmove != NULL)
    {
        insertback(first, pmove->data);
        pmove = pmove->next;
    }
}

void listcat_v(LIST first, LIST second, LIST third)
{
    LIST pFirst = first->next;
    while (pFirst != NULL)
    {
        LIST pSecond = second->next;
        while (pSecond != NULL)
        {
            if (pFirst->data == pSecond->data)
            {
                insertback(third,pFirst->data);
                break;
            }
            pSecond = pSecond->next;
        }
        pFirst = pFirst->next;
    }
}

void reList(LIST* headNode)
{
    LIST preNode = NULL;
    LIST curNode = (*headNode)->next;
    LIST nextNode = (*headNode)->next;
    while (curNode != NULL)
    {

        nextNode = curNode->next;
        curNode->next = preNode;

        preNode = curNode;
        curNode = nextNode;
    }
    (*headNode)->next=preNode;
}
//判断链表中指定值的个数(头节点,指定值)
int CountX(LPNODE headNode,int n) {
    LPNODE newNode = headNode;
    int c = 0;
    for (int i = 0; i < headNode->num; ++i) {
        if (newNode->data == n) {
            c++;
        }
        if (newNode->next != NULL) {
            newNode = newNode->next;
        } else {
            return c;
        }


    }
}
//逆置
LPNODE nizhi(LPNODE headNode)
{
    LPNODE preNode=headNode;
    LPNODE seNode=headNode->next;
    LPNODE lastNode=seNode->next;
    while (lastNode!=NULL)
    {
        seNode->next=preNode;
        lastNode= lastNode->next;
        seNode=seNode->next;
        preNode=preNode->next;
    }
    headNode->next=seNode;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值