#include <stdio.h>
#include <stdlib.h>
typedef struct pilhaElement{
int data;
struct pilhaElement *next;
}PilhaElment;
typedef struct pilha{
PilhaElment *head;
PilhaElment *tail;
int size;
}Pilha;
Pilha *iniciarpilha(){
Pilha *pi = (Pilha *) malloc(sizeof(Pilha));
if(pi!=NULL){
pi->size = 0;
pi->head = NULL;
pi->tail = NULL;
}
return pi;
}
void liberapilha(Pilha *pi){
if(pi != NULL){
PilhaElment *node;
while(pi->head!=NULL){
node = pi->head;
pi->head = pi->head->next;
free(node);
}
free(pi);
}
}
void push(Pilha *pi,int num){
if(pi == NULL){
return;
}
PilhaElment *node = (PilhaElment *) malloc(sizeof(PilhaElment));
if(pi->size == 0){
node->data = num;
node->next = NULL;
pi->head = node;
pi->tail = node;
}
else{
node->data = num;
node->next = NULL;
pi->tail->next = node;
pi->tail = node;
}
(pi->size)++;
}
void printpilha(Pilha *pi){
if(pi == NULL){
printf("Pilha vazia.\n");
return;
}
PilhaElment *node = pi->head;
while(node != NULL){
printf("%d ",node->data);
node = node->next;
}
}
void pop(Pilha *pi){
if(pi == NULL){
printf("Pilha vazia.\n");
}
PilhaElment *node = pi->head;
while(node!=pi->tail){
node = node->next;
}
node->next = NULL;
free();
}
int main(){
Pilha *pi = iniciarpilha();
push(pi,1);
push(pi,2);
push(pi,3);
printpilha(pi);
pop(pi);
printf("\n%d",pi->tail->data);
//printpilha(pi);
return 0;
}
Someone helps me solve the problem of the pop function, it should be pulling off at the end, but I'm breaking the stack when I try, the stack insertion is in the end.