实现代码:
#include <iostream>
#include <vector>
#include <stack>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkList
{
public:
LinkList(){head = new(Node);head->next = NULL;}
void setRandList(int len);
void insert(int i,int index = 1);
void print() const;
void invertPrint_vector() const;
void invertPrint_stack() const;
void invertprint_linklist();
void invertprint_recursion() const;
void invertprint_recursion(Node * node) const;
private:
Node * head;
int length = 0;
};
void LinkList::setRandList(int len)
{
srand((unsigned)time(NULL));
for(int i = 0;i < len; i++)
{
insert(rand()%99+1);
}
}
void LinkList::insert(int data,int index)
{
if(index > length + 1)
{
throw "位置出错!";
}
else
{
Node *p = head;
for(int i = 1; i < index; i++)
{
p = p->next;
}
Node *newnode = new Node;
newnode->data = data;
newnode->next = p->next;
p->next = newnode;
length++;
}
}
void LinkList::print() const
{
if(NULL == head->next){return;}
cout << "\n当前正序输出:";
Node *p = head->next;
while(NULL != p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void LinkList::invertPrint_vector() const
{
if(NULL == head->next){return;}
cout << "数组倒序:";
vector<int> v;
Node *p = head->next;
while(NULL != p)
{
v.emplace_back(p->data);
p = p->next;
}
for(auto it = v.end() - 1; it >= v.begin(); it--)
{
cout << *it << " ";
}
cout << endl;
}
void LinkList::invertPrint_stack() const
{
if(NULL == head->next){return;}
cout << "栈 倒 序:";
stack<int> s;
Node *p = head->next;
while(NULL != p)
{
s.push(p->data);
p = p->next;
}
while(!s.empty())
{
cout << s.top() << " ";
s.pop();
}
cout << endl;
}
void LinkList::invertprint_linklist()
{
if(NULL == head->next){return;}
cout << "逆序链表(此处使用方法执行过程不打印):\n";
Node *p = new(Node);
p->next = head->next;
Node *q = new(Node);
q->next = p->next->next;
head->next->next = NULL;
head->next = q->next->next;
for(int i = 1; i < length - 2; i++)
{
q->next->next = p->next;
p->next = q->next;
q->next = head->next;
head->next = head->next->next;
}
q->next->next = p->next;
head->next->next = q->next;
delete p;
delete q;
}
void LinkList::invertprint_recursion() const
{
if(NULL == head->next){return;}
cout << "递归倒序:";
invertprint_recursion(head);
cout << endl;
}
void LinkList::invertprint_recursion(Node * node) const
{
if(NULL == node->next)
{
return;
}
invertprint_recursion(node->next);
cout << node->next->data << " ";
}
int main()
{
LinkList ll;
ll.setRandList(10);
ll.print();
ll.invertPrint_vector();
ll.print();
ll.invertPrint_stack();
ll.print();
ll.invertprint_linklist1();
ll.print();
ll.invertprint_recursion();
ll.print();
ll.invertprint_recursion();
ll.print();
return 0;
}
运行结果:
当前正序输出:94 91 78 54 26 90 75 79 21 80
数组倒序:80 21 79 75 90 26 54 78 91 94
当前正序输出:94 91 78 54 26 90 75 79 21 80
栈 倒 序:80 21 79 75 90 26 54 78 91 94
当前正序输出:94 91 78 54 26 90 75 79 21 80
逆序链表(此处使用方法执行过程不打印):
当前正序输出:80 21 79 75 90 26 54 78 91 94
递归倒序:94 91 78 54 26 90 75 79 21 80
当前正序输出:80 21 79 75 90 26 54 78 91 94
递归倒序:94 91 78 54 26 90 75 79 21 80
当前正序输出:80 21 79 75 90 26 54 78 91 94