3、单向链表
| 版权声明:本文为博主原创文章,未经博主允许不得转载。
1. 单向链表的基础知识点。
(1)、链接的存储线性表的方法称为链表,可以有一组任意的存储单元来存储线性表的结点,其中这组存储单元可以是连续的,也可以是不连续的。
(2)、链表中结点的逻辑次序和物理次序是不一定相同的。在每个链表的节点之间通过指针来表示节点之间的关系。这个指针称为next指针。
(3)、链表的结构:包括数据域(data)和指针域(next);其中data域存放的是节点的数据值,next域存放的是节点的直接后继的地址(位置),也
就是说next这存放的是下一个节点的地址值。
(4)、链表这种结构在进行插入和删除操作的时候是非常便利的,时间复杂度为O(1),但是在进行查询操作的时候是不方便的,它的时间复杂度为O(n)。
链表的优缺点正好和顺序表相反。
(5)、图示:
单链表的一般图示
链表的插入和删除图示
>>.分步代码
(1)、链表结构体的定义
(2)、尾插法创建链表
×. 还有一个头插法创建链表,在下节循环链表中贴出。
(3)、链表打印函数
(4)、链表查询
(5)、插入结点
(6)、删除结点
(7)、链表长度
(8)、销毁链表
>>.完整代码
1 /************************************************ 2 * 单项链表 * 3 * * 4 * 2016/7/26 * 5 * by Lipei * 6 ************************************************/ 7 8 #include <iostream> 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 using namespace std; 13 14 #define OK 1 15 #define ERROR 0 16 #define OVERFLOW -1 17 18 typedef int STATUS; 19 typedef int ElemType; 20 21 typedef struct ChainList 22 { 23 //指针域 24 struct ChainList* next; 25 //数据域 26 ElemType data; 27 }chainList; 28 29 //尾插法创建 30 void createChainList(chainList** headNode, ElemType& elem) 31 { 32 //首先判断是否已经创建了头节点 33 if((*headNode) == NULL) 34 { 35 //没有头节点的时候创建头节点 36 (*headNode) = (chainList*) malloc(sizeof(chainList)); 37 if(!(*headNode)) 38 { 39 cout << "The head node create failure, Error!" << endl; 40 exit(OVERFLOW); 41 } 42 (*headNode)->next = NULL; 43 (*headNode)->data = 0; 44 } 45 chainList* newNode = NULL; 46 chainList* ptrNode = NULL; 47 48 ptrNode = (*headNode); 49 //找到尾部节点 50 while(ptrNode->next != NULL) 51 ptrNode = ptrNode->next; 52 //创建新节点 53 newNode = (chainList*) malloc(sizeof(chainList)); 54 if(!newNode) 55 { 56 cout << "The new node create failure, Error!" << endl; 57 exit(OVERFLOW); 58 } 59 newNode->data = elem; 60 newNode->next = ptrNode->next; 61 ptrNode->next = newNode; 62 } 63 64 //链表打印函数 65 STATUS printChainList(chainList* headNode) 66 { 67 //打印之前,首先应该判断是否存在此链表 68 if(headNode == NULL) 69 { 70 cout << "The chain list is not exist, Error!" << endl; 71 return OVERFLOW; 72 } 73 chainList* ptrNode; 74 ptrNode = headNode->next; 75 cout << "The list: "; 76 while(ptrNode) 77 { 78 cout << ptrNode->data << ", "; 79 ptrNode = ptrNode->next; 80 } 81 cout << endl; 82 return OK; 83 } 84 85 //在链表中查找指定的元素的节点是否存在 86 chainList* SeachNode(chainList& headNode, ElemType elem) 87 { 88 //查找元素之前判断传来的链表是否创建 89 if(&headNode == NULL) 90 { 91 cout << "The chain list is not exist, Error!" << endl; 92 return NULL; 93 } 94 chainList* ptrNode = &headNode; 95 96 while(ptrNode != NULL) 97 { 98 if(ptrNode->data == elem) 99 return ptrNode; 100 ptrNode = ptrNode->next; 101 } 102 cout << "The chain without " << elem << " elems!" << endl; 103 return NULL; 104 } 105 106 //插入节点,在相应的位置 107 STATUS insertNode(chainList& address, ElemType elem) 108 { 109 //插入元素之前判断,传入的插入位置的地址参数是否正确 110 if(&address==NULL) 111 { 112 cout << "Incoming address parameter error!" << endl; 113 return OVERFLOW; 114 } 115 chainList* ptrNode = &address; 116 chainList* newNode = NULL; 117 118 newNode = (chainList*) malloc(sizeof(chainList)); 119 if(!newNode) 120 { 121 cout << "The new node create failure!" << endl; 122 exit(OVERFLOW); 123 } 124 newNode->data = elem; 125 newNode->next = ptrNode->next; 126 ptrNode->next = newNode; 127 cout << "Insert successful!" << endl; 128 return OK; 129 } 130 131 //删除节点,在相应的位置 132 STATUS deleteNode(chainList* address, ElemType* elem) 133 { 134 if(!address) 135 { 136 cout << "The address is not exist, Error!" << endl; 137 return OVERFLOW; 138 } 139 chainList* ptrNode = address->next; 140 address->next = ptrNode->next; 141 *elem = ptrNode->data; 142 free(ptrNode); 143 return OK; 144 } 145 146 //统计链表的长度 147 STATUS lengthChainList(chainList& headNode) 148 { 149 int length = 0; 150 chainList* ptrNode = NULL; 151 ptrNode = headNode.next; 152 while(ptrNode) 153 { 154 length++; 155 ptrNode = ptrNode->next; 156 } 157 return length; 158 } 159 160 //销毁链表 161 STATUS destoryChainList(chainList* headNode) 162 { 163 chainList* ptrNode = NULL; 164 chainList* tmpNode = NULL; 165 166 if(!headNode) 167 { 168 return ERROR; 169 } 170 171 ptrNode = headNode; 172 while(ptrNode) 173 { 174 tmpNode = ptrNode->next; 175 free(ptrNode); 176 ptrNode = tmpNode; 177 } 178 return OK; 179 } 180 181 //链表功能显示函数 182 void showFunc() 183 { 184 cout << "1: 创建链表;" << endl; 185 cout << "2: 打印链表;" << endl; 186 cout << "3: 查找节点;" << endl; 187 cout << "4: 插入节点;" << endl; 188 cout << "5: 删除节点;" << endl; 189 cout << "6: 统计长度;" << endl; 190 cout << "7: 销毁链表;" << endl; 191 cout << "0: 退出程序;" << endl << endl << endl; 192 } 193 194 //链表功能操作函数 195 void listFunc() 196 { 197 int choice = 0; 198 chainList* headNode = NULL; 199 chainList* address = NULL; 200 int count = 0; 201 int i = 0; 202 int length = 0; 203 ElemType elem; 204 205 while(1) 206 { 207 cout << "Please enter your choice: "; 208 cin >> choice; 209 210 switch(choice) 211 { 212 case 1: 213 cout << "Enter create list node mount: "; 214 cin >> count; 215 cout << "Enter " << count << " elems of list data: "; 216 for(i=0; i < count; i++) 217 { 218 cin >> elem; 219 createChainList(&headNode, elem); 220 } 221 cout << endl; 222 break; 223 case 2: 224 cout << "Printf ChainList:" << endl; 225 printChainList(headNode); 226 cout << endl; 227 break; 228 case 3: 229 cout << "Please enter search elem: "; 230 cin >> elem; 231 address = SeachNode(*headNode, elem); 232 if(address!=NULL) 233 cout << "The " << elem << " elems is find successful!" << endl; 234 cout << endl; 235 break; 236 case 4: 237 cout << "Enter elem of insert: "; 238 cin >> elem; 239 insertNode(*address, elem); 240 printChainList(headNode); 241 cout << endl; 242 break; 243 case 5: 244 cout << "Delete Node:" <<endl; 245 deleteNode(address, &elem); 246 printChainList(headNode); 247 cout << "The delete elem in list is: " << elem <<" ." << endl; 248 cout <<endl; 249 break; 250 case 6: 251 cout << "The length of chain list: "; 252 length = lengthChainList(*headNode); 253 cout << length << " ." << endl; 254 cout <<endl; 255 break; 256 case 7: 257 cout << "Destory chain list: "; 258 if(destoryChainList(headNode)) 259 { 260 cout << "Successful !" << endl; 261 } 262 else 263 { 264 cout << "Failure !" << endl; 265 } 266 cout <<endl; 267 break; 268 case 0: 269 exit(0); 270 cout <<endl; 271 break; 272 } 273 } 274 } 275 276 int main(void) 277 { 278 showFunc(); 279 listFunc(); 280 return 0; 281 }
运行截图: