链表倒序排列

来源:百度知道 编辑:UC知道 时间:2024/05/10 21:52:20
请帮我列举一个用链表实现字符串倒序排列的例子,并说明。\**************\

例:ABC DEF
结果1:FED CBA
结果2:DEF ABC
以下给出的两个程序都是用堆栈的方法接收,从而倒序!
/*结果1*/
#include "stdio.h"
#include <stdlib.h>
#define N 5
struct str
{
char a;
struct str *next;
};
struct str *creat(struct str *head)
{
int i;
struct str *p1;
printf("请输入5个字符:");
for(i=0;i<N;i++)
{
p1=malloc(sizeof(struct str));
p1->a=getche();
p1->next=head;
head=p1;
}
return(head);
}
void main()
{
struct str *head;
head=NULL;
head=creat(head);
printf("\n");
do{
printf("%c",head->a);
head=head->next;
}while(head!=NULL);

}
/*结果2*/
#include "stdio.h"
#include <stdlib.h>
#define N 3
struct str
{
char a[30];
struct str *next;
};
struct str *creat(struct str *head