| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 268 人关注过本帖
标题:链表问题求解答~~~
只看楼主 加入收藏
hywhll888
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2011-6-9
结帖率:100%
收藏
 问题点数:0 回复次数:1 
链表问题求解答~~~
#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()
{
    void sc(struct lb *head,int i);
    struct lb *p,*head;
    int i;
    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("要删除学号请输入学号,否则输入0\n");
    fflush(stdin);
    scanf("%d",&i);    /*输入要删除的号码*/

    if(i>0)
    {
        sc(head,i);
    }
    else
        printf("谢谢使用\n");
    while(p!=NULL)
    {
        printf("%d %d\n",p->c1,p->c2);
        p=p->next;
    }

}

void sc(struct lb *head,int i)   /*删除输入的号码*/

{
    struct lb *p1,*p2;
    p1=head;
    if(head==NULL)
    {   
        printf("空表");
    }
    else
    {
        while((p1->c1!=i)||(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p1->next==NULL)          /*这条IF语句为什么执行不了?*/

        {
            printf("找不到信息");
        }
        else
            if(p1==head)
            {
                *head=*(p1->next);
            }
            else
                if(p1->c1==i)
                {
                    p2->next=p1->next;
                }
    }
}
搜索更多相关主题的帖子: include 空间 信息 
2011-08-07 13:12
hywhll888
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2011-6-9
收藏
得分:0 
解决了。。
2011-08-07 13:51
快速回复:链表问题求解答~~~
数据加载中...
 
   



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

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