【算法设计思路】
通过画利用尾插法构建单链表的示意图帮助分析,然后转化为代码的方法,很可行。当然,前提是需要知道某个阶段示意图对应的是什么代码。
【算法代码】
#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
Node *next;
};
void wcf(Node *L,int n) {
L=new Node;
L->next=NULL;
Node *r;
r=L;
for(int i=0; i<n; i++) {
Node *p;
p=new Node;
p->next=NULL;
cin>>p->data;
r->next=p;
r=p;
}
for(int i=0; i<n; i++) {
cout<<L->next->data<<" ";
L->next=L->next->next;
}
}
int main() {
int n;
cin>>n;
Node *p;
wcf(p,n);
return 0;
}
/*
in:
7
a b c d e f g
out:
a b c d e f g
*/