链表操作面试题
来源:互联网 发布:淘宝ar怎么用 编辑:程序博客网 时间:2024/06/05 06:06
#include<stdio.h> #include<stdlib.h> #include<assert.h>#define DateType int typedef struct Node { DateType data; struct Node* next;}Node, *pNode;void PrintList(pNode head)//打印链表 { pNode tmp = head; while (tmp) { printf("%d-->", tmp->data); tmp = tmp->next; } printf("NULL\n");}void BubbleSort(pNode *pHead)// 使用冒泡对单链表进行排序{ pNode p = NULL, q = NULL, ptail = NULL; int cmp, tag = 0; assert(pHead); if (*pHead == NULL || (*pHead)->next == NULL) return; p = *pHead; q = *pHead; while (p) { q = *pHead; tag = 0; while (q->next != ptail) { if (q->data > q->next->data) { cmp = q->data; q->data = q->next->data; q->next->data = cmp; tag = 1; } q = q->next; } if (tag == 0) return; ptail = q; p = p->next; }}pNode MergeList(pNode pHead1, pNode pHead2)// 合并两个已序链表,合并之后新链表依然有序{ pNode pl1 = NULL, pl2 = NULL,pnewhead=NULL,ptail=NULL; assert(pHead1||pHead2); pl1 = pHead1; pl2 = pHead2; if (!pl1) return pl2; if (!pl2) return pl1; if (pl1->data < pl2->data) { pnewhead = pl1; ptail = pl1; pl1 = pl1->next; } else { pnewhead = pl2; ptail = pl2; pl2 = pl2->next; } while (pl1&&pl2) { if (pl1->data < pl2->data) { ptail->next = pl1; ptail = pl1; pl1 = pl1->next; } else { ptail->next = pl2; ptail = pl2; pl2 = pl2->next; } } if (!pl1) ptail->next = pl2; else ptail->next = pl1; return pnewhead;}pNode FindLastKNode(pNode pHead, int K)// 查找无头单链表的倒数第K个结点{ pNode pleft = NULL, pright = NULL; assert(pHead); if (pHead == NULL|| K <= 0) return NULL; pleft = pright = pHead; while (--K) { if (pright->next == NULL) return NULL; pright = pright->next; } while (pright->next !=NULL) { pright = pright->next; pleft = pleft->next; } return pleft;}pNode DeleteLastKNode(pNode pHead, int K)// 删除无头单链表的倒数第K个结点{ pNode pdelnode = NULL, pdelleftnode = NULL; pdelnode=FindLastKNode(pHead, K); if (pdelnode == NULL) { printf("要删除的节点不存在!!!\n"); return NULL; } else if (pdelnode == pHead) { pdelleftnode = pdelnode; pHead = pHead->next; free(pdelnode); pdelnode = NULL; } else { pdelleftnode = FindLastKNode(pHead, K+1); pdelleftnode->next = pdelnode->next; free(pdelnode); pdelnode = NULL; } return pHead;}// 判断链表是否带环,如果带环求环的长度,并给出入口点pNode HasCircle(pNode pHead)//判断链表是否带环{ pNode pleft = NULL, pright = NULL; assert(pHead); if (pHead == NULL || pHead->next == NULL) return NULL; pleft = pHead; pright = pHead->next; while (pright || pright->next) { if (pleft != pright) { pleft = pleft->next; pright = pright->next->next; } else return pleft; } return NULL;}int GetCircleLen(pNode pHead)//如果带环,求环的长度{ pNode ptail=HasCircle(pHead); pNode q = ptail->next ; int counter=1; assert(pHead); if (ptail == NULL) return 0; while (q != ptail) { counter++; q = q->next; } return counter;}pNode GetEnterNode(pNode pHead, pNode pMeetNode)//给出带环入口点{ pNode ptail = NULL, q = NULL; ptail =pMeetNode; q = pHead; while (q==ptail) { q = q->next; ptail = ptail->next; } return q;}void test(){ pNode list1; pNode list2; pNode pos; pNode q; InitList(&list1); PushBack(&list1,7); //7->NULL PushBack(&list1, 5); //7->5->NULL PushBack(&list1, 3); //7->5->3->NULL PrintList(list1); InitList(&list2); PushBack(&list2, 1); //1->NULL PushBack(&list2, 4); //1->4->NULL PushBack(&list2, 8); //1->4->8->NULL PrintList(list2); BubbleSort(&list1); PrintList(list1); //3->5->7->NULL q=MergeList(list1,list2); PrintList(q); //1->3->4->5->7->8->NULL pos=FindLastKNode(q,2); if (pos == NULL) printf("没有找到合适的值!!!\n"); else printf("%d\n",pos->data); //7 pos=DeleteLastKNode(q, 2); if (pos == NULL) printf("没能删除合适的值!!!\n"); else PrintList(pos); //1->3->4->5->8->NULL }int main(void){ test(); return 0;}
阅读全文
0 0
- 链表操作面试题
- 链表操作面试题
- 链表操作 算法面试题
- 雅虎面试题之链表操作
- 链表常用操作及面试题
- 面试题—链表操作
- 面试题—链表操作2
- 常见面试题之链表操作
- 链表基本操作及面试题
- (程序员面试题)链表的基本操作
- 面试题5: 链表的相关操作
- LeetCode链表操作常见面试题汇总
- Java面试题链表、树和位操作
- 面试题-位操作
- 面试题:位操作
- 链表相关面试题
- 链表相关面试题
- 链表笔试面试题
- Java8 I/O源码-ByteArrayOutputStream
- 字符串算法——最长回文子串
- 工作周记
- 7.MariaDB笔记——cmake使用介绍二
- shell编程--数值运算
- 链表操作面试题
- 数据库连接池c3p0优化开源框架Dbutils的简单介绍
- HTML5新增的8个结构元素
- 8.MariaDB笔记——cmake使用介绍三关于库
- HTTP知识
- 9.MariaDB笔记——cmake使用介绍四安装与测试
- 计算机系统的重装教程
- 字符串笔试题
- 10.MariaDB笔记——cmake使用介绍五系统自带函数