鱼C论坛

 找回密码
 立即注册
查看: 4172|回复: 12

最近遇到两个C程序看不懂想请教一下

[复制链接]
发表于 2013-7-14 15:19:08 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 番茄 于 2013-7-16 07:46 编辑

    最近遇到两个C程序看不懂想请教一下,懂的回答一下,帮忙写一下注释。。。非常感谢!!!!!
程序1
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define LEN sizeof(struct Lnode)

struct Lnode
{
    int data;
    struct Lnode *next;
};
struct Lnode * deletep(struct Lnode *h,int i)
{
    int j=1;
    struct Lnode  *q,*p;
    p=h;
    if(i==1)
    {
        h=p->next;
        free(p);
    }else{
        while((j<i)&&(p!=NULL))
        {
            p=p->next;
            j++;
        }
        if(p!=NULL)
        {
            q->next=p->next;
            free(p);
        }
    }
    return h;
}

void main()
{
    struct Lnode *p,*q,*head;
    int l,len=0;
    p=(struct Lnode *)malloc(LEN);
    head=NULL;
    printf("input num:\n");
    scanf("%d",&p->data);
    while(p->data!=0)
    {
        p->next=NULL;
        if(head==NULL){head=p;q=p;}
        else{
            q->next=p;
            q=p;
        }
        len++;
        p=(struct Lnode*)malloc(LEN);
        scanf("%d",&p->data);
    }
    printf("input the position:");
        scanf("%d",&l);
        if(l>len||l<=0)
        {
            printf("error,input please");
            printf("\n");
            return;
        }
        head=deletep(head,l);
        len--;
        printf("the new list:");
        p=head;
        for(l=0;l<=len-1;l++)
        {
            printf("%3d",p->data);
            p=p->next;
        }
    }


程序2

#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct Lnode)
struct Lnode
{
    int data;
    struct Lnode *next;
};
int search(struct Lnode *head,int key)
{
    struct Lnode *p;
    int z;
    if(head==NULL)
        printf("this is empty list!\n");
    else
    {
        p=head;
        while(p->data!=key&&p->next!=NULL)
            p=p->next;
        if(p->data==key)
            z=key;
        else
            z=0;
    }
    return (z);
}
void main()
{
    struct Lnode *p,*q,*head;
    int x,k;
    p=(struct Lnode *)malloc(LEN);
    head=NULL;
    printf("input num:\n");
    scanf("%d",&p->data);
    while(p->data!=0)
    {
        p->next=NULL;
        if(head==NULL)
        {
            head=p;q=p;
        }
        else
        {
            q->next=p;
            q=p;
        }
        p=(struct Lnode*)malloc(LEN);
        scanf("%d",&p->data);
    }
    printf("input the key:");
    scanf("%d",&x);
    k=search(head,x);
    if(k>0)
    {
        printf("search success!");
        printf("\nkey=%d",k);
    }
    else
        printf("search no success!");
}

最佳答案

查看完整内容

#include #include #define NULL 0 #define LEN sizeof(struct Lnode) struct Lnode //链表的节点类型 { int data; struct Lnode *next; }; struct Lnode * deletep(struct Lnode *h,int i)//该子函数作用为删除链表h的第i个节点,刚进原来函数有些问题,我自己修改了一下,修改的部分增加了备注 { int j=1; struct Lnode *q,*p; p=h->next;//修改,p初始化为指向首节点 if(i==1) //i为1, ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-7-14 15:19:09 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define LEN sizeof(struct Lnode)

struct Lnode //链表的节点类型
{
    int data;
    struct Lnode *next;
};
struct Lnode * deletep(struct Lnode *h,int i)//该子函数作用为删除链表h的第i个节点,刚进原来函数有些问题,我自己修改了一下,修改的部分增加了备注
{
    int j=1;
    struct Lnode  *q,*p;
    p=h->next;//修改,p初始化为指向首节点
    if(i==1) //i为1,删除的是首节点
    {
        h-next=p->next;//修改,头结点指向了原链表的首节点的下一个节点
        free(p);//释放原首节点
    }
    else //删除的为第i个节点
    {
        while((j<i-1)&&(NULL != (p-next)))//修改,循环找到第i-1节点并用p指向它
        {
            p=p->next;
            j++;
        }
        if(p!=NULL)//找到了该节点
        {   
            q = p; //新增,记录第i-1个节点的位置
            p = p->next;//新增,p指向第i个节点
            q->next=p->next;//用第i-1节点的指针域指向第i+1个节点
            free(p);//是否第i个节点
        }
    }
    return h;
}

