• 单链表插入与删除数据


      1 //按元素大小顺序插入到链表中
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 #include<string.h>
      5 
      6 struct Node
      7 {
      8     int value;
      9     struct Node *next;
     10 };
     11 
     12 void insertNode(struct Node **head, int value)
     13 {
     14     struct Node *previous;
     15     struct Node *current;
     16     struct Node *newnode;
     17 
     18     current = *head;
     19     previous = NULL;
     20     
     21     
     22     while (current != NULL && current->value < value)
     23     {
     24         int b = current->value;
     25         previous = current;        //previous记录current上一个节点的位置
     26         current = current->next;
     27     }
     28     newnode = (struct Node *)malloc(sizeof(struct Node));//分配内存
     29     if (newnode == NULL)
     30     {
     31         printf("内存分配失败!");
     32         exit(1);
     33     }
     34     newnode->value = value;
     35     newnode->next = current;
     36     if (previous == NULL)
     37     {
     38         *head = newnode;
     39     }
     40     else
     41     {
     42         previous->next = newnode;
     43     }
     44     
     45 }
     46 
     47 void printNode(struct Node *head)
     48 {
     49     struct Node *current;
     50     current = head;
     51     while (current != NULL)
     52     {
     53         printf("%d ", current->value);
     54         current = current->next;
     55     }
     56     printf("
    ");
     57 }
     58 
     59 void deleteNode(struct Node **head, int value)
     60 {
     61     struct Node *previous;
     62     struct Node *current;
     63 
     64     current = *head;
     65     previous = NULL;
     66 
     67     while (current != NULL && current->value != value)
     68     {
     69         previous = current;
     70         current = current->next;
     71     }
     72     if (current == NULL)
     73     {
     74         printf("找不到匹配的节点
    ");
     75         return;
     76     }
     77     else
     78     {
     79         if (previous == NULL)
     80         {
     81             *head = current->next;
     82         }
     83         else
     84         {
     85             previous->next = current->next;
     86         }
     87         free(current);
     88     }
     89 }
     90 int main()
     91 {
     92     struct Node *head = NULL;
     93     int input;
     94     printf("开始测试插入整数...
    ");
     95     while (1)
     96     {
     97         printf("
    请输入一个整数(-1表示结束):");
     98         scanf("%d", &input);
     99         printf("
    ");
    100         if (input == -1)
    101         {
    102             break;
    103         }
    104         insertNode(&head, input);
    105         printNode(head);
    106     }
    107 
    108     printf("开始测试删除整数...
    ");
    109     while (1)
    110     {
    111         printf("
    请输入一个整数(-1表示结束):");
    112         scanf("%d", &input);
    113         printf("
    ");
    114         if (input == -1)
    115         {
    116             break;
    117         }
    118         deleteNode(&head, input);
    119         printNode(head);
    120     }
    121 
    122     return 0;
    123 }

    转载自:khq溪风

  • 相关阅读:
    快速排序的分析与优化
    矩阵原地转置
    变位词问题
    一维向量旋转算法
    位图排序
    Linux网络编程:UDP实现可靠的文件传输
    Linux网络编程:UDP Socket编程范例
    es6+最佳入门实践(6)
    es6+最佳入门实践(5)
    es6+最佳入门实践(4)
  • 原文地址:https://www.cnblogs.com/hsy1941/p/11505172.html
Copyright © 2020-2023  润新知