求问cpp

crazy_cpp 2015-11-03 07:06:18
#include<iostream>
using namespace std;
template<class T>
struct Node
{
T data;//数据域
struct Node<T>*next;//指针域
};
template<class T>
class LinkList
{
public :
LinkList()
{front=new Node<T>;
front->next=NULL;
}//无参构造
LinkList(T a[],int n);//有参构造
~LinkList();//析构
void PrintList(T a[],int n); //遍历
int GetLength(); //获取长度
Node<T>*Get(int i);//获取地址
int Locate(T x);//查找
void Insert(int i,T x);//在第I个位置插入值为x的新元素
T Delete(int i);//删除第i个元素,
private:
Node<T>*front;//头指针
};
//头插法建立单链表
template<class T>
LinkList<T>::LinkList(T a[],int n)
{
front=new Node<T>;
front->next=NULL;
for(int i=n-1;i>=0;i--)
{
Node <T>*s=new Node<T>;//建立新节点
s->data=a[i];
s->next=front->next;//修改新节点的指针域
front->next=s;//修改头结点的指针域,并将新节点加入链表
}
}
template<class T>
LinkList<T>::~LinkList() //析构函数
{
Node<T>*p=front;//初始化工作指针P
while(p)//要释放的指针存在
{
front=p; //暂存要释放的结点
p=p->next;//移动工作指针
delete front; //释放结点
}
}
//遍历
template<class T>
void LinkList<T>::PrintList(T a[],int n)//
{
cout<<"按次序遍历线性表中的元素"<<endl;
for(int i=0;i<GetLength();i++)
{
cout<<a[i]<<" ";}
cout<<endl;
Node<T>*LinkList<T>::Get (int i)
{
Node<T>*p=front->next;//初始化工作指针
int j=1;//初始化计数器
while(p&&j!=i)//两个条件都满足,则继续循环
{
p=p->next;//工作指针后移
j++;
}
return p;//查找到第i个元素返回地址,或未找到,返回0
}
template<class T>
Node<T>*LinkList<T>::Locate<T x>
{
Node<T>*p=front->next;//初始化
int j=1;
while(p)
{
if(p->data==x)return j;
p=p->next;
j++;
}
return -1;
}
//运算符重载
class ComplexNumber//自定义复数类
{
public:
ComplexNumber(){a=0;b=0;}//无参
ComplexNumber(double a1,double b1){a=0;b=0;}//有参
bool operator==(ComplexNumber&cn)//==操作符重载
{return (a==cn.a&&b==cn.b);}
private:
double a;//实部
double b;
};
//复数类的实现
/*int main()
{LinkList<ComplexNumber>cnlist;
//这里可添加插入元素到链表中的代码
ComplexNumber cn(1,2);
int pos=cnlist.Locate(cn);
return 0;
}*/
//插入
template<class T>
void LinkList<T>::Insert(int i,T x)
{
Node<T>*p=front;//初始化
if(i!=1)p=Get(i-1);
if(p){
Node<T>*s=new Node<T>;//建立
s->data=x;
s->next=p->next;
p->next=s;
}
else throw"插入位置错误";
}
template<class T>
T LinkList<T>::Delete(int i)
{
Node<T>*p=front;//初始化工作指针
if(i!=1)p=Get(i-1);
Node<T>*q=p->next;
T x=q->data;
delete q;
return x;
}



#include"单链表.h"
void main()
{
try
{
int a[7]={1,2,3,4,5,6,7};
LinkList<int>List(a,7);
List.PrintList();
int m=List.GetLength();
cout<<"链表长度:"<<m<<endl;
Node<int >*Get(5);
List.PrintList(a,7);
int x=List.Delete(1);
cout<<"删除元素:"<<x<<endl;
List.PrintList();
int p=List.Locate(4);
cout<<"元素4的位置:"<<p<<endl;
}

catch(char* s)
{cout<<s;
return 1;
}
return 0;
}

主要错误有:[color=#FF0000]

62 26 C:\Users\crazy cao\Desktop\单链表.h [Error] qualified-id in declaration before '(' token
62 27 C:\Users\crazy cao\Desktop\单链表.h [Error] expected primary-expression before 'int'
24 1 C:\Users\crazy cao\Desktop\未命名2.cpp [Error] expected '}' at end of input
28 C:\Users\crazy cao\Desktop\Makefile.win recipe for target '未命名2.o' failed[/color]

...全文
253 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
twp16899168 2015-11-04
  • 打赏
  • 举报
回复
看起来总共有4个错误,还是对每个错误定位出来具体找找效率高些

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