c语言链表面试题
2017-06-28 19:32
399 查看
链表是c语言中颇为重要的简单数据结构,在面试中常常被面试官所考到,接下来我就为大家提供一些经常面试中常常被问到面试题的源码。
函数声明:
typedef int DataType; typedef struct Node { DataType data; struct Node* next; }Node,*pNode,*pList; void InitList(pList *pplist); void Display(const pList plist); void PushBack(pList *pplist,DataType d); void PopBack(pList *pplist); void PushFront(pList *pplist,DataType d); void PopFront(pList *pplist); void DistroyList(pList *pplist); pNode Find(pList *pplist,DataType d); void Insert(pList *pplist,pNode pos,DataType d); void Erase(pList *pplist,pNode pos); void Remove(pList *pplist,DataType d); void RemoveAll(pList *pplist,DataType d); void BubbleSort(pList *pplist); void Reverse(pList *pplist); //逆序 void EraseNotTail(pList *pplist,pNode pos); //删除无头单链表的非尾节点 void ReversePrint(pList plist); //逆序打印 void InsertFrontNode(pNode pos, DataType x);//在无头单链表的非头结点前插入一个元素 void JosephCycle(pList* pplist, int k); //约瑟夫环问题 pList Merge(pList* p1, pList* p2); //合并两个有序列表 pNode FindMidNode(pList plist); //查找单链表的中间节点,要求只能遍历一次链表 pNode FindKNode(pList plist, int k); //查找单链表的倒数第k个节点,要求只能遍历一次链表 pNode CheckCircle(pList plist);//判断链表时候带环 int GetCircleLength(pNode meet); //求环的长度 pNode GetCycleEntryNode(pList plist, pNode meet); //求环的入口点 int CheckCross(pList list1, pList list2); //判断两条单项链表时候相交 pNode GetCrossNode(pList list1, pList list2);
函数实现:
void InitList(pList *pplist) { assert(pplist); *pplist=NULL; } void Display(const pList plist) { pNode cur = plist; while (cur) { printf("%d-->",cur->data); cur=cur->next; } printf("NULL\n"); } pNode BuyNode(DataType d) { pNode newNode = (pNode)malloc(sizeof(Node)); newNode->data=d; newNode->next=NULL; return newNode; } void PushBack(pList *pplist,DataType d) { pNode newNode = BuyNode(d); pNode cur = *pplist; assert(pplist); if (*pplist==NULL) { *pplist=newNode; return; } while (cur->next!=NULL) { cur=cur->next; } cur->next=newNode; } void PopBack(pList *pplist) { pNode cur = *pplist; assert(pplist); if (*pplist==NULL) { return; } if (cur->next==NULL) { free(cur); *pplist=NULL; return; } while (cur->next->next!=NULL) { cur=cur->next; } free(cur->next); cur->next=NULL; } void PushFront(pList *pplist,DataType d) { pNode newNode=BuyNode(d); assert(pplist); newNode->next=*pplist; *pplist=newNode; } void PopFront(pList *pplist) { pNode cur = *pplist; assert(pplist); if (*pplist==NULL) { return; } *pplist=(*pplist)->next; free(cur); cur=NULL; } void DistroyList(pList *pplist) { pNode cur = *pplist; pNode del = NULL; assert(pplist); while (cur) { del = cur; cur=cur->next; free(del); del=NULL; } *pplist=NULL; } pNode Find(pList *pplist,DataType d) { pNode cur = *pplist; assert(pplist); while (cur) { if (cur->data==d) { return cur; } else { cur=cur->next; } } return NULL; } void Insert(pList *pplist,pNode pos,DataType d) { pNode newNode = BuyNode(d); assert(pplist); assert(pos); if (*pplist==NULL) { PushBack(pplist,d); return; } newNode->next=pos->next; pos->next=newNode; } void Erase(pList *pplist,pNode pos) { pNode cur = *pplist; assert(pplist&&pos); while (cur->next!=pos&&cur->next!=NULL) { cur=cur->next; } if (cur->next==NULL) { return; } cur->next=pos->next; free(pos); pos=NULL; } void Remove(pList *pplist,DataType d) { pNode pos = NULL; assert(pplist); pos = Find(pplist,d); Erase(pplist,pos); } void RemoveAll(pList *pplist,DataType d) { assert(pplist); while ((Find(pplist,d)!=NULL)) { Erase(pplist,Find(pplist,d)); } } void BubbleSort(pList *pplist) { DataType tmp = 0; pNode first = NULL; pNode second = NULL; pNode end = NULL; d629 assert(pplist); if(NULL==*pplist) { return ; } while (end!=*pplist) { first = *pplist; second = first->next; while (second!=end) { if (first->data>second->data) { tmp=first->data; first->data=second->data; second->data=tmp; } first=first->next; second=second->next; } end=first; } } void Reverse(pList* pplist) { assert(pplist); if (*pplist==NULL) { return; } else { pNode tail = *pplist; pNode cur = *pplist; pNode _next = cur->next; pNode tmp = NULL; while (_next!=NULL) { tmp = _next->next; _next->next=cur; cur = _next; _next = tmp; } tail->next = NULL; *pplist = cur; } } void EraseNotTail(pList *pplist,pNode pos) { pNode cur = NULL; pNode del = NULL; assert(pplist); if (*pplist==NULL) { return; } pos->data = pos->next->data; del = pos->next; cur = pos->next->next; free(del); pos->next = cur; } void ReversePrint(pList plist) { if(plist!=NULL) { if (plist->next!=NULL) { ReversePrint(plist->next); } printf("%d-->",plist->data); } } void InsertFrontNode(pNode pos, DataType x) { pNode newnode = BuyNode(x); DataType tmp = 0; newnode->next = pos->next; pos->next = newnode; tmp = pos->data; pos->data = newnode->data; newnode->data = tmp; } void JosephCycle(pList* pplist, int k) { pNode del = NULL; pNode cur = NULL; int i = 0; assert(pplist); cur = *pplist; while (cur->next!=cur) { for (i = 0;i<k-1;i++) { cur = cur->next; } del = cur->next; printf("%d",cur->data); cur->data = cur->next->data; cur->next = cur->next->next; free(del); del = NULL; } } pList Merge(pList* p1, pList* p2) { pList newlist; pNode P1 = *p1; pNode P2 = *p2; pNode tail = NULL; if ((*p1==NULL)||(*p2==NULL)) { return NULL; } if (P1->data<P2->data) { newlist = P1; P1 = P1->next; } else { newlist = P2; P2 = P2->next; } tail = newlist; while (P1&&P2) { if (P1->data<P2->data) { tail->next = P1; P1 = P1->next; } else { tail->next = P2; P2 = P2->next; } tail = tail->next; } if (P1==NULL) { tail->next = P2; } else { tail->next = P1; } return newlist; } pNode FindMidNode(pList plist) { pNode slow = NULL; pNode fast = NULL; slow = plist; fast = plist; while (fast->next!=NULL) { slow = slow->next; fast = fast->next->next; } return slow; } pNode FindKNode(pList plist, int k) { int i = 0; pNode fast = plist; pNode slow = plist; for (i=0;i<k-1;i++) { if (fast!=NULL) { fast = fast->next; } else { return NULL; } } while (fast->next!=NULL) { fast = fast->next; slow = slow->next; } return slow; } pNode CheckCircle(pList plist) { pNode slow = NULL; pNode fast = NULL; fast = plist; slow = plist; while (1) { fast=fast->next->next; slow=slow->next; if (fast->next==NULL) { return NULL; } if (fast==slow) { return fast; } } } int GetCircleLength(pNode meet) { pNode cur = meet; int count = 1; while(cur->next!=meet) { count++; cur = cur->next; } return count; } pNode GetCycleEntryNode(pList plist, pNode meet) { pNode cur = plist; pNode tail = meet; while (cur!=tail) { cur=cur->next; tail = tail->next; } return cur; } int CheckCross(pList list1, pList list2) { pNode cur = list1; pNode tail = list2; while (cur->next!=NULL) { cur = cur->next; } while (tail->next!=NULL) { tail = tail->next; } if (cur==tail) { return 1; } else { return 0; } } pNode GetCrossNode(pList list1, pList list2) { pNode tmp = NULL; pNode cur = list1; pNode meet = NULL; while (cur->next!=NULL) { cur = cur->next; } cur->next=list1; meet = CheckCircle(list2); tmp = GetCycleEntryNode(list2,meet); }
相关文章推荐
- 常见C语言面试题之九:链表逆序
- C语言实现单链表的基本操作及其部分面试题
- 经典面试题---单链表的基本操作(C语言实现)
- C语言—链表面试题之带环问题的总结
- 【数据结构】C语言单链表面试题
- C语言实现单链表-面试题(基础篇)
- C语言实现单链表的基本操作及其部分面试题
- C语言中的单链表面试题----基础
- C语言的单链表面试题----进阶
- C语言实现单链表常见面试题
- 单链表的实现和相关面试题及其详解(C语言)
- C语言实现单链表以及链表对应的面试题
- c语言实现链表面试题
- C语言单链表面试题(进阶)
- 链表面试题——C语言
- C语言 — 链表面试题复杂链表问题
- 【C语言】单链表的相关热点面试题(包括:从尾到头打印,逆置,冒泡,寻找中间节点,倒数k节点)
- C语言单链表面试题1
- C语言单链表面试题2
- 自杀环---约瑟夫环(单链表经典面试题)------>C语言实现