在C中实现带有链表的堆栈

时间:2012-05-17 10:11:41

标签: c list linked-list stack

我在使用带有struct的链表实现Stack时遇到了麻烦。程序编译得很好,但是当我运行它时,它打印第一个元素,然后将下一个节点作为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);
}

任何帮助都将不胜感激。

由于

3 个答案:

答案 0 :(得分:2)

我可以看到几个问题:

1)printStack(&stack);应为printStack(stackPtr);,因为您将stackPtr的地址传递给推送功能。

2)

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

应该是:

node = malloc(sizeof(StackNode));

3)

push(stackPtr, 'a');

应该是:

push(&stackPtr, 'a');

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

答案 1 :(得分:1)

这是不正确的:

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

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

node = malloc(sizeof(StackNode));

-

请参阅Do I cast the result of malloc?

答案 2 :(得分: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;
}