【问题标题】:implementing stack with linked list in C在C中使用链表实现堆栈
【发布时间】:2012-05-24 21:50:17
【问题描述】:

我在使用带有结构的链表实现堆栈时遇到问题。该程序编译得很好,但是当我运行它时,它会打印第一个元素,然后将下一个节点读取为 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);
}

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: c list linked-list stack


    【解决方案1】:
    #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】:

    我可以看到的几个问题:

    1) printStack(&amp;stack); 应该是 printStack(stackPtr);,因为您将 stackPtr 的地址传递给推送函数。

    2)

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

    应该是:

    node = malloc(sizeof(StackNode));
    

    3)

    push(stackPtr, 'a');
    

    应该是:

    push(&stackPtr, 'a');
    

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

    【讨论】:

    • 非常感谢,比我意识到的问题多得多,但现在感谢您解决。
    • 堆栈中没有 I 或溢出 :)
    【解决方案3】:

    这是不正确的:

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

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

    node = malloc(sizeof(StackNode));
    

    --

    Do I cast the result of malloc?

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 2021-10-30
      相关资源
      最近更新 更多