数据结构:假设有一个带头结点的单链表L,每个结点值由单个数字、小写字母和大写字母构成。设计一个算法将其拆分成3个带头结点的单链表L1、L2和L3,L1包含L中的所有数字结点,L2包含L中的所有小写字母


假设有一个带头结点的单链表L,每个结点值由单个数字、小写字母和大写字母构成。设计一个算法将其拆分成3个带头结点的单链表L1、L2和L3,L1包含L中的所有数字结点,L2包含L中的所有小写字母结点,L3包含L中的所有大写字母结点。


#include<stdio.h>
#include<malloc.h>

typedef char ElemType;
typedef struct LNode
{
	ElemType data;
	struct LNode *next;
}LN;
void CreatListR(LN *&L, ElemType a[], int n)
{
	LN *s, *r;
	L = (LN *)malloc(sizeof(LN));
	r = L;
	for (int i = 0;i < n;i++)
	{
		s = (LN *)malloc(sizeof(LN));
		s->data = a[i];
		r->next = s;
		r = s;
	}
	r->next = NULL;
}
void resultlist(LN *L, LN *&L1, LN *&L2, LN *&L3)
{
	LN *p = L->next, *a, *b, *c,*m,*n,*q;
	L1 = (LN *)malloc(sizeof(LN));
	L2 = (LN *)malloc(sizeof(LN));
	L3 = (LN *)malloc(sizeof(LN));
	a = L1;
	b = L2;
	c = L3;
	while (p != NULL)
	{
		if (p->data >= '0' && p->data <= '9')
		{
			m = (LN *)malloc(sizeof(LN));
			m->data = p->data;
			a->next = m;
			a = m;
			
		}
		else if (p->data >= 'a' &&p->data <= 'z')
		{
			n = (LN *)malloc(sizeof(LN));
			n->data = p->data;
			b->next = n;
			b = n;
			
		}
		else if (p->data >= 'A' &&p->data <= 'Z')
		{
			q = (LN *)malloc(sizeof(LN));
			q->data = p->data;
			c->next = q;
			c = q;
			
		}
		else
		{	printf("错误");
		}
		p = p->next;
	}
	a->next = NULL;
	b->next = NULL;
	c->next = NULL;
}
void DispList1(LN *L)
{
	LN *p = L->next;
	while (p != NULL)
	{
		printf(" %c ", p->data);
		p = p->next;
	}
	printf("\n");
}

void DestroyList(LN*L)
{
	LN*pre = L, *p = L->next;
	while (p != NULL)
	{
		free(pre);
		pre = p;
		p = pre->next;;
	}
	free(pre);
}
int main()
{
	LN*L, *L1, *L2, *L3;
 char a[]={'a','b','1','2','3','C','D','5'};
  CreatListR(L, a,8  );
  resultlist(L,L1,L2, L3);
  DispList1(L);
  DispList1(L1);
  DispList1(L2);
  DispList1(L3);
  DestroyList(L);
  DestroyList(L1);
  DestroyList(L2);
  DestroyList(L3);
 return 0;
}

主要遍历整个链表,不同分类的分到不同的列表中,对号入座。 

void resultlist(LN *L, LN *&L1, LN *&L2, LN *&L3)
{
	LN *p = L->next, *a, *b, *c,*m,*n,*q;
	L1 = (LN *)malloc(sizeof(LN));
	L2 = (LN *)malloc(sizeof(LN));
	L3 = (LN *)malloc(sizeof(LN));
	a = L1;
	b = L2;
	c = L3;
	while (p != NULL)
	{
		if (p->data >= '0' && p->data <= '9')
		{
			m = (LN *)malloc(sizeof(LN));
			m->data = p->data;
			a->next = m;
			a = m;
			
		}
		else if (p->data >= 'a' &&p->data <= 'z')
		{
			n = (LN *)malloc(sizeof(LN));
			n->data = p->data;
			b->next = n;
			b = n;
			
		}
		else if (p->data >= 'A' &&p->data <= 'Z')
		{
			q = (LN *)malloc(sizeof(LN));
			q->data = p->data;
			c->next = q;
			c = q;
			
		}
		else
		{	printf("错误");
		}
		p = p->next;
	}
	a->next = NULL;
	b->next = NULL;
	c->next = NULL;
}

 

结束之后最好将用到的空间释放。 

void DestroyList(LN*L)
{
	LN*pre = L, *p = L->next;
	while (p != NULL)
	{
		free(pre);
		pre = p;
		p = pre->next;;
	}
	free(pre);
}

 

在使用main函数来调用,最终得到结果 

int main()
{
	LN*L, *L1, *L2, *L3;
 char a[]={'a','b','1','2','3','C','D','5'};
  CreatListR(L, a,8  );
  resultlist(L,L1,L2, L3);
  DispList1(L);
  DispList1(L1);
  DispList1(L2);
  DispList1(L3);
  DestroyList(L);
  DestroyList(L1);
  DestroyList(L2);
  DestroyList(L3);
 return 0;
}

 

这是我得到的结果 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值