题目描述
建立一个长度为n的单链表,删除链表中所有数据元素为x的结点。(数据类型为整型)
输入
第一行为链表的长度n; 第二行为链表中的数据元素; 第三行为要删除的数据元素x的值。输出
删除数据x后,单链表中的数据元素。样例输入
10 1 2 3 4 5 6 7 8 9 10 5样例输出
1 2 3 4 6 7 8 9 10#include<stdio.h> #include<string.h> #include<math.h> #include<malloc.h> #define INF 0x3f3f3f3f #define MAX(x,y) (x>y?x:y) #define MIN(x,y) (x>y?y:x) #define M 300 typedef struct LNode { int data; struct LNode* next; }LNode; void delLNode(LNode* L, int* sum) { LNode* temp = L; while(temp->next) { LNode* p = temp->next; if (p->data == (*sum)) { temp->next = p->next; free(p); } else { temp = temp->next; } } } void printfLNode(LNode* L) { LNode* temp = L; while (temp->next) { temp = temp->next; printf("%d ", temp->data); } } int main() { LNode* L; int len; scanf("%d", &len); L = (LNode*)malloc(sizeof(LNode)); L->next = NULL; LNode* temp = L; for (int i = 0; i < len; i++) { LNode* p = (LNode*)malloc(sizeof(LNode)); scanf("%d", &p->data); p->next = NULL; temp->next = p; temp = p; } int sum; scanf("%d", &sum); delLNode(L, &sum); printfLNode(L); return 0; }
SWUST OJ959: 单链表的删除
于 2022-03-19 22:15:57 首次发布