| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 247 人关注过本帖
标题:Turbo C写线性链表遇到奇怪问题
收藏  订阅  推荐  打印 
iamyou
Rank: 1
等级:新手上路
帖子:1
积分:110
注册:2008-7-27
Turbo C写线性链表遇到奇怪问题

#include <stdio.h>

#define NULL 0

struct unit
{
    int num;
    struct unit *next;
    struct unit *forward;
};

struct unit *head;

struct unit *create(void)
{
    struct unit *p1, *p2;
    int i;
    p2 = p1 = (struct unit*)malloc(sizeof(struct unit));
    head = p1;
    for (i = 0; i < 10; i++)
    {
        p1 -> num = i;
        p2 = p1;
        p1 = (struct unit*)malloc(sizeof(struct unit));
        p2 -> next = p1;
        p1 -> forward = p2;
    }
    p2 -> next = head;
    head -> forward = p2;
}

void print()
{
    struct unit *p;
    int i = 0;
    p = head;
    printf("............\n");
    for (i = 0; i < 10; i++)
    {
        printf("%d\n",p->num);
        p = p -> next;
    }
    printf("\n");
    p = head -> forward;
    for (i = 0; i < 10; i++)
    {
        printf("%d\n",p->num);
        p = p -> forward;
    }
    printf("............\n");
}

int search(int input)
{
    struct unit *p;
    if (head->num == input)
        return 0;
    else
    {
        p = head -> next;
        do
        {
            if (p->num == input)
                return 0;
            else
                p = p ->next;
        }while(p != head);
    }
    return 1;
}

void delete()
{
    struct unit *p;
    int del;
    p = head;
    printf("Input the number you want to delete!\n");
    scanf("%d\n",&del);
    if (head->num == del)
        head = p -> next;
    if (search(del) == 1)
        printf("The number you want to delete doesn't exist!\n");
    else
    {
        while (p->next->num != del)
            p = p -> next;
        p -> next -> next -> forward = p;
        p -> next = p -> next -> next;
    }
    print();
}

int searchcore()
{
    int input;
    printf("Please input the number you want to search!\n");
    scanf("%d\n",&input);
    return search(input);
}

void insert()
{
    int ins,pos;
    int i;
    struct unit *p, *new;
    printf("Please input the number you want to insert!\n");
    scanf("%d\n",&ins);
    printf("Please input the position you want to put the number!\n");
    scanf("%d\n",&pos);
    p = head;
    for (i = 0; i < pos; i++)
        p = p -> next;
    new =  (struct unit*)malloc(sizeof(struct unit));
    new -> num = ins;
    p -> next -> forward = new;
    new -> next = p -> next;
    p -> next = new;
    new -> forward = p;
    print();
}

main()
{
    create();
    /*print();
    printf("\n");
    if (searchcore() == 0)
        printf("Found!\n");
    else
        printf("Not Found!\n");
    delete(); */
    insert();
    getch();
}

小弟多年不写,前些天想写个Linear找找感觉
但是void insert()时遇到比较奇怪的事情
就是两次Scanf前一次被要求输入两次,好像第一次没有用,(这样就总共输入两次),但计算机采取的数据却只是3次中的两次,我以前从来没遇到过这种情况啊。。。
大家可以试试在自己这里运行,到底是我写的有问题,还是TC有问题呢?
搜索更多相关主题的帖子: 链表  Turbo  线性  
2008-7-27 20:48
fxstudenting
Rank: 1
等级:新手上路
帖子:13
积分:242
注册:2008-7-28

这是数据结构方面的基础问题,我能帮你解答,去这个两博客其中的一个,把问题写到留言版上,我马上给你解答。
http://dlstudenting.51.com
http://fxstudenting.blog.sohu.com
2008-7-28 16:58
woshiyun
Rank: 3Rank: 3
等级:中级会员
帖子:173
积分:2404
注册:2008-6-16

去掉scanf中的\n
2008-7-28 17:02
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.049980 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved