2

I'm having trouble implementing a Stack using a linked list with struct. The program compiles fine but when I run it, it prints the first element but then reads the next node as a NULL. I think it might be an error with my passing of the stack to the push method but I am not sure and I have not been successful in fixing it so I'm asking for your help:

#include <stdio.h>
#include <stdlib.h>

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);
}

Any help would be appreciated.

Thanks

3 Answers 3

2

Several problems I could see:

1) printStack(&stack); should be printStack(stackPtr); as you are passing address of stackPtr to the push function.

2)

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

should be:

node = malloc(sizeof(StackNode));

3)

push(stackPtr, 'a');

should be:

push(&stackPtr, 'a');

As you need to pass the address of the top pointer.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, a lot more problems than I realised but fixed now thanks to you.
There is no I in stack or overflow :)
1

This is incorrect:

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

as it is only allocating memory for a struct stackNode* (commonly 4-bytes for any pointer type), when it should be allocating memory for a struct stackNode (at least 5 bytes):

node = malloc(sizeof(StackNode));

--

See Do I cast the result of malloc?

Comments

0
#include <stdio.h>
#include <stdlib.h>

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;
}

2 Comments

How this answer to the question ?
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.