纯C语言实现双循环链表

/*编译环境:VC6.0*/
/*
	Name: 纯C语言实现双循环链表
	Copyright: All right Reserved 
	Author:刁肥宅 
	Date: 03/08/18 17:12
	Description: 整合书上的代码 
*/


/*StdAfx.h*/

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__D6DFDF58_B77F_4BBA_9C58_4CAEC45711A2__INCLUDED_)
#define AFX_STDAFX_H__D6DFDF58_B77F_4BBA_9C58_4CAEC45711A2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__D6DFDF58_B77F_4BBA_9C58_4CAEC45711A2__INCLUDED_)



/*DblList.h*/

#ifndef DBLLIST_H_INCLUDED
#define DBLLIST_H_INCLUDED
#include 
#include 
#include 
#include 
#include "stdafx.h"
//#include 
//#define maxSize 30

typedef int DataType;
typedef struct node
{				//双向链表定义
	DataType data;					//结点数据
	/*int freq;*/						//访问计数
	struct node *rLink, *lLink; 	//后继与前驱结点指针
} DblNode, *DblList;

DblList initDblList ( DblList first );/*建立循环双链表的头结点, 并形成空表*/
void createListR ( DblList first, DataType value, int n);/*顺序从A[n]输入数据,使用尾插法沿右链(后继)方向建立循环双链表。每插入一个结
点,需要在前趋和后继方向的两个链中进行链接。要求first已存在并已初始化*/
bool Insert ( DblList first, int i, DataType x, int d );/*建立一个包含有值x的新结点, 
并将其按d指定的方向插入到第i个结点位置*/
DblList Locate ( DblList first, int i, int d );/*在带头结点的循环双链表中按d所指方向寻找第i个结点。
若d = 0, 在前趋方向寻找第i个结点, 若d ≠ 0, 在后继方向寻找第i个结点*/
void printList ( DblList first, int d );/*输出循环双链表所有元素的值,d = 0按前趋方向, d = 1按后继方向*/
bool Remove ( DblList first, int i, DataType *x, int d );/*在带头结点的循环双链表中按照d所指方向
删除第i个结点, 被删元素的值通过引用参数x返回。如果删除成功,函数返回1,否则函数返回0*/
DblList Search ( DblList first, DataType x, int d );/*在带头结点的循环双链表中寻找其值等于x的结点, 
若找到, 则函数返回该结点地址, 否则函数返回NULL。参数d = 0, 按前趋方向查找;d ≠ 0,按后继方向查找.*/
bool EmptyDblList(DblList fisrt);

#endif // DBLLIST_H_INCLUDED


/*StdAfx.cpp*/

// stdafx.cpp : source file that includes just the standard includes
//	test_1.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file



/*test_1.cpp*/

// test_1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "DblList.h"

int main(int argc, char* argv[])
{
	srand(time(NULL));
	DblList DL;
	DataType x;
	int i, j, n , num;
	//int *Len = NULL;
	while(1)
	{
		DL = initDblList ( DL );
		n = rand() % 100 + 1;
		createListR ( DL, num , n); /*这个函数出问题*/

		printList ( DL, 1 );
		printList ( DL, 0 );
	
		x = rand() % 100 + 1;
		i = rand() % 100 + 1;
		Insert ( DL, i, x, 1 );
		printList ( DL, 1 );
		x = rand() % 100 + 1;
		i = rand() % 100 + 1;
		Insert ( DL, i, x, 1 );
		printList ( DL, 1 );
		x = rand() % 100 + 1;
		i = rand() % 100 + 1;
		Insert ( DL, i, x, 1 );
		printList ( DL, 1 );
		printList ( DL, 0 );

		i= rand() % 100 + 1;
		j = Remove ( DL, i, &x, 1 );
		if ( j == 0 )
			printf ( "删除i = %d出错!\n", i );
		else
			printf ( "删除i = %d成功,值为%d!\n", i, x );
		printList ( DL, 1 );
		printList ( DL, 0 );

		i= rand() % 100 + 1;
		j = Remove ( DL, i, &x, 1 );
		if ( j == 0 )
			printf ( "删除i = %d出错!\n", i );
		else
			printf ( "删除i = %d成功,值为%d!\n", i, x );
		printList ( DL, 1 );
		printList ( DL, 0 );

		i = rand() % 100 + 1;
		j = Remove ( DL, i, &x, 1 );
		if ( j == 0 )
			printf ( "删除i = %d出错!\n", i );
		else
			printf ( "删除i = %d成功,值为%d!\n", i, x );
		printList ( DL, 1 );
		printList ( DL, 0 );

		i = rand() % 100 + 1;
		j = Remove ( DL, i, &x, 1 );
		if ( j == 0 )
			printf ( "删除i = %d出错!\n", i );
		else
			printf ( "删除i = %d成功,值为%d!\n", i, x );
		printList ( DL, 1 );
		printList ( DL, 0 );
		Sleep(1000 * 60);
		system("cls");
	}
	return 0;
}

