#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 LEN sizeof( struct dlist) // LEN 表示双向链表的长度
pdlist initdlist( 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>,不知道怎么回事。 |