简体   繁体   中英

I am Getting segmentation fault while implementing Stack Data Structure

I am getting a segmentation fault in the following code snippet. It is a code that is supposed to assign values to various properties in a trading card and to display it. I usually mess up in Data Structures, so if you guys can please suggest some resources to learn about segmentation fault and similar things that would be very helpful.

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

typedef struct cards
{
    int power;
    int energy;
    int heal;
    int karma;

    struct cards *next;
}node;

node *createCards()
{
    node *new_node=(node*)malloc(sizeof(node));

    new_node->energy=500+rand()%400;
    new_node->heal=100+rand()%200;
    new_node->karma=50+rand()%100;
    new_node->power=1000+rand()%501;

    return new_node;

}

void createStack(node *head, int no_cards)
{
    if(head==NULL)
        head=createCards();

    head->next=NULL;

    int i=0;

    while(i<no_cards-1)    
    {
        node *tmp=createCards();
        tmp->next=head;
        head=tmp;

        i++;
    }
}


void displayCards(node *head)
{
    node *crt=head;
    int i=1;

    while(crt->next)
    {
        printf("\n  ------------------------------------- ");
        printf("\n |                <%d>                 |", i);
        printf("\n |                                     |");
        printf("\n |   POWER :   %d                      |", crt->power);
        printf("\n |                                     |");
        printf("\n |                                     |");
        printf("\n |   ENERGY:   %d                      |", crt->energy);
        printf("\n |                                     |");
        printf("\n |                                     |");
        printf("\n |   HEAL  :   %d                      |", crt->heal);
        printf("\n |                                     |");
        printf("\n |                                     |");
        printf("\n |   KARMA :    %d                     |", crt->karma);
        printf("\n |                                     |");
        printf("\n  -------------------------------------");

        i++;
        crt=crt->next;
    }
}

node *player1=NULL;
int main()
{
    createStack(player1, 10);

    displayCards(player1);
}

I suspect you want to write the createStack() function like

void createStack (node *head, int no_cards) {
    if (head == NULL) {
        head = createCards();
    }

    int i = 0;
    node *tmp = head;

    while(i < no_cards) {
        tmp->next = createCards();
        tmp = tmp->next;
        i++;
    }
}

And I notice that you are creating a linked list and not a stack. And remove the line crt->next at the bottom of the while loop in function displayCards() . Thanks for asking :)

PS If you have any other queries, do comment. I will edit my answer to answer that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM