求助!单向链表 工程文件 编译能通过但不能执行 求解!!!
//head.cpp内容#include<iostream>
using namespace std;
#include"link_head.h"
bool CreateList(PNODE &L)
{
L->data=0;
L->pNext=NULL;
int e;
PNODE p = new NODE;
int s=MAX;
while(--s)
{
PNODE p = new NODE;
if(!p){cout<<"创建失败!"; return false;}
cout<<"请输入您需要创建的新节点的值:"<<endl;
cin>>e;
p->data=e;
p->pNext=L->pNext;
L->pNext=p;
}
return true;
}
bool GetElem(PNODE L,int i,Elemtype &e)
{
PNODE p;
p=L->pNext;int j=1;
while(p&&j<i)
{
p=p->pNext;j++;
}
if(!p||j>i) return false;
e=p->data;
cout<<"您查找的元素为:"<<e<<endl;
return true;
}
bool Insert(PNODE& L,int i,Elemtype e)
{
PNODE p;
p=L;int j=0;
while(p&&j<i-1)
{
p=p->pNext;
j++;
}
if(!p||j>i-1) return false;
PNODE s;
s=new NODE;
s->data=e;s->pNext=p->pNext;p->pNext=s;
return true;
}
bool Delete(PNODE& L,int i,Elemtype &e)
{
PNODE p;
p=L;int j=0;
while(p->pNext&&j<i-1)
{
p=p->pNext;j++;
}
if(!(p->pNext)||j>i-1) return false;
e=p->pNext->data;
p->pNext=p->pNext->pNext;
delete p->pNext;
cout<<"您删除的元素为:"<<e<<endl;
return true;
}
void ShowList(PNODE L)
{
PNODE p=L;
while(!p)
{
cout<<"链表的元素为:"<<p->data<<" ";
p=p->pNext;
}
}
//main.cpp内容
#include"link_head.h"
void main()
{
Elemtype val;
PNODE L;
CreateList(L);
Insert( L,1,1);
Insert( L,2,2);
Insert( L,3,3);
Insert( L,4,4);
Insert( L,5,5);
Insert( L,6,6);
Insert( L,7,7);
Insert( L,8,8);
Insert( L,9,9);
Insert( L,10,10);
ShowList(L);
Delete(L,3,val);
GetElem(L,10,val);
}
//head.h内容
#ifndef LINK_HEAD
#define LINK_HEAD
typedef int Elemtype;
#define MAX 10
typedef struct Node
{
Elemtype data;
struct Node * pNext;
}NODE,*PNODE;
bool CreateList(PNODE &L);
bool GetElem(PNODE L,int i,Elemtype &e);
bool Delete(PNODE& L,int i,Elemtype &e);
bool Insert(PNODE& L,int i,Elemtype e);
void ShowList(PNODE L);
#endif