-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10to2.c
More file actions
98 lines (90 loc) · 1.87 KB
/
Copy path10to2.c
File metadata and controls
98 lines (90 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// main.c
// AM
//
// Created by 余彬 on 15/7/10.
// Copyright (c) 2015年 余彬. All rights reserved.
//
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int num;
struct Node * pNext;
}Node,*pNode;
typedef struct Stack{
pNode pBase;
pNode pOver;
}Stack,*pStack;
int Show(pStack Stack){
pNode pTmp;
if(Stack->pBase==NULL){
printf("The Stack is empty!\n");
return 0;
}else {
pTmp=Stack->pBase;
while(pTmp!=NULL){
printf("%d ",pTmp->num);
pTmp=pTmp->pNext;
}
printf("\n");
return 1;
}
}
int Push(pStack Stack,int i){
pNode pNew;
pNew=(pNode)malloc(sizeof(Node));
if(pNew==NULL){
printf("Fail!\n");
return 0;
}else {
pNew->num=i;
if(Stack->pBase==NULL){
Stack->pBase=Stack->pOver=pNew;
}
else {
Stack->pOver->pNext=pNew;
Stack->pOver=pNew;
}
}
pNew->pNext=NULL;
return 1;
}
int Pop(pStack Stack){
pNode pTmp;
if(Stack->pBase==NULL){
printf("The stack has been empty!\n");
return 0;
}else {
pTmp=Stack->pBase;
if(Stack->pBase!=Stack->pOver){
printf("%d ",Stack->pOver->num);
while(pTmp->pNext!=(Stack->pOver))
pTmp=pTmp->pNext;
Stack->pOver=pTmp;
}
else {
printf("%d ",Stack->pBase->num);
Stack->pBase=Stack->pOver=NULL;
}
}
return 1;
}
int main(){
Stack s;
s.pBase=s.pOver=NULL;
int i;
printf("Please enter a number(10):");
scanf("%d",&i);
while((i/2)>0){
Push(&s,i%2);
// printf("%d",i%2);
i=i/2;
}
// printf("%d\n",i%2);
Push(&s,i%2);
// Show(&s);
while(s.pBase!=NULL)
Pop(&s);
printf("\n");
return 1;
}