Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4170954
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:23:38+00:00 2026-05-21T00:23:38+00:00

I just learned how to use linked lists in C. Everything is okay if

  • 0

I just learned how to use linked lists in C. Everything is okay if I use a single struct, but I am having problems when I use two structures to separate the data properly. I am having problems allocating. I am using Visual C++ Express 2010, and here’s the entire code. Anyone could explain to me what went wrong? Thanks a lot. And this isn’t homework. I am just trying to learn it further.

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

struct Student 
{
    char name[20];
    int num;

};

struct Node
{
    struct Student data;
    struct Node *next;
};


//void initList(struct Student **);
struct Node *createNode(struct Student );
void append(struct Node **, struct Student);
void traverse(struct Node *);
void destroyList(struct Node *);
int main()
{
    char choice = 'y';
    struct Student dat;
    struct Node *head;
    head = NULL;
    //struct Student *tmp;
    //initList(&head);
    char buff[20];
    while(choice != 'n')
    {
        scanf("%s %d", buff, &dat.num);
        strcpy(dat.name, buff);
        append(&head, dat);


        printf("\n\nWould you like to enter another?");
        fflush(stdin);
        scanf("%c", &choice);
    }

    traverse(head);
    destroyList(head);

    system("PAUSE");
    return 0;
}
/*
void initList(struct Student **list)
{
    *list = NULL;
}
*/
struct Node *createNode(struct Student dat)
{
    struct Node *temp;
    temp = (struct Node *)malloc(sizeof(Node));
    temp->data = dat;

    return temp;
}

void append(struct Node **pos, struct Student d)
{
    struct Node *node;
    node = createNode(d);

    if(*pos == NULL)
    {
        *pos = node;
    }
    else
    {
        struct Node *tmp;
        tmp = *pos;
        while(tmp->next != NULL)
        {
            tmp = tmp->next;
        }
        tmp->next = node;

    }
}

void traverse(struct Node *head)
{
    struct Node *tmp;
    for(tmp = head; tmp != NULL; tmp = tmp->next)
    {
        printf("\n %s %d\n\n", tmp->data.name, tmp->data.num);
    }

}
void destroyList(struct Node *list)
{
    if(list->next == NULL)
    {
        free(list);
    }
    else
    {
        destroyList(list->next);
        free(list);
    }
    printf("\n\nList has been destroyed.\n");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T00:23:38+00:00Added an answer on May 21, 2026 at 12:23 am

    In createNode() you should set the next member to NULL; or even in append() if it makes you feel better 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just learned about how the Java Collections Framework implements data structures in linked
I just learned how to use tkinter in Python (3.2.2), and I'm having some
I've just learned one of Oracle features: External table. But when I use this
I'm new to shell,I just learned that use (command) will create a new subshell
as I've just learned in in my other question , I could use a
I just learned about how to include FxCop on a build. But it's slow
I have just learned how to use the SQLite database for local storage in
I just learned about jquery's .makeArray and I am trying to use JSON.stringify to
Having just learned that many cpp features (including the stl vector class) do not
I just learned that I could use chmod make myscript.sh executable and the run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.