LinkList.h
#pragma once #include<iostream> using namespace std; class LNode { public: int data; LNode* next; }; class LinkList { public: LNode* first; LinkList() { first = new LNode(); first->data = 666; first->next = nullptr; } void creat(int* arr, int n) { LNode* p; LNode* s; p = first; for (int i = 0; i < n; i++) { s = new LNode(); s->data = arr[i]; s->next = p->next; p->next = s; p = s; } } void add(int e) { LNode* p; LNode* s; p = first; while (p->next != nullptr) { p = p->next; } s = new LNode(); s->data = e; s->next = p->next; p->next = s; } void show() { LNode* p; p = first->next; while (p != nullptr) { cout << p->data << " "; p = p->next; } cout << endl; } };
Stack.h
#pragma once #include<iostream> using namespace std; class Stack { public: int* elements; int maxSize; int top; Stack(int size = 50) { maxSize = size; elements = new int[maxSize]; top = -1; } void push(int e) { if (top == maxSize - 1) { //略 } else { elements[++top] = e; } } bool pop(int& e) { bool res = true; if (top == -1) { res = false; } else { e = elements[top--]; } return res; } };
MyTool.h
#pragma once #include "LinkList.h" #include"Stack.h" class MyTool { public: static void turn(LinkList& L) { Stack s; LinkList L1; LNode* p; p = L.first->next; while (p != nullptr) { s.push(p->data); p = p->next; } int temp; while (s.pop(temp)) { L1.add(temp); } L = L1; } };
main.cpp
#include"MyTool.h" int main() { LinkList L; int arr[] = { 1,2,3,4,5 }; L.creat(arr, 5); MyTool::turn(L); L.show(); return 0; }
原文地址:https://www.cnblogs.com/SlowIsFast/p/12630873.html
时间: 2024-09-28 04:21:44