#include <iostream>
#include <string.h>
typedef struct Record
{
int n;
char s[5];
Record *next;
}Record,*RList;
void buildList(RList &list)//创建链表
{
Record *p;
list=new Record;//创建头结点,不存储数据
p=list;
for(int i=0;i<10;i++)
{
p->next=new Record;
p=p->next;
p->n=i;
strcpy(p->s,"str");
p->s[3]=i+0x30;//将数字i转换成对应字符
p->s[4]='\0';//字符串结尾
}
p->next=NULL;//链表结束
}
void reverseList(RList list)//逆置链表
{
Record *p=list->next->next,*r;
list->next->next=NULL;//链表尾
//头插法
while(p)
{
r=p->next;
p->next=list->next;
list->next=p;
p=r;
}
}
void printList(RList list)//打印链表
{
Record *p = list->next;
while(p)
{
printf("n=%d s=%s\n",p->n,p->s);
p=p->next;
}
}
int main(int argc, char** argv) {
RList list;
buildList(list);
printf("\n顺序输出:\n");
printList(list);
printf("\n倒序输出:\n");
reverseList(list);
printList(list);
reverseList(list);//还原链表
return 0;
}
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/7e3e6709c93d70cfd9234bc5f1dcd100bba12b19?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
追问求问:如果是要求用户输入,然后再输出呢?应该如何改