P71,3-10
很简单的程序,debug了好一会。
最重要的是思路清晰。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef struct node *link;
struct node
{
int item;
link next;
};
int n;
void visit(link p)
{
printf("%d ", p->item);
}
void traverse(link head)
{
link p = head;
while(p != NULL)
{
printf("%d ", p->item);
p = p->next;
}
printf("\n");
}
link reverse(link head)
{
link p = head, next, prev = NULL;
while(p != NULL)
{
next = p->next;
p->next = prev;
prev = p;
p = next;
}
return prev;
}
int main()
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
scanf("%d", &n);
link p, head;
head = p = (link)malloc(sizeof(node)); p->item = 1, p->next = p;
for(int i = 2; i <= n; ++ i)
{
p = (p->next = (link)malloc(sizeof(node)));
p->item = i, p->next = NULL;
}
traverse(head);
link tail = reverse(head);
traverse(tail);
return 0;
}