1. 程式人生 > >002 -- Circle LinkList 2 -- connect 2 circle link lists

002 -- Circle LinkList 2 -- connect 2 circle link lists

++ eof lis length chan all sca only cif

技術分享

The operation fullfilled codes:

// A, B is the rear for 2 LinkList with nodes 

LinkList Connect(LinkList A,LinkList B)
{
    p = A->next; // use p to store A list head 
    A->next = B->next->next; // A change to point to b1 as the next. 
    free(B->next); // release the head of B list -- this is very important 
B->next = p; // Now B as the last node for the new List, points to the head return B; }

001-- To check if a list has a circle

技術分享

solution1 -- pointer p,q ; p continue move forward, and q will start from head for every rund . If for a specific node, p and q takes diffirent amount steps, then there is a circle. As above, for p, from Node6 -> Node3, the steps for p is: 1->2->3->4->5->6->3, total 6 steps. But for q, start from head, only 2 steps: 1->2->3

Solution2 -- pointer p, q, p move 1 step but q move 2 steps every time. If there is a senario, p = q, then there is a circle.

#include "stdio.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Status;/* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */
typedef int ElemType;/* ElemType類型根據實際情況而定,這裏假設為int 
*/ typedef struct Node { ElemType data; struct Node *next; }Node, *LinkList; /* 初始化帶頭結點的空鏈表 */ Status InitList(LinkList *L) { *L = (LinkList)malloc(sizeof(Node)); /* 產生頭結點,並使L指向此頭結點 */ if(!(*L)) /* 存儲分配失敗 */ return ERROR; (*L)->next=NULL; /* 指針域為空 */ return OK; } /* 初始條件:順序線性表L已存在。操作結果:返回L中數據元素個數 */ int ListLength(LinkList L) { int i=0; LinkList p=L->next; /* p指向第一個結點 */ while(p) { i++; p=p->next; } return i; } /* 隨機產生n個元素的值,建立帶表頭結點的單鏈線性表L(頭插法) */ void CreateListHead(LinkList *L, int n) { LinkList p; int i; srand(time(0)); /* 初始化隨機數種子 */ *L = (LinkList)malloc(sizeof(Node)); (*L)->next = NULL; /* 建立一個帶頭結點的單鏈表 */ for (i=0; i < n; i++) { p = (LinkList)malloc(sizeof(Node)); /* 生成新結點 */ p->data = rand()%100+1; /* 隨機生成100以內的數字 */ p->next = (*L)->next; (*L)->next = p; /* 插入到表頭 */ } } /* 隨機產生n個元素的值,建立帶表頭結點的單鏈線性表L(尾插法) */ void CreateListTail(LinkList *L, int n) { LinkList p,r; int i; srand(time(0)); /* 初始化隨機數種子 */ *L = (LinkList)malloc(sizeof(Node)); /* L為整個線性表 */ r = *L; /* r為指向尾部的結點 */ for (i=0; i < n; i++) { p = (Node *)malloc(sizeof(Node)); /* 生成新結點 */ p->data = rand()%100+1; /* 隨機生成100以內的數字 */ r->next=p; /* 將表尾終端結點的指針指向新結點 */ r = p; /* 將當前的新結點定義為表尾終端結點 */ } r->next = (*L)->next->next; } // 比較步數的方法 int HasLoop1(LinkList L) { LinkList cur1 = L; // 定義結點 cur1 int pos1 = 0; // cur1 的步數 while(cur1) { // cur1 結點存在 LinkList cur2 = L; // 定義結點 cur2 int pos2 = 0; // cur2 的步數 while(cur2) { // cur2 結點不為空 if(cur2 == cur1) { // 當cur1與cur2到達相同結點時 if(pos1 == pos2) // 走過的步數一樣 break; // 說明沒有環 else // 否則 { printf("環的位置在第%d個結點處。\n\n", pos2); return 1; // 有環並返回1 } } cur2 = cur2->next; // 如果沒發現環,繼續下一個結點 pos2++; // cur2 步數自增 } cur1 = cur1->next; // cur1繼續向後一個結點 pos1++; // cur1 步數自增 } return 0; } // 利用快慢指針的方法 int HasLoop2(LinkList L) { int step1 = 1; int step2 = 2; LinkList p = L; LinkList q = L; while (p != NULL && q != NULL && q->next != NULL) { p = p->next; if (q->next != NULL) q = q->next->next; printf("p:%d, q:%d \n", p->data, q->data); if (p == q) return 1; } return 0; } int main() { LinkList L; Status i; char opp; ElemType e; int find; int tmp; i = InitList(&L); printf("初始化L後:ListLength(L)=%d\n",ListLength(L)); printf("\n1.創建有環鏈表(尾插法) \n2.創建無環鏈表(頭插法) \n3.判斷鏈表是否有環 \n0.退出 \n\n請選擇你的操作:\n"); while(opp != 0) { scanf("%c",&opp); switch(opp) { case 1: CreateListTail(&L, 10); printf("成功創建有環L(尾插法)\n"); printf("\n"); break; case 2: CreateListHead(&L, 10); printf("成功創建無環L(頭插法)\n"); printf("\n"); break; case 3: printf("方法一: \n\n"); if( HasLoop1(L) ) { printf("結論:鏈表有環\n\n\n"); } else { printf("結論:鏈表無環\n\n\n"); } printf("方法二:\n\n"); if( HasLoop2(L) ) { printf("結論:鏈表有環\n\n\n"); } else { printf("結論:鏈表無環\n\n\n"); } printf("\n"); break; case 0: exit(0); } } }

002 -- Circle LinkList 2 -- connect 2 circle link lists