码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构--实验1--栈的操作

时间:2018-07-02 20:16:46      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:else   span   链式   顺序栈   显示   栈结构   top   color   node   

  1 #include "stdio.h"
  2 #include "malloc.h"
  3 typedef  int datatype;
  4 typedef  struct node   //定义链式栈结构
  5 {    datatype data;
  6     struct node *next;
  7 }StackNode,*LinkStack;
  8 LinkStack  Init_LinkStack()
  9 {  
 10     return NULL;
 11 }
 12 
 13 //入栈   
 14 LinkStack  Push_LinkStack(LinkStack  top, datatype x)         
 15 {    
 16         StackNode *s;
 17         s=new StackNode;
 18         s->data=x;
 19         s->next=top;
 20         top=s;
 21         return top;
 22 }
 23 
 24 //出栈
 25 LinkStack   Pop_LinkStack (LinkStack  top, datatype  *x)
 26 {
 27     StackNode *p;
 28     if(top==NULL)return NULL;
 29     else
 30     {
 31     *x=top->data;
 32     p=top;
 33     top=top->next;
 34     free(p);
 35     return top;
 36     }
 37 
 38 }
 39 void print(LinkStack  top)
 40 {    StackNode  *p=top;
 41     while(p != NULL) 
 42     {
 43         printf("%d->",p->data);
 44         p=p->next;
 45     }
 46 }
 47 //顺序栈
 48 #define MAXSIZE  1024   
 49 typedef  struct
 50 {    datatype  data[MAXSIZE];
 51     int  top;
 52 }SeqStack;
 53 
 54 //顺序栈置空栈:首先建立栈空间,然后初始化栈顶指针。
 55 SeqStack  *Init_SeqStack()
 56 {    SeqStack  *s;
 57     s=new SeqStack;
 58     s->top= -1;  
 59     return s;
 60 }
 61 
 62 //顺序栈判空栈
 63 int Empty_SeqStack(SeqStack *s)
 64 {    if (s->top == -1)  return 1;
 65     else  return 0;
 66 }
 67 
 68 //顺序栈入栈
 69 int Push_SeqStack (SeqStack *s, datatype  x)
 70 {if (s->top == MAXSIZE-1)  return 0; //栈满不能入栈
 71 else {    s->top++;
 72         s->data[s->top]=x;
 73         return 1;
 74     }
 75 }
 76 
 77 //顺序栈出栈
 78 int  Pop_SeqStack(SeqStack *s, datatype *x)
 79 {  
 80     if  (Empty_SeqStack(s))  return 0; //栈空不能出栈 
 81     else  { *x=s->data[s->top];
 82             s->top--;  return 1;        //栈顶元素存入*x,返回
 83           }
 84 }
 85 void conversion(int N,int r)
 86 {    SeqStack  *s;
 87     datatype   x;                         
 88     s=Init_SeqStack();        //初始化栈
 89     printf("\n %d 的十进制数转换成 %d 进制为: ",N,r);
 90     while ( N )                            
 91     {    Push_SeqStack (s,N%r);     //余数入栈 
 92         N=N/r ;                //商作为被除数继续 
 93     }   
 94     while  ( !Empty_SeqStack(s))  
 95     {    Pop_SeqStack (s,&x) ;   
 96         printf(" %d ",x) ;      
 97     }         
 98 }  
 99 void main()
100 {
101     datatype x;
102     int i,j,k;
103     LinkStack top;
104     top=Init_LinkStack();
105     do
106     {
107         printf("\n\n\n\n");
108     printf("\t\t\t 栈的应用子系统\n");
109     printf("\t\t*******************************\n");
110     printf("\t\t*        1----链式进栈     *\n");
111     printf("\t\t*        2----链式出栈    *\n");
112     printf("\t\t*        3----链栈显示    *\n");
113     printf("\t\t*        4----进制转换    *\n");
114     printf("\t\t*        0----返  回    *\n");
115     printf("\t\t*******************************\n");
116     printf("\t\t 请选择菜单项(0-4):");
117     scanf("%d",&k);
118     getchar();
119     switch(k)
120     {
121     case 1:
122         printf("\n   请输入要进栈的数据X:");
123         scanf("%d",&x);
124         top=Push_LinkStack(top,x);
125         break;
126     case 2:
127         Pop_LinkStack(top,&x);
128         break;
129     case 3:
130         printf("\n 链式栈的元素有:");
131         print(top);
132         break;
133     case 4: 
134         int N,r;
135         printf("\n  请输入一个整数N=");
136         scanf("%d",&N);
137         printf("\n 请输入一个要转换的进制数r=");
138         scanf("%d",&r);
139         conversion(N,r);
140         break;
141     
142     }
143         
144     }while(k);
145 }

 

数据结构--实验1--栈的操作

标签:else   span   链式   顺序栈   显示   栈结构   top   color   node   

原文地址:https://www.cnblogs.com/zongyao/p/9255378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!