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;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章