单链表的插入 删除 及带环 问题

来源:互联网 发布:通过淘宝助理的模块 编辑:程序博客网 时间:2024/05/16 05:46
#include<stdio.h>#include<assert.h>#include<malloc.h>#include<stdlib.h>typedef int Datatype;typedef struct SListNode{Datatype data;struct SListNode*next;}SListNode;void Erase(SListNode*&pHead, SListNode *pos);void PushBack(SListNode*&pHead, Datatype x);void PopBack(SListNode *&pHead);void PrintSlist(SListNode *&PHead);void PushFrot(SListNode*&pHead, Datatype x);void PopFront(SListNode*&pHead);SListNode *Find(SListNode*pHead, Datatype x);SListNode* _BuyNode(Datatype x){SListNode *temp = (SListNode*)malloc(sizeof(SListNode));temp->data = x;temp->next = NULL;return temp;}void PushBack(SListNode*&pHead, Datatype x){//1 空   2  不为空if (pHead == NULL){pHead = _BuyNode(x);}else{SListNode *tail = pHead;while (tail->next != NULL){tail = tail->next;}tail->next = _BuyNode(x);}}void PopBack(SListNode *&pHead){//1空  2 一个节点  3 多个节点  if (pHead == NULL){return;}else if (pHead->next == NULL){free(pHead);pHead = NULL;}else{SListNode *tail = pHead;SListNode *tem = NULL;while (tail->next != NULL){tem = tail;tail = tail->next;}free(tail);tem->next = NULL;}}void PrintSlist(SListNode *&PHead){SListNode*cur = PHead;while (cur != NULL){printf("%d->", cur->data);cur = cur->next;}printf("NULL\n");}void PushFrot(SListNode*&pHead, Datatype x){if (pHead == NULL){pHead = _BuyNode(x);}else{SListNode *tmp = _BuyNode(x);tmp->next = pHead;pHead = tmp;}}void PopFront(SListNode*&pHead){//1 空//2 一个结点//3 一个以上的节点if (pHead == NULL){return;}else if (pHead->next == NULL){free(pHead);pHead = NULL;}else{SListNode *tmp = pHead;pHead = pHead->next;free(tmp);}}SListNode *Find(SListNode*pHead, Datatype x){SListNode *tail = pHead;while (tail){if (tail->data == x){return tail;}tail = tail->next;}return NULL;}void Erase(SListNode *&pHead, SListNode *pos){assert(pos);assert(pHead);if (pHead == pos){pHead = pHead->next;free(pos);return;}SListNode *prv = pHead;while (prv){if (prv->next == pos){prv->next = pos->next;free(pos);break;}prv = prv->next;}}SListNode *yuesefu(SListNode*phead, int k){SListNode *tail = phead;while (1){if (tail->next == tail){return tail;}int count = k;while (--count){tail = tail->next;}SListNode *del = tail->next;//删除节点tail->data = del->data;tail->next = del->next;free(del);}}//合并两个有序链表,合并后依然有序SListNode *hebing(SListNode *L1, SListNode *L2){//1 L1为空//2 L2  为空//3 两者都为空if (L1 == NULL){return L2;}if (L2 == NULL){return L1;}SListNode *newhead = NULL;SListNode *phead1 = L1;SListNode *phead2 = L2;if (phead1->data < phead2->data){newhead = phead1;phead1 = phead1->next;}else{newhead = phead2;phead2 = phead2->next;}SListNode *tail = newhead;while (phead1&&phead2){if (phead1->data < phead2->data){tail->next = phead1;tail = tail->next;phead1 = phead1->next;}else{tail->next = phead2;phead2 = phead2->next;tail = tail->next;}}if (phead1){tail->next = phead1;}else{tail->next = phead2;}return newhead;}//判断链表是否带环 环的长度SListNode *checkcycle(SListNode * phead){SListNode * fast = phead;SListNode *slow = phead;while (fast&&fast->next){slow = slow->next;fast = fast->next->next;if (fast == slow){return fast;}}return NULL;}int getcyclelength(SListNode *meetNode){assert(meetNode);SListNode *cur = meetNode;int count = 0;do{++count;cur = cur->next;} while (cur != meetNode);return count;}void test6(){SListNode*list1= NULL;SListNode*list2= NULL;PushBack(list1, 1);PushBack(list1, 3);PushBack(list1, 5);     PushBack(list1, 7);PushBack(list1, 8);PushBack(list1, 5);PushBack(list1, 7);PushBack(list1, 8);//PushBack(list1, 9);/*PushBack(list2, 2);PushBack(list2, 4);PushBack(list2, 6);PushBack(list2, 8);PushBack(list2, 10);*///SListNode *tmp= hebing(list1, list2);//SListNode* ret = Find(list, 6);//ret->next = list;//SListNode*ret2 = yuesefu(list, 3);//printf("%d \n", ret2->data);//PrintSlist(tmp); /*SListNode * cmp=checkcycle(list1); int len = getcyclelength(cmp); printf("%d \n", len);*/}void test7(){SListNode*list1 = NULL;PushBack(list1, 1);PushBack(list1, 2);PushBack(list1, 3);PushBack(list1, 4);PushBack(list1, 5);PrintSlist(list1);SListNode* ret1 = Find(list1, 5);SListNode* ret2 = Find(list1, 3);ret1->next = ret2;SListNode* ret = checkcycle(list1);printf("%d\n", ret->data);    int len = getcyclelength(ret);printf("%d ", len);}int main(){//test1();test7();system("pause");return 0;}


0 0