关于程序停止工作的问题
为什么我的程序编译都没问题,在执行是却显示停止工作,以前编程也遇到过这样的问题,不知道是软件问题还是程序问题,我是新手,求高手解决!半成品程序如下:
/*
程序设计:
双向链表
a)构造空表,直接从键盘输入数据元素
b)插入、删除、一般查找(给定某一学号x,找到并显示该学生信息)、显示等函数功能
c)设计特殊查找函数findbeforeAndAfter(...),给定某一学号x,能同时找到该学生第i个前驱的学生信息,
以及该学生第j个后继的学生信息,参数x, i,j从键盘输入.
*/
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef bool Status;
typedef int HeadEType;
typedef struct Student
{
int num; //学号
char name[10]; //姓名
} Student;
typedef struct DoubleNode
{
Student data;
DoubleNode *plink;
DoubleNode *nlink;
} DoubleNode; //链表结点
typedef struct
{
HeadEType Hdata;
DoubleNode *first;
} DoubleChainList;//链表的头
typedef struct
{
DoubleNode *plink;
DoubleNode *nlink;
} ResultNode;//返回直接前驱和直接后驱的数据元素的地址结果
typedef DoubleChainList ChainList;
typedef DoubleNode ChainNode;
ChainList *L;
ChainList *Creat(ChainList *L)
{// 构造一个空链表
L=new ChainList;//产生一个仅有表头结点的链表
L->first=NULL;//first的值设为空(NULL)值
L->Hdata=0;
return L;
}
Status Delete(DoubleChainList *L, int k )
{// 在双向链表L中删除第k个数据元素,如果不存在第k个元素返回出错状态码
if (k < 1 || ! L->first) return ERROR;
DoubleNode * current = L->first;
DoubleNode * p;
if (k == 1) // 删除的是链表中第一个结点
{
p = current ->nlink;
p ->plink = NULL;
L->first = p;
}
else
{
DoubleNode *q = L->first;
for (int index = 1; index < k && q ; index++)
q = q->nlink;
if(!q)
return ERROR;
current = q; // current 指向第k个结点
q = current ->plink;
p = current ->nlink;
q->nlink = p;
p->plink = q;
}
delete current; // 释放被删除结点current的空间
return OK;
}
Status Insert(DoubleChainList *L, int k, Student &x )
{
if (k < 0)
return ERROR;
int index = 1,i=0;
DoubleNode *current= L->first;
while (index < k && current)
{//找第k个结点
index++;
current = current ->nlink;
}
if (k > 0 && ! current)
return ERROR;
DoubleNode *q= new DoubleNode;
while(i<10)
{q->data.name[i] = x.name[i];
q->data.num=x.num;
}
if (k)
{// 插入在current 之后, 两个方向的链域的修改
q->nlink = current ->nlink;
q->plink = current;
DoubleNode *p = current ->nlink ;
current ->nlink = q;
if(p)
p->plink = q;
}
else
{// 作为第一个元素结点插入
q->nlink = L->first;
q->plink = NULL;
DoubleNode *p = L->first;
if(p)
p->plink = q;
L->first = q;
}
return OK;
}
void Output(DoubleChainList *L)
{// 逐个地输出链表L中的数据元素
DoubleNode *current=L->first;
while (current)
{
cout<<current->data.name << " ";
cout<<current->data.num << " ";
current=current->nlink ;
}
cout<<endl;
}
bool Search(DoubleChainList *L,int x)
{
DoubleNode *current = L->first;
while (current &¤t->data.num!=x)
current=current->nlink;
if(!current) return 0;
cout<<current->data.name << " ";
cout<<current->data.num << " ";
return 1;
}
//bool findbeforeAndAfter(DoubleChainList *L,int i,int j)
int main()
{
int i,j,m;
Student S;
L=Creat(L);
ChainNode *current=L->first;
printf("please enter student information :\n");
for(i=0;i<5;i++)
{scanf("%d%s",current->data.num,current->data.name);
printf("\n");
current=current->nlink;
}
Output(L);
printf("要删除第几项\n");
scanf("%d",&j);
Delete(L,j);
Output(L);
printf("要插入第几个\n");
printf("要插入学生学号、姓名\n");
scanf("%d",&m);
scanf("%d%s",current->data.num,current->data.name);
Insert(L,m,S);
Output(L);
return 0;
}