来源:互联网 发布:淘宝欠款 编辑:程序博客网 时间:2024/04/28 10:00
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE  100     /*设置栈的最大长度为100*/
typedef int ElemType;
typedef  struct{
ElemType  elem[MAXSIZE];    /*定义一个一维数组存放栈中各元素,元素类型ElemType根据实际需要确定(此注释应该在上一行)*/
    int  top;     /*栈顶指针*/
}SqStack;
typedef struct node{
ElemType data;
struct node *next;
}Node, *LinkStack;
void PushL (LinkStack *s, ElemType e)
{Node *p;
p=(Node *) malloc (sizeof (Node));
p->data=e;
p->next=*s;
*s=p;}
void PopL (LinkStack *s, ElemType *e)
{Node *p;
if (*s==NULL)
   {printf ("stack is empty/n"); return;}
*e=(*s)->data;
p=*s;
(*s)=(*s)->next;
free (p);}
void InitStackL(LinkStack *s)
{
  *s=NULL;
}
void GetTopL(LinkStack s,ElemType *e)
{
if(s==NULL)
{ printf ("Stack is empty/n");
        return;
}
*e=s->data;
}
void InitStack(SqStack *s)
{s->top=-1;}
int StackEmpty(SqStack s)
{if (s.top==-1) return 1;
else return 0;}
void Push(SqStack *s, ElemType e)
{if (s->top==MAXSIZE-1)    //判栈满
{
     printf ("Stack is full/n"); return;
}
s->elem[++s->top]=e;
}
void Pop(SqStack *s, ElemType *e)
{
    if (s->top==-1)     /*判栈空*/
  {printf ("Stack is empty/n");
        return;
     }
*e=s->elem[s->top--];
}
void GetTop(SqStack s, ElemType *e)
{if (s.top==-1)     //判栈空
{printf ("Stack is empty/n"); return;}
*e=s.elem[s.top];}
void conversion(int N, int r){
/*将一个非负十进制数N转换成r进制数*/
SqStack s; ElemType x;
InitStack(&s);        /*构造空栈*/
while(N)
{Push(&s, N%r);     /*余数入栈*/
  N=N/r;            /*商作为被除数继续运算*/
}
while(!StackEmpty(s))
{Pop(&s, &x);
  printf("%d", x);
}
}
int main()
{
int x,y;
SqStack s;
LinkStack L;
InitStack(&s);
Push(&s,1);
Push(&s,2);
Push(&s,3);
Pop(&s,&y);
printf("%d ",y);
Pop(&s,&y);
printf("%d ",y);
GetTop(s,&y);
printf("%d ",y);
Pop(&s,&y);
printf("%d ",y);
Pop(&s,&y);

InitStackL(&L);

scanf("%d",&x);
printf("/n转换为:");
conversion(x,8);
return 0;
}