栈的简单应用-进制转换

余数的序列正好与8进制数字序列相反,利用栈后进先出的特点,用栈来保存

 1 #include <iostream>//带头栈
 2 #include <cstdlib>
 3 using namespace std;
 4 typedef struct node 
 5 {
 6     int data;
 7     struct node *next;
 8 }*linkstack,stacknode;
 9 void initstack(linkstack *top)
10 {
11     *top=new stacknode;
12     (*top)->next=NULL;
13 }
14 bool isempty(linkstack top)
15 {
16     if(top->next==NULL)
17         return 1;
18     return 0;
19 }
20 int pushstack(linkstack top,int num)
21 {
22     linkstack p=new stacknode;
23     p->data=num;
24     p->next=top->next;
25     top->next=p;
26 }
27 int popstack(linkstack top)
28 {
29     if(isempty(top)==1)
30         return 0;
31     linkstack p;
32     p=top->next;
33     top->next=p->next;
34     free(p);
35 }
36 int getstack(linkstack top)
37 {
38     if(isempty(top)==1)
39         return 0;
40     else
41         cout<<(top->next)->data<<" ";
42 
43 }
44 int clearstack(linkstack top)
45 {
46     top->next=NULL;
47 }
48 int main()
49 {
50     int i=0;
51     linkstack top;
52     initstack(&top);
53     int n=1568;
54     while(n!=0)
55     {
56         pushstack(top,n%8);
57         n=n/8;
58     }
59     while(isempty(top)!=1)
60     {
61         getstack(top);
62         popstack(top);
63     }
64     clearstack(top);
65     return 0;
66 }

 

posted @ 2015-07-21 18:26  御心飞行  阅读(274)  评论(0编辑  收藏  举报