**构建单链表,用递归算法实现对其转置,然后输出转置后各个元素的值(倒序输出)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct Stu
{
double M;
int N;
struct Stu* next;
}Stu;
void createlinklist(Stu* l)
{
Stu* p;
Stu* r;
int x;
char ch;
r = l;
while (1)
{
scanf("%d", &x);
ch = getchar();
p = (Stu*)malloc(sizeof(Stu));
(*p).N = x;
(*r).next = p;
r = p;
if (ch == '\n')
{
(*r).next = NULL;
return;
}
}
}
Stu* reverselinklist(Stu* l)
{
Stu* newhead;
if (l == NULL || l->next == NULL)
{
return l;
}
newhead = reverselinklist(l->next);
l->next->next = l;
l->next = NULL;
return newhead;
}
int main(int argc, const char* argv[])
{
Stu* L, * p;
L = (Stu*)malloc(sizeof(Stu));
L->next = NULL;
createlinklist(L);
p = reverselinklist(L->next);
while (p)
{
printf("%d ", (*p).N);
p = (*p).next;
}
printf("\n");
return 0;
}
|