C类型堆栈:链接列表实现

时间:2011-09-28 06:39:45

标签: c stack linked-list

我有一个计算机科学考试即将开始,很大一部分将用于ansi C编程。我是C的新手,作为一个评论,我尝试使用链表实现堆栈。我的代码编译但它没有按预期运行。

我相信我的push()函数出错了。我假设我的堆栈引用未正确更新。

我从头开始编写以下代码进行学习/练习。任何关于如何修复我的代码或改进我的编程风格的建议都将非常感激。

谢谢你们!


stack.h

#ifndef __STACK__
#define __STACK__

#include <stdlib.h>
#include "bool.h"

#define EMPTY -1

typedef struct Node {
    int index;
    enum { INT = 0, CHAR, STRING } type;
    union {
        int i;
        char c;
        char* s;
    } value;
    struct Node* prev;
} Node;

typedef Node* Stack;

Stack init();
void push(Stack stack, int type, void* value);
void pop(Stack stack, Node* node);
void empty(Stack stack);
Bool isempty(Stack stack);

#endif

stack.c

#include <stdlib.h>
#include <string.h>
#include "stack.h"

Stack init() {
    Stack stack = (Node*) malloc(sizeof(Node));
    stack->index = EMPTY;
    stack->type = INT;
    stack->value.i = 0;
    stack->prev = 0;
    return stack;
}

void push(Stack stack, int type, void* value) {
    int length;
    Node* node = (Node*) malloc(sizeof(Node));
    node->index = stack->index + 1;
    node->type = type;
    switch(node->type) {
        case INT:
            node->value.i = *((int*) value);
        break;
        case CHAR:
            node->value.c =  *((char*) value);
        break;
        case STRING:
            length = strlen((char*) value) + 1;
            node->value.s = (char*) malloc(length * sizeof(char));
            strcpy(node->value.s, value);
        break;
    }       
    node->prev = stack;
    stack = node;
}

void pop(Stack stack, Node* node) {
    int length;
    Node* temp = stack;
    if (!isempty(stack)) {
        node->index = stack->index;
        node->type = stack->type;
        switch(stack->type) {
            case INT:
                node->value.i = stack->value.i;
            break;
            case CHAR:
                node->value.c = stack->value.c;
            break;
            case STRING:
                length = strlen(stack->value.s) + 1;
                node->value.s = (char*) malloc(length * sizeof(char));
                strcpy(node->value.s, stack->value.s);
                free(stack->value.s);
            break;
        }
        node->prev = 0;
        stack = stack->prev;
        free(temp); 
    } else {
        /*TODO: handle empty case */    
        puts("Stack empty!");
    }
}

void empty(Stack stack) {
    while (!isempty(stack)) {
        Node* temp = malloc(sizeof(Node));
        pop(stack, temp);
        free(temp);
    }
}

Bool isempty(Stack stack) {
    return stack->index == EMPTY ? TRUE : FALSE;
}

bool.h

#ifndef __BOOL__
#define __BOOL__

typedef int Bool;

#define FALSE 0
#define TRUE 1

#endif

的main.c

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

int main() {
    Stack stack = init();
    Node* node = malloc(sizeof(Node));
    int i = 5;
    push(stack, 0, &i);
    pop(stack, node);
    printf("Node value: %d\n", node->value.i);
    free(node);
    empty(stack);
    puts("done.");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

你真的没有修改堆栈。您必须使用&amp;堆栈来执行push,pop和empty方法。他们将签名如下:

void push(Stack * stack, int type, void* value);
void pop(Stack * stack, Node* node);
void empty(Stack *stack);

当然,在这些方法中使用指针内容,例如:

void push(Stack * stack, int type, void* value) {
    int length;
    Node* node = (Node*) malloc(sizeof(Node));
    node->index = (*stack)->index + 1;
    node->type = type;
    switch(node->type) {
        case INT:
            node->value.i = *((int*) value);
        break;
        case CHAR:
            node->value.c =  *((char*) value);
        break;
        case STRING:
            length = strlen((char*) value) + 1;
            node->value.s = (char*) malloc(length * sizeof(char));
            strcpy(node->value.s, value);
        break;
    }
    node->prev = *stack;
    *stack = node;
}

void pop(Stack * stack, Node* node) {
    int length;
    Node* temp = *stack;
    if (!isempty(*stack)) {
        node->index = (*stack)->index;
        node->type = (*stack)->type;
        switch((*stack)->type) {
            case INT:
                node->value.i = (*stack)->value.i;
            break;
            case CHAR:
                node->value.c = (*stack)->value.c;
            break;
            case STRING:
                length = strlen((*stack)->value.s) + 1;
                node->value.s = (char*) malloc(length * sizeof(char));
                strcpy(node->value.s, (*stack)->value.s);
                free((*stack)->value.s);
            break;
        }
        node->prev = 0;
        *stack = (*stack)->prev;
        free(temp);
    } else {
        /*TODO: handle empty case */
        puts("Stack empty!");
    }
}

void empty(Stack *stack) {
    while (!isempty(*stack)) {
        Node* temp = malloc(sizeof(Node));
        pop(stack, temp);
        free(temp);
    }
}