-
题目内容
-
解题思路心得
很直接的头插尾插
-
代码
// 链表逆序
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Book
{
/* data */
string note;
string name;
double price;
};
struct LNode
{
/* data */
Book elem;
LNode *next;
};
void input(LNode *&L)
{
int length=0;
cin >> length;
LNode *s=L;
Book list;
for (int i = 0; i < length; i++)
{
cin >> list.note >> list.name >> list.price;
if (list.note == "0" && list.name == "0" && list.price == 0)
break;
LNode *p=new LNode;
p->elem=list;
p->next=NULL;
s->next=p;
s=p;
}
s->next=NULL;
}
void reve(LNode *&L)
{
LNode *p,*r;
p=L->next;
L->next=NULL;
while (p!=NULL)
{
r=p->next;
p->next=L->next;
L->next=p;
p=r;
}
}
void output(LNode *&L)
{
LNode *p=L->next;
while(p!=NULL)
{
cout << p->elem.note << " " << p->elem.name << " " << fixed << setprecision(2) << p->elem.price << endl;
p=p->next;
}
}
int main()
{
LNode *L1 = new LNode;
L1->next=NULL;
input(L1);
reve(L1);
output(L1);
return 0;
}