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 7892375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:51:15+00:00 2026-06-03T06:51:15+00:00

This program is to create a link list. When I run this program it

  • 0

This program is to create a link list.
When I run this program it gives me a segmentation fault.

#include <stdio.h>
#include <stdlib.h>
struct node  {
    int data;
    struct node *next, *prev;
};
struct node *root=NULL;
void push (int);
void pop (void );
struct node * create_node (int );
void travel (void );
int main()
{
    int i, j, choice, count;
    printf("enter choice\n");
    scanf("%d", &choice);
    count = 0;
    while (choice == 1) {
        printf("enter a data element");
        scanf("%d", &j);
        count++;
        printf("enter choice\n");
        scanf("%d", &choice);
    }
    printf("the link list is \n");
//travel function to be created
    travel();
}

void push(int data)
{
  struct node *t1;
   t1=root;
  while( t1->next!=NULL)
  {
   t1=t1->next;
  }
   t1->next=create_node( data);
}

void pop()
{
}


void travel (void )
 {
  struct node *t1;
   t1=root;
  while (t1->next!=NULL)
  {
       printf("%d ",t1->data);
   }
   printf("%d ",t1->data);
 }
struct node * create_node (int data)
 {
   struct node *p = (struct node *) malloc (sizeof(struct node));
    p->data=data;
    p->next=NULL;
    p->prev=NULL;
    return p;   
 }

What could be the error?
This is how I executed

user@ubuntu:~/programming$ ./a.out 
enter choice
1
enter a data element45
enter choice
1
enter a data element67
enter choice
1
enter a data element89
enter choice
0
the link list is 
Segmentation fault
  • 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-06-03T06:51:16+00:00Added an answer on June 3, 2026 at 6:51 am

    You never assign anything to root after its initial declaration and initialization, so it is always NULL, and you proceed to dereference it in travel().

    struct node *t1;
    t1=root;
    
    // what if root is NULL?  Too late... segfault (you hope)
    while( t1->next!=NULL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my program which creates a link list and also reverses it. #include<stdio.h>
I am receiving a Segmentation Fault error when I run this program. To summarize,
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
struct node { int data; struct node *next; }; struct node * list,root; list=NULL;
This program I use has it's own variables to set when you run it,
This suppose to be an easy link list problem, but when I add input_info()
I copy this code to create my first program with maps in java script
I'm following this guide: http://www.math.umd.edu/~dcarrera/ruby/0.3/chp_01/programs.html and I'm trying to create my first ruby program.
I bought this program that created a pretty nice imageuploader flash script however I
I have made a program that scans rss feeds. This same program creates feeds

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.