用链表存储字符串 && 新建链表筛选

Description:

用链表A存储一组字符串([1, 80]),建立一个链表B,存储其中ASCLL码字符最小的数出现的次数等于n的字符串。

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

struct N
{
	char s[85];       //一开始定义成了char *s,下接[1];
	struct N *next;
    int t;               //t用来记录s中ascll码最小的数出现的次数
    
};

int t(char *s)                //计算s中ascll码最小的数出现的次数
{
	int len = strlen(s);
	char min = s[0];
	int t = 1;
	for(int i=1; i<=len-1; i++)
	{
		if(s[i]<min)
		{
			t=1;
			min = s[i];
		 } 
		else if(s[i]==min) t++;
    }
	return t;
}

struct N *createA(int num)
{
	struct N *headA = (struct N*)malloc(sizeof(struct N));
	struct N *end = headA;
	for(int i=0; i<num; i++)
	{
		struct N *tmp = (struct N*)malloc(sizeof(struct N));
		scanf("%s", tmp->s);    //上接[1],对于char*s,没有办法正常循环输入,因为还未分配内存
		tmp->t = t(tmp->s);
		end->next = tmp;
		end = tmp; 
	}
	end->next = NULL;
	return headA;
}

struct N *createB(struct N* headA, int n)
{
	struct N *headB = (struct N*)malloc(sizeof(struct N));
	struct N *endB = headB;
	struct N *p = headA->next;
	while(p!=NULL)
	{
		if(p->t == n)
		{
			endB->next = p;
			endB = p;
		}
		p = p->next;
	}
	endB->next = NULL;
	return headB;
}

int main()
{
    int num;
    scanf("%d", &num);
    struct N *headA = createA(num); 
    int n;
    scanf("%d", &n);
	struct N *headB = createB(headA, n);
	struct N *p = headB->next;
	while(p!=NULL)
	{
		printf("%s ", p->s);
		p = p->next;
	}
	return 0;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
链表实现字符串的替换可以分为以下几个步骤: 1. 定义链表节点结构体,包含一个字符类型的数据域和一个指向下一个节点的指针域。 2. 定义一个链表的头指针,指向链表的第一个节点。 3. 读入需要替换的字符串和替换内容,遍历字符串,将每个字符作为节点插入链表尾部。 4. 遍历链表,查找需要替换的字符,如果找到就将其替换成替换内容。 5. 将链表中的字符输出或者存储一个新的字符串中。 下面是一个示例代码: ```c++ #include <iostream> #include <string> using namespace std; struct Node { char data; Node* next; }; int main() { string str, old_str, new_str; cout << "Enter a string: "; getline(cin, str); cout << "Enter the old string: "; getline(cin, old_str); cout << "Enter the new string: "; getline(cin, new_str); Node* head = NULL; Node* tail = NULL; // 将字符串转换为链表 for (int i = 0; i < str.length(); i++) { Node* node = new Node; node->data = str[i]; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } // 遍历链表,查找并替换字符 Node* p = head; while (p != NULL) { if (p->data == old_str[0]) { bool found = true; Node* q = p->next; for (int i = 1; i < old_str.length(); i++) { if (q == NULL || q->data != old_str[i]) { found = false; break; } else { q = q->next; } } if (found) { // 删除旧的字符串 Node* r = p; p = p->next; while (r != q) { Node* temp = r; r = r->next; delete temp; } // 插入新的字符串 for (int i = 0; i < new_str.length(); i++) { Node* node = new Node; node->data = new_str[i]; node->next = p; p = node; } } else { p = p->next; } } else { p = p->next; } } // 输出链表中的字符 p = head; while (p != NULL) { cout << p->data; p = p->next; } // 释放链表中的内存 p = head; while (p != NULL) { Node* temp = p; p = p->next; delete temp; } return 0; } ``` 这个程序可以读入一个字符串和两个子字符串,然后使用链表实现将原始字符串中的子字符串替换为新的子字符串,最后输出替换后的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值