zoukankan      html  css  js  c++  java
  • 数据结构-单链表-类定义2-C++

    上一次的C++链表实现两个单链表的连接不太理想,此次听了一些视频课,自己补了个尾插法,很好的实现了两个链表的连接,当然了,我也是刚接触,可能是C++的一些语法还不太清楚,不过硬是花了一些时间尽量在数据结构中将c++的语言特点表现出来。一开始也是不愿意读c++的数据结构,只是一种挑战心里,不想读着读着感觉自己太low了,c++的内容更加丰富,所以还得多多练习......

    头文件

      1 #ifndef LIST_H
      2 #define LIST_H
      3 #include <iostream>
      4 
      5 template <class Type> class List;          //前置声明
      6 template <class Type> class ListIterator;  //前置声明
      7 
      8 //创建结点类
      9 template <class Type>  //类模板
     10 class ListNode
     11 {
     12     friend class List<Type>;           //友元函数--29行
     13     friend class ListIterator<Type>;   //友元函数--46行
     14 private:
     15     Type data;
     16     ListNode *link;
     17     ListNode(Type);
     18 };
     19 
     20 template <class Type>
     21 ListNode<Type>::ListNode(Type element)  //创建头结点
     22 {
     23     data = element;
     24     link = 0;
     25 }
     26 
     27 //创建链表类
     28 template <class Type>
     29 class List
     30 {
     31     friend class ListIterator<Type>;  //友元函数--46行
     32 public:
     33     List() { first=tail= 0; }; 
     34     void Insert(Type);      //头插法
     35     void Inserttail(Type);  //尾插法
     36     void Delete(Type);      //按值删除元素
     37     void Invert();          //反转链表
     38     void Concatenate(List<Type>);  //连接链表
     39     void Show();  //显示链表做测试用,创建迭代器后可以不用它显示
     40 
     41 private:
     42     ListNode<Type> *first, *tail;  // 创建头指针
     43 };
     44 
     45 /**************************************************************/
     46 //创建迭代器类
     47 template <class Type>
     48 class ListIterator
     49 {
     50 public:
     51     ListIterator(const List<Type>& l):list(l),current(l.first){}
     52     bool NotNull();
     53     bool NextNotNull();
     54     Type* First();
     55     Type* Next();
     56 private:
     57     const List<Type> &list;
     58     ListNode<Type>* current;
     59 };
     60 
     61 template <class Type>
     62 bool ListIterator<Type>::NotNull()
     63 {
     64     if (current) return true;
     65     else return false;
     66 }
     67 
     68 template <class Type>
     69 bool ListIterator<Type>::NextNotNull()
     70 {
     71     if (current && current->link) return true;
     72     else return false;
     73 }
     74 
     75 template <class Type>
     76 Type* ListIterator<Type>::First()
     77 {
     78     if (list.first) return &list.first->data;
     79     else return 0;
     80 }
     81 
     82 template <class Type>
     83 Type* ListIterator<Type>::Next()
     84 {
     85     if (current)
     86     {
     87         current = current->link;
     88         return &current->data;
     89     }
     90     else return 0;
     91 }
     92 /**************************************************************/
     93 
     94 // 前插法
     95 template <class Type>
     96 void List<Type>::Insert(Type k)
     97 {
     98     ListNode<Type> *newnode = new ListNode<Type>(k);  // 21行,新建结点并为data域赋值k
     99     
    100     //下面两行的意义就是头插法,将新建立的结点从头插入
    101     newnode->link = first;
    102     first = newnode;
    103 }
    104 
    105 template <class Type>
    106 void List<Type>::Inserttail(Type k)
    107 {
    108     if (tail != 0) {
    109         tail->link = new ListNode<Type>(k);
    110         tail = tail->link;
    111     }
    112     else first = tail = new ListNode<Type>(k);
    113 }
    114 
    115 template <class Type>
    116 void List<Type>::Delete(Type k)
    117 {
    118     ListNode<Type> *previous = 0;
    119     ListNode<Type> *current;
    120     for (current = first; current && current->data != k;
    121         previous = current, current = current->link);
    122 
    123     if (current)
    124     {
    125         if (previous) previous->link = current->link;
    126         else first = first->link;
    127         delete current;
    128     }
    129 }
    130 
    131 template <class Type>
    132 void List<Type>::Invert()
    133 {
    134     ListNode<Type> *p = first, *q = 0;
    135     while (p)
    136     {
    137         ListNode<Type> *r = q; q = p;
    138         p = p->link;
    139         q->link = r;
    140     }
    141     first = q;
    142 }
    143 
    144 template <class Type>
    145 void List<Type>::Concatenate(List<Type> b)
    146 {
    147     if (!first) { first = b.first; return; }
    148     if (b.first)
    149     {
    150         ListNode<Type> *p;
    151         for (p = first; p->link; p = p->link);
    152         p->link = b.first;
    153     }
    154 }
    155 
    156 template <class Type>
    157 void List<Type>::Show()
    158 {
    159     for (ListNode<Type> *current = first; current; current = current->link)
    160     {
    161         std::cout << current->data;
    162         if (current->link) std::cout << "->";
    163     }
    164     std::cout << std::endl;
    165 }
    166 
    167 #endif

    源文件

     1 #include<iostream>
     2 #include"List.h"
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     cout << "测试" << endl;
     8     cout << "自建的迭代器" << endl;
     9     List<int> intList;
    10     intList.Insert(5);
    11     intList.Insert(15);
    12     intList.Insert(25);
    13     intList.Insert(35);
    14 
    15     /**************************************************************/
    16     //可以先注释这段迭代器输出
    17     ListIterator<int> li(intList);             
    18     if (li.NotNull())   
    19     {
    20         cout << *li.First();
    21         while (li.NextNotNull())
    22             cout << "->" << *li.Next();
    23         cout << endl;
    24     }
    25     /**************************************************************/
    26 
    27     intList.Show();
    28     intList.Invert();
    29     intList.Show();
    30 
    31     intList.Delete(15);
    32     intList.Show();
    33     intList.Delete(20);
    34     intList.Show();
    35 
    36     List<char> charList;
    37     charList.Insert('a');
    38     charList.Insert('b');
    39     charList.Insert('c');
    40     charList.Insert('d');
    41     charList.Show();
    42     charList.Invert();
    43     charList.Show();
    44 
    45     List<char> char2List;
    46     char2List.Insert('e');
    47     char2List.Insert('f');
    48     char2List.Show();
    49     char2List.Invert();
    50     char2List.Show();
    51 
    52     charList.Concatenate(char2List);
    53     charList.Show();
    54 
    55     List<int> intList2;
    56     intList2.Inserttail(1);
    57     intList2.Inserttail(2);
    58     intList2.Inserttail(3);
    59     intList2.Inserttail(4);
    60     intList2.Show();
    61 
    62     intList.Concatenate(intList2);
    63     ListIterator<int> li2(intList);
    64     if (li2.NotNull())
    65     {
    66         cout << *li2.First();
    67         while (li2.NextNotNull())
    68             cout << "->" << *li2.Next();
    69         cout << endl;
    70     }
    71 
    72     return 0;
    73 }
  • 相关阅读:
    java面试题总汇
    数据库sql语句规则
    docker安装redis(网上很多答案都是错误的,小心误入歧途!)
    docker 安装redis后,可视化工具无法连接
    Session
    #{}和${}区别
    axios和ajax区别
    Git使用
    Git常用命令
    如何简单粗暴的搞定dubbo调用模块
  • 原文地址:https://www.cnblogs.com/yang901112/p/11780935.html
Copyright © 2011-2022 走看看