原blog被 誤刪TAT 大寫的尷尬!!!!
數(shù)據(jù)結(jié)構(gòu)上機(jī)測(cè)試2-1:單鏈表操作A PRoblem Description 輸入n個(gè)整數(shù),先按照數(shù)據(jù)輸入的順序建立一個(gè)帶頭結(jié)點(diǎn)的單鏈表,再輸入一個(gè)數(shù)據(jù)m,將單鏈表中的值為m的結(jié)點(diǎn)全部刪除。分別輸出建立的初始單鏈表和完成刪除后的單鏈表。 Input 第一行輸入數(shù)據(jù)個(gè)數(shù)n; 第二行依次輸入n個(gè)整數(shù); 第三行輸入欲刪除數(shù)據(jù)m。 Output 第一行輸出原始單鏈表的長(zhǎng)度; 第二行依次輸出原始單鏈表的數(shù)據(jù); 第三行輸出完成刪除后的單鏈表長(zhǎng)度; 第四行依次輸出完成刪除后的單鏈表數(shù)據(jù)。 Example Input
10 56 25 12 33 66 54 7 12 33 12 12
Example Output
10 56 25 12 33 66 54 7 12 33 12 7 56 25 33 66 54 7 33
#include<stdio.h>#include<string.h>#include<stdlib.h>struct node{ int data; struct node *next;};int main() { int n, count, key; struct node *head, *p, *q, *t; head = (struct node *)malloc(sizeof(struct node)); head -> next = NULL; q = head; scanf("%d",&n); count = n; while(n -- ) //建立順序鏈表 { p = (struct node*)malloc(sizeof(struct node)); scanf("%d",&p -> data); p -> next = NULL; q -> next = p; q = p; } p = head -> next; scanf("%d",&key); printf("%d/n",count); p = head -> next; while (p) { printf("%d",p -> data); if (p ->next !=NULL) printf(" "); else printf("/n"); p = p -> next; } p = head; q = p; while(p) // 刪除重復(fù)元素 { while(q->next) { if(q->next->data == key) { t = q->next; q->next = t->next; free(t); count--; } else q = q->next; } p = p->next; q = p; }printf("%d/n",count); p = head -> next; while (p) //輸出 { printf("%d",p -> data); if (p ->next !=NULL) printf(" "); else printf("/n"); p = p -> next; } return 0; }新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注