问题 F: 实验11_15_拆分链表

题目描述

已知有一个乱序的字符序列L,序列中的字符可能是英文字母、数字字符或其它字符,字符的个数未知,每个字符之间用空格分开。字符序列用“-1”作为输入结束标志,这里你要把-1当做一个字符串对待,并且不算作字符序列中的元素。如下即为一个合法的字符序列:“a c 3 b a d 6 , & j m 8 7 2 V -1”。你的任务是将这个字符序列拆分为三个独立的序列A、B和C,其中序列A存放序列L中的字母,序列B存放序列L中的数字,序列C存放序列L中的其他字符,然后,将序列A、B和C分别按照ASCII码的大小关系进行升序排序。最终序列L将变为空序列。
要求:
建立四个单链表,分别存储序列L、A、B、C中的元素。字符序列的输入用“-1”作为结束标志。建立链表L时,建议使用scanf(“%s”,s);来读取字符序列中的字符,即把单独的字符看做一个字符串读取。当L建立后,你要按照问题描述中所述,将L拆分为A、B、C三个链表,然后对每个链表都进行排序,这部分的操作都应该是对指针进行修改,而不是删除节点与建立新节点。在程序结束前要释放链表A、B、C中的所有节点。

输入

一个乱序的字符序列,序列元素的个数未知,以输入“-1”结束,输入“-1”前可能没有其它元素,每个字符序列占一行。

输出

链表A中的元素,占一行;然后是链表B中的元素,占一行。最后是链表C中的元素,占一行。每行的每个元素后有一个空格,注意最后一个元素后只有换行符,如果某个链表为空则,则输出“There is no item in X list.”
数据最多的测试用例节点数在100这个数量级。
请注意输入输出格式。

样例输入 
Sample 1:
a c 3 b a d 6 , & j m 8 7 2 V -1

Sample 2:
z m v 1 a K 2 m p 9 a 0 a d -1
样例输出 
Sample 1:
The list A is: V a a b c d j m
The list B is: 2 3 6 7 8
The list C is: & ,

Sample 2:
The list A is: K a a a d m m p v z
The list B is: 0 1 2 9
There is no item in C list.
#include<stdio.h>
#include<string.h>
char a[110],b[110],c[110];
void fun(char a[],int n){
	for(int i=0;i<n;i++)
		for(int j=0;j<n-i-1;j++)
			if(a[j]>a[j+1]){
				char tmp=a[j];
				a[j]=a[j+1];
				a[j+1]=tmp;
			}
}
int main(){
	char ch[2];
	int n1=0,n2=0,n3=0;
	while(1){
		scanf("%s",ch);
		if(strcmp(ch,"-1")==0)break; 
		char p=ch[0];
		if(p>='0'&&p<='9')b[n2++]=p;
		else if(p>='A'&&p<='Z'||p>='a'&&p<='z')a[n1++]=p;
		else c[n3++]=p;
	}
	fun(a,n1);fun(b,n2);fun(c,n3);
	if(n1==0)printf("There is no item in A list.\n");
	else{
		printf("The list A is:");
		for(int i=0;i<n1;i++)printf(" %c",a[i]);
		printf("\n");
	}
	if(n2==0)printf("There is no item in B list.\n");
	else{
		printf("The list B is:");
		for(int i=0;i<n2;i++)printf(" %c",b[i]);
		printf("\n");
	}
	if(n3==0)printf("There is no item in C list.\n");
	else{
		printf("The list C is:");
		for(int i=0;i<n3;i++)printf(" %c",c[i]);
		printf("\n");
	}
	return 0;
} 

 

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题目是要求我们实现链表的基本操作,包括创建链表、插入节点、删除节点、遍历链表等。以下是一个简单的链表实现代码示例: ```c++ #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; // 创建链表 ListNode* createList(int nums[], int n) { if (n == 0) return NULL; ListNode *head = new ListNode(nums[0]); ListNode *cur = head; for (int i = 1; i < n; i++) { cur->next = new ListNode(nums[i]); cur = cur->next; } return head; } // 插入节点 void insertNode(ListNode *&head, int pos, int val) { if (pos < 0) return; if (pos == 0) { ListNode *newNode = new ListNode(val); newNode->next = head; head = newNode; return; } ListNode *cur = head; while (cur && pos > 1) { cur = cur->next; pos--; } if (cur) { ListNode *newNode = new ListNode(val); newNode->next = cur->next; cur->next = newNode; } } // 删除节点 void deleteNode(ListNode *&head, int pos) { if (pos < 0) return; if (pos == 0) { ListNode *delNode = head; head = head->next; delete delNode; return; } ListNode *cur = head; while (cur && pos > 1) { cur = cur->next; pos--; } if (cur && cur->next) { ListNode *delNode = cur->next; cur->next = delNode->next; delete delNode; } } // 遍历链表 void traverseList(ListNode *head) { while (head) { cout << head->val << " "; head = head->next; } cout << endl; } int main() { int nums[] = {1, 2, 3, 4, 5}; int n = sizeof(nums) / sizeof(int); ListNode *head = createList(nums, n); traverseList(head); insertNode(head, 2, 6); traverseList(head); deleteNode(head, 3); traverseList(head); return 0; } ``` 在上面的代码中,我们定义了一个 `ListNode` 结构体作为链表节点,包括节点值 `val` 和指向下一个节点的指针 `next`。同时,我们实现了创建链表、插入节点、删除节点和遍历链表等基本操作。在使用链表时,我们可以先通过 `createList` 函数创建一个链表,然后对链表进行插入、删除和遍历操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值