`
jaychang
  • 浏览: 719605 次
  • 性别: Icon_minigender_1
  • 来自: 嘉兴
社区版块
存档分类
最新评论

链式栈实现进制转化

 
阅读更多
#include<stdio.h>
#include<iostream>

using namespace std;

#define FALSE -1


typedef struct Node{
  int data;
  Node *next;
}Node,*Stack;

Stack top=(Node *)malloc(sizeof(Node));

void InitialStack()
{
  top->next=NULL;
}

bool IsEmpty()
{
  bool flag;
  top->next==NULL?flag=true:flag=false;
  return flag;
}

int PopNode()
{
  Node *temp=(Node *)malloc(sizeof(Node));
  temp=top->next;
  if(top->next==NULL) return FALSE;
  int data=temp->data;
  top->next=temp->next;
  free(temp);
  return data;
}

void Push(int data)
{
  Node *temp=(Node *)malloc(sizeof(Node));
  temp->data=data;
  temp->next=top->next;
  top->next=temp;
}

void procOutput()
{
  cout<<"转换后的值为:\n";
  while(!IsEmpty())
  {
    cout<<PopNode();
  }
  cout<<"\n";
}

int main()
{
  int N;
  while(cin>>N)
  {
    InitialStack();
    while(N!=0)
    {
      Push(N%2);
      N=N/2;
     }
     procOutput();
  }
  return 0;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics