数据结构-单链表-类定义C++

原理可访问https://www.cnblogs.com/yang901112/p/11674333.html

头文件

  1 #ifndef RLIST_H
  2 #define RLIST_H
  3 #include <iostream>
  4 
  5 template <class T> class List;
  6 
  7 template <class T>
  8 class ListNode
  9 {
 10     friend class List<T>;
 11 public:
 12     ListNode() { next = 0; }
 13     ListNode(T el, ListNode* ptr = 0) {
 14         data = el; next = ptr;
 15     }
 16 private:
 17     T data;
 18     ListNode *next;
 19 };
 20 
 21 template <class T>
 22 class List
 23 {
 24 public:
 25     List() { 
 26         head = 0;
 27         head = tail = 0; }
 28     ~List();
 29     int isEmpty() {
 30         return head == 0;
 31     }
 32     void addToHead(T);  //从头插入结点
 33     void addToTail(T);  //从尾部插入节点
 34     void disPlay() const;
 35     T deleteFromHead();  //从头删除结点//若不需要返回值可以使用void
 36     T deleteFromTail();  //从尾部删除节点
 37     void deleteNode(T);  //按值删除结点
 38     bool isInList(T) const;  //判断值是否在链表里
 39     void Invert();  //反转链表
 40 private:
 41     ListNode<T> *head, *tail;
 42 };
 43 
 44 template <class T>
 45 List<T>::~List() {
 46     for (ListNode<T> *p; !isEmpty();) {
 47         p = head->next;
 48         delete head;
 49         head = p;
 50     }
 51 }
 52 
 53 template <class T>
 54 void List<T>::addToHead(T el) {
 55     head = new ListNode<T>(el, head);
 56     if (tail == 0)
 57         tail = head;
 58 }
 59 
 60 template <class T>
 61 void List<T>::addToTail(T el) {
 62     if (tail != 0) {
 63         tail->next = new ListNode<T>(el);
 64         tail = tail->next;
 65     }
 66     else head = tail = new ListNode<T>(el);
 67 }
 68 
 69 template <class T>
 70 T List<T>::deleteFromHead() {
 71     T el = head->data;
 72     ListNode<T> *tmp = head;
 73     if(head==tail)
 74     {
 75         head = tail = 0;
 76     }
 77     else head = head->next;
 78     delete tmp;
 79     return el;
 80 }
 81 
 82 template <class T>
 83 T List<T>::deleteFromTail() {
 84     T el = tail->data;
 85     if(head==tail)
 86     {
 87         delete head;
 88         head = tail = 0;
 89     }
 90     else {
 91         ListNode<T> *tmp;
 92         for (tmp = head; tmp->next != tail; tmp = tmp->next);
 93         delete tail;
 94         tail = tmp;
 95         tail->next = 0;
 96     }
 97     return el;
 98 }
 99 
100 template <class T>
101 void List<T>::deleteNode(T el) {
102     ListNode<T> *previous = 0;
103     ListNode<T> *current;
104     for (current = head; current && current->data != el;
105         previous = current, current = current->next);
106     if (current)
107     {
108         if (previous) { previous->next = current->next; }
109         else head = head->next;
110         delete current;
111     }
112 }
113 
114 /*****************************************/
115 //template <class T>
116 //bool List<T>::isInList(T el) const {
117 //    bool flag = false;
118 //    ListNode<T> *tmp;
119 //    tmp = head->next;
120 //    while (tmp) {
121 //        if (tmp->data == el) {
122 //            flag = true;
123 //            break;
124 //        }
125 //        tmp = tmp->next;
126 //    }
127 //    return flag;
128 //}
129 
130 //可以使用for循环代替while代码可以简洁一些
131 template <class T>
132 bool List<T>::isInList(T el) const {
133     ListNode<T> *tmp;
134     for (tmp = head; tmp && tmp->data != el; tmp = tmp->next);
135     return tmp != 0;
136 }
137 /*****************************************/
138 
139 template <class T>
140 void List<T>::Invert() {
141     ListNode<T> *p = head, *q = 0;
142     while (p)
143     {
144         ListNode<T> *r = q; q = p;
145         p = p->next;
146         q->next = r;
147     }
148     head = q;
149 }
150 
151 
152 template <class T>
153 void List<T>::disPlay() const{
154     //ListNode<T>  *p;
155     //p = head;
156     //while (p)
157     //{
158     //    std::cout << p->data;
159     //    if (p->next) { std::cout << "->"; } //就是仅在数字之间加"->"
160     //    p = p->next;
161     //}
162     for (ListNode<T> *current = head; current; current = current->next)
163     {
164         std::cout << current->data;
165         if (current->next) std::cout << "->";
166     }
167     std::cout << std::endl;
168 }
169 
170 #endif

源文件

 1 #include <iostream>
 2 #include"Rlist.h"
 3 #include <iomanip>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     cout << "测试" << endl;
 9     List<int> ilist;
10     ilist.addToTail(1);
11     ilist.addToTail(2);
12     ilist.addToTail(3);
13     ilist.addToTail(4);
14     ilist.disPlay();
15     ilist.deleteFromHead();
16     ilist.disPlay();
17 
18     List<int> ilist2;
19     ilist2.addToHead(5);
20     ilist2.addToHead(6);
21     ilist2.addToHead(7);
22     ilist2.addToHead(8);
23     ilist2.disPlay();
24     cout << ilist2.isInList(5) << endl;
25     ilist2.deleteFromTail();
26     ilist2.disPlay();
27     cout << ilist2.isInList(5) << endl;
28     
29     List<char> charlist1;
30     charlist1.addToTail('a');
31     charlist1.addToTail('b');
32     charlist1.addToTail('c');
33     charlist1.addToTail('d');
34     charlist1.disPlay();
35     charlist1.Invert();
36     charlist1.disPlay();
37     charlist1.deleteNode('c');
38     charlist1.disPlay();
39     charlist1.deleteNode('e'); //虽然'e'不存在,但是不影响
40     charlist1.disPlay();
41 
42 
43 
44     return 0;
45 }

 

posted @ 2019-11-01 22:14  yg_staring  阅读(811)  评论(0编辑  收藏  举报