1、使用类模板封装实现
主要代码:
#ifndef __LIST_H__
#define __LIST_H__
#include <iostream>
using namespace std;
#pragma once
template <class T,int n>
class mylist
{
private:
T* phead;
int size;
int len;
public:
mylist()
{
this->phead = new T[n];
if (this->phead)
{
this->size = n;
this->len = 0;
}
else
{
cerr << "list error";
}
}
~mylist()
{
delete[] this->phead;
this->size = 0;
this->len;
}
int insert_elem(int index, T& newdata)
{
if (index<0 || index >= this->size || index>this->len)
{
cerr << "out of range" << endl;
return -1;
}
int i;
for (i = this->len - 1; i >=index; i--)
{
this->phead[i + 1] = this->phead[i];
}
this->phead[i + 1] = newdata;
this->len += 1;
return 0;
}
int insert_end(T& newdata)
{
if (this->len == this->size)
{
cerr << "this list fully!" << endl;
return -1;
}
cout << newdata << "\t" << this->len << endl;
this->phead[this->len] = newdata;
this->len += 1;
return 0;
}
int insert_head(T& newdata)
{
if (this->len == this->size)
{
cerr << "this list is full!" << endl;
return -1;
}
for (int i = this->len - 1; i >= 0; i--)
{
this->phead[i + 1] = this->phead[i];
}
this->phead[0] = newdata;
this->len += 1;
return 0;
}
int delete_elem(char c, int index = 0)
{
if (this->len == 0)
{
cerr << "this list is empty";
return -1;
}
else
{
for (int i = 0; i < this->len; i++)
{
this->phead[i] = this->phead[i + 1];
}
this->len -= 1;
return 0;
}
}
int delete_elem(T& newdata)
{
if (this->len == 0)
{
cerr << "this list is enpty";
return -1;
}
else
{
for (int i = 0; i < this->len; i++)
{
if (this->phead[i] == newdata)
{
for (int j = i; i < this->len; j++)
{
this->phead[j] = this->phead[j + 1];
}
this->len -= 1;
}
return 0;
}
}
}
auto operator[](int index)->decltype(this->phead[index])
{
return *(this->phead + index);
}
int getlen()
{
return this->len;
}
};
#endif
(list.h)代码:
#ifndef __LIST_H__
#define __LIST_H__
#include <iostream>
using namespace std;
#pragma once
template <class T,int n>
class mylist
{
private:
T* phead;
int size;
int len;
public:
mylist()
{
this->phead = new T[n];
if (this->phead)
{
this->size = n;
this->len = 0;
}
else
{
cerr << "list error";
}
}
~mylist()
{
delete[] this->phead;
this->size = 0;
this->len;
}
int insert_elem(int index, T& newdata)
{
if (index<0 || index >= this->size || index>this->len)
{
cerr << "out of range" << endl;
return -1;
}
int i;
for (i = this->len - 1; i >=index; i--)
{
this->phead[i + 1] = this->phead[i];
}
this->phead[i + 1] = newdata;
this->len += 1;
return 0;
}
int insert_end(T& newdata)
{
if (this->len == this->size)
{
cerr << "this list fully!" << endl;
return -1;
}
cout << newdata << "\t" << this->len << endl;
this->phead[this->len] = newdata;
this->len += 1;
return 0;
}
int insert_head(T& newdata)
{
if (this->len == this->size)
{
cerr << "this list is full!" << endl;
return -1;
}
for (int i = this->len - 1; i >= 0; i--)
{
this->phead[i + 1] = this->phead[i];
}
this->phead[0] = newdata;
this->len += 1;
return 0;
}
int delete_elem(char c, int index = 0)
{
if (this->len == 0)
{
cerr << "this list is empty";
return -1;
}
else
{
for (int i = 0; i < this->len; i++)
{
this->phead[i] = this->phead[i + 1];
}
this->len -= 1;
return 0;
}
}
int delete_elem(T& newdata)
{
if (this->len == 0)
{
cerr << "this list is enpty";
return -1;
}
else
{
for (int i = 0; i < this->len; i++)
{
if (this->phead[i] == newdata)
{
for (int j = i; i < this->len; j++)
{
this->phead[j] = this->phead[j + 1];
}
this->len -= 1;
}
return 0;
}
}
}
auto operator[](int index)->decltype(this->phead[index])
{
return *(this->phead + index);
}
int getlen()
{
return this->len;
}
};
#endif