国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

數(shù)據(jù)結(jié)構(gòu)上機(jī)測試2-1:單鏈表操作A

2019-11-10 23:03:03
字體:
供稿:網(wǎng)友

PRoblem Description

輸入n個整數(shù),先按照數(shù)據(jù)輸入的順序建立一個帶頭結(jié)點(diǎn)的單鏈表,再輸入一個數(shù)據(jù)m,將單鏈表中的值為m的結(jié)點(diǎn)全部刪除。分別輸出建立的初始單鏈表和完成刪除后的單鏈表。

Input

第一行輸入數(shù)據(jù)個數(shù)n;第二行依次輸入n個整數(shù);第三行輸入欲刪除數(shù)據(jù)m。

Output

第一行輸出原始單鏈表的長度;第二行依次輸出原始單鏈表的數(shù)據(jù);第三行輸出完成刪除后的單鏈表長度;第四行依次輸出完成刪除后的單鏈表數(shù)據(jù)。

Example Input

1056 25 12 33 66 54 7 12 33 1212

Example Output

1056 25 12 33 66 54 7 12 33 12756 25 33 66 54 7 3301
 #include<stdio.h>
02#include<stdlib.h>
03#include<string.h>
04struct node
05{
06    int data;
07    struct node *next;
08};
09struct node *creat(int n)
10{
11    struct node *head,*p,*q;
12    int i;
13    head=(struct node *)malloc(sizeof(struct node));
14    head->next=NULL;q=head;
15    for(i=0;i<n;i++)
16    {
17        p=(struct node *)malloc(sizeof(struct node));
18        scanf("%d",&p->data);
19        p->next=NULL;q->next=p;
20        q=p;
21    }
22    q=head->next;
23    return head;
24};
25int del(struct node *head,int n,int m)
26{
27    struct node *p,*q;
28    p=head;
29    while(p->next!=NULL)
30    {
31            if(p->next->data==m)
32            {
33                q=p->next;
34                p->next=q->next;
35                free(q);
36                n--;               
37            }
38            else
39                p=p->next;   
40    }
41    return n;
42}
43void show(struct node *head)
44{
45    struct node *p;
46    p=head->next;
47    while(p!=NULL)
48    {
49        if(p->next!=NULL)
50            printf("%d ",p->data);
51        else
52            printf("%d/n",p->data);
53        p=p->next;
54    }
55}
56void main()
57{
58    int n,m,flag;
59    struct node *head;
60    scanf("%d",&n);
61    head=creat(n);
62    scanf("%d",&m);
63    printf("%d/n",n);
64    show(head);
65    flag=del(head,n,m);
66    printf("%d/n",flag);
67    show(head);
68}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五莲县| 建昌县| 如皋市| 抚宁县| 沙河市| 虎林市| 河津市| 新竹市| 卫辉市| 呈贡县| 东明县| 阿拉善盟| 泽库县| 祁连县| 大同县| 阿拉善盟| 阳新县| 镇平县| 天等县| 蓬安县| 西充县| 通榆县| 浑源县| 德兴市| 武安市| 信宜市| 泉州市| 永清县| 新邵县| 石景山区| 大理市| 克东县| 湾仔区| 孝义市| 大英县| 五华县| 彰化市| 达孜县| 朝阳区| 徐闻县| 科技|