最近在学习数据结构,刚开始一直在看书,但是总是感觉似懂非懂,心想还不如自己操练一波,势必有所收获。现将实现代码发表此地,以备日后复习,同时由于本人C++基础薄弱,若有错误或者建议,欢迎告知本人!
1. 节点类
1 class Node { 2 public: 3 int data; 4 Node *next; 5 Node(int da): 6 data(da), next(NULL){} 7 };
这里节点类的定义采用大多数刷题网站的模版(当然,也可以使用 struct )
2. 链表类ADT
1 class List{ 2 private: 3 Node *head; 4 public: 5 List(): head(NULL){} 6 ~List(){ 7 delete head; 8 cout<<"The list is deleted."<<endl; 9 }; 10 int size(); // 链表长度 11 bool isEmpty(); // 是否为空 12 void printList(); // 打印链表 13 void insert(int position, int value); // 指定位置插入 14 void insertHead(int value); // 插入到最前 15 void insertTail(int value); // 插入到最后 16 int getValue(int position); // 查找指定位置的值 17 int search(int value); // 查找指定元素的位置 18 void update(int position, int value); // 更新指定位置的值 19 int erase(int position); // 删除指定位置的节点 20 void reverse(); // 反转链表 21 //void clearList() // 清空链表 22 };
3. 完整代码
1 #include<iostream> 2 using namespace std; 3 4 class Node { 5 public: 6 int data; 7 Node *next; 8 Node(int da): 9 data(da), next(NULL){} 10 }; 11 12 class List{ 13 private: 14 Node *head; 15 public: 16 List(): head(NULL){} 17 ~List(){ 18 delete head; 19 cout<<"The list is deleted."<<endl; 20 }; 21 int size(); // 链表长度 22 bool isEmpty(); // 是否为空 23 void printList(); // 打印链表 24 void insert(int position, int value); // 指定位置插入 25 void insertHead(int value); // 插入到最前 26 void insertTail(int value); // 插入到最后 27 int getValue(int position); // 查找指定位置的值 28 int search(int value); // 查找指定元素的位置 29 void update(int position, int value); // 更新指定位置的值 30 int erase(int position); // 删除指定位置的节点 31 void reverse(); // 反转链表 32 //void clearList() // 清空链表 33 }; 34 35 // 返回链表大小 36 int List::size(){ 37 Node *p = head; 38 int index = 0; 39 while(p != NULL){ 40 index++; 41 p = p->next; 42 } 43 return index; 44 } 45 46 // 判断链表是否为空 47 bool List::isEmpty(){ 48 if(List::size() == 0) 49 return true; 50 else 51 return false; 52 } 53 54 // 打印链表 55 void List::printList(){ 56 Node *p = head; 57 while(p != NULL){ 58 cout<<p->data<<" "; 59 p = p->next; 60 } 61 cout<<endl; 62 cout<<endl; 63 } 64 65 // 在position位置插入value 66 void List::insert(int position, int value){ 67 if(position<0 || position>List::size()){ 68 cout<<"position error, please check."<<endl; 69 return ; 70 } 71 Node *s = new Node(value); // new node 72 Node *p = head; 73 if(head == NULL){ // isEmpty = true 74 head = s; 75 } 76 else{ // isEmpty = false 77 if(position == 0){ 78 s->next = p; 79 head = s; 80 } 81 else{ 82 int index = 0; 83 while(index != position-1){ 84 p = p->next; 85 index++; 86 } 87 s->next = p->next; 88 p->next = s; 89 } 90 } 91 if (position == 0) 92 cout<<"insert "<<value<<" at the first."<<endl; 93 else if (position == List::size()) 94 cout<<"insert "<<value<<" at the tail."<<endl; 95 else 96 cout<<"insert "<<value<<" at position "<<position<<endl; 97 } 98 99 // 头部插入 100 void List::insertHead(int value){ 101 List::insert(0, value); 102 } 103 104 // 尾部插入 105 void List::insertTail(int value){ 106 List::insert(List::size(), value); 107 } 108 109 // 搜索数据value,并返回位置 110 int List::search(int value){ 111 Node *p = head; 112 int index = 0; 113 while(p != NULL && p->data != value){ 114 index++; 115 p = p->next; 116 } 117 if(p == NULL){ 118 cout<<"the value is not in the list, please check."<<endl; 119 return -1; 120 } 121 else{ 122 cout<<value<<" at position "<<index<<endl; 123 return index; 124 } 125 } 126 127 // 将position位置的数据更新为value 128 void List::update(int position, int value){ 129 if(position<0 || position>(List::size()-1)){ 130 cout<<"position error, please check."<<endl; 131 return ; 132 } 133 Node *p = head; 134 int index = 0; 135 while(index != position){ 136 p = p->next; 137 index++; 138 } 139 p->data = value; 140 cout<<"update "<<value<<" at position "<<position<<endl; 141 } 142 143 // 删除position位置的数据,并返回 144 int List::erase(int position){ 145 if(position<0 || position>(List::size()-1)){ 146 cout<<"position error, please check."<<endl; 147 return -1; 148 } 149 int res = List::getValue(position); 150 Node *p = head; 151 int index = 0; 152 cout<<"erase data at position "<<position<<endl; 153 if(position == 0){ 154 head = p->next; 155 return res; 156 } 157 else{ 158 while(index != position-1){ 159 p = p->next; 160 index++; 161 } 162 Node *temp = p->next; 163 p->next = temp->next; 164 return res; 165 } 166 } 167 168 // 反转链表 169 void List::reverse(){ 170 if (head == NULL || head->next == NULL) 171 return ; 172 Node *prev = head; 173 Node *cur = head->next; 174 Node *temp = head->next->next; 175 while(cur){ 176 temp = cur->next; 177 cur->next = prev; 178 prev = cur; 179 cur = temp; 180 } 181 head->next = NULL; // 更新末尾元素的指针 182 head = prev; // 更新头结点 183 cout<<"reverse the list."<<endl; 184 } 185 186 // 返回position位置的数据 187 int List::getValue(int position){ 188 if(position<0 || position>(List::size()-1)){ 189 cout<<"position error, please check."<<endl; 190 return -1; 191 } 192 Node *p = head; 193 int index = 0; 194 while(index != position){ 195 p = p->next; 196 index++; 197 } 198 cout<<"position "<<position<<" is "<<p->data<<endl; 199 return p->data; 200 } 201 /* 202 void List::clearList(){ 203 Node *p = head; 204 while(p){ 205 Node *temp = p->next; 206 delete p; 207 p = temp; 208 } 209 } 210 */ 211 int main() { 212 List l1; 213 l1.insertTail(6);l1.printList(); 214 l1.insertHead(7);l1.printList(); 215 l1.insert(1, 5);l1.printList(); 216 l1.insert(0, 16);l1.printList(); 217 l1.insert(2, 56);l1.printList(); 218 l1.insert(0, 169);l1.printList(); 219 l1.insert(6, 16);l1.printList(); 220 l1.search(16); 221 l1.getValue(5); 222 l1.update(1, 666);l1.printList(); 223 l1.erase(0);l1.printList(); 224 l1.reverse(); l1.printList(); 225 cout<<"The size of the list: "<<l1.size()<<endl; 226 return 0; 227 }
4. 运行结果
insert 6 at the first. 6 insert 7 at the first. 7 6 insert 5 at position 1 7 5 6 insert 16 at the first. 16 7 5 6 insert 56 at position 2 16 7 56 5 6 insert 169 at the first. 169 16 7 56 5 6 insert 16 at position 6 169 16 7 56 5 6 16 16 at position 1 position 5 is 6 update 666 at position 1 169 666 7 56 5 6 16 position 0 is 169 erase data at position 0 666 7 56 5 6 16 reverse the list. 16 6 5 56 7 666 The size of the list: 6 The list is deleted. [Finished in 2.0s]
关于每一部分的详解会继续补充。
原文地址:https://www.cnblogs.com/iwangzhengchao/p/9769768.html
时间: 2024-10-25 14:22:20