用头插法输出数据,发现是倒序的。请问大佬们,不用尾插法,怎么将数据正序输出
#include<stdio.h>
#include<stdlib.h>
struct list
{
char data;
struct list *next;
};
typedef struct list node;
typedef node *link;
//创建链表
link create_list (link head)
{
link pointer,newpointer;
char data1;
head=(link)malloc(sizeof(node));
if(head==NULL)
{
printf("error");
}
else
{
printf("input data:\n");
scanf("%c",&data1);
head->data=data1;
head->next=NULL;
}
while(1)
{
newpointer=(link)malloc(sizeof(node));
if(newpointer==NULL)
{
printf("error");
}
else
{
scanf("%c",&data1);
if(data1=='0')
break;
newpointer->data=data1;
newpointer->next=head;
head=newpointer;
}
}
return head;
}
//输出链表
void printf_list (link head)
{
link pointer;
pointer=head;
printf("data is:");
while(pointer!=NULL)
{
printf("%c",pointer->data);
pointer=pointer->next;
}
}
//释放链表
void free_list (link head)
{
link pointer;
while(head!=NULL)
{
pointer=head;
head=head->next;
free(pointer);
}
}
//主函数
int main()
{
link head;
head=(link)malloc(sizeof(node));
if(head==NULL)
printf("error");
else
{
head=create_list(head);
printf_list(head);
free_list(head);
}
return 0;
}