DblList initDblList ( DblList first )
{
/*建立循环双链表的头结点, 并形成空表*/
	first = ( DblNode * ) malloc ( sizeof ( DblNode ) );//建立头结点
	first->rLink = first;
    first->lLink = first;		//初始化
	return first;
}

void createListR ( DblList first, DataType value, int n)
{
/*顺序从A[n]输入数据,使用尾插法沿右链(后继)方向建立循环双链表。每插入一个结
点,需要在前趋和后继方向的两个链中进行链接。要求first已存在并已初始化*/
	srand(time(NULL));
	DblNode *s, *q, *r = first;
	int i;
	printf("n = %d\n",n);
	/*s->freq = 0;*/
	for ( i = 0; i < n; i++ )
    {							
		/*使用尾插法顺序建立链表*/
		s = ( DblNode * ) malloc ( sizeof ( DblNode ) );/*建立新结点*/
		value = rand() % 1000 + 1;
		s->data = value;    /*新结点赋值*/
        /* s->freq = 0;*/					
		q = r->rLink;
		s->lLink = r;
		q->lLink = s;		/*前趋方向链接*/
		r->rLink = s;
		s->rLink = q;
		r = s;			/*后继方向链接*/
		/*s->freq++;*/
	}
	/*
	*Length = s->freq;
	*/
}

void printList ( DblList first, int d )
{
/*输出循环双链表所有元素的值,d = 0按前趋方向, d = 1按后继方向*/
	DblNode *p = ( d == 0 ) ? first->lLink : first->rLink;	/*按前趋/后继方向输出*/
	int Line = 0;
	while ( p != first )
    {								
		/*循链输出*/
		printf ( "%-5d", p->data );
		Line++;
		if(Line % 10 == 0)
			printf("\n");
		p = ( d == 0 ) ? p->lLink : p->rLink;
		/*if ( p != first )
            printf ( " " );*/
	}
	printf ( "\n" );									/*输出收尾*/
}

DblList Search ( DblList first, DataType x, int d )
{
/*在带头结点的循环双链表中寻找其值等于x的结点, 若找到, 则函数返回该结点地
址, 否则函数返回NULL。参数d = 0, 按前趋方向查找;d ≠ 0,按后继方向查找.*/
	DblNode *p = ( d == 0 ) ? first->lLink : first->rLink;
	while ( p != first && p->data != x )
        p = ( d == 0 ) ? p->lLink : p->rLink;
	return ( p != first ) ? p : NULL;					/*返回查找结果*/
}

DblList Locate ( DblList first, int i, int d )
{
/*在带头结点的循环双链表中按d所指方向寻找第i个结点。若d = 0, 在前趋方向
寻找第i个结点, 若d ≠ 0, 在后继方向寻找第i个结点*/
	if ( i < 0 )
        return NULL;							/*i不合理返回NULL*/
	if ( i == 0 )
        return first;							//i = 0定位于头结点
	DblNode *p = ( d == 0 ) ? first->lLink : first->rLink;
	for ( int j = 1; j < i; j++ )						//逐个结点检测
		if ( p == first )
            break;						//链太短退出搜索
		else
            p = (d == 0) ? p->lLink : p->rLink;
	return ( p != first ) ? p : NULL;					//返回查找结果
}

bool Insert ( DblList first, int i, DataType x, int d )
{
/*建立一个包含有值x的新结点, 并将其按d指定的方向插入到第i个结点位置*/
	DblNode *p = Locate( first, i-1, d );				/*定位到第i-1个结点*/
	if ( p == NULL ) return false;						/*i不合理, 插入失败*/
	DblNode *s = (DblNode*)malloc(sizeof(DblNode));		/*创建新结点*/
	s->data = x;
	if ( d == 0 )
    {										/*在前趋方向插入*/
		s->lLink = p->lLink;
        p->lLink = s;
		s->lLink->rLink = s;
		s->rLink = p;
	}
	else
    {												/*在后继方向插入*/
		s->rLink = p->rLink;
        p->rLink = s;
		s->rLink->lLink = s;
		s->lLink = p;
	}
	return true;										/*插入成功*/
};

bool Remove ( DblList first, int i, DataType *x, int d )
{
/*在带头结点的循环双链表中按照d所指方向删除第i个结点, 被删元素的值通过引用参数x
返回。如果删除成功,函数返回1,否则函数返回0*/
	DblNode *p = Locate ( first, i, d );				/*按d所指方向定位于第i个结点*/
	if ( p == NULL )
        return false;						/*空表或i不合理, 删除失败*/
	p->rLink->lLink = p->lLink;							/*从lLink链中摘下*/
	p->lLink->rLink = p->rLink;							/*从rLink链中摘下*/
	*x = p->data;
	free (p); 							/*删除*/
	return true;										/*删除成功*/
};

bool EmptyDblList(DblList first)
{
	if(first->lLink == first && first->rLink == first)
		return true;
	return false;
}

 

你可能感兴趣的:(算法,数据结构)