链表处理函数运行出错
有LinkFunSeries.cpp和LinkMain.cpp两个源文件,LinkFunSeries.cpp里是各个链表处理函数的定义,LinkMain.cpp是对链表处理函数简单调用以验证其正确性。
代码如下:
LinkFunSeries.cpp
LinkMain.cpp
这两个源文件放在同一文件夹中,在vc6里打开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单文件编译运行,结果插入函数不能插入节点,删除节点若为删除头节点,则内存操作错误报警,求解救。
...全文
相关推荐
2023年C++常见笔试题及答案.docx
静态存储区域分配的内存是在编译时分配,并在整个程序运行期间都存在;栈上分配的内存在函数调用时分配,在函数返回时自动释放,其分派效率高但有限制;堆上分配的内存则需程序员手动申请和释放,较为灵活但容易出错。
coreutils-8.13源码
`make check`可以运行测试用例,确保代码的正确性。
职工信息管理系统(C 链表)
**错误处理** 在实际应用中,需要考虑各种可能的异常情况,如文件读写错误、内存分配失败、链表操作出错等,都需要进行适当的错误处理,以保证系统的稳定性和可靠性。
C++——数组模拟链表
数组模拟链表是一种非常有用的技术,可以用于实现链表的插入和遍历操作。通过使用数组模拟链表,我们可以避免使用指针,减少代码的复杂度和出错的可能性。
循环链表和双向链表
双向链表的删除操作则需要断开被删除节点与相邻节点之间的连接,并更新相邻节点的指针。这些操作是链表结构数据处理中的基础,但也是容易出错的地方,需要在编程实践中不断练习和掌握。


新手乐园
