zoukankan      html  css  js  c++  java
  • 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("
    please 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("
    begin	");
        while(tmp)
        {
            printf("%d ", tmp->data);
            tmp = tmp->next;
        }
        printf("	End
    ");
        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
    ", 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;
    }
  • 相关阅读:
    Sublime Text 最佳插件列表(转)
    MyBatis入门(六)---mybatis与spring的整合
    MyBatis入门(五)---延时加载、缓存
    MyBatis入门(三)---多个参数
    MyBatis入门(二)---一对一,一对多
    AccessRandomFile多线程下载文件
    MyBatis入门(一)---基本使用
    JAVA基础学习day27--反射机制
    简明 Vim 练级攻略(转)
    JAVA基础学习day26--正则表达式
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/12005523.html
Copyright © 2011-2022 走看看