| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 660 人关注过本帖
标题:来看看这个链表程序
只看楼主 加入收藏
davidguhao
Rank: 1
来 自:广东
等 级:新手上路
帖 子:126
专家分:7
注 册:2016-7-18
结帖率:89.47%
收藏
 问题点数:0 回复次数:1 
来看看这个链表程序
要死……打了半天不知道为什么报错……

程序代码:
#include <stdio.h>
#include <stdlib.h>

#ifndef NULL
#define NULL 0
#endif

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

typedef struct list LIST;
typedef LIST *LISTPTR;

LISTPTR add_to_list(int, LISTPTR);
void show_list(LISTPTR);
void free_memory_list(LISTPTR);

int main(void)
{
LISTPTR first = NULL;
int i = 0;
int ch;
char trash[256];

while( i++<5)
{
ch = 0;
printf("\nEnter character %d, ", i);

do
{
printf("\nMust be a to z: ");
ch = getc(stdin);
gets(trash);
}while( (ch <'a' || ch >'z')&& (ch <'A' || ch >'Z'));

first = add_to_list(ch, first);
}

show_list(first);
free_memory_list(first);
return 0;
}




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

new_rec = (LISTPTR)malloc(sizeof(LIST));
if(!new_rec)
{
printf("\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( new_rec -> ch < first -> ch)
{
new_rec -> next_rec = first;
first =new_rec;
}
else
{
tmp_rec = first->next_rec;
prev_rec = first;

if( tmp_rec == NULL)
{
prev_rec->next_rec = new_rec;
}
else
{
while(( tmp_rec -> next_rec != NULL))
{
if(new_rec -> ch < tmp_rec ->ch)
{
new_rec->next_rec = tmp_rec;
if(new_rec -> next_rec != prev_rec->next_rec)
{
printf("ERROR");
getc(stdin);
exit(0);
}
prev_rec->next_rec = new_rec;
break;
}
else
{
tmp_rec = tmp_rec -> next_rec;
prev_rec = prev_rec -> next_rec;
}
}

if(tmp_rec -> next_rec == NULL)
{
if(new_rec -> ch < tmp_rec ->ch)
new_rec -> next_rec = tmp_rec;
prev_rec -> next_rec = new_rec;
}
else 
{
tmp_rec -> next_rec =new_rec;
new_rec -> next_rec = NULL;
}
}
}
}
}


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    %c", 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_ptr;
cur_ptr = first;
while(cur_ptr !=NULL)
{
next_ptr = cur_ptr->next_rec;
free(cur_ptr);
cur_ptr = next_ptr;
}
}
搜索更多相关主题的帖子: 链表程序 color 
2016-08-14 23:04
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10559
专家分:42996
注 册:2014-5-20
收藏
得分:0 
编译器有提示 { 或 } 出问题?
2016-08-15 07:20
快速回复:来看看这个链表程序
数据加载中...
 
   



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

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