#include <stdio.h>
#include <malloc.h>
typedef struct node {
int info;
struct node *next;
} Stack;
void push(Stack**head, int value){
Stack
*q
=(Stack
*)malloc(sizeof(Stack
)); q->info = value;
q->next = *head;
*head = q;
}
int isEmpty(Stack*head) {
if(head==NULL) return 1;
return 0;
}
void pop(Stack**head) {
Stack*q;
q = *head;
if(isEmpty(*head)) return;
(*head) = (*head)->next;
}
int top(Stack**head){
if(!isEmpty(*head))
return (*head)->info;
return -1;
}
void topPlusPlus(Stack**head) {
(*head)->info = (*head)->info + 1;
}
void write(Stack*head) {
while(head){
head = head->next;
}
}
int main(int argc, char const *argv[]) {
Stack*head=NULL;
int n;
push(&head,1);
while(!isEmpty(head)) {
write(head);
if(top(&head) < n) push(&head,top(&head)+1);
else {
pop(&head);
if(!isEmpty(head)) topPlusPlus(&head);
}
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KCnR5cGVkZWYgc3RydWN0IG5vZGUgewogICAgaW50IGluZm87CiAgICBzdHJ1Y3Qgbm9kZSAqbmV4dDsKfSBTdGFjazsKCnZvaWQgcHVzaChTdGFjayoqaGVhZCwgaW50IHZhbHVlKXsKICAgICBTdGFjaypxPShTdGFjayopbWFsbG9jKHNpemVvZihTdGFjaykpOwogICAgIHEtPmluZm8gPSB2YWx1ZTsKICAgICBxLT5uZXh0ID0gKmhlYWQ7CiAgICAgKmhlYWQgPSBxOwp9CgppbnQgaXNFbXB0eShTdGFjaypoZWFkKSB7CiAgaWYoaGVhZD09TlVMTCkgcmV0dXJuIDE7CiAgcmV0dXJuIDA7Cn0KCnZvaWQgcG9wKFN0YWNrKipoZWFkKSB7CiAgICAgU3RhY2sqcTsKICAgICBxID0gKmhlYWQ7CiAgICAgaWYoaXNFbXB0eSgqaGVhZCkpIHJldHVybjsKICAgICAoKmhlYWQpID0gKCpoZWFkKS0+bmV4dDsKICAgICBmcmVlKHEpOwp9CgoKaW50IHRvcChTdGFjayoqaGVhZCl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