jeans chen
we've got them by the balls
随笔 - 306,  文章 - 0,  评论 - 18,  阅读 - 18万

http://www.oschina.net/code/snippet_252667_27314#comments

这个代码有很多错误,估计是从老谭书上抄来但是很多还抄错了:对照老谭的书好好研究下。切记!

p2是p1的跟屁虫!切记

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
#include"stdio.h"
#include"malloc.h"
struct stu
{
    int num;//这个是学号
    float score;//这个是分数
    struct stu *next;
};
struct stu*create()
{
    int n;//这个n这里不大合适 ,最好弄个全局的
    struct stu *head,*p1,*p2;
    p1=p2=(struct stu*)malloc(sizeof(struct stu));
    scanf("%d %f",&p1->num,&p1->score);
    head=NULL;
    n=0;
    while(p1->num!=0)
    {
        n++;
        if(n==1)head=p1;
        else
        {
            p2->next=p1;
            p2=p1;
            p1=(struct stu*)malloc(sizeof(struct stu));
            scanf("%d %f",&p1->num,&p1->score);
        }
    }
    p2->next=NULL;
    return head;
};
//难点在插入操作的实现上(默认从小到大排列的)
struct stu *insert(struct stu *head,struct stu *stud)
{
    struct stu *p0,*p1,*p2;
    p1=head;
    p0=stud;
    while((p0->num>p1->num)&&(p1->next!=NULL))
    {
        p2=p1;
        p1=p1->next;
    }
    if(p0->num<p1->num)//能在队列中插入(不是尾巴)
    {
        if(head==p1)head=p0;//在头部前插入
        else//在非头部插入
        {
            p2->next=p0;
            p0->next=p1;
        }
    }
    else//在尾巴插入
    {
        p1->next=p0;
        p0->next=NULL;
    }  //这个地方要怎么维护总的num呢 ?
    return head;
};
//删除操作难度次之
struct stu *del(struct stu *head,int num)
{
    struct stu *p1,*p2;
    p1=head;
    if(head!=NULL)
    {
        while((p1->num<num)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
     if(num==p1->num)//找到节点了
     {   //这里要考虑删除头节点的情况
         p2->next=p1->next;
         p2=p1;      //要考虑删除的节点内存释放      //要考虑维护n的数目
     }
     else//没有找到节点
     {
         printf("NO Result!!!\n");
     }
    }
    else
        printf("The linklist is EMPTY\n");
    return head;
};
void print(struct stu *head)
{
    struct stu *p;
    p=head;
    while(p)
    {
        printf("%d %f\n",p->num,p->score);
        p=p->next;
    }
}
void main()
{
    int n;
    struct stu *create();
    struct stu *insert(struct stu *head,struct stu *stud);
    void print(struct stu *head);
    struct stu *head,*p,*p1;
    head=create();
    p=head;
    print(p);
  printf("please input :\n");
    scanf("%d %f",&p1->num,&p1->score);
    p=insert(head,p1);
    print(p);
    printf("please input the del num:\n");
    scanf("%d",&n);
    p=del(head,n);
    print(p);
}

 

 

posted on   jeans chen  阅读(216)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示