每天积一小步,虽然之前学过数据结构,此次试着把其实现代码都写出来,这是第一天。
List类如下图所示:
代码如下:
#include <iostream>
#include <assert.h>
using namespace std;
struct Node{
int info;
Node *link;
};
class List{
public:
List();
List(const List &other);
bool create();
void print() const;
void destory();
Node* getHead()const;
~List();
//--------------------------------------------------
int length()const;
Node* find(const int x)const;
bool insert(const int x, const int local)const;
bool delValue(const int x)const;
//--------------------------------------------------
private:
Node *head;
};
List::List(){
head = NULL;
}
List::List(const List &other){//要理解复制构造函数的运行时间,复制构造函数与复制功能的函数的由来目的是不一样的。
head = NULL;//构造函数只是运行一个就ok的
Node *temp = other.getHead();
Node *last = NULL;
while(NULL != temp){
Node *temp2 = new Node;
temp2->info = temp->info;
temp2->link = NULL;
if(NULL == head){
head = last =temp2;
}else{
last->link = temp2;
last = temp2;
}
temp = temp->link;
}
}
bool List::create(){
Node *last = NULL;
Node *newNode = NULL;
int input=0;
cout << "input your number (enter -1 to over your input):" << endl;
cin >> input;
while(-1 != input){
newNode = new Node;
assert(newNode);
newNode->info = input;
newNode->link = NULL;
if(NULL == head){
head = newNode;
last = newNode;
}else{
last->link = newNode;
last = newNode;
}
cout << "input your number (enter -1 to over your input):" << endl;
cin >> input;
}
cout << "create function run over" << endl;
return true;
}
Node* List::getHead()const{
return head;
}
void List::destory(){
while(NULL != head){
Node *temp = head->link;
delete head;
head = temp;
}
}
List::~List(){
cout << "deconstruction has been run." << endl;
destory();
}
void List::print() const{
Node *temp = head;
cout << "here is list contain:" << endl;
while(NULL != temp){
cout << temp->info;
cout << endl;
temp = temp->link;
}
cout << "print list over!" << endl;
}
int List::length()const{
Node *temp = head;
int i = 0;
while(NULL != temp){
i++;
temp = temp->link;
}
return i;
}
Node* List::find(const int x)const{
Node *temp = head;
int i = 0;
while(NULL != temp){
++i;
if(x == temp->info){
cout << x << " has been found. it's localtion is " << i << endl;
return temp;
break;
}
temp = temp->link;
}
return NULL;
}
bool List::insert(const int x, const int local)const{
if(local > length()){
cout << "out of range." <<endl;
return false;
}
Node *temp = head;
int i = 0;
while(NULL != temp){
i++;
temp = temp->link;
if((local-1) == i)break;
}
Node *newNode = new Node;
newNode->info = x;
newNode->link = temp->link;
temp->link =newNode;
cout << x << " has been inserted into list." << endl;
return true;
}
bool List::delValue(const int x)const{
if(NULL == head)return false;
Node *pretemp = head;
Node *temp = head->link;
while(NULL != temp){
if(x == temp->info){
pretemp->link = temp->link;
delete temp;
return true;
}
pretemp = pretemp->link;
temp = temp->link;
}
cout << "there is not x in this list." << endl;
return false;
//不要限定自己使用变量的数量
}
int main()
{
List list;
list.create();
list.print();
list.find(3);
list.insert(10,2);
list.print();
list.delValue(10);
list.print();
return 0;
}
一个体会:我们可以定义变量为我们所用,所以不要困在自己没有资源可用的地步。定义变量其实就是建造资源的过程。