数据结构——链表
该代码是读大学期间写的,今天整理电脑就将原来写的代码贴出来。
#include "iostream" using namespace std; int const null=0; //下面所有的如果超出链表的长度范围,就返回0 struct listNode { int data; listNode *next; };
//创建链表 listNode* InitList() { listNode *headNode = new listNode; headNode->next = null; headNode->data = null; listNode *currentNode = headNode; int data; cin>>data; //以0作为链表的创建结束 while(data != 0) { listNode *newNode = new listNode; newNode->data = data; newNode->next = null; currentNode->next = newNode; currentNode = newNode; cin>>data; } return headNode; }
//销毁链表 listNode* DestroyList(listNode *headNode) { listNode *currentNode = headNode->next; listNode *deleteNode; while(currentNode != null) { deleteNode = currentNode; currentNode = currentNode->next; delete deleteNode; } headNode->next = null; return headNode; }
//清空链表 void ClearList(listNode *headNode) { listNode *currentNode = headNode; while(currentNode != null) { currentNode->data = null; currentNode = currentNode->next; } }
//获取链表的长度 int listLength(listNode *headNode) { int count=0; listNode *currentNode = headNode->next ; while(currentNode != null) { count++; currentNode=currentNode->next ; } return count; }
//判断链表是否为空,如果为空返回True,否则返回FALSE bool ListEmpty(listNode *headNode) { if(listLength(headNode)==0) { return true ; } else { return false; } }
//获取链表中第i个元素的值,i的取值不能大于链表的长度 int GetElem(listNode *headNode,int i) { if(i < 1 || i > listLength(headNode)) { return 0; } else { listNode *currentNode = headNode ; for(int count = 0 ; count < i ; count++) { currentNode=currentNode->next ; } return currentNode->data ; } }
//添加新的元素 void AddElem(listNode *headNode,int data) { listNode *lastNode = headNode; while(lastNode->next != null) { lastNode=lastNode->next ; } listNode *newNode = new listNode; newNode->data = data; newNode->next = null; lastNode->next = newNode; lastNode = newNode; }
//在链表中寻找第一个结点中的值与data相等的元素的索引,若存在,就返回索引,否则为-1 int LocateElem(listNode *headNode,int data) { listNode *currentNode = headNode; int count = 0; while(currentNode != null) { if(currentNode->data == data) { break; } else { currentNode = currentNode->next ; count++; } } if(currentNode == null) { return -1; } else { return count; } }
//返回链表第i个节点的前驱值(i必须大于1且不能大于链表的长度) int PriorElem(listNode *headNode,int i) { if(i <= 1 || i > listLength(headNode)) { return 0; } else { listNode *priNode = headNode; listNode *currentNode = headNode->next ; for(int count = 0 ; count < i-1 ; count++) { priNode = currentNode; currentNode = currentNode->next ; } return priNode->data ; } }
//返回链表第i个结点的后继的值(i不能大于等于链表的长度且不能小于1) int NextElem(listNode *headNode,int i) { if(i <1 || i >= listLength(headNode)) { return 0; } else { listNode *currentNode = headNode; listNode *nextNode = currentNode->next ; for(int count = 0; count < i ; count++) { currentNode = currentNode ->next ; nextNode = currentNode->next ; } return nextNode->data; } }
//在链表的第i个位置处插入一个节点(i不能小于1,且不能大于链表的长度) void listInsert(listNode *headNode,int i,int data) { if(i < 1 || i > listLength(headNode)) { return ; } else { listNode *currentNode = headNode; for(int count = 0 ; count < i ; count++) { currentNode = currentNode->next; } listNode *newNode = new listNode; newNode->data = data; newNode->next = currentNode->next ; currentNode->next = newNode; int temp ; temp = currentNode->data ; currentNode->data = newNode->data ; newNode->data = temp; } }
//删除链表的地i个位置处的节点(i不能小于1,且不能大于链表的长度) void ListDelete(listNode *headNode,int i) { if(i <= 1 || i > listLength(headNode)) { return ; } else { listNode *currentNode = headNode; for(int count = 0 ; count < i ; count++) { currentNode = currentNode->next ; } listNode *deleteNode = currentNode->next ; currentNode->next = currentNode->next ->next ; currentNode->data =deleteNode->data; delete deleteNode; count--; } }
//遍历链表中的各节点 void ListTraverse(listNode *headNode) { listNode *currentNode = headNode->next ; while(currentNode !=null) { cout<<currentNode->data<<"------>"; currentNode=currentNode->next ; } cout<<endl; }
//链表的合并(即将所有在第二个链表但不在第一个链表中的元素插入到第一个链表的尾部) void Listunion(listNode *headNode1,listNode *headNode2) { listNode *currentNodeList1 = headNode1->next ; listNode *currentNodeList2 = headNode2->next ; int listData2 = 0; listNode *lastNode = null; while(currentNodeList1 !=null) { if(currentNodeList1->next ==null) { lastNode = currentNodeList1; } currentNodeList1 = currentNodeList1->next; } currentNodeList1=headNode1->next ; while(currentNodeList2 != null) { listData2 = currentNodeList2->data; while(currentNodeList1 != null) { if(currentNodeList1->data != listData2) { currentNodeList1 = currentNodeList1->next; } else { break; } } if(currentNodeList1==null) { listNode *newNode = new listNode; newNode->next = null; newNode->data =listData2; lastNode->next = newNode; lastNode = newNode; } currentNodeList2=currentNodeList2->next ; currentNodeList1=headNode1->next ; } }
//将链表中的元素进行排序(升序) void listSort(listNode *headNode) { //运用选择排序法进行排序 listNode *currentNodePre = headNode->next ; listNode *currentNodeNext = currentNodePre->next; while(currentNodePre->next != null) { while(currentNodeNext != null) { if(currentNodeNext->data < currentNodePre->data) { int temp; temp = currentNodeNext->data; currentNodeNext->data=currentNodePre->data; currentNodePre->data = temp; } currentNodeNext = currentNodeNext->next; } currentNodePre = currentNodePre->next; currentNodeNext = currentNodePre; } }
//将非降序的链表1和链表2按照非降序合并成一个链表,但是不利用额外的空间,只利用现有的空间 listNode* MergeList(listNode *headNode1,listNode *headNode2) { listSort(headNode1); listSort(headNode2); listNode *headNode = new listNode; headNode->data = null; headNode->next = null; listNode *currentNode = headNode; listNode *currentNodeList1 = headNode1->next; listNode *currentNodeList2 = headNode2->next; while(currentNodeList1 != null && currentNodeList2 != null) { listNode *newNode = new listNode; if(currentNodeList1->data < currentNodeList2->data) { newNode->data = currentNodeList1->data ; currentNodeList1 = currentNodeList1->next ; } else { newNode->data = currentNodeList2->data ; currentNodeList2 = currentNodeList2->next ; } newNode->next = null; currentNode->next = newNode; currentNode = newNode; } if(currentNodeList1 != null) { while(currentNodeList1 != null) { AddElem(headNode,currentNodeList1->data); currentNodeList1 = currentNodeList1->next ; } } else { while(currentNodeList2 != null) { AddElem(headNode,currentNodeList2->data ); currentNodeList2 = currentNodeList2->next ; } } return headNode; }
//将链表倒序 void reverse(listNode *headNode) { //在原有的空间上进行倒置,不引用其他的空间 listNode *currentNodeNext = headNode->next; listNode *temp; listNode *firstNode = headNode; while(firstNode->next !=null) { while(currentNodeNext->next != null) { temp = currentNodeNext; currentNodeNext = currentNodeNext->next ; } currentNodeNext->next = firstNode->next ; firstNode->next = currentNodeNext; temp->next = null; firstNode= currentNodeNext; currentNodeNext=firstNode->next ; } }