0

I was trying to implement stack with linked list implementation but seems to encounter 'segmentation error' . The code seems to compile and also run but when I actually enter the value of 'times' int variable in the stdin. The code shows segmentation error. The scanf also doesn't show the formatted string.

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define NAME_MAX 40

struct NODE{
    char name[NAME_MAX];
    int regno;
    int age;
    struct NODE* next;
};

struct STACK{
    struct NODE *head;
    int size;  
};

void printStack(const struct STACK *stack);
bool pushStack(struct STACK *stack);
bool makeStack(struct STACK *stack, int data_count);
void cleanStack(struct STACK *stack);

int main(){
    struct STACK list = {NULL,0};
    int times = 0;
    scanf("How many data you want to enter: %d", &times);
    if(!makeStack(&list, times)){
        fprintf(stderr, "Stack wasn't created\n");
    }
    printStack(&list);
    cleanStack(&list);
    return 0;
}

void cleanStack(struct STACK *stack){
    struct NODE *trav = stack->head;
    while(trav!=NULL){
        struct NODE *temp = trav;
        trav = trav->next;
        temp = NULL;
    }
}

void printStack(const struct STACK *stack){
    struct NODE *trav = stack->head;
    for(int counter=1; stack!=NULL; trav=trav->next,++counter,++stack){
        printf("%d: %s %d %d",counter,trav->name,trav->regno, trav->age);
    }
}

bool makeStack(struct STACK *stack, int data_count){
    while(data_count--){
        if(!pushStack(stack)){
            return false;
        }
    }
    return true;
}

bool pushStack(struct STACK *stack){
    struct NODE *temp = malloc(sizeof(struct NODE));
    if(temp==NULL) return false;
    scanf("Input Name: %s", temp->name);
    scanf("Input RegNo: %d", &temp->regno);
    scanf("Input age: %d", &temp->age);
    temp->next = stack->head;
    stack->head = temp;
    ++stack->size;
    return true;
}
4
  • Please read: How to debug small programs Commented Mar 17, 2021 at 14:45
  • @Jonathon could also share me a link from where I can learn how to use debuggers in IDE or code editors please. Commented Mar 17, 2021 at 15:13
  • Does this answer your question? Error in stack with linked list implementation Commented Mar 17, 2021 at 15:47
  • @MANA624 yeh it does but I changed and appended the cleanStack function. I took advices from this too and modified the functions as you see above. Thank you for helping though Commented Mar 18, 2021 at 5:00

1 Answer 1

0

There are a few things wrong with your implementation.

You have to first print to stdout using printf and then you can scan for an input using scanf.

printf("Number of data points: ");
scanf("%d", &times);

And in the printStack function, you need to traverse over the linked list with a while-loop.

void printStack(const struct STACK *stack)
{
    struct NODE *trav = stack->head;
    int counter = 1;
    while (trav != NULL) {
        printf("%d: %s %d %d\n", counter, trav->name, trav->regno, trav->age);
        trav = trav->next;
        counter++;
    }
}

And in cleanStack, I would free(temp) instead of setting it to NULL.

By the way... You don't need to, but you can allocate memory for struct Node *temp like this (In my opinion it looks nicer):

struct NODE *temp = malloc(sizeof *temp);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.