1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h>
struct node { int data; struct node* next; };
int main() { struct node* head = NULL; struct node* current, * prev = NULL;
int i = 3; while (i--) { current = (struct node*)malloc(sizeof(struct node)); if (current == NULL) { printf("内存分配失败。\n"); exit(EXIT_FAILURE); } if (head == NULL) head = current; else prev->next = current; current->next = NULL; printf("请输入数据(int类型):\n"); scanf("%d", ¤t->data); prev = current; }
current = head; while (current != NULL) { printf("当前数据为: %d \n", current->data); current = current->next; }
current = head; while (current->next != NULL) { current = current->next; } struct node* new_node = (struct node*)malloc(sizeof(struct node)); if (new_node == NULL) { printf("内存分配失败。\n"); exit(EXIT_FAILURE); } printf("请输入数据(int类型):\n"); scanf("%d", &new_node->data); new_node->next = NULL; current->next = new_node;
current = head; while (current != NULL) { printf("当前数据为: %d \n", current->data); current = current->next; }
printf("请输入你想在哪个数据的后面插入新的数据:\n"); int num; scanf("%d", &num); current = head; while ((current->next != NULL) && (current->data != num)) { current = current->next; } struct node* news_node = (struct node*)malloc(sizeof(struct node)); if (news_node == NULL) { printf("内存分配失败。\n"); exit(EXIT_FAILURE); } printf("请输入数据(int类型):\n"); scanf("%d", &news_node->data); news_node->next = current->next; current->next = news_node;
current = head; while (current != NULL) { printf("当前数据为: %d \n", current->data); current = current->next; }
current = head; struct node* newss_node = (struct node*)malloc(sizeof(struct node)); if (newss_node == NULL) { printf("内存分配失败。\n"); exit(EXIT_FAILURE); } printf("请输入数据(int类型):\n"); scanf("%d", &newss_node->data); newss_node->next = current; head = newss_node;
current = head; while (current != NULL) { printf("当前数据为: %d \n", current->data); current = current->next; } printf("请输入想要删除的数据:\n"); int nuum; scanf("%d", &nuum);
if (head->data == nuum) { struct node* temp = head; head = head->next; free(temp); } else { current = head; while (current->next != NULL && current->next->data != nuum) { current = current->next; } if (current->next != NULL) { struct node* temp = current->next; current->next = current->next->next; free(temp); } else { printf("要删除的数据在链表中不存在,请检查\n"); } }
current = head; while (current != NULL) { printf("当前数据为: %d \n", current->data); current = current->next; }
current = head; while (current != NULL) { head = current->next; free(current); current = head; } return 0; }
|