1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| // 链表栈.cpp: 定义控制台应用程序的入口点。 //
#include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> using namespace std;
#define MAX_SIZE 20
typedef int Elemtype; typedef struct Node Lnode; struct Node { public: Elemtype data; Node *next; Node() { data = 0; next = NULL; } }; struct Stack { Lnode *top = new Lnode(); void Push(int value) { Lnode *temp = new Lnode(); temp->data = value; temp->next = top->next; top->next = temp; } int Pop() { int result; if (top->next == NULL) return -1; result = top->data; top->next = top->next->next; top->data--; return result; } //初始化 void InitStack(Stack tem, int size) { for (int i = 0; i < size; i++) { int temp = rand() % MAX_SIZE + 1; tem.Push(temp); } top->data = size; } bool empty() { if (top == NULL) return true; else return false; } int Top() { return top->next->data; } //遍历 void TraverseStack() { while (top) { cout << top->data << " "; top = top->next; } cout << endl; } };
//插入排序
int main() { srand(time(0)); Stack s; s.InitStack(s, MAX_SIZE); s.TraverseStack(); return 0; }
|