zoukankan      html  css  js  c++  java
  • 单链表的翻转,插入,删除,遍历操作

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 typedef struct Node {
      5     int data;
      6     struct Node *next;
      7 }SList;
      8 
      9 int SList_Create(SList **p/**out*/) {
     10     int data = 0;
     11     int ret = 0;
     12     SList *pHead = NULL;
     13     SList *node = NULL;
     14     SList *tmp = NULL;
     15     pHead = (SList *)malloc(sizeof(SList));
     16     if(pHead == NULL) {
     17         ret = -1;
     18         printf("SList_Create erro
    ");
     19         goto End;
     20     }
     21     tmp = pHead;
     22     printf("请输入一个整数数据
    ");
     23     scanf("%d", &data);
     24     while(data != -1) {
     25         node = (SList *)malloc(sizeof(SList));
     26         if(node == NULL) {
     27             ret = -1;
     28             printf("SList_Create erro
    ");
     29             goto End;
     30         }
     31         node->data = data;
     32         tmp->next = node;
     33         tmp = node;
     34         printf("请输入一个整数数据
    ");
     35         scanf("%d", &data);
     36     }
     37     node->next = NULL;
     38     *p = pHead;
     39 End:
     40     if(ret != 0) {
     41         if(pHead != NULL) {
     42             free(pHead);
     43             pHead = NULL;
     44         }
     45         if(node != NULL) {
     46             free(node);
     47             node = NULL;
     48         }
     49     }
     50     return ret;
     51 }
     52 
     53 void print_List(SList *pHead) {
     54     SList *pTmp;
     55     if(pHead == NULL || pHead->next == NULL) {
     56         printf("SList is NULL
    ");
     57     }
     58     pTmp = pHead;
     59     while(pTmp->next) {
     60         printf("%d ", pTmp->next->data);
     61         pTmp = pTmp->next;
     62     }
     63 }
     64 
     65 int insert_List(SList *pHead/**in*/, int targetNum, int insertNum) {
     66     int ret = 0;
     67     SList *pPre = NULL;
     68     SList *pCur = NULL;
     69     SList *pNode = NULL;
     70     if(pHead == NULL || pHead->next == NULL) {
     71         ret = -1;
     72         printf("insert_List erro
    ");
     73         return ret;
     74     }
     75     pCur = pHead->next;
     76     pPre = pHead;
     77     pNode = (SList *)malloc(sizeof(SList));
     78     if(pNode == NULL) {
     79         ret = -1;
     80         printf("insert_List erro
    ");
     81         return ret;
     82     }
     83     pNode->data = insertNum;
     84     while(pCur) {
     85         if(pCur->data == targetNum) {
     86             break;
     87         }else {
     88             pPre = pCur;
     89             pCur = pCur->next;
     90         }
     91     }
     92     pNode->next = pCur;
     93     pPre->next = pNode;
     94     return ret;
     95 }
     96 
     97 int delete_List(SList *pHead, int deleteNum) {
     98     int ret = 0;
     99     SList *pPre = NULL;
    100     SList *pCur = NULL;
    101     if(pHead == NULL || pHead->next == NULL) {
    102         ret = -1;
    103         printf("delete_List erro
    ");
    104         return ret;
    105     }
    106     pPre = pHead;
    107     pCur = pHead->next;
    108     while(pCur) {
    109         if(pCur->data == deleteNum) {
    110             break;
    111         }else {
    112             pPre = pCur;
    113             pCur = pCur->next;
    114         }
    115     }
    116     if(pCur == NULL) {
    117         ret = -2;
    118         printf("not exist this num");
    119         return ret;
    120     }
    121     pPre->next = pCur->next;
    122     pCur->next = NULL;
    123     if(pCur != NULL) {
    124         free(pCur);
    125     }
    126     return ret;
    127 }
    128 
    129 void destory_List(SList **p) {
    130     SList *pHead = NULL;
    131     SList *tmp = NULL;
    132     if(p == NULL || *p == NULL) {
    133         return;
    134     }
    135     pHead = *p;
    136     while(pHead) {
    137         tmp = pHead->next;
    138         if(pHead != NULL) {
    139             free(pHead);
    140         }
    141         pHead = tmp;
    142     }
    143     *p = NULL;
    144 }
    145 
    146 void reverse_List(SList *pHead) {
    147     SList *pPre = NULL;
    148     SList *pCur = NULL;
    149     SList *pTmp = NULL;
    150     if(pHead == NULL || pHead->next == NULL || pHead->next->next == NULL) {
    151         return;
    152     }
    153     pPre = pHead->next;
    154     pCur = pHead->next->next;
    155     while(pCur) {
    156         pTmp = pCur->next;
    157         pCur->next = pPre;
    158         pPre = pCur;
    159         pCur = pTmp;
    160     }
    161     pHead->next->next = NULL;
    162     pHead->next = pPre;
    163 }
    164 
    165 int main() {
    166     int ret = 0;
    167     SList *node1 = NULL;
    168     ret = SList_Create(&node1);
    169     if(ret) {
    170         printf("create list erro
    ");
    171     }
    172     print_List(node1);
    173     printf("
     insert_List
    ");
    174     insert_List(node1, 20, 19);
    175     print_List(node1);
    176     printf("
     delete_List
    ");
    177     delete_List(node1, 19);
    178     print_List(node1);
    179     printf("
     reverse_List
    ");
    180     reverse_List(node1);
    181     print_List(node1);
    182     printf("
     destory_List
    ");
    183     destory_List(&node1);
    184     system("pause");
    185     return 0;
    186 }
  • 相关阅读:
    工作总结
    JSON数据使用
    DataTable知识
    树形结构菜单
    区域树前后台
    跨域总结
    工作一年感想
    项目整体架构分析
    springboot 和 mongdb连接问题 Exception in thread "main" com.mongodb.MongoSecurityException:
    go函数、方法、接口笔记
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/8903594.html
Copyright © 2011-2022 走看看