在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例Android手机应用开发 → 不带头节点的单链表创建

不带头节点的单链表创建

Android手机应用开发

下载此实例
  • 开发语言:C/C++
  • 实例大小:0.07M
  • 下载次数:6
  • 浏览次数:66
  • 发布时间:2020-09-28
  • 实例类别:Android手机应用开发
  • 发 布 人:ysy0205
  • 文件格式:.zip
  • 所需积分:3
 相关标签: 链表 创建

实例介绍

【实例简介】
【实例截图】

【核心代码】

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
typedef int T;

struct LinkNode //结点定义
{
          T data;//数据域
      LinkNode* next;//链域
      LinkNode(const T& item, LinkNode* ptr=NULL)
      {
            data=item;
            next=ptr;
      }
      LinkNode(LinkNode* ptr=NULL)
      {
            next=ptr;
      }
};
bool compare (const LinkNode* a,const LinkNode *b);
class List
{
private:
      LinkNode * first;
public:
      List(){
        first=NULL;
      }
      List(const List& L){
          first=NULL;
          LinkNode * p;
          for(p=L.first;p!=NULL;p=p->next){
            this->InputRear(p->data);
          }
      }
      List& operator=(const List& L){
          if(&L==this)return *this;
          MakeEmpty();
          LinkNode * p;
          for(p=L.first;p!=NULL;p=p->next){
            this->InputRear(p->data);
          }
          return *this;
      }
      ~List(){
          MakeEmpty();
      }
      void InputFront(const T& elem){
          LinkNode * newnode=new LinkNode (elem,first);
          first=newnode;
      }
      void InputRear(const T& elem){
          LinkNode * newnode=new LinkNode (elem,NULL);
          LinkNode *p=first;
          if(p==NULL){
            InputFront(elem);
            return ;
          }

          for(;p->next!=NULL;p=p->next){
                ;
          }
          p->next=newnode;
      }
      void MakeEmpty(){
          LinkNode *tem;
          LinkNode *p=first;
          if(p==NULL){
            return;
          }
          for(;p->next!=NULL;p=tem){
            tem=p->next;
            delete p;
          }
          delete p;
          first=NULL;
      }
      int Length() const{
          int i=0;
          for(LinkNode *p=first;p!=NULL;p=p->next){
            i  ;
          }
          return i;
      }
      LinkNode* Search(const T& x){
          for(LinkNode*p=first;p!=NULL;p=p->next){
            if(p->data==x)return p;
          }
          return NULL;
      }
      LinkNode* Locate(int i){
        if(i>=1&&i<=this->Length()){
            for(LinkNode*p=first;p!=NULL;p=p->next){
                i--;
                if(i==0)return p;
            }
            return NULL;
        }
        else
            return NULL;
      }
      bool GetData(int i, T& x){
          if(this->Locate(i)==NULL)
                return false;
          else{
                LinkNode * p=this->Locate(i);
                x=p->data;
                return true;
          }

      }
      void SetData(int i, const T& x){
          LinkNode * p=this->Locate(i);
          p->data=x;
      }
      bool Insert(int i, const T& x){
          if(i<1)return false;
          if(i==1)this->InputFront(x);
          else if(i<=this->Length() 1){
                LinkNode * p=this->Locate(i-1);
                if(p==NULL)return false;
                LinkNode *newnode=new LinkNode(x);
                newnode->next=p->next;
                p->next=newnode;
                return true;
          }
          return false;
      }
      bool Remove(int i, T& x){
          if(this->IsEmpty()==true)return false;
          if(i==1){
                LinkNode *p=first;
                first=first->next;
                x=first->data;
            delete p;
            return false;
          }
          LinkNode *p=this->Locate(i-1);

          if(p==NULL)return false;
          else {
                LinkNode *de=p->next;
                p->next=(de)->next;
          x=de->data;
            delete de;
            return true;
          }

      }
      bool IsEmpty() const{
          if(first==NULL)return true;
          return false;
      }
      bool IsFull() const;
      void CopyList(const List& L){
          T tem;
          this->MakeEmpty();
          for(LinkNode * p=L.first;p!=NULL;p=p->next){
                tem=p->data;
                this->InputRear(tem);
          }
      }
      void Sort(){
          LinkNode *a[Length()];
          int i=0;
          if(first==NULL)return;
          for(LinkNode*p=first;p!=NULL;p=p->next){
                a[i]=new LinkNode ;
                a[i]->data=p->data;
                i  ;
          }
          sort(a,a i,compare);
          for(int j=0;j<i-1;j  ){
            a[j]->next=a[j 1];
          }
          a[i-1]->next=NULL;
          MakeEmpty();
          first=a[0];
      }
      friend ostream& operator<<(ostream& out, const List& L){
          for(LinkNode *p=L.first;p!=NULL;p=p->next){
            out<<p->data<<" ";
          }
          out<<endl;
            return out;
      }
      friend istream& operator>>(istream& in, List& L){
          T t;
          L.MakeEmpty();
            while(in>>t){
                L.InputRear(t);
                char c=cin.get();
                 if(c=='\n')break;
            }
            return in;
      }
};

int main()
{
      List lst;

      srand(time(NULL));
      for(int i=1; i<=2; i  )
            lst.Insert(i,rand()%50);
      lst.Sort();
      cout<<lst;
      int val;
      lst.Remove(2,val);
      cout<<lst;
      List lst1=lst;
      lst.MakeEmpty();
      lst=lst1;
      cout<<lst;
return 0;
}
bool compare (const LinkNode* a,const LinkNode *b){
    return a->data<b->data;
}

标签: 链表 创建

实例下载地址

不带头节点的单链表创建

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警