C++数据结构链表的实现代码

C++数据结构链表的实现代码,第1张

概述C++链表之前一直没怎么在意C++中的链表,但是突然一下子让自己写,就老是出错。没办法,决定好好恶补一下该方面的知识,也为今后的数据结构大下个良好的基础,于是我总结出以下几点,有些地方可能不正确,还望大家不

C++ 链表

之前一直没怎么在意C++中的链表,但是突然一下子让自己写,就老是出错。没办法,决定好好恶补一下该方面的知识,也为今后的数据结构大下个良好的基础,于是我总结出以下几点,有些地方可能不正确,还望大家不吝赐教,旨在共同进步。

总结:

1、链表List的基本单元是节点Node,因此想要 *** 作方便,就必须为每一步打好基础,Node的基本结构如下:

class Node{public:  int data;  Node *next;  Node(int da=0,Node *p=NulL){    this->data=da;    this->next=p;  }};

我们可以看出,Node的成员变量一共有两个,都是public,因为我们要对这两个变量进行 *** 作,所以不能是private类型的。然后是一个构造函数,第二个参数默认值为NulL,也就是说如果我们创建新节点时只指定第一个参数,而不写第二个参数,那么它默认的就是NulL,以这种方式可以更灵活的使用Node,个人建议这么使用哦。

2、第二步就是创建我们的链表了,同样我们这里先给出链表的代码,在进行一一的解释。

class List{private:  Node *head,*tail;  int position;public:  List(){head=tail=NulL;};  ~List(){delete head;delete tail;};  voID print();  voID Insert(int da=0);  voID Delete(int da=0);  voID Search(int da=0);};

我们这里面有两个数据类型,一个是Node。另一个是指代节点位置的成员变量(起不到什么作用,且不去管它吧)。使用head和tail来命名便是为了见名知意,使 *** 作更加准确。然后是重要的六个函数,各自的功能不言而喻咯,其实最重要的是在每一个函数中我们都默认能 *** 作head和tail两个成员变量,这样能简化我们的参数列表,使得函数更加优雅。

下面是我的一个单链表的实现,包含创建链表,插入值,删除特定的值,查找特定值得在链表中的位置。

#include<iostream>using namespace std;class Node{public:  int data;  Node *next;  Node(int da=0,Node *p=NulL){    this->data=da;    this->next=p;  }};class List{private:  Node *head,*tail;  int position;public:  List(){head=tail=NulL;};  ~List(){delete head;delete tail;};  voID print();  voID Insert(int da=0);  voID Delete(int da=0);  voID Search(int da=0);  int getValueAt(int position);  voID setValueAt(int position,int da);};int List::getValueAt(int position){  Node *p=head;  if(p==NulL){    cout<<"The List is Empty!"<<endl;  }else{    int posi=0;    while(p!=NulL&&posi!=position){      posi++;      p=p->next;    }    if(p==NulL){      cout<<"There is no value of this position in this List!"<<endl;    }else{      cout<<"In this position,the value is"<<p->data<<endl;    }  }  return p->data;}voID List::setValueAt(int position,int da){  Node *p=head;  if(p==NulL){    cout<<"The List is Empty!"<<endl;  }else{    int posi=0;    while(p!=NulL&&posi!=position){      posi++;      p=p->next;    }    if(p==NulL){      cout<<"There is No position in this List!"<<endl;    }else{      p->data=da;      cout<<"The Value in this position has been Updated!"<<endl;    }  }}voID List::Search(int da){Node *p=head;  if(p==NulL){    cout<<"Sorry,The List is Empty!"<<endl;    return;  }  int count=0;  while(p!=NulL&&p->data!=da){    p=p->next;    count++;  }  cout<<"the value you want to search is at position %d"<<count<<endl;}voID List::Delete(int da){  Node *p=head,*q=head;  if(p==NulL){    cout<<"Sorry,The List is Empty!"<<endl;    return;  }  while(p!=NulL&&p->data!=da){    q=p;    p=p->next;  }  q->next=p->next;  cout<<"The Deletion Operation had been finished!"<<endl;}voID List::Insert(int da){  if(head==NulL){    head=tail=new Node(da);    head->next=NulL;    tail->next=NulL;  }else{    Node *p=new Node(da);    tail->next=p;    tail=p;    tail->next=NulL;  }}voID List::print(){  Node *p=head;  while(p!=NulL){    cout<<p->data<<" \a";    p=p->next;  }  cout<<endl;}int main(){  cout<<"Hello World!"<<endl;  List l1;  l1.Insert(1);  l1.Insert(2);  l1.Insert(3);  l1.Insert(4);  l1.Insert(5);  l1.Insert(6);  l1.Insert(7);  l1.print();  l1.Search(4);  l1.Delete(6);  l1.print();  l1.getValueAt(3);  l1.setValueAt(3,9);  l1.print();  cout<<"The End!"<<endl;  return 0;}//在此我想解释的是,之所以数字4在链表中的位置为3,是因为其是从零开始计数的

下面是代码运行后的结果:


好了,单链表的基本 *** 作大致就是这样了,希望我们都能从中有所收获。如果您发现代码中有什么错误,还望不吝赐教,让我们共同进步吧。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的C++ 数据结构链表的实现代码全部内容,希望文章能够帮你解决C++ 数据结构链表的实现代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1245598.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存