【问题描述】输入一个字符串,以字符串中每个字符作为结点数据创建链表。删除链表中所有结点数据为非字母的结点。
【输入形式】输入一个字符串。
【输出形式】输出删除操作前、后的链表(删除操作前、后的链表均不会是空表)。
【样例输入】102a6vf
【样例输出】
102a6vf
avf
【样例输入】38IFfla045fDg
【样例输出】
38IFfla045fDg
IFflafDg
基本的链表操作,代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
char data;
struct _node* next;
}Node;
int main()
{
Node* head, * p, * t;
char str[1000];
int i = 0;
gets_s(str);
head = (Node*)malloc(sizeof(Node));
head->next = 0;
p = head;
while (str[i] != '\0')
{
t = (Node*)malloc(sizeof(Node));
t->data = str[i];
t->next = 0;
p->next = t;
p = t;
i++;
}
//删除
p = head;
t = p->next;
while (t)
{
if ((t->data >= 'a' && t->data <= 'z') || (t->data >= 'A' && t->data <= 'Z'))
{
p = t;
t = t->next;
}
else
{
p->next = t->next;
free(t);
t = p->next;
}
}
p = head->next;
while (p)
{
printf("%c", p->data);
p = p->next;
}
return 0;
}
#include <stdio.h>
typedef struct _node
{
char data;
struct _node *next;
}node;
void createlist(node *head)
{
char ch;
while((ch=getchar()) != '\n')
{
node *q = (node*)malloc(sizeof(node));
q->data = ch;
q->next = NULL;
head->next = q;
head = q;
}
}
void removelist(node *head)
{
node *p = head;
node *q = head->next;
while(q != NULL)
{
if((q->data <'a' || q->data > 'z') && (q->data < 'A' || q->data > 'Z'))
{
p->next = q->next;
free(q);
q = p->next;
}
else
{
p = q;
q = q->next;
}
}
}
void printlist(node *head)
{
node *p = head->next;
while(p != NULL)
{
printf("%c",p->data);
p = p->next;
}
}
int main()
{
node head;
head.next = NULL;
createlist(&head);
removelist(&head);
printlist(&head);
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!