链表处理函数运行出错

li6177 li6177 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
有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单文件编译运行,结果插入函数不能插入节点,删除节点若为删除头节点,则内存操作错误报警,求解救。
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
li6177
新手乐园 33,315 成员 41,769 内容