1 #include<iostream> 2 #include<ctime> 3 using namespace std; 4 5 struct linknode 6 { 7 int number; 8 linknode *next; 9 }; 10 11 12 struct linkstack 13 { 14 int count; 15 linknode *top; 16 }; 17 18 19 20 21 void Push(linkstack *s, int e) 22 { 23 linknode *t = new linknode; 24 t->number = e; 25 t->next = s->top; 26 s->count++; 27 s->top = t; 28 } 29 30 31 int Pop(linkstack *s) 32 { 33 int t; 34 if (s->count > 0) 35 { 36 s->count--; 37 linknode *p = s->top; 38 t = s->top->number; 39 s->top = p->next; 40 delete p; 41 } 42 return t; 43 } 44 45 46 void initstack(linkstack *s,int n) 47 { 48 linknode *t; 49 srand(time(0)); 50 for (int i = 1; i <= n; i++) 51 { 52 t = new linknode; 53 t->number = rand() % 100 + 1; 54 t->next = s->top; 55 s->top = t; 56 } 57 s->count += n; 58 } 59 60 61 void main() 62 { 63 linkstack *t = new linkstack; 64 t->top = new linknode; 65 t->top->number = rand() % 100 + 1; 66 t->count = 0; 67 t->top->next = NULL; 68 initstack(t, 10); 69 for (int i = 1; i <= 10; i++) 70 cout << Pop(t)<<endl; 71 }