| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 303 人关注过本帖
标题:链表的插入问题~~
只看楼主 加入收藏
hywhll888
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2011-6-9
结帖率:100%
收藏
 问题点数:0 回复次数:2 
链表的插入问题~~
#include <stdio.h>
#include <math.h>
#include <string.h>
#define LEN sizeof(struct lb)
struct lb
{
    int c1;
    int c2;
    struct lb *next;
};

int n;
struct lb *tou(void)
{
    struct lb *head,*p1,*p2;
    n=0;
    p1=(struct lb*)malloc(LEN);
    p2=p1;
    scanf("%d %d",&p1->c1,&p1->c2);
    head=NULL;
    while(p1->c1!=0)
    {
        n=n+1;
        if(n==1)
        head=p1;
        else
        {
            p2=p1;
            p1=(struct lb*)malloc(LEN);
            fflush(stdin);
            scanf("%d %d",&p1->c1,&p1->c2);
            p2->next=p1;
        }
    }
    p2->next=NULL;
    return(head);
}

void main()
{
    struct lb *cr(struct lb *head);
    struct lb *sc(struct lb *head,int i);
    struct lb *p,*head;
    int i,k;
    head=tou();
    p=head;
    while(p!=NULL)
    {
        printf("%d %d\n",p->c1,p->c2);
        p=p->next;
    }
    p=head;
    printf("n=%d\n",n-1);
    printf("要删除学号请输入1\n");
    printf("要添加学号请输入2\n");
    printf("退出输入0\n");
    fflush(stdin);
    scanf("%d",&i);
    if(i==1)
    {
        printf("请输入要删除的学号:\n");
        fflush(stdin);
        scanf("%d",&k);
        head=sc(head,k);
    }
    else
        if(i==2)
        {
            head=cr(head);
        }
        else
            printf("谢谢使用\n");
    p=head;
    while(p!=NULL)
    {
        printf("%d %d\n",p->c1,p->c2);
        p=p->next;
    }

}

struct lb *sc(struct lb *head,int k)
{
    struct lb *p1,*p2;
    p1=head;
    if(head==NULL)
    {   
        printf("空表");
    }
    else
    {
        while((p1->c1!=k)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p1->c1==k)
        {
            if(p1==head)
            {
                head=p1->next;
            }
            else
            {
                p2->next=p1->next;
            }
        }
        else
            if(p1->next==NULL)
        {
            printf("找不到信息\n");
        }
    }
    return(head);
}



struct lb *cr(struct lb *head)
{
    struct lb *p0,*p1,*p2;
    p1=head;
    p2=p1;
    p0=(struct lb*)malloc(LEN);
    printf("请输入要添加的学号和成绩:\n");
    scanf("%d %d",&p0->c1,&p0->c2);
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        while((p1->c1)<(p0->c1)&&(p1->next!=NULL)) /*这个地方不明白,原来是用的逻辑运算符"||"可是不行p1->c1已经大于p0->c1了 可是程序还是在自加,用&&却正常。为什么?高手能给个解释吗?*/
        {
            p2=p1;
            p1=p1->next;
        }
        if((p1->c1)>(p0->c1))
        {
            printf("c1=%d  next=%o c1=%d\n",p1->c1,p1->next,p2->c1);
            p2->next=p0;
            p0->next=p1;
            
        }
        else
            if(p1->c1==p0->c1)
            {
                printf("学号重复\n");
            }
            else
                if(p1->next==NULL)
                {
                    p1->next=p0;
                    p0->next=NULL;
                }
    }
    return(head);
}
搜索更多相关主题的帖子: include 
2011-08-07 20:32
ybjkl
Rank: 2
等 级:论坛游民
帖 子:86
专家分:85
注 册:2011-6-21
收藏
得分:0 
用||运算,(p1->c1)<(p0->c1)为假但是p1->next!=NULL为真的嘛,最后还是真。所以程序要继续自加。
用&&运算,(p1->c1)<(p0->c1)为假,整个都为假。
2011-08-09 10:57
小菜小C
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:70
专家分:111
注 册:2011-3-18
收藏
得分:0 
回复 楼主 hywhll888
#include <stdio.h>
#include <math.h>
#include <string.h>
#define LEN sizeof(struct lb)
struct lb
{
    int c1;
    int c2;
    struct lb *next;
};

