2

我在使用带有结构的链表实现堆栈时遇到问题。该程序编译得很好,但是当我运行它时,它会打印第一个元素,然后将下一个节点读取为 NULL。我认为我将堆栈传递给 push 方法可能是一个错误,但我不确定并且我没有成功修复它,所以我请求您的帮助:

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

任何帮助,将不胜感激。

谢谢

4

3 回答 3

2

我可以看到几个问题:

1)printStack(&stack);应该printStack(stackPtr);像您将地址传递stackPtr给 push 函数一样。

2)

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

应该:

node = malloc(sizeof(StackNode));

3)

push(stackPtr, 'a');

应该:

push(&stackPtr, 'a');

因为您需要传递顶部指针的地址。

于 2012-05-17T10:17:33.677 回答
1

这是不正确的:

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

因为它只为 a 分配内存struct stackNode*(对于任何指针类型通常为 4 字节),所以它应该为 a 分配内存struct stackNode(至少 5 个字节):

node = malloc(sizeof(StackNode));

--

请参阅是否强制转换 malloc 的结果?

于 2012-05-17T10:27:21.167 回答
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;
}
于 2015-10-02T17:44:44.623 回答