C++缺少类模板list的参数列表??
#include<iostream>usingnamespacestd;classnode{public:intdata;node*prior;node*next;};e...
#include<iostream>
using namespace std;
class node{
public:
int data;
node*prior;
node*next;
};
enum error_code{
success
};
template<typename elementype>
class list{
private:
int count;
node*head;
public:
list();
~list();
int length()const;//求长度
bool get_element(const int i, int &x)const;//按序号取元素
node *locate(const int x)const;//搜索
bool insert(const int i, const int x);//插入;
bool delete_element(const int i);
node *get_head(){ return head; }
void inverse();
list::list(){
head = new node;
head->prior=head->next = head;
count = 0;
}
int list::length()const{
return count;
}
bool list::get_element(const int i, int &x)const{
node *p = head->next;
int j = 1;
while(p !=head&&j != i){
p = p->next; j++;
}
if (p == head){ return false };
x = p->data;
return true;
};
node * list::locate(const int x)const{
node *p = head->next;
while (p != head){
if (p->data == x)return p;
else p = p->next;
}
return head;
}
bool list::insert(const int i, const int x){
if (count = 0){ node*q = new node;
q->prior = head; head->next = q; q->next = head; head->prior = q;
q->data = x; count = 1; return true;
}
node *p = head; int j = 0;
while (j != i&& p != head){
p = p->next; j++
}
if (i<1 || i>count + 1)return false;
node *s = new node;
s->prior - p->prior;
s->next = p;
p->prior = s;
s->prior->next = s;
s->data = x;
count++;
return true;
}
bool list::delete_element(const int i){
node *p = head->next;int j = 0;
while (j != i - i && p!=head){
p = p->next; j++;
}
if (i<1 || i>count)
return false;
p->prior->next = p->next;
p->next->prior = p->prior;
delete p;
count--;
return true;
}
list::list(){
while (count != 0){
delete_element(1);
}
}
void list::inverse(){
node *s = new node;
node *p = head;
for (int i = 0; i <= count;i++){
s->prior = p->prior;
s->next = p - next;
p->prior = s->next;
p->next = s->prior;
p = p->prior;
}
};
};
int main(){
list data1;
int x;
node *q;
for (int i = 1; i <= 10; i++) {
data1.insert(i, rand() % 100 + 1);
data1.get_element(i, x);
cout << x << " " << data1.locate(x) << " ";
}
cout << endl;
data1.inverse();
for (int j = 1; j <= 10; j++){
data1.get_element(j, x);
cout << x << " " << data1.locate(x) << " ";
}
cout << endl;
return 0;
}
问题出在主函数第一行,求解大佬怎么改?? 展开
using namespace std;
class node{
public:
int data;
node*prior;
node*next;
};
enum error_code{
success
};
template<typename elementype>
class list{
private:
int count;
node*head;
public:
list();
~list();
int length()const;//求长度
bool get_element(const int i, int &x)const;//按序号取元素
node *locate(const int x)const;//搜索
bool insert(const int i, const int x);//插入;
bool delete_element(const int i);
node *get_head(){ return head; }
void inverse();
list::list(){
head = new node;
head->prior=head->next = head;
count = 0;
}
int list::length()const{
return count;
}
bool list::get_element(const int i, int &x)const{
node *p = head->next;
int j = 1;
while(p !=head&&j != i){
p = p->next; j++;
}
if (p == head){ return false };
x = p->data;
return true;
};
node * list::locate(const int x)const{
node *p = head->next;
while (p != head){
if (p->data == x)return p;
else p = p->next;
}
return head;
}
bool list::insert(const int i, const int x){
if (count = 0){ node*q = new node;
q->prior = head; head->next = q; q->next = head; head->prior = q;
q->data = x; count = 1; return true;
}
node *p = head; int j = 0;
while (j != i&& p != head){
p = p->next; j++
}
if (i<1 || i>count + 1)return false;
node *s = new node;
s->prior - p->prior;
s->next = p;
p->prior = s;
s->prior->next = s;
s->data = x;
count++;
return true;
}
bool list::delete_element(const int i){
node *p = head->next;int j = 0;
while (j != i - i && p!=head){
p = p->next; j++;
}
if (i<1 || i>count)
return false;
p->prior->next = p->next;
p->next->prior = p->prior;
delete p;
count--;
return true;
}
list::list(){
while (count != 0){
delete_element(1);
}
}
void list::inverse(){
node *s = new node;
node *p = head;
for (int i = 0; i <= count;i++){
s->prior = p->prior;
s->next = p - next;
p->prior = s->next;
p->next = s->prior;
p = p->prior;
}
};
};
int main(){
list data1;
int x;
node *q;
for (int i = 1; i <= 10; i++) {
data1.insert(i, rand() % 100 + 1);
data1.get_element(i, x);
cout << x << " " << data1.locate(x) << " ";
}
cout << endl;
data1.inverse();
for (int j = 1; j <= 10; j++){
data1.get_element(j, x);
cout << x << " " << data1.locate(x) << " ";
}
cout << endl;
return 0;
}
问题出在主函数第一行,求解大佬怎么改?? 展开
1个回答
展开全部
#include<iostream>
using namespace std;
class node{
public:
int data;
node*prior;
node*next;
};
enum error_code{
success
};
template<typename elementype>
class list{
private:
int count;
node*head;
public:
list();
~list();
int length()const;//求长度
bool get_element(const int i, int &x)const;//按序号取元素
node *locate(const int x)const;//搜索
bool insert(const int i, const int x);//插入;
bool delete_element(const int i);
node *get_head(){ return head; }
void inverse();
list::list() // list();
{
head = new node;
head->prior=head->next = head;
count = 0;
}
int list::length()const // int length()const;//求长度
{
return count;
}
bool list::get_element(const int i, int &x)const // bool get_element(const int i, int &x)const;//按序号取元素
{
node *p = head->next;
int j = 1;
while(p !=head&&j != i)
{
p = p->next; j++;
}
if (p == head){ return false };
x = p->data;
return true;
}
node * list::locate(const int x)const // node *locate(const int x)const;//搜索
{
node *p = head->next;
while (p != head){
if (p->data == x)
return p;
else
p = p->next;
}
return head;
}
bool list::insert(const int i, const int x) // bool insert(const int i, const int x);//插入;
{
if (count == 0)
{ node*q = new node;
q->prior = head;
head->next = q;
q->next = head;
head->prior = q;
q->data = x;
count = 1; return true;
}
bool insert(const int i, const int x);//插入;
{
{
node *p = head; int j = 0;
while (j != i&& p != head)
{
p = p->next; j++
}
if (i<1 || i>count + 1)
return false;
node *s = new node;
s->prior - p->prior;
s->next = p;
p->prior = s;
s->prior->next = s;
s->data = x;
count++;
return true;
}
}
bool list::delete_element(const int i) // bool delete_element(const int i);
{
node *p = head->next;
int j = 0;
while (j != i - i && p!=head)
{
p = p->next; j++;
}
if (i<1 || i>count)
return false;
p->prior->next = p->next;
p->next->prior = p->prior;
delete p;
count--;
return true;
}
list::list() // list();
{
while (count != 0){
delete_element(1);
}
}
void list::inverse() // void inverse();
{
node *s = new node;
node *p = head;
for (int i = 0; i <= count;i++)
{
s->prior = p->prior;
s->next = p - next;
p->prior = s->next;
p->next = s->prior;
p = p->prior;
}
}
int main()
{
list data1;
int x;
node *q;
for (int i = 1; i <= 10; i++)
{
data1.insert(i, rand() % 100 + 1);
data1.get_element(i, x);
cout << x << " " << data1.locate(x) << " ";
}
cout << endl;
data1.inverse();
for (int j = 1; j <= 10; j++){
data1.get_element(j, x);
cout << x << " " << data1.locate(x) << " ";
}
cout << endl;
return 0;
}
问题一大推,我修改了一下。
自己写的嘛, 之家 LIST容器啊
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询