c语言使用链表进行字符数组处理

本文介绍了一个使用链表来处理字符数组的C程序。该程序能够接收多个字符数组,移除其中的所有数字,并将所有出现的'$'字符替换为's'。接着检查处理后的字符串是否为回文串。如果字符串与其反转相同,则输出'yes',否则输出'no'。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用链表  输入若干个字符数组,然后把数字去掉,再把$改为s,最后判断是否为回文  是的话yes否则no

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int i,l;
char chushu(char *a,char *b);
char qian(char *a );
void huiwen(char *a);
int main(void)
{
    struct zhifu
    {
        int no;
        char chuan[80];
        char chuan2[80];
        char huiwen;
        struct zhifu *next;
    };
struct zhifu *first = NULL;
struct zhifu *ing = NULL;
struct zhifu *ing2=NULL;
struct zhifu *previous = NULL;
char test='\0';
for(;;)
{
    printf("\n你想继续输入字符数组吗(Y or N)?);
    scanf("%c",&test);
    if(tolower(test)=='n')
        break;
 
    ing=(struct zhifu*)malloc(sizeof(struct zhifu));
 
    if(first==NULL)
        first =ing;
 
    if(previous!=NULL)
        previous->next=ing;
    printf("\n这是第几次定义字符数组:");
    scanf("%d",&ing->no);
    printf("\n请定义第%d次字符数组:",ing->no);
    scanf("%s",ing->chuan);
	getchar();   
    ing->next=NULL;
    previous=ing;
 }

	for (ing = first; ing != NULL; ing = ing->next) {
		printf("%d: %s\n", ing->no, ing->chuan);
	}

ing=first;
while(ing!=NULL)
{
    char *p = ing->chuan;
    char *q = ing->chuan2;
	
   chushu(p,q);
   qian(q);
   huiwen(q);
   
    ing=ing->next;
   
}
	printf("\nafter modified:\n");
	for (ing = first; ing != NULL; ing = ing->next) {
		printf("%d: %s\n", ing->no, ing->chuan2);
	}
return 0;
}
char chushu(char *a,char *b)
{
	for( ; *b = *a; a++)     
        if( *a <'0' || *a >'9' ) 
			++b;
		return(*b);
}
char qian(char *a)
{
	for(;*a;a++)
	{	if(*a=='$')
			*a='s';
	i++;
	}
	return(*a);
}
void huiwen(char *a) 
{
    printf("%s\n", a); 
    char *p1 = a;
    char *p2 = (a+strlen(a)-1);
    while(p1 <  p2) {
        if(*(p1++) !=  *(p2--)){
            printf("NO\n");
            break;
        }   
        else  if(p1>=p2)
            printf("yes\n");
    }   
}



 


 

 

 

 

 

在C语言中,链表可以用于存储多个字符数组数据,这种结构非常适合处理动态数量的数据集合。通过链表,可以灵活地添加、删除和访问各个字符数组。 ### 定义链表节点结构 每个链表节点通常包含两个部分:一个用于存储字符数组字符串),另一个是指向下一个节点的指针。定义如下: ```c typedef struct Node { char *data; // 指向字符数组的指针 struct Node *next; // 指向下一个节点 } ListNode; ``` ### 初始化链表 链表的初始化涉及创建一个头节点,并将其 `next` 指针设置为 `NULL`,表示空链表。 ```c ListNode *head = (ListNode *)malloc(sizeof(ListNode)); if (head == NULL) { printf("Memory allocation failed\n"); exit(1); } head->data = NULL; head->next = NULL; ``` ### 添加字符数组链表 为了将字符数组添加到链表中,需要分配新的节点内存,并复制字符数组的内容。 ```c void addStringToList(ListNode *head, const char *str) { ListNode *newNode = (ListNode *)malloc(sizeof(ListNode)); if (newNode == NULL) { printf("Memory allocation failed\n"); exit(1); } newNode->data = (char *)malloc(strlen(str) + 1); // 分配足够的空间存储字符串 if (newNode->data == NULL) { printf("Memory allocation failed\n"); free(newNode); exit(1); } strcpy(newNode->data, str); // 复制字符串内容 newNode->next = NULL; ListNode *current = head; while (current->next != NULL) { current = current->next; } current->next = newNode; } ``` ### 遍历链表并输出字符数组 遍历链表时,逐个访问每个节点,并打印其中存储的字符数组。 ```c void printList(ListNode *head) { ListNode *current = head->next; // 跳过头节点 while (current != NULL) { printf("%s\n", current->data); current = current->next; } } ``` ### 释放链表内存 为了避免内存泄漏,程序结束前应释放链表的所有节点及其存储的字符数组。 ```c void freeList(ListNode *head) { ListNode *current = head; while (current != NULL) { ListNode *temp = current; current = current->next; if (temp->data != NULL) { free(temp->data); // 释放字符数组 } free(temp); // 释放节点本身 } } ``` ### 完整示例代码 以下是一个完整的示例代码,展示如何使用链表存储和操作多个字符数组。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义链表节点结构 typedef struct Node { char *data; struct Node *next; } ListNode; // 初始化链表头节点 ListNode *initializeList() { ListNode *head = (ListNode *)malloc(sizeof(ListNode)); if (head == NULL) { printf("Memory allocation failed\n"); exit(1); } head->data = NULL; head->next = NULL; return head; } // 向链表添加字符串 void addStringToList(ListNode *head, const char *str) { ListNode *newNode = (ListNode *)malloc(sizeof(ListNode)); if (newNode == NULL) { printf("Memory allocation failed\n"); exit(1); } newNode->data = (char *)malloc(strlen(str) + 1); if (newNode->data == NULL) { printf("Memory allocation failed\n"); free(newNode); exit(1); } strcpy(newNode->data, str); newNode->next = NULL; ListNode *current = head; while (current->next != NULL) { current = current->next; } current->next = newNode; } // 打印链表中的所有字符串 void printList(ListNode *head) { ListNode *current = head->next; while (current != NULL) { printf("%s\n", current->data); current = current->next; } } // 释放链表内存 void freeList(ListNode *head) { ListNode *current = head; while (current != NULL) { ListNode *temp = current; current = current->next; if (temp->data != NULL) { free(temp->data); } free(temp); } } int main() { ListNode *head = initializeList(); addStringToList(head, "Hello"); addStringToList(head, "World"); addStringToList(head, "Welcome to C programming"); printf("Strings in the linked list:\n"); printList(head); freeList(head); return 0; } ``` 通过上述方法,可以在C语言中有效地使用链表来存储和操作多个字符数组[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值