链表实现代码

每天积一小步,虽然之前学过数据结构,此次试着把其实现代码都写出来,这是第一天。

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;
}


一个体会:我们可以定义变量为我们所用,所以不要困在自己没有资源可用的地步。定义变量其实就是建造资源的过程。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值