I’m trying to create a program that accepts a string of characters, which should be able to perform push, pop and peek. The code is running, but the program only accepts the first letter of the string. I’ve tried many different ways to implement the code, but I’m still not getting the program to work as expected. I have a fair understanding how to use stack to accept numbers, but I’m unable to perform the task on a string of characters. Each node should store one character from the word.

Note: some functions were snippets of code from online source that were slightly implemented in a different way.

#define MAX 100

struct stack
{
    char data;
    struct stack *next;
};

struct stack* top;

int size = 0;

void push(char *element);
char pop();
int peek();
void display();

int main()
{
    int choice;
    char str[MAX];

    printf("******** Stack using linked list ********\n\n");

    printf("Enter a word(no longer than 100 characters) :");
    scanf("%s",str);
    char* strcpy(str);

    while(1)
    {
        printf("******** Menu ********\n");
        printf(" 1. Push\n 2. Pop\n 3. Peek \n 4. Display\n 5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1:
                push(str);
                break;

            case 2:
                *str = pop();

                if (str != '\0')
                    printf("Data => %s\n", str);
                break;

            case 3: if(isEmpty(top)){
                        printf("Stack is empty!\n");
                    }else{
                        printf("Stack is not empty!\n");
                    }

                    peek(str);
                break;

            case 4: display();
                break;

            case 5:
                printf("\nProgram Exited\n");
                exit(0);
                break;

            default:
                printf("Incorrect selection!\n");
        }

        printf("\n");
    }

    //reverse(str);

    return 0;
}

void push(char *element)
{
    int i;
    if (size >= MAX)
    {
        printf("Stack Overflow.\n");
        return;
    }

    struct stack * newNode = (struct stack*)malloc(sizeof(struct stack));
    newNode -> data = *element;
    newNode -> next = top;
    top = newNode;

    size++;

    printf("\nSuccessfully added to stack!!\n");
}

char pop()
{
    char data;
    struct stack *newNode;

    if (size <= 0 || !top)
    {
        printf("Stack is empty.\n");
        return 1;
    }

    newNode = top;
    data = top->data;
    top = top->next;
    free(newNode);

    size--;

    return data;
}

int isEmpty(struct stack* root)
{
    return root == NULL;
}

int peek(struct stack *top;)
{
    if (!isEmpty(top))
        return top-> data;
    exit(0);
}


void display(){

    struct stack* newNode;
    if(top == NULL){
        printf("\nStack is Empty!!!\n");
    }else{
        newNode = top;
        while(newNode != NULL){
            printf("%c--->", newNode -> data);
            newNode = newNode -> next;
        }
    }
}

I'll take a crack at this even thouh my c is a tad rusty.

If you are storing references to strings in your stack then the struct should look like

struct stack {
    char *data;
    struct stack *next;
}

otherwise all you will be able to store is a single character rather than a pointer to a string. Likewise, your pop function should return an address (pointer) rather than a char.

Because you are passing an address to push, your assignment (with the new struct def) should be

newnode->data = element;

Check the rest of your code to make sure you are consistently using pointers. One of the biggest mistakes in c is mixing pointers with non-pointers. It's the reason I gave up on c.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.