数据结构单链表问题 总是提示: error C2065: “T”: 未声明的标识符,error C2061: 语法错误: 标识符“T 20
//数据结构1.2.cpp:定义控制台应用程序的入口点。//#include"stdafx.h"#include"iostream"usingnamespacestd;t...
// 数据结构1.2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
using namespace std;
template <class T>
class Node //构造单链表节点类
{
public:
T data;
Node<T> *next;
Node()
{
this->next=NULL;
}
Node(T data,Node<T> *next=NULL)
{
this->data=data;
this->next=next;
}
};
class SinglyLinkedList
{
public:
Node<T> *head; //头指针
SinglyLinkedList(); //构造单链表
~SinglyLinkedList(); //析构
int length(); //返回长度
bool isEmpty(); //判断是否为空
Node<T> *insert(int i,T x); //插入x作为第i个节点,返回新插入节点指针
bool remove(int i,T &old); //删除第i个节点,把删除的元素放到old中
};
template <class T>
SinglyLinkedList<T>::SinglyLinkedList()
{
this->head=NULL;
}
template <class T>
SinglyLinkedList<T>::~SinglyLinkedList()
{
Node<T> *q=p;
while(p!=head)
{
Node<T> *q=p;
p=p->next;
delete q;
}
head=NULL;
}
template <class T>
int SinglyLinkedList<T>::length()
{
int i=0;
while (p!=NULL)
{
i++;
p=p->next;
}
return i;
}
template <class T>
bool SinglyLinkedList<T>::isEmpty()
{
return head==NULL;
}
template <class T>
Node<T> SinglyLinkedList<T>::insert(int i,T x)
{
Node<T> *p=NULL;
if (head==NULL||i<=0)
{
q=new Node<T>(x,head);
head=q;
}
else
{
int j=0;
Node<T> *p!=next;
while (p->next!=NULL&&j<i-1)
{
j++;
p=p->next;
}
q=new Node<T>(x,p->next);
p->next=q;
}
return q;
}
template <class T>
bool SinglyLinkedList<T>::remove(int i,T &old)
{
if (head!=NULL&&i>=0)
{
if (i==0)
{
Node<T> *q=head;
old=q->data;
head=head->next;
delete q;
return true;
}
}
else
{
int j=0; //取第i节点指针
Node<T> *n=head;
while (n!=NULL&&j<i-1)
{
j++;
n=n->next;
}
Node<T> *p=n;
if (p!=NULL&&p->next!=NULL)
{
Node<T> *q=p->next;
old=q->data;
p->next=q->next;
delete q;
return ture;
}
return false;
}
}
int main()
{
SinglyLinkedList::SinglyLinkedList();
SinglyLinkedList::insert(0,1);
SinglyLinkedList::insert(1,2);
SinglyLinkedList::insert(2,3);
SinglyLinkedList::insert(3,4);
SinglyLinkedList::insert(4,5);
SinglyLinkedList::insert(5,6);
int t=0;
t=SinglyLinkedList::length();
cout<<t;
system("pause");
return 0;
} 展开
//
#include "stdafx.h"
#include "iostream"
using namespace std;
template <class T>
class Node //构造单链表节点类
{
public:
T data;
Node<T> *next;
Node()
{
this->next=NULL;
}
Node(T data,Node<T> *next=NULL)
{
this->data=data;
this->next=next;
}
};
class SinglyLinkedList
{
public:
Node<T> *head; //头指针
SinglyLinkedList(); //构造单链表
~SinglyLinkedList(); //析构
int length(); //返回长度
bool isEmpty(); //判断是否为空
Node<T> *insert(int i,T x); //插入x作为第i个节点,返回新插入节点指针
bool remove(int i,T &old); //删除第i个节点,把删除的元素放到old中
};
template <class T>
SinglyLinkedList<T>::SinglyLinkedList()
{
this->head=NULL;
}
template <class T>
SinglyLinkedList<T>::~SinglyLinkedList()
{
Node<T> *q=p;
while(p!=head)
{
Node<T> *q=p;
p=p->next;
delete q;
}
head=NULL;
}
template <class T>
int SinglyLinkedList<T>::length()
{
int i=0;
while (p!=NULL)
{
i++;
p=p->next;
}
return i;
}
template <class T>
bool SinglyLinkedList<T>::isEmpty()
{
return head==NULL;
}
template <class T>
Node<T> SinglyLinkedList<T>::insert(int i,T x)
{
Node<T> *p=NULL;
if (head==NULL||i<=0)
{
q=new Node<T>(x,head);
head=q;
}
else
{
int j=0;
Node<T> *p!=next;
while (p->next!=NULL&&j<i-1)
{
j++;
p=p->next;
}
q=new Node<T>(x,p->next);
p->next=q;
}
return q;
}
template <class T>
bool SinglyLinkedList<T>::remove(int i,T &old)
{
if (head!=NULL&&i>=0)
{
if (i==0)
{
Node<T> *q=head;
old=q->data;
head=head->next;
delete q;
return true;
}
}
else
{
int j=0; //取第i节点指针
Node<T> *n=head;
while (n!=NULL&&j<i-1)
{
j++;
n=n->next;
}
Node<T> *p=n;
if (p!=NULL&&p->next!=NULL)
{
Node<T> *q=p->next;
old=q->data;
p->next=q->next;
delete q;
return ture;
}
return false;
}
}
int main()
{
SinglyLinkedList::SinglyLinkedList();
SinglyLinkedList::insert(0,1);
SinglyLinkedList::insert(1,2);
SinglyLinkedList::insert(2,3);
SinglyLinkedList::insert(3,4);
SinglyLinkedList::insert(4,5);
SinglyLinkedList::insert(5,6);
int t=0;
t=SinglyLinkedList::length();
cout<<t;
system("pause");
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询