#include <stdio.h>
#include <stdlib.h>
struct s_list
{
char c ;
struct s_list *next;
};
void creat_list(struct s_list **headp)
{
char ch;
struct s_list * loc_head = NULL,*tail;
if((ch = getchar()) == EOF);
else
{
loc_head = (struct s_list *)malloc(sizeof(struct s_list));
loc_head -> c = ch;
tail = loc_head;
while((ch = getchar()) != EOF)
{
tail -> next = (struct s_list *)malloc(sizeof(struct s_list));
tail = tail -> next;
tail -> c = ch;
}
tail -> next = NULL;
}
*headp = loc_head ;
}
void print_list(struct s_list *headp)
{
while(headp != NULL)
{
printf("%c",headp->c );
headp = headp -> next;
}
printf("\n\n");
}
void print_array(struct s_list *headp)
{
int i,tot;
struct s_list * p = headp;
char * ch;
for(tot = 1; p->next != NULL; tot++)
{
p = p->next;
}
p = headp;
ch = (char *)malloc(tot*sizeof(char));
for(i = 0 ; i < tot ;i++)
{
ch[i] = p->c;
p = p -> next;
}
ch[i] = '\0';
puts(ch);
}
int main()
{
struct s_list *head = NULL;
creat_list(&head);
print_list(head);
print_array(head);
return 0;
}
#job 59 超长字符串
最新推荐文章于 2021-04-11 23:03:22 发布
本文介绍了一个简单的链表实现过程,包括链表的创建、打印及转换为字符数组的方法。通过C语言,演示了如何从标准输入读取字符并构建单向链表,最后输出链表内容。
3675

被折叠的 条评论
为什么被折叠?