void main()
{
    struct Lnode *p,*q,*head;
    int l,len=0;
    p=(struct Lnode *)malloc(LEN);//动态生成一个struct Lnode类型节点(第1个一般用做头节点,便于链表操作),并用p指针指向该节点
    head=NULL;
    printf("input num:\n");
    scanf("%d",&p->data);// 对上面新生成的头节点的int部分输入数值,
    while(p->data!=0) //当节点中int部分不为0时,循环生成链表中的新节点
    {
        p->next=NULL;
        if(head==NULL){head=p;q=p;}//head指向新生成的头节点
        else //生成新节点后添加进链表,q是指向链表尾部的,p指向新节点
        {
            q->next=p;
            q=p;
        }
        len++;//链表计数增加,此处应该不包含头节点
        p=(struct Lnode*)malloc(LEN);//生成节点,p指向
        scanf("%d",&p->data);//给节点输入值
    }
    printf("input the position:");
        scanf("%d",&l);
        if(l>len||l<=0)//对l的合法性进行判读
        {
            printf("error,input please");
            printf("\n");
            return;
        }
        head=deletep(head,l);//调用子函数删除链表中的第l个节点
        len--;//删除后链表中节点个数减一
        printf("the new list:");
        p=head;//将头节点的位置赋给p
        for(l=0;l<=len-1;l++)//循环输出节点的值
        {
            printf("%3d",p->data);
            p=p->next;
        }
    }
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-7-14 16:00:45 | 显示全部楼层
struct Lnode * deletep(struct Lnode *h,int i)函数构成一链表。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-14 17:04:35 | 显示全部楼层
本帖最后由 天之骄子 于 2013-7-14 17:06 编辑

是整个代码的注释!!!谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-14 17:05:35 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 12:56:32 | 显示全部楼层
这么多,才给20币。。。咋解释。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 13:01:41 | 显示全部楼层
#include <stdio.h>包含stdio.h
#include <stdlib.h>包含stdlib.h
#define NULL 0  定义一个宏NULL代替0这个值,以后用到0的时候直接用NULL就是0了。
#define LEN sizeof(struct Lnode)定义一个宏,LEN代替后面的{sizeof(struct Lnode)这个是结构体的大小。}

struct Lnode 定义一个结构体,它的名字就是Lnode
{
    int data;  定义一个整型变量data
    struct Lnode *next; 定义一个结构体的指针next;
};
struct Lnode * deletep(struct Lnode *h,int i)函数
{
    int j=1;
    struct Lnode  *q,*p;
    p=h;
    if(i==1)
    {
        h=p->next;
        free(p);
    }else{
        while((j<i)&&(p!=NULL))
        {
            p=p->next;
            j++;
        }
        if(p!=NULL)
        {
            q->next=p->next;
            free(p);
        }
    }
    return h;
}

void main()
{
    struct Lnode *p,*q,*head;
    int l,len=0;
    p=(struct Lnode *)malloc(LEN);
    head=NULL;
    printf("input num:\n");
    scanf("%d",&p->data);
    while(p->data!=0)
    {
        p->next=NULL;
        if(head==NULL){head=p;q=p;}
        else{
            q->next=p;
            q=p;
        }
        len++;
        p=(struct Lnode*)malloc(LEN);
        scanf("%d",&p->data);
    }
    printf("input the position:");
        scanf("%d",&l);
        if(l>len||l<=0)
        {
            printf("error,input please");
            printf("\n");
            return;
        }
        head=deletep(head,l);
        len--;
        printf("the new list:");
        p=head;
        for(l=0;l<=len-1;l++)
        {
            printf("%3d",p->data);
            p=p->next;
        }
    }



解释的太慢,你加我QQ 我语音给你解释吧。这样太累了。QQ15393770307
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 13:03:27 | 显示全部楼层
我建议你还是再看看群主的C语言视频。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 13:04:42 | 显示全部楼层
我认真看了看楼上的解释,他解释的就挺好!如果你会点C语言的基本语法,他说的你就看懂了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 13:05:52 | 显示全部楼层
不用加我QQ了,我也是边看看聊,边升级哈!楼上的解释挺好!就不用我解释了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 13:07:38 | 显示全部楼层
你有三天没看了吧?人家解释的好的话,就把鱼币给人家吧!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-24 23:57:28 | 显示全部楼层
白纸黑字 发表于 2013-7-14 15:19
#include
#include
#define NULL 0

非常感谢你的回答,很久来看,所以忘了回复
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-8-19 11:12:57 | 显示全部楼层
白纸黑字 发表于 2013-7-14 15:19
#include
#include
#define NULL 0

很详细的解释
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 18:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表