• C++链表基本操作


    LinearList.cpp

      1 /*
      2     458043535@qq.com
      3     xxdfly
      4 */
      5 #include "LinearList.h"
      6 
      7 //构造函数
      8 LinearList::LinearList()
      9 {
     10     head = (Node*)malloc(sizeof(Node));
     11     head->next=0;
     12 }
     13 
     14 //返回链表长度
     15 int LinearList::ListLength()
     16 {
     17     Node *p;
     18     int length=0;
     19     p=head->next;
     20     while(p)
     21     {
     22         p=p->next;
     23         length++;
     24     }
     25     return length;
     26 }
     27 
     28 //判断链表是否为空,若为空返回true,否则返回false
     29 bool LinearList::EmptyList()
     30 {
     31     if(head->next==0)
     32         return true;
     33     else
     34         return false;
     35 }
     36 
     37 //打印链表
     38 LinearList::ShowList()
     39 {
     40     Node *p=head->next;
     41     while(p)
     42     {
     43         cout << p->data << "	";
     44         p=p->next;
     45     }
     46     cout << endl;
     47 }
     48 
     49 //在链表i位置上插入元素e
     50 LinearList::InserList(int i,int e)
     51 {
     52     int length=ListLength();
     53 
     54     if(i<1||i>length+1)
     55     {
     56         cout << "insert location parameter is illegal" << endl;
     57     }
     58     else
     59     {
     60         int count=1;
     61         Node *p,*q;
     62         Node *elem=new Node;
     63         
     64         p=q=head;
     65         elem->data=e;
     66         elem->next=0;
     67         
     68         while(count<=length+1)
     69         {
     70             if(count==i)
     71             {
     72                 elem->next=p->next;
     73                 q->next=elem;
     74                 break;
     75             }
     76 
     77             p=q=q->next;
     78             count++;
     79         }
     80     }
     81 }
     82 
     83 //删除链表i位置上的元素
     84 LinearList::DeletList(int i)
     85 {
     86     int length=ListLength();
     87 
     88     if(i<1||i>length+1)
     89     {
     90         cout << "delete location parameter is illegal" << endl;
     91     }
     92     else
     93     {
     94         int count=1;
     95         Node *p,*q;
     96         p=q=head;
     97         while(count<=length+1)
     98         {
     99             if(count==i)
    100             {
    101                 q->next=p->next->next;
    102                 break;
    103             }
    104             q=p=p->next;
    105             count++;
    106         }
    107     }
    108 }
    109 
    110 //获取链表i位置上的数据
    111 int LinearList::GetData(int i)
    112 {
    113     int length=ListLength();
    114     int result;
    115     if(i<1||i>length)
    116     {
    117         cout << "delete location parameter is illegal" << endl;
    118     }
    119     else
    120     {
    121         int count=0;
    122         Node *p=head;
    123         while(count<=length)
    124         {
    125             if(count==i)
    126             {
    127                 result=p->data;
    128                 break;
    129             }
    130             p=p->next;
    131             count++;
    132         }
    133     }
    134     return result;
    135 }
    136 
    137 //将链表置空
    138 LinearList::ClearList()
    139 {
    140     head->next=0;
    141 }
    142 
    143 //获取包含e元素的节点(只寻找第一个)
    144 Node* LinearList::Locate(int e)
    145 {
    146     Node *elem=0;
    147     Node *p=head->next;
    148     while(p)
    149     {
    150         if(e==p->data)
    151         {
    152             elem=p;
    153             break;    
    154         }
    155         p=p->next;
    156     }
    157     return elem;
    158 }
    159 
    160 //销毁链表
    161 LinearList::~LinearList()
    162 {
    163     if(head)
    164         delete []head;
    165     cout << "the nodelist is destroyed" << endl;
    166 }

    LinearList.h

     1 #include <iostream>
     2 using namespace std;
     3 
     4 struct Node
     5 {
     6     int data;
     7     Node *next;
     8 };
     9 
    10 class LinearList
    11 {
    12     public:
    13         Node *head;
    14     public:
    15         LinearList();
    16         ~LinearList();
    17         InserList(int i,int e);
    18         DeletList(int i);
    19         DestroyList();
    20         ClearList();
    21         ShowList();
    22 
    23         int ListLength();
    24         int GetData(int i);
    25 
    26         Node* Locate(int e);
    27         bool EmptyList();
    28 };

    main.cpp

     1 #include "LinearList.h"
     2 
     3 int main(int argc, char* argv[])
     4 {
     5     Node* node=new Node;
     6     LinearList *list=new LinearList;
     7 
     8     cout << list->head << endl;
     9     cout << list << endl;
    10 
    11     cout << list->EmptyList() << endl;
    12     cout << list->ListLength() << endl;
    13     
    14     int data1=23;
    15 
    16     list->InserList(1,data1);
    17     list->ShowList();
    18     list->InserList(2,520);
    19     list->InserList(3,1314);
    20     list->ShowList();
    21 
    22     Node *e = list->Locate(1314);
    23     cout << e->data << "	" << "&e=" << e << "	e->next=" << e->next << endl;
    24     int a=list->GetData(2);
    25     cout << "a=" << a << endl;
    26 
    27     list->DeletList(2);
    28     list->ShowList();
    29 
    30     list->ClearList();
    31     list->ShowList();
    32     delete list;
    33     list->head;
    34     cout << list->head << endl;
    35     cout << list << endl;
    36     return 0;
    37 }
  • 相关阅读:
    做凭证时不显示货币
    凭证字段显示、保存布局等操作
    没有控制范围分配给公司代码
    拓端tecdat|R语言用Garch模型和回归模型对股票价格分析
    拓端tecdat|R语言混合图形模型MGM的网络可预测性分析
    拓端tecdat|R语言对推特twitter数据进行文本情感分析
    拓端tecdat|R语言广义线性模型GLM、多项式回归和广义可加模型GAM预测泰坦尼克号幸存者
    拓端tecdat|R语言:结构方程模型、潜变量分析
    拓端tecdat|R语言对股票风险“溃疡指数”( Ulcer Index)曲面图可视化
    django项目设置
  • 原文地址:https://www.cnblogs.com/xxdfly/p/4374335.html
Copyright © 2020-2023  润新知