#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
linklist *head,*p;
//建立链表
linklist *CREATLISTF()
{
char ch;
head = NULL; /*链表开始为空*/
ch = getchar(); /*读入第一个节点*/
while(ch !='$') /*逐个输入字符,以"-1"为结束的标志,返回头指针*/
{
p = (linklist*)malloc(sizeof(linklist)); /*生成新节点*/
p->data = ch; /*将输入的值放入数据域中*/
p->next = head;
head = p; /*将新节点插入到表头上*/
ch = getchar(); /*读入下一个节点*/
}
return head; /*返回头指针*/
} /*CREATLISTF*/
//输出链表
void print(linklist *head)
{
linklist *p;
p = head;
if(head != NULL)
do
{
printf("%s ",p->data);
p = p->next;
}while(p != NULL);
}
main()
{
linklist *start;
printf("Please input records: \n");
start = CREATLISTF();
printf("The records are :\n");
print(start);
}
[此贴子已经被作者于2006-4-13 23:26:18编辑过]
----------------解决方案--------------------------------------------------------
我喜欢你的风格,一点小问题
[CODE]
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
linklist *head=NULL,*p;
linklist *CREATLISTF()
{
char ch;
ch = getchar();
while(ch !='$')
{
p = (linklist*)malloc(sizeof(linklist));
p->data = ch;
p->next = head;
head = p;
ch = getchar();
}
return head;
}
void print(linklist *head)
{
linklist *p;
p = head;
if(head != NULL)
do
{
printf("%c",p->data);
p = p->next;
}while(p != NULL);
}
main()
{
linklist *start;
printf("Please input records: \n");
start = CREATLISTF();
printf("The records are :\n");
print(start);
getch();
}
[/CODE]
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
linklist *head,*p;
//建立链表
linklist *CREATLISTF()
{
char ch;
head = NULL; /*链表开始为空*/
ch = getchar(); /*读入第一个节点*/
while(ch !='$') /*逐个输入字符,以"-1"为结束的标志,返回头指针*/
{
p = (linklist*)malloc(sizeof(linklist)); /*生成新节点*/
p->data = ch; /*将输入的值放入数据域中*/
p->next = head;
head = p; /*将新节点插入到表头上*/
ch = getchar(); /*读入下一个节点*/
}
return head; /*返回头指针*/
} /*CREATLISTF*/
//输出链表
void print(linklist *head)
{
linklist *p;
p = head;
if(head != NULL)
do
{
printf("%c",p->data); /*是输出格式的问题*/
p = p->next;
}while(p != NULL);
}
main()
{
linklist *start;
printf("Please input records: \n");
start = CREATLISTF();
printf("The records are :\n");
print(start);
}
楼主的编程风格还是很值得学习的。
----------------解决方案--------------------------------------------------------