怎样用链式结构实现线性表的逆置?请给出源程序来!
答案:1 悬赏:0 手机版
解决时间 2021-05-15 07:48
- 提问者网友:龅牙恐龙妹
- 2021-05-14 12:01
请问各位高手,怎样用链式结构实现线性表的逆置?要求用C++编写。
最佳答案
- 五星知识达人网友:持酒劝斜阳
- 2021-05-14 12:56
#include <iostream>
using namespace std;
struct list {
int data;
list* next;
};
int main()
{
int a[] = { 0,1,2,3,4,5,6,7,8,9 };
int i;
list l[10], *prev, *next, *t;
prev = 0;
for ( i = 0; i < 10; ++i ) { // 从数组构造链表
l[i].data = a[i];
l[i].next = 0;
if ( prev )
prev->next = &l[i];
prev = &l[i];
}
prev = 0; next = l;
while ( next ) { // 逆序
t = next->next;
next->next = prev;
prev = next;
next = t;
}
while( prev ) { // 输出
cout << prev->data << ' ';
prev = prev->next;
}
cout.put( '\n' );
}
using namespace std;
struct list {
int data;
list* next;
};
int main()
{
int a[] = { 0,1,2,3,4,5,6,7,8,9 };
int i;
list l[10], *prev, *next, *t;
prev = 0;
for ( i = 0; i < 10; ++i ) { // 从数组构造链表
l[i].data = a[i];
l[i].next = 0;
if ( prev )
prev->next = &l[i];
prev = &l[i];
}
prev = 0; next = l;
while ( next ) { // 逆序
t = next->next;
next->next = prev;
prev = next;
next = t;
}
while( prev ) { // 输出
cout << prev->data << ' ';
prev = prev->next;
}
cout.put( '\n' );
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