头文件
#include <stdio.h>
#include <stdlib.h>
#define WRONG 999;
typedef struct Stack{
int data;
struct Stack * next;
}Stack;
typedef struct Queue{
int data;
struct Queue * next;
}Queue;
int get_string_length(char * s);
int string_to_int(char * s);
int power_operation(int base, int exponent);
Stack * init_stack();
void push(Stack * bottom, int data);
int pop(Stack * bottom);
void traverse_stack(Stack * bottom);
int get_string_length(char * s);
char * int_to_string_reverse(int a);
char * int_to_string(int a);
//queue;
Queue * init_queue();
void en_queue(Queue * head, int data);
int de_queue(Queue * head);
void traverse_queue(Queue * head);
函数实现:
#include "head.h"
int power_operation(int base, int exponent){
if(base == 0){
return 0;
}
int res = 1;
for (int i = 0; i < exponent; ++i) {
res *= base;
}
return res;
}
int string_to_int(char * s){
int length = get_string_length(s);
int res = 0;
while(length){
res += (*s- 48) * power_operation(10, --length);
/**
* 10^4
*/
s++;
}
return res;
}
Stack * init_stack(){
Stack * bottom = (Stack *) malloc(sizeof (Stack));
bottom->next = NULL;
bottom->data = 0; // length;
}
void push(Stack * bottom, int data){
int length = bottom->data;
Stack * temp = bottom;
for (int i = 0; i < length; ++i) {
temp = temp ->next;
}
Stack * add_node = (Stack *) malloc(sizeof(Stack));
add_node->data = data;
add_node->next = NULL;
temp->next = add_node;
bottom->data++; // length;
}
int pop(Stack * bottom){
int length = bottom->data;
Stack * pop_node = bottom;
Stack * node_before_pop = bottom;
for (int i = 0; i < length - 1; ++i) {
node_before_pop = node_before_pop->next;
}
for (int i = 0; i < length; ++i) {
pop_node = pop_node->next;
}
int res = pop_node->data;
node_before_pop->next = NULL;
free(pop_node);
bottom->data--;
return res;
}
void traverse_stack(Stack * bottom){
int length = bottom->data;
Stack * temp = bottom->next;
printf("bottom ----- top\n");
for (int i = 0; i < length; ++i) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int get_string_length(char * s){
int count = 0;
while (*s){ // s = 0 false;
s++;
count ++;
}
return count;
}
//queue
Queue * init_queue(){
Queue * head = (Queue*) malloc(sizeof(Queue));
head->data = 0; // length;
head->next = NULL;
return head;
}
void en_queue(Queue * head, int data){
Queue * temp = head;
int length = head->data;
for (int i = 0; i < length; ++i) {
temp = temp->next;
}
Queue * node = (Queue *) malloc(sizeof(Queue));
node->data = data;
node->next = NULL;
temp->next = node;
head->data++;
}
int de_queue(Queue * head){
if(head->data){
Queue * temp = head->next;
int res = temp->data;
head->next = temp->next;
free(temp);
return res;
}
return WRONG; // wrong;
}
void traverse_queue(Queue * head){
printf("front --- behind\n");
while (head->next){
printf("%d ",head->next->data);
head = head->next;
}
printf("\n");
}
char * int_to_string_reverse(int a){
Queue * qhead = init_queue();
int temp = a;
while(temp){
en_queue(qhead, temp%10);
temp /= 10;
}
printf("\n");
traverse_queue(qhead);
int length = qhead->data;
printf("%d\n",length);
char * s = (char *) malloc((length + 1) * sizeof(char));
for (int i = 0; i < length; ++i) {
*(s + i) = de_queue(qhead) + 0x30;
}
*(s+length) = '\0';
return s;
}
char * int_to_string(int a){
Stack * s = init_stack();
int temp = a;
while(temp){
push(s, temp%10);
temp /= 10;
}
traverse_stack(s);
int length = s->data;
char * ss = (char *) malloc(sizeof(char) * (length + 1));
for (int i = 0; i < length; ++i) {
*(ss+i) = pop(s) + 0x30;
}
*(ss+length) = '\0';
return ss;
}
测试:
#include "head.h"
int main(){
// printf("%d\n", '\0');// 0
Queue * qhead = init_queue();
int a = 123;
char * s_reverse = int_to_string_reverse(a);
printf("int_to_string_reverse(a) : %s\n",s_reverse);
printf("========\n");
char * s_normal = int_to_string(a);
printf("int_to_string(a) : %s\n", s_normal);
return 0;
}
运行结果: