雙向循環鏈表

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct Link
{
	int data;
	struct Link *llink;
	struct Link *rlink;
}Node,*pNode;


//創建結點
pNode CreateNode(int data)
{
	pNode p;
	p = (pNode)malloc(sizeof(Node));

	if ( !p )
	{
		printf("內存分配失敗......");
		exit(-1);
	}

	p->data  = data;
	p->llink = p->rlink = p; //創建新結點時,讓其前驅和後繼指針都指向自身

	return p;
} 


//創建鏈表
pNode CreateList(int data)
{
	pNode p;
	p = (pNode)malloc(sizeof(Node));

	if ( !p )
	{
		exit(-1);
	}

	p->data = data;
	p->llink = p->rlink = p;

	return p;
}


//插入新結點,總是在表尾插入; 返回表頭結點
pNode InsertNode(pNode node,int data)
{
	pNode p;
	p = CreateNode(data);

	 // 從左到右恢復鏈接
	node->llink->rlink = p;
	p->rlink = node;

	// 從右到左恢復鏈接
	p->llink = node->llink;
	node->llink = p;

	return node;
}


//查找結點,成功則返回滿足條件的結點指針,否則返回NULL
pNode FindNode(pNode node,int data)
{
	pNode p = node->rlink;

	while ( p != node && p->data != data )
	{
		p = p->rlink;
	}
	if ( p == node )
	{
		return NULL;
	}
	return p;

}

pLink DeleteNode(pLink node,int data)
{
	pLink p = FindNode(node,data);
	if ( p )
	{
		p->llink->rlink = p->rlink;
		p->rlink->llink = p->llink;
		free(p);

		return node;
	}
	else
	{
		return NULL;
	}
}

int GetLength(pLink node)
{
	int count=0;
	pLink p = node->rlink;

	while ( p != node )
	{
		p = p->rlink;
		count++;
	}
	return count;
}

void DisplayList(pLink node)
{
	pLink p;
	if ( NULL == node )
	{
		return NULL;
	}
	p = node->rlink;
	while ( p != node )
	{
		printf("%4d",p->data);
		p = p->rlink;
	}
	printf("\n");

}


void DisplayList1(pLink node)
{
	pLink p;
	if ( NULL == node )
	{
		return NULL;
	}
	p = node->llink;
	while ( p != node )
	{
		printf("%4d",p->data);
		p = p->llink;
	}
	printf("\n");

}


void DestoryList(pLink node)
{
	pLink p,ptr;

	if ( NULL == node )
	{
		return NULL;
	}
	p = node->rlink;

	while ( p != node )
	{
		ptr = p->rlink;
		free(p);
		p = ptr;
	}
	free(node);
}

int main(void)
{
	int choose,i;
	int data,n;
	int r,k=1,r1;
	pLink p;
	pLink list = CreateList(0);

	while ( k == 1 )
	{
		printf("主菜單:\n");
		printf("1. 插入元素\n");
		printf("2. 刪除元素\n");
		printf("3. 查找元素\n");
		printf("4. 鏈表長度\n");
		printf("5. 正向打印\n");
		printf("6. 逆向打印\n");
		printf("7. 刪除鏈表\n");
		printf("0. 退出\n");
		
		r = scanf("%d",&choose);
		flushall();
		if ( r == 1 )
		{
			switch(choose)
			{
			case 1:

				do{
					printf("輸入要插入的數字個數:");
					r1 = scanf("%d",&n);
					flushall();
					if ( r1 == 1 )
					{
						for ( i=0 ; i<n ; i++)
						{
							do{
								printf("第%d個數: ",i+1);
								r1 = scanf("%d",&data);
								flushall();
								if ( r1 == 1 )
								{						
									list = InsertList(list,data);
								}
							}while ( !(r1 == 1) );
						}
						DisplayList(list);	
					}
				}while ( !( r1 == 1 ) );
				printf("\n");
				break;

			case 2:
				printf("輸入要刪除的數字:");
				scanf("%d",&data);
				DeleteNode(list,data);
				DisplayList(list);
				printf("\n");
				break;
			
			case 3:
				printf("輸入要查找的數字:");
				scanf("%d",&data);
				p = FindNode(list,data);
				if ( NULL != p )
				{
					printf("查找成功!\n");
				}
				else
				{
					printf("查找失敗!\n");
				}
				printf("\n");
				break;
				
			case 4:
				printf("鏈表長度爲:%d\n",GetLength(list));
				printf("\n");
				break;

			case 5:
				DisplayList(list);
				printf("\n");
				break;

			case 6:
				DisplayList1(list);
				printf("\n");
				break;

			case 7:
				DestoryList(list);
				printf("\n");
				break;

			case 0:
				DestoryList(list);
				k = 0;
				break;
			}
		}
	}



	getch();
	return 0;
}


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