C链表操作

链表创建删除插入查找销毁操作

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct Node
{
    int data;
    struct Node *next;
}SLIST;

SLIST *SList_Create();
int SList_Print(SLIST *pHead);
int SList_NodeInsert(SLIST *pHead, int x, int y);
int SList_NodeDel(SLIST *pHead, int y);
int SList_Destory(SLIST *pHead);
int SList_Reverse(SLIST *pHead);

int main()
{
    int ret = 0;
    SLIST *pHead = NULL;
    pHead = SList_Create();
    ret = SList_Print(pHead);
    
    ret = SList_NodeInsert(pHead, 20, 19);
    ret = SList_Print(pHead);

    ret = SList_NodeDel(pHead, 19);
    ret = SList_Print(pHead);

    ret = SList_Reverse(pHead);
    ret = SList_Print(pHead);

    SList_Destory(pHead);

    return 0;

}
SLIST *SList_Create()
{
    SLIST *pHead, *pM, *pCur;
    int data;
    pHead = (SLIST *)malloc(sizeof(SLIST));
    if (pHead == NULL)
    {
        return NULL;
    }
    pHead->data = 0;
    pHead->next = NULL;

    printf("please enter yout data : ");
    scanf("%d", &data);

    pCur = pHead;
    while (-1 != data)
    {
        pM = (SLIST *)malloc(sizeof(SLIST));
        if (NULL == pM)
        {
            return NULL;
        }
        pM->data = data;
        pM->next = NULL;

        pCur->next = pM;
        pCur = pM;

        printf("\nplease enter yout data : ");
        scanf("%d", &data);
    }

    return pHead;
}

int SList_Print(SLIST *pHead)
{
    SLIST *tmp = NULL;
    if (NULL == pHead)
    {
        return -1;
    }
    tmp = pHead->next;

    printf("\nbegin\t");
    while(tmp)
    {
        printf("%d ", tmp->data);
        tmp = tmp->next;
    }
    printf("\tEnd\n");
    return 0;

}

int SList_NodeInsert(SLIST *pHead, int x, int y)
{
    SLIST *pM, *pCur, *pPre;

    pM = (SLIST *)malloc(sizeof(SLIST));
    if (NULL == pM)
    {
        return -1;
    }
    pM->data = y;
    pM->next = NULL;

    pPre = pHead;
    pCur = pHead->next;

    while(pCur)
    {
        if (pCur->data == x)
        {
            break;
        }
        pPre = pCur;
        pCur = pCur->next;
    }
    pM->next = pPre->next;
    pPre->next = pM;

    return 0;
}

int SList_NodeDel(SLIST *pHead, int y)
{
    SLIST *pCur, *pPre;

    pPre = pHead;
    pCur = pHead->next;

    while(pCur != NULL)
    {
        if (pCur->data == y)
        {
            break;
        }
        pPre = pCur;
        pCur = pCur->next;
    }
    if (pCur == NULL)
    {
        printf("Can't find the node that value is %d\n", y);
        return -1;
    }
    pPre->next = pCur->next ;
    
    if (pCur != NULL)
    {
        free(pCur);
    }
    return 0;
}

int SList_Destory(SLIST *pHead)
{
    SLIST *tmp = NULL;
    if(NULL == pHead)
    {
        return -1;
    }

    while(pHead != NULL)
    {
        tmp = pHead->next;
        free(pHead);
        pHead = tmp;
    }

    return 0;
}

int SList_Reverse(SLIST *pHead)
{
    SLIST *p = NULL; //previous node
    SLIST *q = NULL;    //current node
    SLIST *t = NULL;    //buffer node

    p = pHead->next;
    q = pHead->next->next;

    while(q)
    {
        t = q->next;
        q->next = p;
        p = q;
        q = t;
    }

    pHead->next->next = NULL;
    pHead->next = p;

    return 0;
}

 

posted @ 2019-12-08 13:33  王清河  阅读(266)  评论(0编辑  收藏  举报