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

  • Home
  • SEARCH
  • 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 9246761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:34:06+00:00 2026-06-18T09:34:06+00:00

I’ve written an implementation of a doubly linked list with a .h prototype here

  • 0

I’ve written an implementation of a doubly linked list with a .h prototype here and everything runs fine until I start entering values in the terminal. I get a segmentation fault after entering the second value, but, if I just use 1 value it executes normally. I’ve gone through it several times, but, I can’t find my mistake. Could you guys help me find why I’m getting the error?

Here’s the .h file:

#include <stdio.h>
typedef struct node Node;

struct node
{
    int d;
    Node *link;
}*head,*current,*prev;

int num_nodes;

void linked_list_init(int data);
void linked_list_sort();
void linked_list_print();

And here’s the .c file:

#include "link.h"
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

void main(){

    int n,e,i;
    printf("How many numbers do you want to sort: ");
    scanf("%d",&e);
    for(i=0;i<e;i++){
        printf("Enter number: ");
        scanf("%d",&n);
        linked_list_init(n);
    }
    linked_list_sort();
    printf("The sorted numbers are: ");
    linked_list_print();
}

void linked_list_init(int data){
    Node *prev=0,*next=0;

    current=(Node*)malloc(sizeof(Node));    
    if(head==0)
    {
        head=current;
        current->d=data;
        current->link=0;
        prev=current;

    }
    else{
        current->d=data;
        current->link=0;
        prev->link=current;
        prev=current;
    }
    }

void linked_list_sort(){
    int i,j;
    Node *prev=0,*next=0;

    current=head;
    prev=head;
    next=head->link;

    for(i=0;i<num_nodes-1;i++)
    {
        for(j=0;j<num_nodes-i-1;j++)
        {
            if(current->d>next->d)
            {
                current->link=next->link;
                next->link=current;
                if(current==head)
                {
                    head=next;prev=next;
                }
                else
                {
                    prev->link=next;prev=next;
                }
                if(next!=0) //check whether final node is reached
                    next=current->link;

            }
            else //move each node pointer by one position
            {
                prev=current;
                current=next;
                next=current->link;
            }

        }
        //next iteration
        current=head;
        prev=head;
        next=current->link;
    }

}

void linked_list_print(){
    current=head;
    while(current!=0){
        printf("%d ",current->d);
        current=current->link;
    }
}
  • 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-18T09:34:07+00:00Added an answer on June 18, 2026 at 9:34 am

    The problem is that you’re masking the global variables with local declarations in your function. That means that the variable prev in the functions are not the same as the global variable prev.

    Aside from that, you should never place variable definitions in header files, as those will clash with each other if the header file is included in multiple files.

    There is also another small bug, in that you don’t increase the counter when inserting a new node into the list.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.