• C语言 数据结构 顺序表的 插入与删除


    请输入n个元素,-1结束:
    1
    2
    3
    4
    100
    -1
    顺序表
    1 2 3 4 100
    指定一个元素,在此元素之前插入一个新元素:5
    5在表的[-1]下标位置
    .请输入一个新元素值:11
    插入位置无效
    顺序表:
    1 2 3 4 100
    请输入列表中要删除的元素:4
    删除元素后的顺序表:
    1 2 3 100

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. #define MAX_SIZE 100 // 定义顺序表的最大容量
    4. // 定义顺序表结构
    5. typedef struct {
    6. int data[MAX_SIZE]; // 用于存储数据的数组
    7. int length; // 当前顺序表的长度
    8. } SeqList;
    9. // 初始化顺序表
    10. void init(SeqList* list) {
    11. list->length = 0;
    12. }
    13. // 插入元素位置从0开始算
    14. bool insert(SeqList* list, int position, int element) {
    15. if (list->length >= MAX_SIZE) {
    16. printf("顺序表已满,无法插入元素\n");
    17. return false;
    18. }
    19. if (position < 0 || position > list->length) {
    20. printf("插入位置无效\n");
    21. return false;
    22. }
    23. // 将插入位置后的元素依次后移
    24. for (int i = list->length - 1; i >= position; i--) {
    25. list->data[i + 1] = list->data[i];
    26. }
    27. // 插入元素
    28. list->data[position] = element;
    29. list->length++;
    30. return true;
    31. }
    32. // 删除元素
    33. bool removeElement(SeqList* list, int data) {
    34. if (list->length == 0) {
    35. printf("顺序表为空,无法删除元素\n");
    36. return false;
    37. }
    38. int position=-1;
    39. for (int i = 0; i < list->length ; i++) {
    40. if (list->data[i] == data) {
    41. position = i;
    42. break;
    43. }
    44. }
    45. if (position == -1) {
    46. printf("没找到此元素,无法删除\n");
    47. return false;
    48. }
    49. // 将删除位置后的元素依次前移
    50. for (int i = position; i < list->length - 1; i++) {
    51. list->data[i] = list->data[i + 1];
    52. }
    53. list->length--;
    54. return true;
    55. }
    56. // 打印顺序表中的元素
    57. void printList(SeqList* list) {
    58. for (int i = 0; i < list->length; i++) {
    59. printf("%d ", list->data[i]);
    60. }
    61. printf("\n");
    62. }
    63. //找n的位置
    64. int find(SeqList* list, int n) {
    65. int position = -1;
    66. for(int i=0;ilength;i++)
    67. if (list->data[i] == n) {
    68. position = i;
    69. break;
    70. }
    71. return position;
    72. }
    73. int main() {
    74. SeqList list; //list是变量
    75. int n;
    76. int res;
    77. init(&list);
    78. puts("请输入n个元素,-1结束:");
    79. int i = 0;
    80. while (1) {
    81. scanf("%d",&n);
    82. if (n == -1) {
    83. break;
    84. }
    85. else {
    86. insert(&list, i++, n);
    87. }
    88. }
    89. printf("顺序表:\n");
    90. printList(&list);
    91. printf("指定一个元素,在此元素之前插入一个新元素:");
    92. scanf("%d",&n);
    93. res = find(&list, n);
    94. printf("%d在表的[%d]下标位置\n.",n,res);
    95. printf("请输入一个新元素值:");
    96. scanf("%d", &n);
    97. insert(&list, res, n);
    98. printf("顺序表:\n");
    99. printList(&list);
    100. printf("请输入列表中要删除的元素:");
    101. scanf("%d", &n);
    102. res = removeElement(&list, n);
    103. if (res) {
    104. printf("删除元素后的顺序表:\n");
    105. printList(&list);
    106. }
    107. return 0;
    108. }

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. #include
    4. // 定义链表节点结构
    5. typedef struct Node {
    6. int data; // 节点数据
    7. struct Node* next; // 指向下一个节点的指针
    8. }Node_t,*List;
    9. // 初始化链表
    10. struct Node* initList() {
    11. return NULL; // 返回一个空链表
    12. }
    13. // 插入节点到链表头部
    14. void insertNode(List* head, int data, int position) {
    15. Node_t* newNode = (Node_t*)malloc(sizeof(Node_t)); // 创建新节点
    16. if (newNode == NULL) {
    17. printf("内存分配失败\n");
    18. return ;
    19. }
    20. newNode->data = data; // 设置节点数据
    21. if (position == 0 || head == NULL) {
    22. // 插入位置为链表头或链表为空
    23. newNode->next = *head;
    24. *head=newNode; // 新节点成为新的头节点
    25. return;
    26. }
    27. struct Node* current = *head;
    28. int index = 0;
    29. while (index < position - 1 && current->next != NULL) {
    30. current = current->next;
    31. index++;
    32. }
    33. newNode->next = current->next; // 新节点指向当前节点的下一个节点
    34. current->next = newNode; // 当前节点指向新节点
    35. }
    36. // 删除节点
    37. Node_t* deleteNode(List head, int data) {
    38. Node_t* current = head;
    39. Node_t* prev = NULL;
    40. while (current != NULL) {
    41. if (current->data == data) {
    42. if (prev == NULL) {
    43. // 删除头节点
    44. head = current->next;
    45. }
    46. else {
    47. // 删除非头节点
    48. prev->next = current->next;
    49. }
    50. free(current); // 释放删除的节点
    51. return head; // 返回新的头节点
    52. }
    53. prev = current;
    54. current = current->next;
    55. }
    56. printf("未找到要删除的节点\n");
    57. return head;
    58. }
    59. // 遍历并显示链表节点
    60. void displayList(List head) {
    61. Node_t* current = head;
    62. while (current != NULL) {
    63. printf("%d ", current->data);
    64. current = current->next;
    65. }
    66. printf("\n");
    67. }
    68. // 释放链表内存
    69. void freeList(List head) {
    70. struct Node* current = head;
    71. while (current != NULL) {
    72. struct Node* temp = current;
    73. current = current->next;
    74. free(temp); // 释放节点内存
    75. }
    76. }
    77. int find_n(List head, int n) {
    78. Node_t* current = head;
    79. int i = 0;
    80. while (current != NULL) {
    81. if (current->data == n)
    82. return i;
    83. current = current->next;
    84. i += 1;
    85. }
    86. return -1;
    87. }
    88. int main() {
    89. struct Node* myList = initList();
    90. puts("请输入n个元素,-1结束:");
    91. int n,res;
    92. int i = 0;
    93. while (1) {
    94. scanf("%d", &n);
    95. if (n == -1) {
    96. break;
    97. }
    98. else {
    99. insertNode(&myList, n,i++);
    100. }
    101. }
    102. printf("链表内容:\n");
    103. displayList(myList);
    104. printf("指定一个元素,在此元素之前插入一个新元素:");
    105. scanf("%d", &n);
    106. res = find_n(myList, n);
    107. printf("%d在表的[%d]下标位置\n", n, res);
    108. printf("请输入一个新元素值:");
    109. scanf("%d", &n);
    110. insertNode(&myList, n,res);
    111. printf("链表内容:\n");
    112. displayList(myList);
    113. printf("请输入链上要删的元素值:");
    114. scanf("%d", &n);
    115. myList = deleteNode(myList, n);
    116. printf("删除节点后的链表内容:\n");
    117. displayList(myList);
    118. freeList(myList); // 释放链表内存
    119. return 0;
    120. }

  • 相关阅读:
    unsubscribe:Angular 项目中常见场景以及是否需要 unsubscribe
    数据分析 — Matplotlib 、Pandas、Seaborn 绘图
    Plop 简化重复工作流,维持团队代码一致性
    平板摄像头+算力搞定3D空间实时重建和理解,清华和禾多科技新成果入选CVPR 2022 Oral...
    蓝桥杯---棋盘(典型的二维差分问题)
    Java集合框架(二)List
    【原创】浅谈EtherCAT主站EOE(上)-EOE网络
    深度学习实战06-循环神经网络(RNN)实现股票预测
    光伏仪器-1763卫星帆板电源阵列模拟器
    Linux命令使用案例
  • 原文地址:https://blog.csdn.net/laocooon/article/details/133211046