數據結構-循環鏈表

/*國嵌版的循環*/

#ifndef _CIRCLELIST_H_
#define _CIRCLELIST_H_

typedef void CircleList;
typedef struct _tag_CircleListNode CircleListNode;
struct _tag_CircleListNode
{
    CircleListNode* next;    
};


CircleList* CircleList_Create();

void CircleList_Destroy(CircleList* list);

void CircleList_Clear(CircleList* list);

int CircleList_Empty(CircleList* list);

int CircleList_Length(CircleList* list);

int CircleList_Insert(CircleList* list, CircleListNode* node, int pos);

CircleListNode* CircleList_Get(CircleList* list, int pos);

CircleListNode* CircleList_Delete(CircleList* list, int pos);

CircleListNode* CircleList_DeleteNode(CircleList* list,CircleListNode* node); 

CircleListNode* CircleList_Reset(CircleList* list);

CircleListNode* CircleList_Current(CircleList* list);

CircleListNode* CircleList_Next(CircleList* list);


#endif







<pre name="code" class="cpp">#include <stdio.h>
#include <malloc.h>
#include "CircleList.h"
typedef struct _tag_CircleList
{
	CircleListNode header;
	int length;
	CircleListNode* slider;  //遊標 
} TCircleList; 
CircleList* CircleList_Create()
{
	TCircleList* ret = (TCircleList*)malloc(sizeof(TCircleList));
	if(ret != NULL)
	{
		ret->header.next = NULL;
		ret->length = 0;
		ret->slider = NULL;
	}
	return ret;
}

void CircleList_Destroy(CircleList* list)
{
	free(list);
}

void CircleList_Clear(CircleList* list)
{
	TCircleList* slist = (TCircleList*)list;
	if(slist != NULL)
	{
		slist->header.next = NULL;
		slist->length = 0;	
		slist->slider = NULL; 
	} 
}
int CircleList_Empty(CircleList* list)
{
	TCircleList* slist = (TCircleList*)list;
	int ret = 0;
	if((slist != NULL) && (slist->length == 0) && (slist->slider == NULL))
	{
		ret = 1;
	}
	return ret;
}
int CircleList_Length(CircleList* list)
{
	TCircleList* slist = (TCircleList*)list;
	int ret = -1;
	if(slist != NULL)
	{
		ret = slist->length;	
	}
	return ret;
}

int CircleList_Insert(CircleList* list, CircleListNode* node, int pos)
{
	TCircleList* slist = (TCircleList*)list;
	int i = 0;
	int ret = (slist != NULL) && (0 <= pos);
	
	if(ret)
	{
		CircleListNode* current = (CircleListNode*)slist;
		for(i=0;(i < pos) && (current->next != NULL);i++)
		{
			current = current->next;	
		}
		node->next = current->next;
		current->next = node;
		//判斷是否是在空的時候插入節點 
		if(slist->length == 0)
		{
			slist->slider = node;
			node->next = node;
		}
		slist->length++;
                //重新設置尾結點的指向第0個節點
		if(current == (CircleListNode*)slist)
		{
			CircleListNode* last = CircleList_Get(list,slist->length-1);
			last->next = current->next;	
		}
		
	} 
	return ret;
}

CircleListNode* CircleList_Get(CircleList* list, int pos)
{
	TCircleList* slist = (TCircleList*)list;
	CircleListNode* ret = NULL;
	int i = 0;
	if(0 <= pos)
	{
		CircleListNode* current  = (CircleListNode*)slist;
		for(i=0;i<pos;i++)
		{
			current = current->next;	
		}	
		ret = current->next;
	}
	return ret;
}

