双向链表练手程序

看了《系统程序员成长计划》,开始的时候,要求练习双向链表。以下为自己写的练习程序:

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

typedef int ElemType;	//element type
typedef struct DulNode
{
	ElemType data;
	struct DulNode *prior, *next;
} DulNode, *DuLinkList;

int m = 0;	//about insert

int
Count( DuLinkList L )
{
//count the length of list
	DuLinkList p;
	ElemType count = 0;
	
	p = L->next;
	
	while( p )
	{
		count++;
		p = p->next;
	}
	
	return count;
}

DuLinkList
Find( DuLinkList L, int i )
{
//find situation by i
	DuLinkList p;
	ElemType j = 0, count;
	
	p = L->next;
	count = Count( L );
	
	//deal with the last number in insertion
	
	if( count < i )
	{
		m = 1;
		i = count;
	}
	
	while( p )
	{
		j++;
		if( j == i )
			break;
		p = p->next;
	}
	
	//if the value of i is illegal
	
	if( i <= 0 || j != i )
		return 0;
		
	return p;
}

DuLinkList
Create( DuLinkList L )
{
//establish double link list
	DuLinkList p, q;
	ElemType n, i;
	
	L = ( DuLinkList )malloc( sizeof( DulNode ) );
	L->next = NULL;
	q = L;
	
	printf( "Please input the total number of data.\n" );
	scanf( "%d", &n );
	printf( "Please input data using blank to separate: " );
	
	for( i = 0; i < n; i++ )
	{
		p = ( DuLinkList )malloc( sizeof( DulNode ) );
		scanf( "%d", &p->data );
		p->prior = q;
		q->next = p;
		p->next = NULL;
		q = q->next;
	}
	printf( "\n" );
	
	return L;
}

int
Visit( DuLinkList L )
{
//ergodic double link list
	DuLinkList p;
	
	p = L->next;
	printf( "Double link list is : " );
	
	while( p )
	{
		printf( "%4d", p->data );
		p = p->next;
	}
	printf( "\n" );
	
	return 0;
}

int
ListInsert( DuLinkList L, int i, ElemType e )
{
//Insert double link list
	DuLinkList s, p;
	int count;
	
	p = Find( L, i );
	count = Count( L );
	
	s = ( DuLinkList )malloc( sizeof( DulNode ) );
	s->data = e;
	
	//deal with the last number in insertion
	
	if( m == 0 )
	{
		s->prior = p->prior;
		p->prior->next= s;
		s->next = p;
		p->prior = s;
	}
	else
	{
		s->prior = p;
		p->next = s;
		s->next = NULL;
	}
	return 0;
}

int
Delete( DuLinkList L, int i, ElemType *e )
{
//delete element in list
	DuLinkList p;
	
	p = Find( L, i );
	
	*e = p->data;
	p->prior->next = p->next;
	
	if( p->next )
		p->next->prior = p->prior;
	
	free( p );
	
	return 0;
}

int
Same( DuLinkList L )
{
//check the symmetry of list
	DuLinkList p, tail;
	ElemType i;	//for loop
	ElemType listsize = 0, flag = 1;
	
	p = L->next;
	while( p )
	{
		listsize++;	//get the length of list
		tail = p;
		p = p->next;
	}
	
	p = L->next;
	for( i = 0; i < ( listsize / 2 ); i++ )
	{
		if( p->data != tail->data )
		{
			flag = 0;
			break;
		}
		else
		{
			tail = tail->prior;
			p = p->next;
		}
	}
	
	return flag;
}

int
Jiou( DuLinkList L )
{
//let odd first, even behind
//if you want to realize "even first", that's so easy

	DuLinkList p, q, tail;
	ElemType i = 0, count = 0;
	
	p = L->next;
	while( p )
	{
		count++;
		tail = p;
		p = p->next;
	}
	
	p = L->next;
	while( i < count )
	{
		if( p->data % 2 == 0 )
		{
			//strip node whose data is even, then link the end
			
			q = p;
			p = p->next;
		
			q->prior->next = p;	//strip
			p->prior = q->prior;
			
			q->prior = tail;	//link the end
			tail->next = q;
			q->next = NULL;
			
			tail = q;	//point the end
			i++;
		}
		else
		{	
			q = p;
			p = p->next;
			i++;
		}
	}
	
	return 0;
}

