题目:
代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct SList
{
char data;
struct SList* next;
}SL;
void SListInit(SL** ps)
{
*ps = (SL*)malloc(sizeof(SL));
(*ps)->next = NULL;
}
void SListCreate(SL** ps, int n)
{
SL* cur = *ps;
char x = 0;
while (n)
{
scanf("%c", &x);
SL* newnode = (SL*)malloc(sizeof(SL));
newnode->data = x;
cur->next = newnode;
cur = newnode;
n--;
}
cur->next = NULL;
}
SL* SListReverse(SL** ps)
{
SL* i1 = NULL;
SL* i2 = (*ps)->next;
SL* i3 = i2->next;
while (i2)
{
i2->next = i1;
i1 = i2;
i2 = i3;
if (i3)
i3 = i3->next;
}
return i1;
}
void SLPrint(SL** ps)
{
SL* cur = *ps;
while (cur)
{
printf("%c ", cur->data);
cur = cur->next;
}
}
int main()
{
SL* plist = NULL;
SListInit(&plist);
int n = 0;
scanf("%d", &n);
getchar();
SListCreate(&plist, n);
SL* newHead = SListReverse(&plist);
SLPrint(&newHead);
return 0;
}