CircleListNode* CircleList_Delete(CircleList* list, int pos)
{
	TCircleList* slist = (TCircleList*)list;
	CircleListNode* ret = NULL;
	int i = 0;
	if((slist != NULL) && (0 <= pos)&& (slist->length > 0)) //提高健壯性 
	{
		CircleListNode* current = (CircleListNode*)slist;
		CircleListNode* first = (CircleListNode*)CircleList_Get(list,0);
		CircleListNode* last = (CircleListNode*)CircleList_Get(list,slist->length-1);  //提高效率;
		for(i=0;i<pos;i++)
		{
			current = current->next;
		}	
		ret = current->next;
		current->next = ret->next;
	
		//判斷刪除的是否是第一個元素 
		if(ret == first)
		{ 
			slist->header.next = ret->next;
			last->next = ret->next;	
		}
		slist->length--;
		//判斷刪除的與遊標使用同一個節點 ,slider指向刪除結點的下一個 
		if(slist->slider == ret)
		{
			slist->slider = ret->next;
		}
		//判斷鏈表是否爲空 
		if(slist->length == 0)
		{
			slist->header.next = NULL;	
			slist->slider == NULL;
		}
	}	
	return ret;
}
CircleListNode* CircleList_DeleteNode(CircleList* list,CircleListNode* node)
{
	TCircleList* slist = (TCircleList*)list;
	CircleListNode* ret = NULL;
	int i=0;
	
	if(slist != NULL)
	{
		CircleListNode* current = (CircleListNode*)slist;
		printf("current->next:0x%x, slist->header.next:0x%x",current->next, slist->header.next);
		for(i=0;i<slist->length;i++)
		{
			if(current->next == node)
			{
				ret = current->next;
				break;
			}	
			current = current->next;
		}
		if( ret != NULL)
		{ 		
			CircleList_Delete(list,i);			
		} 
	}
	return ret;	
} 
CircleListNode* CircleList_Reset(CircleList* list)
{
	TCircleList* slist = (TCircleList*)list;
	CircleListNode* ret = NULL;
	if(slist != NULL)
	{
		slist->slider = slist->header.next;
		ret = slist->slider;	
	}	
	return ret;
}
CircleListNode* CircleList_Current(CircleList* list)
{
	TCircleList* slist = (TCircleList*)list;
	CircleListNode* ret = NULL;
	if(slist != NULL)
	{
		ret = slist->slider;	
	}	
	return ret;
}
CircleListNode* CircleList_Next(CircleList* list)
{
	TCircleList* slist = (TCircleList*)list;
	CircleListNode* ret = NULL;
	if((slist != NULL) && (slist->slider != NULL))
	{
		ret = slist->slider;
		slist->slider = ret->next;	
	}	
	return ret;
}

/******************************************
*Function :實現約瑟夫問題 
*
******************************************/
/*****************************************/
#include <stdio.h>
#include <stdlib.h>
#include "CircleList.h"


struct Value
{
    CircleListNode header;
    int v;
};

int main(int argc, char *argv[])
{
    int i = 0;
    CircleList* list = CircleList_Create();
 
    struct Value v1;
    struct Value v2;
    struct Value v3;
    struct Value v4;
    struct Value v5;
    struct Value v6;
    struct Value v7;
    struct Value v8;
    
    v1.v = 1;
    v2.v = 2;
    v3.v = 3;
    v4.v = 4;
    v5.v = 5;
    v6.v = 6;
    v7.v = 7;
    v8.v = 8;
    
    CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));
    
    CircleList_Insert(list, (CircleListNode*)&v5, 5);
