链表处理函数运行出错

li6177 2012-10-30 10:48:00
有LinkFunSeries.cpp和LinkMain.cpp两个源文件,LinkFunSeries.cpp里是各个链表处理函数的定义,LinkMain.cpp是对链表处理函数简单调用以验证其正确性。
代码如下:
LinkFunSeries.cpp

#include <iostream>
using namespace std;
#ifndef LinkFunSeries_V_M
#define LinkFunSeries_V_M
struct student{int num;float score;struct student *next;};
void DelOne(struct student*head,struct student *q)//无序表删除一个节点,q为待删除节点
{
struct student *p=head;
if(p==q)
head=head->next;
else
{
while(p->next!=q)
p=p->next;
p->next=q->next;
}
delete q;
}
void DelSort(struct student*head,struct student *q)
{ //有序表删除一个节点,被删节点后所有节点序号自减1
struct student *p=head;
if(p==q)
{
p=head=head->next;
p->num--;
}
else
{
while(p->next!=q)
p=p->next;
p->next=q->next;
}
delete q;
while(p->next!=NULL)
{
p=p->next;
p->num--;
}
}

struct student *CreatNoSort() //手动输入无序链表
{
struct student *head,*p1,*p2;
p1=new student;
cin>>p1->num>>p1->score;
head=p2=p1;
while(p1->num!=0)
{
p1=new student;
cin>>p1->num>>p1->score;
p2->next=p1;
p2=p1;
}
p1->next=NULL;
DelOne(head,p2); //删掉最后作为结束创建的节点,因为那个节点无实际意义
return head;
}

struct student *Creat() //手动输入递增序号链表,序号不得重复
{
struct student *head,*p1,*p2;
p1=new student;
cin>>p1->num>>p1->score;
head=p2=p1;
while(p1->score>=0)
{
p1=new student;
p2->next=p1;
mark: cin>>p1->num>>p1->score;
if(p1->num<=p2->num)
{
cout<<"输入错误,序号必须递增,请重新输入:\n";
goto mark;
}
p2=p1;
}
p1->next=NULL;
DelOne(head,p2);
return head;
}

struct student *CreatAuto() //创建自动生成递增序号的链表
{
struct student *head,*p1,*p2;
int i=1;
p1=new student;
p1->num=i;
cin>>p1->score;
head=p2=p1;
while(p1->score<0) //由于num自动生成,以score为负作为创建结束条件
{
p1=new student;
p2->next=p1;
p1->num=++i;
cin>>p1->score;
p2=p1;
}
p1->next=NULL;
DelOne(head,p2);
return head;
}

void PrintAll(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
cout<<"#"<<p->num<<','<<"score:"<<p->score<<endl;
p=p->next;
}
}

struct student *GetOne(struct student *head,int num)
{
struct student *p;
int n=0;
p=head;
while(p->num!=num&&p!=NULL)
p=p->next;
return p;
}

void Insert(struct student *head,struct student *q)
{
struct student *p1,*p2;
p1=head;
while(p1->num<q->num && p1!=NULL)
{
p2=p1;
p1=p1->next;
}
if(head!=p1)
{
q->next=p1;
p2->next=q;
p2=q;
while(p2->num==p1->num)
{
p1->num++;
p2=p1;
p1=p1->next;
}
}
else
{
q->next=head;
head=q;
}
}

void sort(struct student *head) //选择排序,
{
struct student *p1,*p2,*min,*pmin;
//p1指向每次新排好序的最小值,p2为遍历游标,pmin为最小节点前驱,min保存每趟遍历的最小值
p1=p2=min=head;
while(p2->next!=NULL) //先使head为最小值,找出num最小的节点
{
if(min->num>p2->next->num)
{
pmin=p2;
min=p2->next;
}
p2=p2->next;
}
if(head!=min) //使min前驱为head,然后使head=min
{
min->next=head;
head=min;
p1=head;
}
min=p2=head->next; //min和p2指向未排序部分的第一节点。
while(p1!=NULL) //第一重循环,把每次未排序部分的最小值放在该部分最前,p1前移一位指向该最小值
{
while(p2->next!=NULL) //第二重循环,找出未排序部分的最小值
{
if(min->num>p2->next->num)
{
pmin=p2;
min=p2->next;
}
p2=p2->next;
}
if(min!=p1->next) //min非无序部分第一位,进行min与首位交换
{
pmin->next=min->next;
min->next=p1->next;
p1->next=min;
}
p1=p1->next;
min=p2=p1->next;
}
}
void DelALL(struct student *head)
{
struct student *p1,*p2;
p1=p2=head;
while(NULL!=p2)
{
p2=p2->next;
delete p1;
p1=p2;
}
}
#endif


LinkMain.cpp

