数据结构课程的练习作业,记录一下
编译软件:Dev-C++
一共创建两个源代码(text1.h,text2.c)全部放在一个文件夹里。
text1.h为头文件,text2.c为主函数文件,运行时在text2.c进行编译运行。
注意事项:
text1.h 保存时直接保存为.h文件
text2.c 保存时保存为.c文件
实现代码:
text1.h
#include<stdio.h>
#include<stdlib.h>
typedef struct linknode{
int data;
struct linknode *next;
}Node;
typedef struct {
struct linknode *top;
}stack;
void init(stack *k){
k->top=NULL;
}
void push(stack *k,int x){
Node *s;
s=(Node *)malloc(sizeof(Node));
s->data=x;
s->next=k->top;
k->top=s;
}
int pop(stack *k){
if(k->top!=NULL)
{Node *q;
q=k->top;
int x=q->data;
k->top=q->next;
free(q);
return x;
}
}
void change(int n){
stack s1,*t;
t=&s1;
int m;
init(t);
while(n!=0){
m=n%2;
push(t,m);
n=n/2;
}
while(t->top!=NULL){
printf("%d",pop(t));
}
}
text2.c
#include<stdio.h>
#include "text1.h"
int main(){
int a;
printf("请输入一个十进制整数\n");
scanf("%d",&a);
change(a);
return 0;
}
运行结果:
注意:一定要在text2.c文件里启动编译运行!