看到它,说明你我有缘,一个初级c 程序员双向链表创建时,出现的异常。
本帖最后由 风之残月 于 2014-11-10 10:14 编辑<p>/*</p><p>各路大神,既然看到也算你我有缘,首先感谢你花时间看到这个代码。 </p><p>出现一个小问题, 比如要创建5个结点的链表, 只能输入 两个值,例如 1,2 。然后就是异常, 做好程序返回一个异常的值</p><p>谁能帮忙改进呢?</p><p>*/</p><p>#include<stdio.h>
#include<stdlib.h>// 包含exit 和 malloc
struct dlist{
int num ;
struct dlist *pre ;//*pre 表示前面的结点
struct dlist *next ; //*next 表示后面的结点
} ;
typedef struct dlist* pdlist ; // pdlist 代表指针双向链表
#define LENsizeof( pdlist )// LEN 表示双向链表的长度
pdlistinitdlist( pdlist head ) // 返回双向链表的地址
{
head = ( pdlist )malloc( LEN ) ; // LEN 表示双向链表的长度
if( head == NULL )
{
printf("memery set error ") ;
exit(0 ) ;
}
head->pre = NULL ;
head->next = NULL ;
return head ;
}
void creatdlistnum ( pdlist head , int numpos ) // c创建numpos ge 结点的双向链表
{
pdlist q = NULL , p = NULL ;
int i ;
p = ( pdlist) malloc( LEN ) ;
if( p == NULL )
{
printf("first test head is not NULL");
exit(0) ;
}
head->next = p ;
p->next = NULL ;
p->pre = NULL ;
printf("input the first point value ") ;
scanf("%d",&p->num) ; // 前面p 算第一个结点
for( i = 1 ; i < numpos ; i++)
{
q = ( pdlist ) malloc( LEN ) ;
if( q == NULL )
{
printf("error , check out ,second ") ;
exit(0) ;
}
printf("input the list value") ;
scanf("%d",&q->num) ; // 只能输入两个链表的值 , 其他就不行了
p->next = q ;// 这一句和下面这一句不能换
q->pre = p ;
p = q ;
}
p->next = NULL ;
}
void prindlist( pdlist head )
{
pdlist p = head->next ;
int i = 0 ;
printf("print in next order ");
while( p != NULL )
{
printf("the %d point value %d" , i++, p->num ) ;
if( p->next == NULL )
break ;
else
p = p->next ;
}
printf("prin in pre order") ;
i = 0 ;
while( p != NULL )
{
printf("the %d point value %d", i++ , p->num ) ;
if( p->next == NULL )
break ;
else
p = p->pre ;
}
}
void freedlist( pdlist head )
{
pdlist q , p = head->next ;
while( p != NULL )
{
q = p->next ;
free( p ) ;
p = q ;
}
head->next = NULL ;
}
int main( void )
{
pdlist head = NULL ;</p><p>int numpos ;</p><p>
head = initdlist( head ) ;</p><p>
printf("input the num value in order to creat num point list") ;</p><p>
scanf("%d",&numpos) ;</p><p>
creatdlistnum( head , numpos ) ; //问题出现在这个函数里</p><p>
prindlist( head ) ;
freedlist( head ) ; // 释放头结点
free( head ) ;
head = NULL ;
return 0 ;
}
</p>
#include<stdio.h>
#include<stdlib.h>// 包含exit 和 malloc
struct dlist{
int num ;
struct dlist *pre ;//*pre 表示前面的结点
struct dlist *next ; //*next 表示后面的结点
} ;
typedef struct dlist* pdlist ; // pdlist 代表指针双向链表
//modify by XXXX 20140810
#define LENsizeof( struct dlist)// LEN 表示双向链表的长度
pdlistinitdlist( pdlist head ) // 返回双向链表的地址
{
head = ( pdlist )malloc( LEN ) ; // LEN 表示双向链表的长度
if( head == NULL )
{
printf("memery set error ") ;
exit(0 ) ;
}
head->pre = NULL ;
head->next = NULL ;
return head ;
}
void creatdlistnum ( pdlist head , int numpos ) // c创建numpos ge 结点的双向链表
{
pdlist q = NULL , p = NULL ;
int i ;
p = ( pdlist) malloc( LEN ) ;
if( p == NULL )
{
printf("first test head is not NULL");
exit(0) ;
}
head->next = p ;
p->next = NULL ;
//modify by XXXXX 20140810
p->pre = head ;
printf("input the first point value:") ;
scanf("%d",&p->num) ; // 前面p 算第一个结点
for( i = 1 ; i < numpos ; i++)
{
q = ( pdlist ) malloc( LEN ) ;
if( q == NULL )
{
printf("error , check out ,second ") ;
exit(0) ;
}
printf("input the list value:") ;
scanf("%d",&q->num) ; // 只能输入两个链表的值 , 其他就不行了
//add by XXXX 20140810
q->next = NULL;
q->pre = p ;
p->next = q ;// 这一句和下面这一句不能换
p = q ;
}
//p->next = NULL ;
}
void prindlist( pdlist head )
{
pdlist p = head->next ;
int i = 0 ;
printf("print in next order \n");
//modify by XXXXX 20140810
while( p->next != NULL )
{
printf("the %d point value %d\n" , i++, p->num ) ;
/*if( p->next == NULL )
break ;
else*/
p = p->next ;
}
//add by XXXX 20140810
printf("the %d point value %d\n" , i++, p->num ) ;
printf("print in pre order\n") ;
i = 0 ;
while( p->pre != NULL )
{
printf("the %d point value %d\n", i++ , p->num ) ;
//delete by XXXXX 20140810
/*if( p->next == NULL)
break ;
else*/
p = p->pre ;
}
}
void freedlist( pdlist head )
{
pdlist q , p = head->next ;
while( p != NULL )
{
q = p->next ;
free( p ) ;
p = q ;
}
head->next = NULL ;
}
int main( void )
{
pdlist head = NULL ;
int numpos ;
//1 初始化头结点
head = initdlist( head ) ;
printf("input the num value in order to creat num point list\n") ;
//2 创建结点数
scanf("%d",&numpos) ;
creatdlistnum( head , numpos ) ; //问题出现在这个函数里
prindlist( head ) ;
freedlist( head ) ; // 释放头结点
free( head ) ;
head = NULL ;
return 0 ;
}
我在你的基础之上改了一下,以后提交的代码要能运行,你提交的代码中还有<p>,不知道怎么回事。 还没有人回答,我先坐庄。。。 #define LENsizeof( pdlist )// LEN 表示双向链表的长度
这句错了,。。。。。。应该是
#define LENsizeof( dlist )
LEN应该是结构体dlist的大小,而不是win32中4个字节指针的大小 哎,C,拿着门语言入门的话太烦躁了。 流行语 发表于 2014-8-10 08:56
我在你的基础之上改了一下,以后提交的代码要能运行,你提交的代码中还有,不知道怎么回事。
恩,那个是复制到编辑框里排版的时候,不小心弄得。。感谢热心人 elvo 发表于 2014-8-10 07:49
#define LENsizeof( pdlist )// LEN 表示双向链表的长度
这句错了,。。。。。。应该是
#define LEN...
虽然你的意思是对的,但我测试了以下 #define LENsizeof( dlist ) 是错的,编译器意思是dlist 是没有定义的,用#define LEN sizeof( struct dist ) 就可以通过编译。。正常运行 miles0918 发表于 2014-8-10 08:55
哎,C,拿着门语言入门的话太烦躁了。
虽然看不懂啥意思,但感谢热心回复。。
页:
[1]