implémenter une stack avec liste chaînée en C

Je n’arrive pas à implémenter une stack avec une liste chaînée avec struct. Le programme comstack bien, mais lorsque je l’exécute, il affiche le premier élément, puis lit le nœud suivant sous la forme NULL. Je pense que c’est peut-être une erreur de passer de la stack à la méthode push, mais je ne suis pas sûr et je n’ai pas réussi à la réparer, alors je sollicite votre aide:

#include  #include  struct stackNode{ char data; struct stackNode *nextPtr; }; typedef struct stackNode StackNode; typedef StackNode *StackNodePtr; void convertToPostfix(char infix[], char postfix[]); int isOperator(char c); int precedence(char operator1, char operator2); void push(StackNodePtr *topPtr, char value); char pop(StackNodePtr *topPtr); char stackTop(StackNodePtr topPtr); int isEmpty(StackNodePtr topPtr); void printStack(StackNodePtr topPtr); int main(){ convertToPostfix(NULL, NULL); return 0; } void convertToPostfix(char infix[], char postfix[]){ StackNode stack = {'(', NULL}; StackNodePtr stackPtr = &stack; push(stackPtr, 'a'); //printf("%s\n", stackPtr->data); printStack(&stack); } void push(StackNodePtr *topPtr, char value){ StackNode *node; node=(StackNodePtr)malloc(sizeof(StackNodePtr)); node->data=value; node->nextPtr=*topPtr; *topPtr=node; } void printStack(StackNodePtr topPtr){ if(topPtr == NULL){ printf("%s\n", "NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"); return; } printf("%c\n", topPtr->data); printStack(topPtr->nextPtr); } 

Toute aide serait appréciée.

Merci

Plusieurs problèmes que j’ai pu voir:

1) printStack(&stack); devrait être printStack(stackPtr); lorsque vous passez l’adresse de stackPtr à la fonction push.

2)

 node = (StackNodePtr)malloc(sizeof(StackNodePtr)); 

devrait être:

 node = malloc(sizeof(StackNode)); 

3)

 push(stackPtr, 'a'); 

devrait être:

 push(&stackPtr, 'a'); 

Comme vous devez passer l’adresse du pointeur supérieur.

Ceci est une erreur:

 node=(StackNodePtr)malloc(sizeof(StackNodePtr)); 

comme il struct stackNode* mémoire que pour un struct stackNode* (généralement 4 octets pour tout type de pointeur), alors qu’il devrait allouer de la mémoire pour un struct stackNode (au moins 5 octets):

 node = malloc(sizeof(StackNode)); 

Voir Dois-je jeter le résultat de Malloc?

 #include  #include  struct node { int data; struct node * link; }; void push(struct node **, int); int pop(struct node **); void display(struct node *); void printMenu(); int main() { struct node * p; p = NULL; int data, ch, data1; //char choice[10]; do { printMenu(); printf("Enter your choice\n"); scanf("%d", &ch); switch (ch) { case 1: printf("Enter the element to be pushed\n"); scanf("%d", &data); push(&p, data); break; case 2: data1 = pop(&p); if (data1 != -1000) printf("The popped element is %d\n", data1); break; case 3: printf("The contents of the stack are"); display(p); printf("\n"); break; default: return 0; } } while (1); return 0; } void printMenu() { printf("Choice 1 : Push\n"); printf("Choice 2 : Pop\n"); printf("Choice 3 : Display\n"); printf("Any other choice : Exit\n"); } void push(struct node **q, int num) { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->link = (*q); temp->data = num; (*q) = temp; } void display(struct node *q) { //Fill in the code here struct node *temp = q; if (temp == NULL) printf(" {}"); while (temp != NULL) { printf(" %d", temp->data); temp = temp->link; } } int pop(struct node **q) { //Fill in the code here struct node *temp; int item; if (*q == NULL) { printf("Stack is empty\n"); return -1000; } temp = *q; item = temp->data; *q = (*q)->link; free(temp); return item; }