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

  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("
 %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("



");
108     printf("			 栈的应用子系统
");
109     printf("		*******************************
");
110     printf("		*        1----链式进栈     *
");
111     printf("		*        2----链式出栈    *
");
112     printf("		*        3----链栈显示    *
");
113     printf("		*        4----进制转换    *
");
114     printf("		*        0----返  回    *
");
115     printf("		*******************************
");
116     printf("		 请选择菜单项(0-4):");
117     scanf("%d",&k);
118     getchar();
119     switch(k)
120     {
121     case 1:
122         printf("
   请输入要进栈的数据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("
 链式栈的元素有:");
131         print(top);
132         break;
133     case 4: 
134         int N,r;
135         printf("
  请输入一个整数N=");
136         scanf("%d",&N);
137         printf("
 请输入一个要转换的进制数r=");
138         scanf("%d",&r);
139         conversion(N,r);
140         break;
141     
142     }
143         
144     }while(k);
145 }
不经一番彻骨寒,哪有梅花扑鼻香?
原文地址:https://www.cnblogs.com/zongyao/p/9255378.html