c语言单链表的各种操作<未完>

#include<stdio.h>
#include<stdbool.h>

struct Node
{
    int val;
    Node* next;
};

Node* Create()
{
    bool bFlag=true;
    Node *pHead=NULL;
    Node *pCur=NULL;
    Node* pTemp=NULL;
    int nVal;
    pHead=(Node*)malloc(sizeof(Node));
    if(NULL==pHead)
    {
        return NULL;
    }
    pHead->next=NULL;
    pCur=head;

    while(bFlag)
    {
        pTemp=(Node*)malloc(sizeof(Node));
        printf("please input node value,if the value is 0 input finished.");

        scanf("%d",&nVal);
        if(nVal!=0)
        {
            pTemp->val=nVal;
            pTemp->next=NULL;

            pCur->next=pTemp;
            pCur=pTemp;
        }
        else
        {
            bFlag=false;
        }
    }

    pHead=pHead->next;
    pCur->next=NULL;

    return pHead;
}

int length(Node* head)
{
    if(head==NULL)
    {
        return 0;
    }

    int i=0;
    Node* cur=head;
    while(cur!=NULL)
    {
        i++;
        cur=cur->next;
    }

    return i;
}

bool print(Node *head)
{
    if(NULL==head)
    {
        return false;
    }

    printf("list info:");
    Node *cur=head;
    while(NULL!=cur)
    {
        printf("%d\n",cur->val);
        cur=cur->next;
    }
    return true;
}

Node* del(Node* head, int pos)
{
    if((head==NULL)||(pos<1)||(pos>length(head)))
    {
        return NULL;
    }
    
    Node* cur=head;
    if(pos==1)
    {
        cur=cur->next;
        free(head);
        head=NULL;
        return cur;
    }

    //定位到删除节点的前一个节点
    int i=0;
    for(i=0;i<pos-1;i++)
    {
        cur=cur->next;
    }

    cur->next=cur->next->next;
    
    return head;
}

Node* addafterpos(Node *head, int pos, int val)
{
    if((head==NULL)||(pos<1)||(pos>length(head)))
    {
        return NULL;
    }

    Node* newnode=(Node*)malloc(sizeof(Node));
    if(newnode==NULL)
    {
        return NULL;
    }
    newnode->val=val;
    newnode->next=NULL;

    Node* cur=head;
    if (pos==length(head))
    {
        while(cur->next!=NULL)
        {
            cur=cur->next;
        }
        cur->next=newnode;
    }

    int i=0;
    for(i=0;i<pos;i++)
    {
        cur=cur->next;
    }

    newnode=cur->next;
    cur->next=newnode;

    return head;
}

Node* insertbefore(Node* head, int pos, int val)
{
    if((head==NULL)||(pos<1)||(pos>length(head)))
    {
        return NULL;
    }

    Node* newnode=(Node*)malloc(sizeof(Node));
    if(newnode==NULL)
    {
        return NULL;
    }
    newnode->val=val;
    newnode->next=NULL;
    Node* cur=head;
    if(pos==1)
    {
        newnode->next=cur;
        return newnode;
    }

    int i=0;
    for(i=0;i<pos-1;i++)
    {
        cur=cur->next;
    }

    newnode->next=cur->next;
    cur->next=newnode;

    return head;
}


int main()
{
    Node *head;
    int len;
    head=Create();
    len=length(head);
    printf("len:%d", len);

    print(head);
    head=del(head,1); 
    print(head);

    head=addafterpos(head,4,5);
    print(head);
    
    head=insertbefore(head,1,1);
    print(head);

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值