msafi
msafi

Reputation: 377

Segmenation Fault - Working on stack data structure

I'm attempting to create a program that would allow the user to create a stack data structure using the push, pop, and peek command. However, I keep running into a segmentation fault whenever I try and use the push command! I have no idea why it's not working, because I made sure to use malloc on the stack structure. The peek and pop command are working (as far as I can tell). Any help??

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

int mainMenuChoice, pushValue;


void mainMenu() {
    printf("\nEnter your option: \n");
    printf("1. Push\n");
    printf("2. Pop\n");
    printf("3. Peek\n");
    printf("4. Exit\n");
    scanf("%d", &mainMenuChoice);
}

Stack * initializeStack() {
    Stack *new_stack;
    int capacity = 100;
    new_stack = (Stack *)malloc(sizeof(Stack));
    new_stack->items = (int *)malloc(sizeof(int)*capacity);
    new_stack->size = 0;
    return new_stack;
}

void push(Stack *new_stack, int item) {
    new_stack->items[new_stack->size++] = item;
}
void pop(Stack *new_stack) {
    if(new_stack->size == 0) {
        printf("The stack is empty, you can't pop any items!\n");
    } else {
        new_stack->size--;
    }
}
int peek(Stack *new_stack) {
    if(new_stack->size == 0) {
        printf("The stack is empty.\n");
    } else {
        return new_stack->items[new_stack->size-1];
    }
}


void menuOptions(int option) {
    Stack *new_stack = initializeStack();
    if(option == 1) {
        Stack *new_stack;
        printf("Enter an element to push: ");
        scanf("%d", &pushValue);
        push(new_stack, pushValue);
        mainMenu();
        menuOptions(mainMenuChoice);
    } else if(option == 2) {
        pop(new_stack);
        mainMenu();
        menuOptions(mainMenuChoice);        
    } else if(option == 3) {
        peek(new_stack);
        mainMenu();
        menuOptions(mainMenuChoice);
    } else if(option == 4) {
        printf("Exiting...\n");
        exit(0);
    } else {
        printf("Invalid input, please try again!\n");
        mainMenu();
        menuOptions(mainMenuChoice);
    }
}

void program() {
    mainMenu();
    menuOptions(mainMenuChoice);    
}

Also, this is the how I've structured the stack:

typedef struct Stack {
    int size;
    int *items;
}Stack;

Thank you so much in advance, I appreciate it!

Upvotes: 0

Views: 36

Answers (1)

Craig Estey
Craig Estey

Reputation: 33631

I've simplified things a bit. You were having too many recursive calls and new_stack was being redefined [but not reallocated]. You'll still have more work to do (e.g. peek returns a value, but pop does not)

Here's the code [please pardon the gratuitous style cleanup]:

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

typedef struct Stack {
    int size;
    int *items;
} Stack;

int mainMenuChoice;
int pushValue;
Stack *top_stack;

void
mainMenu()
{
    printf("\nEnter your option: \n");
    printf("1. Push\n");
    printf("2. Pop\n");
    printf("3. Peek\n");
    printf("4. Exit\n");
    scanf("%d", &mainMenuChoice);
}

Stack *
initializeStack()
{
    Stack *new_stack;
    int capacity = 100;

    new_stack = (Stack *) malloc(sizeof(Stack));
    new_stack->items = (int *) malloc(sizeof(int) * capacity);
    new_stack->size = 0;

    return new_stack;
}

void
push(Stack *new_stack, int item)
{
    new_stack->items[new_stack->size++] = item;
}

void
pop(Stack *new_stack)
{
    if (new_stack->size == 0) {
        printf("The stack is empty, you can't pop any items!\n");
    }
    else {
        new_stack->size--;
    }
}

int
peek(Stack *new_stack)
{
    if (new_stack->size == 0) {
        printf("The stack is empty.\n");
        return -1;
    }
    else {
        return new_stack->items[new_stack->size - 1];
    }
}

void
menuOptions(Stack *new_stack,int option)
{

    if (option == 1) {
        printf("Enter an element to push: ");
        scanf("%d", &pushValue);
        push(new_stack, pushValue);
    }
    else if (option == 2) {
        pop(new_stack);
    }
    else if (option == 3) {
        peek(new_stack);
    }
    else if (option == 4) {
        printf("Exiting...\n");
        exit(0);
    }
    else {
        printf("Invalid input, please try again!\n");
    }
}

int
main()
{

    top_stack = initializeStack();

    while (1) {
        mainMenu();
        menuOptions(top_stack,mainMenuChoice);
    }

    return 0;
}

Upvotes: 2

Related Questions