我在同一个目录中有两个文件 ll.cpp 和 ll.h。
ll.cpp
#include <ll.h>
#include <iostream>
using namespace std;
template <class t>
LinkedL<t>::LinkedL()
{
flag=0;
head=NULL;
tail=NULL;
curr=0;
}
template <class t>
LinkedL<t>::void insertS(t inf)
{
Node<t> *n=new Node<t>;
n->next=head;
n->data=inf;
head=n;
if(curr==0)
tail=n;
curr++;
//cout<<curr<<"\n";
}
template <class t>
LinkedL<t>::void insertE(t inf)
{
Node<t> *n=new Node<t>;
n->next=NULL;
n->data=inf;
if(curr!=0)
tail->next=n;
tail=n;
if(curr==0)
head=n;
curr++;
//cout<<curr<<"\n";
}
template <class t>
LinkedL<t>::t delS()
{
if(curr>=1)
{
flag=1;
t temp=head->data;
Node<t> *n=head;
head=head->next;
if(head==NULL)
tail=NULL;
delete n;
curr--;
return temp;
}
else
{
cout<<"EMPTY LIST\n";
}
}
template <class t>
LinkedL<t>::t delE()
{
if(curr>=1)
{
flag=1;
Node<t> *n=head;
t temp;
if(curr>1)
{
while(n->next!=tail)
{
//cout<<n->data<<" ";
n=n->next;
}
//cout<<"\n";
Node<t> *n1=tail;
temp=n1->data;
tail=n;
tail->next=NULL;
//cout<<n->data<<"\n";
delete n1;
}
else if(curr==1)
{
temp=tail->data;
delete tail;
head=NULL;
tail=NULL;
}
curr--;
//cout<<curr<<"\n";
return temp;
}else if(curr==0)
{
cout<<"EMPTY LIST\n";
}
}
template <class t>
LinkedL<t>::bool search(t inf)
{
Node<t> *n=head;
while(n!=NULL && n->data!=inf)
n=n->next;
if(n==NULL)
return false;
return true;
}
template <class t>
LinkedL<t>::void traverse()
{
if(curr!=0)
{
Node<t> *n=head;
while(n!=NULL)
{
cout<<n->data<<" ";
n=n->next;
}
cout<<'\n';
}
else
{
cout<<"EMPTY\n";
}
}
template <class t>
LinkedL<t>::bool isEmpty()
{
if(curr==0)
return true;
return false;
}
template <class t>
LinkedL<t>::t at(int i)
{
if(i<=curr)
{
flag=1;
Node<t> *n=head;
int k=1;
while(k!=i)
{
n=n->next;
k++;
}
return n->data;
}
else
cout<<"Invalid Index\n";
}
template <class t>
LinkedL<t>::void insert(int i,t dat)
{
if(i<=curr+1)
{
flag=1;
if(curr==0 || i==1)
{
insertS(dat);
}
else
{
Node<t> *n1=NULL;
Node<t> *n=head;
int k=1;
while(k!=i)
{
n1=n;
n=n->next;
k++;
}
Node<t> *temp=new Node<t>;
temp->next=n;
n1->next=temp;
temp->data=dat;
curr++;
}
}
else
cout<<"Invalid Index\n";
}
template <class t>
LinkedL<t>::void reverse()
{
if(curr!=0)
{
Node<t> *n=head;
Node<t> *n1=head;
Node<t> *n2=NULL;
while(n!=NULL)
{
n1=n->next;
n->next=n2;
n2=n;
n=n1;
}
n=head;
head=tail;
tail=n;
}
}
template <class t>
LinkedL<t>::t delet(int i)
{
Node<t> *n=head;
if(head!=NULL && i<=curr)
{
flag=1;
t dat;
int k=1;
if(i==1)
dat=delS();
else
{
while(k!=i-1)
{
n=n->next;
k++;
}
Node<t> *n1=n->next;
n->next=n->next->next;
dat=n1->data;
delete n1;
}
curr--;
return dat;
}
else
{
cout<<"Invalid Index\n";
}
}
ll.h
#ifndef LL_H
#define LL_H
template <class t>
class Node
{
public:
Node<t> * next;
t data;
};
template <class t>
class LinkedL
{
private:
Node<t>* head;
Node<t>* tail;
int curr;
public:
int flag;
LinkedL();
void insertS(t inf);
void insertE(t inf);
t delS();
t delE();
bool search(t inf);
void traverse();
bool isEmpty();
t at(int i);
void insert(int i,t dat);
void reverse();
t delet(int i);
};
#endif
我无法弄清楚为什么编译器会给出这个错误。所有 .h 和 .cpp 对都发生同样的事情作为交叉检查我将所有用户的权限更改为 rwx 但仍然出现相同的错误。请帮忙。