#include <iostream>
#include "LinkFunSeries.cpp"
using namespace std;
void main()
{
struct student *head,*p,*q;
int n;
cout<<"创建无序表\n";
head=CreatNoSort();
PrintAll(head);
cout<<"请输入要删除节点的序号值\n";
cin>>n;
p=GetOne(head,n);
cout<<"Delete #"<<p->num<<" score:"<<p->score<<endl;
DelOne(head,p);
cout<<"删除后的链表\n";
PrintAll(head);
cout<<"插入链表,请输入插入元素的值:\n";
q=new student;
cin>>q->num>>q->score;
Insert(head,q);
PrintAll(head);
DelALL(head);
}

这两个源文件放在同一文件夹中,在vc6里打开LinkMain.cpp单文件编译运行,结果插入函数不能插入节点,删除节点若为删除头节点,则内存操作错误报警,求解救。
...全文
198 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
li617745 2012-11-22
  • 打赏
  • 举报
回复
引用 6 楼 anhuizhuanjiao 的回复:
要断电了,发一段代码,希望有帮助 C/C++ code?scanf("%d",&j); p->data=e;……
写错了,是p->data=j;
li617745 2012-11-22
  • 打赏
  • 举报
回复

void DelSort(struct student* &head,struct student *q)
{		//有序表删除一个节点,被删节点后所有节点序号为前驱+1
	//q为待删除节点;
	if(head==NULL) 
	{
		cout<<"Nothing to be deleted\n";
		return;
	}
	struct student *p=head;  
	if(p==q)
		if(head->next==NULL)
		{	
			head=NULL;
			delete q;
			return;
		}
		else
			p=head=head->next;
	else
	{
		while(p->next!=q)
			p=p->next;
		p->next=q->next;
	}
	delete q;
	q=p->next;
	while(q!=NULL && q->num!=p->num+1)
	{
		q->num=p->num+1;
		p=q;
		q=q->next;
	}
}

void DelOne(struct student* &head,struct student *q)//无序表删除一个节点
{		
	if(head==NULL) 
	{
		cout<<"Nothing to be deleted\n";
		return;
	}
	struct student *p=head;
	if(p==q)
			if(head->next==NULL)
		{	
			head=NULL;
			return;
		}
		else
			head=head->next;
	else
	{
		while(p->next!=q)
			p=p->next;
		p->next=q->next;
	}
	delete q;
}
转角天边 2012-11-10
  • 打赏
  • 举报
回复
要断电了,发一段代码,希望有帮助

#include <stdio.h>
#include <malloc.h>

typedef struct LNode{
	int  data;
	struct  LNode  *next;
}LNode,*LinkList;

void CreateLink(LinkList &l,int  n)
{
	printf("输入值\n");
	LNode *p;
	int  i;
	int  j;
	l=(LinkList)malloc(sizeof(LNode));
	l->next=NULL;
	for(i=n;i>=1;i--)
	{
		p=(LinkList)malloc(sizeof(LNode));
		scanf("%d",&j);
		p->data=e;
		p->next=l->next;
		l->next=p;
	}
}

void del(LNode *head,int num)     //建立一个删除节点函数
{
    LNode  *p1,*p2;
    if(head==NULL)
    {
        printf("nlist null!\n");
        return head;
    }
    p1=head;
    while(num!=p1->num&&p1->next!=NULL)       //p1-next!=NULL是用来控制循环次数
    {
        p2=p1;                      
        p1=p1->next;             //p1再指向新的节点
    }
	if(num==p1->num) 
	{
		if(p1==head)
            head=p1->next;
        else
        {
            p2->next=p1->next;        //当p1->next==NULL时,p2->next接到NULL     
            printf("delete:%d",num);
            n--;
        }
		break;
	}
	else
		printf("%ld has not been found!\n",num);
//	return head;
}

void main()
{
	LinkList  l;
	int  length=0;
	printf("输入链表长度\n");
	scanf("%d",&length);
	CreateLink(l,length);
	del(l,6);
	printf("%d %d %d",l->next->data,l->next->next->data,l->next->next->next->data);

}
li6177 2012-11-10
  • 打赏
  • 举报
回复
都无人回答,看来要无满意结贴了。
whizer 2012-10-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
引用 1 楼 的回复:

多半是指针错误,自己动手调试一下,应该不难吧.

早调试过了,不懂汇编,只能找出哪里出错,至于为什么出错,暂时无解中
[/Quote]
什么地方出错了?贴上来.
li6177 2012-10-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 2 楼 的回复:
引用 1 楼 的回复:

多半是指针错误,自己动手调试一下,应该不难吧.

早调试过了,不懂汇编,只能找出哪里出错,至于为什么出错,暂时无解中

什么地方出错了?贴上来.
[/Quote]
DelOne函数和Insert函数有错,顶楼已写得很清楚。
li6177 2012-10-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

多半是指针错误,自己动手调试一下,应该不难吧.
[/Quote]
早调试过了,不懂汇编,只能找出哪里出错,至于为什么出错,暂时无解中
JiMoKuangXiangQu 2012-10-30
  • 打赏
  • 举报
回复
多半是指针错误,自己动手调试一下,应该不难吧.

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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