//    CircleList_Delete(list, 0);
    
    for(i=0; i<2*CircleList_Length(list); i++)
    {
        struct Value* pv = (struct Value*)CircleList_Get(list, i);
        
        printf("%d\n", pv->v);
    }
    
    printf("\n");
    
    while( CircleList_Length(list) > 0 )
    {
        struct Value* pv = (struct Value*)CircleList_Delete(list, 0);
        
        printf("%d\n", pv->v);
    }
    
    printf("\n");
   
    CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v5, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v6, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v7, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v8, CircleList_Length(list));
    
    for(i=0; i<CircleList_Length(list); i++)
    {
        struct Value* pv = (struct Value*)CircleList_Next(list);
        
        printf("%d\n", pv->v);
    }
    
    printf("\n");
   
    struct Value* pv = (struct Value*)CircleList_Reset(list);     
    printf("%d\n", pv->v);
    printf("length is %d\n",CircleList_Length(list));
    printf("\n");
    
    while( CircleList_Length(list) > 0 )
    {
        struct Value* pv = NULL;
        for(i=1; i<3; i++)
        {
		   printf("enter\n");
           pv = (struct Value*) CircleList_Next(list);
           printf("%d\n",pv->v);
           printf("end\n");
        }
        pv = (struct Value*)CircleList_Current(list); 
        printf("即將刪除的元素%d\n", pv->v);
        pv = (struct Value*)CircleList_DeleteNode(list, (CircleListNode*)pv);
        printf("刪除的元素%d\n", pv->v);
    }
    printf("the list is Empty(%d)\n",CircleList_Empty(list));
    printf("\n");
    CircleList_Destroy(list);
    
	return 0;
}
/********************************************************************/
/******************************************
*Function :實現約瑟夫問題 
*
******************************************
#include <stdio.h>
#include <stdlib.h>
#include "CircleList.h"

typedef struct value
{
	CircleListNode header;
	char c;	
} DataElem;
int main(int argc, char *argv[]) 
{
	printf("約瑟夫問題\n");
	
	DataElem d1,d2,d3,d4,d5,d6,d7,d8;
	d1.c = '1';
	d2.c = '2';
	d3.c = '3';
	d4.c = '4';
	d5.c = '5';
	d6.c = '6';
	d7.c = '7';
	d8.c = '8';
	CircleList_Insert(list,(CircleListNode*)&d1,CircleList_Length(list));
	CircleList_Insert(list,(CircleListNode*)&d2,CircleList_Length(list));
	CircleList_Insert(list,(CircleListNode*)&d3,CircleList_Length(list));
	CircleList_Insert(list,(CircleListNode*)&d4,CircleList_Length(list));
	CircleList_Insert(list,(CircleListNode*)&d5,CircleList_Length(list));
	CircleList_Insert(list,(CircleListNode*)&d6,CircleList_Length(list));
	CircleList_Insert(list,(CircleListNode*)&d7,CircleList_Length(list));
	CircleList_Insert(list,(CircleListNode*)&d8,CircleList_Length(list));
	
	for(i=0;i<CircleList_Length(list);i++)
	{
		DataElem* p  = (DataElem*)CircleList_Current(list);
		CircleList_Next(list); 
		printf("the element is %c\n",p->c);
	}
	printf("the length is %d\n",CircleList_Length(list));	

	while( CircleList_Length(list) > 0 )
    {
        DataElem* p = NULL;
        for(i=1; i<3; i++)
        {
           CircleList_Next(list);
        }      
        p = (DataElem*)CircleList_Current(list);    
        printf("%c\n", p->c);
        CircleList_DeleteNode(list, (CircleListNode*)p);
    }
	printf("\n");

	CircleList_Destroy(list);
	return 0;
}

*************************************************************/

 

#include "stdio.h"    
#include "string.h"
#include "ctype.h"      
#include "stdlib.h"   
#include "io.h"  
#include "math.h"  
#include "time.h"

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

#define MAXSIZE 20 /* 存儲空間初始分配量 */

typedef int Status;/* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */
typedef int ElemType;/* ElemType類型根據實際情況而定,這裏假設爲int */


Status visit(ElemType c)
{
    printf("%d ",c);
    return OK;
}

typedef struct Node
{
    ElemType data;
    struct Node *next;
}Node;
typedef struct Node *LinkList; /* 定義LinkList */

