单链表逆向打印
来源:互联网 发布:人工智能计算器 安卓 编辑:程序博客网 时间:2024/05/19 12:23
/*输入一个链表的头结点,从尾到头反过来打印出每个结点的值*/struct ListNode{int m_nKey;ListNode* m_pNext;};#include "vector"#include "iostream"using namespace std;void printReverseList0(ListNode* head){vector<int> tmp;ListNode *p = head;while (p){tmp.push_back(p->m_nKey);p = p->m_pNext;}for (int i = static_cast<int>(tmp.size()); i > 0; i--){cout << tmp[i-1] << ' ';}cout << endl;}#include "algorithm"#include "iterator"void printReverseList01(ListNode* head){vector<int> tmp;ListNode *p = head;while (p){tmp.push_back(p->m_nKey);p = p->m_pNext;}copy(tmp.rbegin(), tmp.rend(),ostream_iterator<int>(cout," "));cout << endl;}#include "stack"void printReverseList1(ListNode* head){stack<int> tmp;ListNode *p = head;while (p){tmp.push(p->m_nKey);p = p->m_pNext;}while (!tmp.empty()){cout << tmp.top() << ' ';tmp.pop();}cout << endl;}void printReverseList2(ListNode* head){if (head){printReverseList2(head->m_pNext);cout << head->m_nKey << ' ';}elsecout << endl;}void freeList(ListNode* head){ListNode* tmp;while (head){tmp = head;head = tmp->m_pNext;delete tmp;}}#include "time.h"#include "fstream"void test(){ListNode *head = NULL;ListNode *tail = head;for (int i = 0; i < 10; i++){ListNode* p = new ListNode;p->m_nKey = i;p->m_pNext = NULL;if (head == NULL){head = p;tail = p;}else{tail->m_pNext = p;tail = p;}}ofstream file;file.open("timeResults.txt", ios::app);clock_t t0 = clock();for (int i = 0; i < 1000; i++){printReverseList0(head);}file << "using vector and print size_t-1~0: " << clock() - t0 << "ms" << endl;t0 = clock();for (int i = 0; i < 1000; i++){printReverseList01(head);}file << "using vector and ostream_iterator: " << clock() - t0 << "ms" << endl;t0 = clock();for (int i = 0; i < 1000; i++){printReverseList1(head);}file << "using stack: " << clock() - t0 << "ms" << endl;t0 = clock();for (int i = 0; i < 1000; i++){printReverseList2(head);}file << "using recursion:" << clock() - t0 << "ms" << endl;file << endl;file.close();freeList(head);}int main(void){test();return 0;}
运行结果(3次):
using vector and print from size_t -1 to 0: 926ms
using vector and ostream_iterator: 806ms
using stack: 798ms
using recursion:751ms
using vector and print from size_t-1 to 0: 956ms
using vector and ostream_iterator: 815ms
using stack: 783ms
using recursion:717ms
using vector and print from size_t-1 to 0: 956ms
using vector and ostream_iterator: 822ms
using stack: 762ms
using recursion:715ms
递归是最快的。
0 0
- 单链表逆向打印
- 逆向打印单链表
- 递归逆向打印单链表
- 逆向打印与销毁单链表
- 文字逆向打印
- 打印逆向螺旋数
- 逆向打印链表
- 逆向打印链表
- 逆向打印字符串--递归实现
- 单链表逆向
- 每日一题——删除无头链表的非尾节点、逆向打印单链表
- [Leetcode #7]Reverse Integer整数反转、逆向打印整数
- 逆向打印、销毁、查找结点之递归实现
- 单链表逆向 c
- 逆向
- 逆向
- 逆向
- 面试题-单链表原地逆向
- 内存管理
- 使用requests模块模拟客户端请求
- 【Qt界面个性化】大杀器——qss
- 问题记录-python写mapper测试时出现urllib.error.HTTPError: HTTP Error 404: Not Found
- RHEL7文件权限管理
- 单链表逆向打印
- centos6.5系统bash损坏之救援模式修复
- 104_自定义CircleProgressBar
- 《UNIX环境高级编程》(竞争条件)
- 2016"百度之星" - 测试赛 1004 放盘子(水)
- 【线段树】浅谈区间问题(2)
- 【内外网映射】通过外网ip来访问虚拟机服务器【原创】
- Codeforces 667C Reberland Linguistics 【dp】
- 数据可视化中需要注意的问题