int n;
struct lb *tou(void)
{
    struct lb *head,*p1,*p2;
    n=0;
    p1=(struct lb*)malloc(LEN);
    p2=p1;
    scanf("%d %d",&p1->c1,&p1->c2);
    head=NULL;
    while(p1->c1!=0)
    {
        n=n+1;
        if(n==1)
        head=p1;
        else
        {
            p2=p1;
            p1=(struct lb*)malloc(LEN);
            fflush(stdin);
            scanf("%d %d",&p1->c1,&p1->c2);
            p2->next=p1;
        }
    }
    p2->next=NULL;
    return(head);
}

void main()
{
    struct lb *cr(struct lb *head);
    struct lb *sc(struct lb *head,int i);
    struct lb *p,*head;
    int i,k;
    head=tou();
    p=head;
    while(p!=NULL)
    {
        printf("%d %d\n",p->c1,p->c2);
        p=p->next;
    }
    p=head;
    printf("n=%d\n",n-1);
    printf("要删除学号请输入1\n");
    printf("要添加学号请输入2\n");
    printf("退出输入0\n");
    fflush(stdin);
    scanf("%d",&i);
    if(i==1)
    {
        printf("请输入要删除的学号:\n");
        fflush(stdin);
        scanf("%d",&k);
        head=sc(head,k);
    }
    else
        if(i==2)
        {
            head=cr(head);
        }
        else
            printf("谢谢使用\n");
    p=head;
    while(p!=NULL)
    {
        printf("%d %d\n",p->c1,p->c2);
        p=p->next;
    }

}

struct lb *sc(struct lb *head,int k)
{
    struct lb *p1,*p2;
    p1=head;
    if(head==NULL)
    {   
        printf("空表");
    }
    else
    {
        while((p1->c1!=k)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p1->c1==k)
        {
            if(p1==head)
            {
                head=p1->next;
            }
            else
            {
                p2->next=p1->next;
            }
        }
        else
            if(p1->next==NULL)
        {
            printf("找不到信息\n");
        }
    }
    return(head);
}



struct lb *cr(struct lb *head)
{
    struct lb *p0,*p1,*p2;
    p1=head;
    p2=p1;
    p0=(struct lb*)malloc(LEN);
    printf("请输入要添加的学号和成绩:\n");
    scanf("%d %d",&p0->c1,&p0->c2);
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        while((p1->c1)<(p0->c1)&&(p1->next!=NULL)) /*这个地方不明白,原来是用的逻辑运算符"||"可是不行p1->c1已经大于p0->c1了
                                                    可是程序还是在自加,用&&却正常。为什么?高手能给个解释吗?*/
        {
            p2=p1;                                   //||是或的意思,而&&是并且的意思,如果你用的是||那么就是说你的条件满足任意一个这个循环都能继续下去,而&&就是仁义个条件不满足就得退出循环
            p1=p1->next;
        }
        if((p1->c1)>(p0->c1))
        {
            printf("c1=%d  next=%o c1=%d\n",p1->c1,p1->next,p2->c1);
            p2->next=p0;
            p0->next=p1;
            
        }
        else
            if(p1->c1==p0->c1)
            {
                printf("学号重复\n");
            }
            else
                if(p1->next==NULL)
                {
                    p1->next=p0;
                    p0->next=NULL;
                }
    }
    return(head);
}

菜鸟一名,准备起飞
2011-08-09 11:57
快速回复:链表的插入问题~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021083 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved