Hey guys, so I'm new at programming and I'm having some troubles with my code.
My code is suppose to order the elements of a stack during the insertion -push(x), that way the biggest value should be at the top of the stack at the end.
For example, the input should be:
6 1 9 4 3 2 10 22
and the output should be:
22 10 9 6 4 3 2 1
Also, for the input I must read each line using the function fgets and to get each number of the line I must use strtok. The conversion of each number obtained with strtok to its integer can be done with the atoi function.
So my code is looking like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int info;
struct node *ant;
}node;
typedef struct Pilha{
node *topo;
}Pilha;
Pilha *P;
void cria(Pilha *P){
P->topo = NULL;
}
void push(Pilha *P, int x){
node *p;
node *aux;
node *proximo;
proximo = NULL;
aux = NULL;
p =(node*)malloc(sizeof(node));
if(p == NULL){
printf("Erro de alocacao!");
exit(1);
}
else{
p->info = x;
if(P->topo == NULL){
p->ant = NULL;
P->topo = p;
}
else{
aux = P->topo;
while(aux != NULL && aux->info > x ){
proximo = aux;
aux = aux->ant;
}
proximo->ant = p;
p->ant = aux;
}
proximo = NULL;
aux = NULL;
}
}
void converte(char string[], Pilha *P){
char *pch;
int x;
pch = strtok(string," ");
while (pch != NULL){
x = atoi(pch);
push(P, x);
pch = strtok (NULL, " ");
}
}
void imprime(Pilha *P){
node *p;
if(P->topo == NULL){
printf("A Pilha esta vazia!");
exit(1);
}
else{
p = P->topo;
while(p!=NULL){
printf("%d ", p->info);
p = p->ant;
}
}
}
void esvazia(Pilha *P){
node *p, *aux;
p = P->topo;
while(p != NULL){
aux = p;
p = p->ant;
free(aux);
}
free(P);
}
int main(){
char string[6000];
Pilha *P = (Pilha*) malloc (sizeof(Pilha));
cria(P);
printf("Informe os valores da pilha: ");
fgets(string, 6000, stdin);
converte(string, P);
imprime(P);
esvazia(P);
return 0;
}
The problem is, when I run the code the terminal stop working and I can't determine what's wrong.
If you guys could lead me the way and show where I'm missing it would be great!