首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

求解 !该如何处理

2012-11-16 
求解 !!//ds22.h文件#includeiostream.htypedef int ElemTypetypedef struct LNode{ElemType datastru

求解 !!
//ds22.h文件
#include<iostream.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next; 
} LNode;
   

/* 函数声明 */
LNode *Creat_L(); /* 建立

线性链表 */
void Out_L(LNode *L); /* 输出单链表中的数据元素*/
void Insert_L(LNode *L,int i ,ElemType e); /* 在单链表

中的i位置插入元素e */
ElemType Delete_L(LNode *L,int i); /* 删除单链表中的第i

个元素,返回其值 */
int Locat_L(LNode *L,ElemType e); /* 查找值为 e 的元素, 

返回它的位置 */

//ds22.cpp文件
#include<iostream.h>
#include<iostream.h>
typedef int ElemType;
typedef struct {
ElemType data;
struct LNode *next; 
} LNode;
//建立链表
LNode *Creat_L( ){ 
LNode *h,*p,*s;  
ElemType x;
//h=(LNode *)malloc(sizeof(LNode));  

  /* 分配头结点 */
h=new LNode;
h->next=NULL;
p=h;  
cout<<"\n data=?";
  /* 输入第一个数据元素 */
cin>>x;
while( x!=-111) /*  

输入-111,结束循环 */

//s=(LNode *)malloc(sizeof(LNode));  

  /* 分配新结点 */
s=new LNode;
s->data=x;  
s->next=NULL;
p->next=s;  
p=s; 
cout<<"data=?( -111 end) ";
cin>>x;
  }
return(h);
 } /* creat_L */

//插入操作
void Insert_L(LNode *L,int i, ElemType e){ 
LNode *s,*p,*q;  
int j;
p=L; /* 找第i-1个结点 */
j=0;
while(p!=NULL && j<i-1) 

p=p->next; 
j++; 
}
if(p==NULL || j>i-1) 
cout<<"\n i ERROR !";
else 

//s=(LNode *)malloc(sizeof(LNode));
s=new LNode;
s->data=e;
s->next=p->next;
p->next=s;
}
 } /* insert_L */
//删除操作
/* 删除第i个元素,返回其值 */
ElemType Delete_L(LNode *L,int i){ 
LNode *p,*q; 
int j; 
ElemType x;
p=L; 
j=0;
while(p->next!=NULL && j<i-1)

p=p->next; 
j++;
}
if(p->next==NULL) 
{
cout<<"\n i ERROR !";
return(-1);
}
else 

q=p->next; 
x=q->data;
p->next=q->next; 
//free(q);
delete q;
return(x);
}
  } /* delete_L */
//按值查找
/* 查找值为 e 的元素, 返回它的位置 */
 int Locat_L(LNode *L,ElemType e){ 
LNode *p; 
int j=1;
p=L->next;
while(p!=NULL && p->data!=e) 
{
p=p->next; 
j++;
}
if(p!=NULL)
return(j); 
else 
return(-1);
 } /* locat_L */

/* 输出单链表中的数据元素*/
void Out_L(LNode *L){ 
LNode *p; 
char ch;
p=L->next;  
cout<<"\n";
while(p!=NULL) {  
cout<<p->data;
p=p->next; 
  }
cout<<"\n 打回车键,继续。";
//ch=getchar(); 
} /* out_link */
//删除链表
 void clearlist(LNode *&HL){
LNode *cp;
LNode *np;
cp=HL->next;
while(cp){
np=cp->next;
delete cp;
cp=np;
}
//delete L;
//HL=NULL;
HL->next=NULL;
 }

 //求链表的长度
 int lenthlist(LNode *HL){
int i=0;
LNode *p=HL->next;
while(p)
{
i++;
p=p->next;
}
return i;
 }
 //更新单链表中第一个等于item为it
 bool updatelist(LNode *HL, ElemType item,ElemType it){
LNode *p=HL->next;


while(p)
{
if(p->data==item)
break;
else
p=p->next;
}
if(!p)
return false; 
else
{
p->data=it;
return true;
}

 }


//ds22main.cpp文件
#include<iostream.h>
#include<stdlib.h>
#include"ds22.h"
LNode *L;
void main(){ 
int i,k,loc; 
ElemType e,x; 
char ch;
do {
cout<<"\n";
cout<<"\n 1. 建立线性链表 ";
cout<<"\n 2. 在i位置插入元素e";
cout<<"\n 3. 删除第i个元素,返回其值

";
cout<<"\n 4. 查找值为 e 的元素";
cout<<"\n 5. 删除链表";
cout<<"\n 6. 求链表的长度";
cout<<"\n 7. 更新单链表中第一个等于

item为it";
cout<<"\n 8. 结束程序运行";


cout<<"\n======================================";
cout<<"\n 请输入您的选择 (1,2,3,4,5

,6,7)";
cin>>k;
switch(k){
case 1:{
L=Creat_L( );  
Out_L(L);

break;
case 2:{ 
cout<<"\n i,e=?";
cin>>i>>e;
Insert_L(L,i,e);  
Out_L(L);

break;
case 3:{ 
cout<<"\n i=?";
cin>>i;
x=Delete_L(L,i);  
Out_L(L);
if(x!=-1)
cout<<"\n x="<<x<<"\n";

break;
case 4:{ 
cout<<"\n e=?"; 
cin>>e;
loc=Locat_L(L,e);
if (loc==-1) 
cout<<"\n 未找到 "<<loc;
else 
cout<<"\n 已找到,元素位

置是 "<<loc;

break;
/*case 5:clearlist(L);
break;
/*case 6:cout<<lenthlist(L)<<endl;
break;
case 7:int cit,nit;
cout<<"输入更新前和更新后的数据

:";
cin>>cit>>nit;
if(updatelist(L,cit,nit))
out_L(L);
else {
cout<<"更新不成

功"<<endl;
Out_L(L);
}
break; */
} /* switch */
cout<<"\n ----------------";
  }while(k>=1 && k<8);
cout<<"\n 再见!";
  cout<<"\n 打回车键,返回。";
//ch=getchar();
} /* main */
这个编译组建的时候怎么总是这个问题啊 怎么解决?问题如下:

C:\Users\Administrator\Documents\Tencent Files\1935838835\FileRecv\shiyan\ds22main.cpp(16) : error C2001: newline in constant
C:\Users\Administrator\Documents\Tencent Files\1935838835\FileRecv\shiyan\ds22main.cpp(17) : error C2146: syntax error : missing ';' before identifier 'cout'

十分感谢!

[解决办法]
ds22main.cpp(16) : error C2001: newline in constant
在字符串里出现了换行符,比如
"I am
a sudent"
把字符串写在一行里就可以了

热点排行