查找单项链表中间元素,若有相同,取第一个
生活随笔
收集整理的這篇文章主要介紹了
查找单项链表中间元素,若有相同,取第一个
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdio.h> #include <stdlib.h>typedef struct Lnode{int data;struct Lnode *next;
}Lnode, *LinkList;//創建一個起始元素為start,終止元素為end的鏈表
Lnode *CreateList_L(int start, int end){Lnode *L, *p, *rear;int i;L = (LinkList) malloc(sizeof(Lnode));L->next = NULL;//尾插法rear = L;for(i = start; i <= end; i++){p = (LinkList) malloc(sizeof(Lnode));p->data = i;rear->next = p;rear = p;}rear->next = NULL;return L;
}Lnode *FindMid_L(Lnode *L){Lnode *pSlow, *pFast;pSlow = pFast = L;//必須判斷pFast 和pFast->next,因為當pFast指向鏈表最后一個元素時,//不判斷pFast->next,則執行循環語句pFast->next->next內存溢出while(pFast && pFast->next){pSlow = pSlow->next;pFast = pFast->next->next;}return pSlow;
}main()
{Lnode *L, *p;//創建鏈表L = CreateList_L(1, 101);p = FindMid_L(L);printf("%d\n",p->data);
}
?
轉載于:https://www.cnblogs.com/wannianma/archive/2013/04/06/3002363.html
總結
以上是生活随笔為你收集整理的查找单项链表中间元素,若有相同,取第一个的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习使用Bing Maps Silver
- 下一篇: 正确获取硬盘序列号源码