/* 初始化順序線性表 */
Status InitList(LinkList *L) 
{ 
    *L=(Node*)malloc(sizeof(Node)); /* 產生頭結點,並使L指向此頭結點 */
    if((*L) == NULL) /* 存儲分配失敗 */
            return ERROR;
    (*L)->next=NULL; /* 指針域爲空 */

    return OK;
}

/* 初始條件:順序線性表L已存在。操作結果:若L爲空表,則返回TRUE,否則返回FALSE */
Status ListEmpty(LinkList L)
{ 
    if(L->next)
            return FALSE;
    else
            return TRUE;
}

/* 初始條件:順序線性表L已存在。操作結果:將L重置爲空表 */
Status ClearList(LinkList *L)
{ 
	LinkList p,q,start;
	start = (*L)->next;       /*記錄第0個結點*/
	p=(*L)->next;           /*  p指向第0個結點 */
	while(p)                /*  沒到表尾 */
	{
		q=p->next;
		free(p);
		if(q == start)
		{
			break;	
		}
		p=q;
	}
	(*L)->next=NULL;        /* 頭結點指針域爲空 */
	return OK;
}

/* 初始條件:順序線性表L已存在。操作結果:返回L中數據元素個數 */
int ListLength(LinkList L)
{
    int i=0;
    Node* start = L->next;       /*記錄第0個結點*/
    Node* p=L->next; /* p指向第0個結點 */
    while(p)                        
    {
        i++;
        p=p->next;
        if(p == start)
        {
			break;	
		}
    }
    return i;
}

/* 初始條件:順序線性表L已存在,0≤i<ListLength(L) */
/* 操作結果:用e返回L中第i個數據元素的值 */
Status GetElem(LinkList L,int pos,ElemType *e)
{
	int i;
	if(0 <= pos && pos<ListLength(L))
	{
		Node *current =  L->next;  /*指向第0個結點*/
		for(i=0; i< pos && current->next != NULL ;i++)
		{
			current = current->next;
		}
		*e = current->data;
		return OK;

	}
	return ERROR;
}

/* 初始條件:順序線性表L已存在 */
/* 操作結果:返回L中第1個與e滿足關係的數據元素的位序。 */
/* 若這樣的數據元素不存在,則返回值爲-1*/
int LocateElem(LinkList L,ElemType e)
{
    int i=0;
   Node *current =  L->next;
    while(current)
    {
        if(current->data==e) /* 找到這樣的數據元素 */
                return i;
        current=current->next;
	     i++;
    }
    return -1;
}


/* 初始條件:順序線性表L已存在,0≤i<ListLength(L), */
/* 操作結果:在L中第pos個位置之前插入新的數據元素e,L的長度加1 */
Status ListInsert(LinkList *L,int pos,ElemType e)
{ 
	int i;
	int length = ListLength(*L);
	Node *tmp = *L;
//	printf("length:%d\n", length);
	if(0 <= pos && L!=NULL)
	{
		Node *current =  *L;  /*current是頭結點,指向第0個節點*/
		if(pos>=ListLength(*L))
		{
			pos = ListLength(*L);
		}
		for(i=0; i<ListLength(*L);i++)
		{
			tmp = tmp->next;
		}
		Node *end = tmp;
		
		for(i=0; i< pos && current->next != NULL ;i++)
		{
			current = current->next;
		}
//		printf("current->next:0x%x\n", current->next);
		Node *Next = current->next;
		Node *s = (Node*)malloc(sizeof(Node)); 
//		printf("s:0x%x\n", s);	
		s->data  = e;
		s->next = Next;
		current->next = s;
		end->next = s;
//		printf("OVER:currentt:0x%x\n", current);
//		printf("OVER:current->next:0x%x\n", current->next);
		if(length == 0)
		{
			s->next = s;	
		}
		return OK;
	}
	return ERROR;
}

