为了账号安全,请及时绑定邮箱和手机立即绑定

为什么会出现相同的单链表插入操作函数在不同的测试程序中进行调用时一个能成功运行,另一个程序停止操作??(Ps:贴了写的两个函数,,都终止了运行)

为什么会出现相同的单链表插入操作函数在不同的测试程序中进行调用时一个能成功运行,另一个程序停止操作??(Ps:贴了写的两个函数,,都终止了运行)

C++ C
天天天小羽 2015-10-15 20:17:41
//文件LinerList.h,包含线性链表的操作声明#include <stdio.h>#include <malloc.h>#include <stdlib.h>//预定义常量#define   OK        1#define   ERROR     0#define   TRUE      1#define   FALSE     0#define   OVERFLOW  -2//线性链表元素数据类型定义typedef   int       ElemType;//布尔类型定义typedef   int       BOOL;//函数返回类型定义typedef   int       Status;typedef struct LNode{  //线性链表的结点类型定义 ElemType data;     //数据域 LNode    *next;    //下一结点指针} *LinkList;//线性链表操作的函数原型声明Status ListInsert(LinkList &L,int i,ElemType e);               //向表中插入指定元素Status ListDelete(LinkList &L,int i,ElemType &e);            //从表中删除指定位置的元素Status ListTraverse(LinkList L);                //遍历表,输出每个表元素值/*函数操作*//*第一种*/Status ListInsert(LinkList &L,int i,ElemType e) //向表中插入指定元素{  int k=0;    LNode *p=L->next,*q=L,*s; if (i<1)             //i非法 return ERROR; while (q) { k++; if (k==i)     {      //新元素插入在q与p之间                s = (LNode *)malloc(sizeof(LNode));   //生成待插入结点        s->data=e;        s->next=p;                            //修改链接        q->next=s; return OK; }    q=p;                //指针后移 p=p->next; }    return ERROR;           //i非法,过大}/*第二种*/Status ListInsert(LinkList &L, int i,ElemType e)       // 在带头结点的单链线性表L中第i个位置之前插入数据元素e{ LinkList p=L,s;  //初始化p指向头节点 int j=0; while(p&&j<i-1)  //使p指向第i-1个节点 { j++; p=p->next; } if(j>i)  //该节点不存在  return ERROR; LinkList q=(LinkList)malloc(sizeof(LNode)); //创建新节点并将之连接到第i个节点之前 q->data=e; q->next=p->next;cout<<"判断"<<endl; p->next=q; return OK;};/*main     PS:停止运行的程序*/#include"SqList.h"int main(){ LinkList L; creat(L); int i; ElemType num; cout<<"      任务一!!"<<endl; srand(time(NULL)); for(i=0;i<10;i++) {   num=rand()%99;    cout<<"随机数"<<endl; ListInsert(L,i+1,num); }PrintList(L);return 0;}/*main  PS:成功运行的程序*///文件mn.cpp#include "LinerList.h"int main(){ LinkList L1,L2,L3; ElemType e; int i; InitList(L1);       //构造空的单链表L1 InitList(L2);       //构造空的单链表L2 printf("请输入表L1元素值,共5个\n"); for (i=0;i<5;i++) { scanf("%d",&e); ListInsert(L1,i+1,e);    //向表中插入用户输入的元素值 }    printf("请输入表L2元素值,共3个\n"); for (i=0;i<3;i++) { scanf("%d",&e); ListInsert(L2,i+1,e);    //向表中插入用户输入的元素值 } return 0;}
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 1615 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号