int
Clear( DuLinkList L )
{
	DuLinkList p;
	
	p = L->next;
	while( p )
	{
		L->next = p->next;
		free( p );
		p = L->next;
	}
	
	return 0;
}

int
XInsert( DuLinkList L, ElemType e )
{
//insert element in front of the first element which is bigger than itself
	DuLinkList p, q, s;
	
	p = L->next;
	q = L;
	s = ( DuLinkList )malloc( sizeof( DulNode ) );
	
	while( p )
	{
		if( p->data >= e )
		{
			s->data = e;
			s->prior = q;
			q->next = s;
			s->next = p;
			p->prior = s;
			break;
		}
		q = p;
		p = p->next;
	}
	
	if( !p )	//arrive the end
	{
		s->data = e;
		s->prior = q;
		q->next = s;
		s->next = NULL;
	}
	
	return 0;
}

int
XCreate( DuLinkList L )
{
//Clear original list, then establish new list increasing by degrees
	ElemType i, n, e;
	
	Clear( L );
	
	printf( "Please input the total number of data.\n" );
	scanf( "%d", &n );
	
	printf( "Please input data using blank to separate: " );
	for( i = 0; i < n; i++ )
	{
		scanf( "%d", &e );
		XInsert( L, e );
	}
	
	Visit( L );
	return 0;
}

void
main()
{
	int i, s;	//for situation
	int e;	//for data
	int num;	//for choose
	int count;	//for total number
	char c = 'y';	//judge loop
	DuLinkList L;
	
	L = Create( L );
	Visit( L );
	
	while( c == 'y' )
	{
		printf( "\n\n\n" );
		printf( "\t\t*************************************************\n" );
		printf( "\t\t******** 1.Insert                              **\n" );
		printf( "\t\t******** 2.Delete                              **\n" );
		printf( "\t\t******** 3.Visit                               **\n" );
		printf( "\t\t******** 4.Check symmetry                      **\n" );
		printf( "\t\t******** 5.Insert in sort                      **\n" );
		printf( "\t\t******** 6.The length of list                  **\n" );
		printf( "\t\t******** 7.Odd first, even behind              **\n" );
		printf( "\t\t******** 8.Establish list increasing by degree **\n" );
		printf( "\t\t*************************************************\n" );
		
		printf( "\n\tPlease input your choice(1~8,else exit):[ ]\b\b" );
		scanf( "%d", &num );
		switch( num )
		{
			case 1:
				printf( "\n\nPlease input situation and data"
				"( use blank to separate ): " );
				
				scanf( "%d%d", &i, &e );
				
				ListInsert( L, i, e );
				Visit( L );
				
				break;
			case 2:
				printf( "\n\nPlease input situation: " );
				scanf( "%d", &i );
				
				Delete( L, i, &s );
				printf( "Element deleted is %d\n", s );
				Visit( L );
				
				break;
			case 3:
				Visit( L );
				break;
			case 4:
				printf( "\n\n" );
				Visit( L );
				
				if( Same( L ) )
					printf( "The list is symmetrial\n" );
				else 
					printf( "The list is not symmetrial\n" );
					
				break;
			case 5:
				printf( "\n\nPlease input data: " );
				scanf( "%d", &i );
				
				XInsert( L, i );
				Visit( L );
				break;
			case 6:
				count = Count( L );
				printf( "\n\nThe length of list is %d\n", count );
				break;
			case 7:
				printf( "\n\nLet odd first, even behind\n" );
				
				Jiou( L );
				Visit( L );
				break;
			case 8:
				XCreate( L );
				break;
			default:
				exit( 0 );
				break;
		}
		printf( "Do you want to continue?( y or n ): " );
		scanf( "%s", &c );
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值