/* 初始條件:順序線性表L已存在,1≤i≤ListLength(L) */
/* 操作結果:刪除L的第i個數據元素,並用e返回其值,L的長度減1 */
Status ListDelete(LinkList *L,int pos,ElemType *e) 
{ 
	int i;
	if(0 <= pos && pos < ListLength(*L)&& L!=NULL && e != NULL)
	{
		Node *current =  *L;  /*current是頭結點,指向第0個節點*/
		Node *tmp = *L;
		Node *start = (*L)->next;
		
		for(i=0; i<ListLength(*L);i++)
		{
			tmp = tmp->next;
		}
		Node *end = tmp;
		
		for(i=0; i< pos && current->next != NULL ;i++)
		{
			current = current->next;
		}
		Node *Next = current->next;
		*e = Next->data;
		current->next = Next->next;
		//判斷刪除的是否是第一個元素   
		if(start == Next)
		{
			end->next 	= Next->next;			
		}
		free(Next);
		
		return OK;
	}
	return ERROR;
}

/* 初始條件:順序線性表L已存在 */
/* 操作結果:依次對L的每個數據元素輸出 */
Status ListTraverse(LinkList L)
{
	int i=0;
    LinkList p=L->next;
    while(p && i<ListLength(L))
    {
        visit(p->data);
        p=p->next;
	 i++;
    }
    printf("\n");
    return OK;
}
#if 0
/*  隨機產生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 = NULL;                       /* 表示當前鏈表結束 */
}
#endif


int main()
{        
    LinkList L;
    ElemType e;
    Status i;
    int j,k;
    i=InitList(&L);
    printf("初始化L後:ListLength(L)=%d\n",ListLength(L));
	for(i=0;i<5;i++)
    	ListInsert(&L,0,i);

    
   //printf("\n");
   ListTraverse(L);

    printf("ListLength(L)=%d \n",ListLength(L));
    i=ListEmpty(L);
    printf("L是否空:i=%d(1:是 0:否)\n",i);

    i=ClearList(&L);
    printf("清空L後:ListLength(L)=%d\n",ListLength(L));
    i=ListEmpty(L);
    printf("L是否空:i=%d(1:是 0:否)\n",i);

    for(j=1;j<=10;j++)
            ListInsert(&L,j-1,j);
    printf("在L的表尾依次插入1~10後:L.data=");
    ListTraverse(L); 

    printf("ListLength(L)=%d \n",ListLength(L));

    ListInsert(&L,0,0);
    printf("在L的表頭插入0後:L.data=");
    ListTraverse(L); 
    printf("ListLength(L)=%d \n",ListLength(L));

    GetElem(L,5,&e);
    printf("第5個元素的值爲:%d\n",e);
    for(j=3;j<=4;j++)
    {
            k=LocateElem(L,j);
            if(k)
                    printf("第%d個元素的值爲%d\n",k,j);
            else
                    printf("沒有值爲%d的元素\n",j);
    }
    

    k=ListLength(L); /* k爲表長 */
    for(j=k+1;j>=k;j--)
    {
            i=ListDelete(&L,j,&e); /* 刪除第j個數據 */
            if(i==ERROR)
                    printf("刪除第%d個數據失敗\n",j);
            else
                    printf("刪除第%d個的元素值爲:%d\n",j,e);
    }
    printf("依次輸出L的元素:");
    ListTraverse(L); 

    j=5;
    ListDelete(&L,j,&e); /* 刪除第5個數據 */
    printf("刪除第%d個的元素值爲:%d\n",j,e);

    printf("依次輸出L的元素:");
    ListTraverse(L); 
    i=ClearList(&L);
    printf("\n清空L後:ListLength(L)=%d\n",ListLength(L));
#if 0 
    CreateListHead(&L,20);
    printf("整體創建L的元素(頭插法):");
    ListTraverse(L); 
   
    i=ClearList(&L);
    printf("\n刪除L後:ListLength(L)=%d\n",ListLength(L));
    CreateListTail(&L,20);
    printf("整體創建L的元素(尾插法):");
    ListTraverse(L); 
#endif

    return 0;
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章