首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

链表处理函数运作出错

2012-12-31 
链表处理函数运行出错本帖最后由 li6177 于 2012-10-30 22:55:11 编辑有LinkFunSeries.cpp和LinkMain.cpp

链表处理函数运行出错
本帖最后由 li6177 于 2012-10-30 22:55:11 编辑 有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 <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);

}


[解决办法]

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;
}

热点排行