Null pointer assignment 是什么错误啊!!(这里有源码)

zerospace01 2004-01-05 06:02:38
#include "stdio.h"
#include "stdlib.h"

typedef struct dbnode
{
int data;
struct dbnode* prior;
struct dbnode* next;
}DBLinkList;

DBLinkList* phead[3];
int county[3];
int countb[3];

void InputData();
void SortData();
void PrintData();


void main()
{
InputData(); /*输入数据*/
SortData(); /*排序*/
PrintData(); /*打印排序结果*/

return;

}

void CreateList(int data, int i);

void InputData()
{
int i = 0, j = 0;
int count = 0;
int data;

printf("\ninput three group numbers!\n");
for(i=0; i<3; i++)
{
phead[i] = malloc(sizeof(DBLinkList));
if(phead[i] == NULL)
{
printf("Cannot malloc memery!\n");
return;
}
phead[i]->data = 0;
phead[i]->prior = phead[i];
phead[i]->next = NULL;
printf("the count of the %dth: ",i+1);
scanf("%d",&count);
printf("input the number of %dth: ",i+1);
for(j=0; j<count; j++)
{
scanf("%d",&data);
CreateList(data, i);
}
printf("\n\n");
count = 0;
}
printf("input completly!\n");
scanf("%d",&count);
}

void SortData()
{
int i = 0;
DBLinkList* pmin = NULL,
* ptr = NULL,
* h = NULL;
for(i=0; i<3; i++)
{
printf("Sort NO.%d\n",i);
h = phead[i]->next;
if( h == NULL)
{
printf("LinkList is Empty!\n");
return;
}
countb[i] = 0;
county[i] = 0;
while( (h->next) != NULL)
{
pmin = h; ptr = pmin->next;
while(ptr != NULL)
{
countb[i]++;
if((ptr->data) < (pmin->data))
{
pmin = ptr;
}
ptr = ptr->next;

}
if(pmin != h)
{
ptr = h->next; h->next = pmin->next;
h->next->prior =h; pmin->next = ptr;
pmin->next->prior = pmin;

ptr = h->prior; h->prior = pmin->prior;
h->prior->next = h; pmin->prior = ptr;
pmin->prior->next = pmin;

county[i]++;
}
h = pmin->next;
}
}
}

void PrintData()
{
int i = 0;
DBLinkList* ptr;
printf("\nSort result:\n\n");

for(i=0; i<3; i++)
{
printf("\nNO.%d : ",i+1);
ptr = phead[i];
while(ptr->next != NULL)
{
ptr = ptr->next;
printf("%d ",ptr->data);
}
printf("\nCompare counts : %d\n",countb[i]);
printf("Move counts : %d\n\n",county[i]);
}
}

void CreateList(int data, int i)
{
DBLinkList* ptr =NULL;
DBLinkList* pnew = malloc(sizeof(DBLinkList));

if(pnew == NULL)
{
printf("\nCannot malloc memery!\n");
return;
}
pnew->data = data; pnew->next = NULL; pnew->prior = NULL;
ptr = phead[i];
printf("ptr = phead[%d]\n",i);
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = pnew;
pnew->prior = ptr;
return;
}

本程序实现的功能是,用直接选择法排序,用双向链表存储。
各位帮忙找找错误啊!!谢谢大家!
...全文
61 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Int345 2004-01-05
  • 打赏
  • 举报
回复
我改了两个地方:在malloc前面加上(DBLinkList*)
VC6.0编译通过,没问题了!也能运行正常!


#include "stdio.h"
#include "stdlib.h"

typedef struct dbnode
{
int data;
struct dbnode* prior;
struct dbnode* next;
}DBLinkList;

DBLinkList* phead[3];
int county[3];
int countb[3];

void InputData();
void SortData();
void PrintData();


void main()
{
InputData(); /*输入数据*/
SortData(); /*排序*/
PrintData(); /*打印排序结果*/

return;

}

void CreateList(int data, int i);

void InputData()
{
int i = 0, j = 0;
int count = 0;
int data;

printf("\ninput three group numbers!\n");
for(i=0; i<3; i++)
{
phead[i] = (DBLinkList*)malloc(sizeof(DBLinkList));//第一个地方
if(phead[i] == NULL)
{
printf("Cannot malloc memery!\n");
return;
}
phead[i]->data = 0;
phead[i]->prior = phead[i];
phead[i]->next = NULL;
printf("the count of the %dth: ",i+1);
scanf("%d",&count);
printf("input the number of %dth: ",i+1);
for(j=0; j<count; j++)
{
scanf("%d",&data);
CreateList(data, i);
}
printf("\n\n");
count = 0;
}
printf("input completly!\n");
scanf("%d",&count);
}

void SortData()
{
int i = 0;
DBLinkList* pmin = NULL,
* ptr = NULL,
* h = NULL;
for(i=0; i<3; i++)
{
printf("Sort NO.%d\n",i);
h = phead[i]->next;
if( h == NULL)
{
printf("LinkList is Empty!\n");
return;
}
countb[i] = 0;
county[i] = 0;
while( (h->next) != NULL)
{
pmin = h; ptr = pmin->next;
while(ptr != NULL)
{
countb[i]++;
if((ptr->data) < (pmin->data))
{
pmin = ptr;
}
ptr = ptr->next;

}
if(pmin != h)
{
ptr = h->next; h->next = pmin->next;
h->next->prior =h; pmin->next = ptr;
pmin->next->prior = pmin;

ptr = h->prior; h->prior = pmin->prior;
h->prior->next = h; pmin->prior = ptr;
pmin->prior->next = pmin;

county[i]++;
}
h = pmin->next;
}
}
}

void PrintData()
{
int i = 0;
DBLinkList* ptr;
printf("\nSort result:\n\n");

for(i=0; i<3; i++)
{
printf("\nNO.%d : ",i+1);
ptr = phead[i];
while(ptr->next != NULL)
{
ptr = ptr->next;
printf("%d ",ptr->data);
}
printf("\nCompare counts : %d\n",countb[i]);
printf("Move counts : %d\n\n",county[i]);
}
}

void CreateList(int data, int i)
{
DBLinkList* ptr =NULL;
DBLinkList* pnew = (DBLinkList*)malloc(sizeof(DBLinkList));//第二个地方

if(pnew == NULL)
{
printf("\nCannot malloc memery!\n");
return;
}
pnew->data = data; pnew->next = NULL; pnew->prior = NULL;
ptr = phead[i];
printf("ptr = phead[%d]\n",i);
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = pnew;
pnew->prior = ptr;
return;
}

zerospace01 2004-01-05
  • 打赏
  • 举报
回复
呵呵,这个程序的每一个函数都是我自己写的,不是书上找的!!

我没想出来哪里有错误,大家帮帮忙了!
zerospace01 2004-01-05
  • 打赏
  • 举报
回复
可不可以具体点,错在哪里!
12l 2004-01-05
  • 打赏
  • 举报
回复
它的意思是 空指针使用错误
12l 2004-01-05
  • 打赏
  • 举报
回复
是的,你的建表函数有问题啊...不要抄书啊,书上也有错误的
abitz 2004-01-05
  • 打赏
  • 举报
回复
建表的方法不对,造成排序时使用非法指针

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