| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1058 人关注过本帖
标题:一个链表程序,不知道为什么不行
取消只看楼主 加入收藏
davidguhao
Rank: 1
来 自:广东
等 级:新手上路
帖 子:126
专家分:7
注 册:2016-7-18
结帖率:89.47%
收藏
已结贴  问题点数:20 回复次数:1 
一个链表程序,不知道为什么不行
程序代码:
#include <stdlib.h>
#include <stdio.h>

#ifndef NULL
#define NULL 0
#endif

struct list
{
    char *ch;
    struct list *next_rec;
};

typedef struct list LIST;
typedef LIST *LISTPTR;

LISTPTR add_to_list(char *, LISTPTR);
void show_list(LISTPTR);
void free_memory_list(LISTPTR);

LISTPTR first = NULL;

int main(void)
{

    int i = 0;
    char *ch;
    
    ch = (char *)malloc(40);

    while(i++ < 5)
    {
        printf("\nEnter name %d,", i);

        do
        {
            printf("\nPlease enter a name(<=40):  ");
            gets(ch);
        }while( (*ch < 'a' || *ch > 'z') && ( *ch < 'A' || *ch > 'Z') );

        add_to_list(ch, first);
    }
    show_list(first);
    free_memory_list(first);
    return 0;
}

LISTPTR add_to_list( char *ch, LISTPTR first)
{
    LISTPTR new_rec = NULL;
    LISTPTR tmp_rec = NULL;
    LISTPTR prev_rec = NULL;

    if((new_rec = (LISTPTR)malloc(sizeof(LIST)) ) == NULL)
    {
        fprintf(stderr, "\nUnable to allocate memory!\n");
        exit(1);
    }

    new_rec->ch = ch;
    new_rec->next_rec = NULL;

    if(first == NULL)
    {
        first = new_rec;
        new_rec->next_rec = NULL;
    }
    else
    {
        if(strcmp(new_rec -> ch, first -> ch) < 0)
        {
            new_rec -> next_rec = first;
            first = new_rec;
        }
        else
        {
            while ( tmp_rec -> next_rec != NULL)
            {
                if( strcmp(new_rec->ch, tmp_rec->ch) < 0)
                {
                    new_rec->next_rec = tmp_rec;
                    if(new_rec->next_rec != prev_rec->next_rec)
                    {
                        fprintf(stderr, "ERROR");
                        getc(stdin);
                        exit(1);
                    }
                    prev_rec->next_rec = new_rec;
                    break;
                }
                else
                {
                    tmp_rec = first -> next_rec;
                    prev_rec = prev_rec->next_rec;
                }
            }

            if(tmp_rec->next_rec == NULL)
            {
                if(strcmp(new_rec->ch, tmp_rec->ch) < 0)
                {
                    new_rec->next_rec = tmp_rec;
                    prev_rec->next_rec = new_rec;
                }
                else
                {
                    tmp_rec = tmp_rec -> next_rec;
                    prev_rec = prev_rec -> next_rec;
                }
            }
        }
    }
    return(first);
}

void show_list(LISTPTR first)
{
    LISTPTR cur_ptr;
    int counter = 1;

    printf("\n\nRec addr    Position    Data    Next Rec addr\n");
    printf("========================================\n");

    cur_ptr = first;
    while(cur_ptr != NULL)
    {
        printf("    %p    ", cur_ptr);
        printf("    %2i    %s", counter++, cur_ptr->ch);
        printf("    %p    \n", cur_ptr->next_rec);
        cur_ptr = cur_ptr->next_rec;
    }
}

void free_memory_list(LISTPTR first)
{
    LISTPTR cur_ptr, next_rec;
    cur_ptr = first;

    while(cur_ptr != NULL)
    {
        next_rec = cur_ptr->next_rec;
        free(cur_ptr);
        cur_ptr = next_rec;
    }
}


以上是我写的程序, 不知道为什么出错
搜索更多相关主题的帖子: 链表程序 
2016-08-28 10:49
davidguhao
Rank: 1
来 自:广东
等 级:新手上路
帖 子:126
专家分:7
注 册:2016-7-18
收藏
得分:0 
回复 5楼 书生牛犊
我记得……它是可以编译通过的……只是在运行过程中输入第二个值时会出现程序崩溃……就类似用scanf没有用取址运算符一样退出来……

暨南大学市场营销……
但是我有一颗计算机专业的心……
2016-09-02 11:05
快速回复:一个链表程序,不知道为什么不行
数据加载中...
 
   



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

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