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
| #include <stdio.h> #include <stdlib.h>
typedef struct LinkList{ int d; struct LinkList *next; } List;
List *add_list(List *head, int data){ List *newnode = (List *)malloc(sizeof(List));
newnode->d = data; newnode->next = NULL;
if (head ==NULL) { head = newnode; }else { List *pT = head; while (pT->next != NULL) { pT = pT->next; } pT->next = newnode; } return head; }
void output(List *head){ List *pT = head; while (pT != NULL) { printf("%d",pT->d); printf(" "); pT = pT->next; } }
void del(List *head,int x){ if (head == NULL) { return; }
if (head->d == x) { List *temp = head; head = head->next; free(temp); } List *p = head; while (p->next != NULL) { if (p->next->d == x) { List *temp = p->next; p->next = p->next->next; free(temp); }else { p = p->next; } } return; }
int main() { int n,i,x; List *head = NULL; scanf("%d",&n); getchar(); scanf("%d",&x); int a[n];
for (i=0; i < n; i++) { scanf("%d",&a[i]); getchar(); } for (i=0; i < n; i++) { head = add_list(head, a[i]); } del(head,x); output(head); return 0; }
|