堆栈的字符串

时间:2012-10-07 22:19:02

标签: c string stack

嗨我这里有程序接受int作为值。我想将它翻译为接受数组中的字符串。我已阅读有关使用struct但我无法进入它。我希望有人可以帮助我进入,而不使用结构我不知道从哪里开始我想保留这行代码。

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

int top = 0;
int *stack = NULL;
int size = 0;

main()
{
    int opt, num;
    char cont[] = { 'y' };
    clrscr();

    /* <start Declaring Stack Size { */
               printf("Stacking Program");
               printf("\n\nData Size: ");
               scanf("%d", &size);
               printf("\n");
    /* } end> */

               /* <start Allocates size of stack { */
               if(size > 0)
               {
                stack = malloc(size * sizeof(int));
                if(stack == NULL)
                {
                    printf("ERROR: malloc() failed\n");
                    exit(2);
                }
               }
               else
               {
                    printf("ERROR: size should be positive integer\n");
                    exit(1);
               }
                /* } end> */

    while((cont[0] == 'y') || (cont[0] == 'Y'))
    {
        clrscr();

        /* <start Main Menu { */
            printf("Stacking Program");
            printf("\n\nData Size: %d\n\n", size);
            printf("MAIN MENU\n1. Pop\n2. Push\n3. Pick\n4. View\nChoose: ");
            scanf("%d", &opt);
            printf("\n");

        switch(opt) {
            case 1:
                pop();
                break;
            case 2:
                if(top==size)
                {
                    printf("You can't push more data");
                }
                else
                {
                    printf("Enter data for Stack[%d]: ", top+1);
                    scanf("%d", &num);
                    push(num);
                }
                break;
            case 3:
                pick();
                break;
            case 4:
                view();
                break;
            default:
                printf("Your choice is not on the list.");
                break;
        }
        /* } end> */

        printf("\n\nDo you want continue\(Y\/N\)?");
        scanf("%s", &cont[0]);
    }
    free(stack);
}

pop()
{
    int a;
    loading();
    if(top <= 0)
    {
        printf("Stack empty.");
        return 0;
    }
    else
    {
        top--;
        a=stack[top];
        printf("\(Stack[%d] = %d\) removed.", top+1, a);
    }
}
push(int a)
{
        stack[top]=a;
        top++;
        loading();
}
pick()
{
    loading();
    if(top <= 0)
    {
        printf("Nothing to display.");
        return 0;
    }
    else
    {
        printf("\(Stack[%d] = %d\) is the last data.", top, stack[top-1]);
    }
}
view()
{
    int i;
    loading();
    if(top <= 0)
    {
        printf("Nothing to display.");
        return 0;
    }
    else
    {
        for(i=0;i<top;i++)
        {
            printf("Stack[%d] = %d\n", i+1, stack[i]);
        }
    }
}

loading()
{
    float i, x;
    float load;
    int loadarea[] = { 5000, 10000, 15000, 20000, 25000, 30000 };
    int percentLoad;
    x=0;
    load=0;
    percentLoad = loadarea[random(5)];
    gotoxy(26,11);
    printf("[");
    for(i=0;i<25;i++)
    {
        x = i+27;
        gotoxy(x, 11);
        printf("=");
        delay(percentLoad);
        gotoxy(51,11);
        printf("]");
        gotoxy(53,11);
        load=(i/25)*104.5;
        if(load>100)
            load = 100.00;
        printf("%.2f\%",load);
    }
    delay(60000);
    for(i=0;i<60;i++) {
        printf("\b \b");
    }
    printf("\n");
}

1 个答案:

答案 0 :(得分:2)

最简单的方法是将您的堆栈转换为存储char*而不是int

char **stack;
stack = malloc( size * sizeof(char*) );

现在,您的push操作将接受来自某个缓冲区的char*,该缓冲区存储刚刚输入的字符串,使用strdup复制它,并将该新指针存储在堆栈中

typedef enum {
    STACK_MEM_ERROR = -1,
    STACK_FULL = 0,
    STACK_OK = 1
} StackStatus;

StackStatus push(const char *str)
{
    char *newstr;
    if( top >= size ) return STACK_FULL;
    newstr = strdup(str);
    if( newstr == NULL ) return STACK_MEM_ERROR;
    stack[top++] = newstr;
    return STACK_OK;
}

当你pop一个字符串时,你只需要一个指针。

char *pop()
{
    if( top == 0 ) return NULL;
    return stack[--top];
}

当您完成指针时(通过调用free),您有责任释放该内存。

char * val;
while( NULL != (val = pop()) )
{
    printf( "I popped: %s\n", val );
    free(val);
}