#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=https://bbs.csdn.net/topics/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=https://bbs.csdn.net/topics/=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=https://bbs.csdn.net/topics/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]
uj5u.com熱心網友回復:
看起來總共有4個錯誤,還是對每個錯誤定位出來具體找找效率高些轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/90234.html
標籤:基礎類
上一篇:c++builder xe8如何在Form的Panel1中可以編輯powerpoint檔案
下一篇:C++ 怎么遍歷json 物件
