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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:37:01+00:00 2026-06-07T15:37:01+00:00

I wrote the following function which returns the middle element of a linked list,

  • 0

I wrote the following function which returns the middle element of a linked list, which uses the double pointer method

struct node
{
int data;
struct node *next;
}*start;

void middleelement()
{
struct node *x=start,*y=start;
int n=0;

if(start==NULL)
{
    printf("\nThere are no elments in the list");
}

else
{
    while((x->next)!=NULL)
    {
        x=x->next->next;
        y=y->next;
        n++;
    }

    printf("\nMiddle element is %d",y->data);
}
}

However, whenever I run the functions, the Windows explorer stops working
What is the flaw in the code?
Is there any better algorithm than this to find the middle element?

  • 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-07T15:37:04+00:00Added an answer on June 7, 2026 at 3:37 pm

    If the number of entries is odd, your x will end up being NULL, so when the next loop iteration dreferences it, your program is going to crash. You should modify your condition to account for that:

    while(x && x->next) {
        ...
    }
    

    Comparing with NULL is optional in C, so you can skip the != NULL to shorten the condition.

    Of course passing the start parameter through a global variable is unorthodox, to say the least. It would be much better to pass it as a regular function parameter.

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

Sidebar

Related Questions

I have the following function, which returns a filesize of a file over HTTP:
I have the following php function which i wrote a while ago. Now however,
I have the following JavaScript function which receives coordinates and returns the nearest tube
Consider the following scenario. I have a method which returns ISomething , but it
I wrote following function void validateUser(void) { string uName; string uPassword; char c; map
I wrote the following function in haskell, as it will enumerate every integer: integers
I wrote the following: Object.prototype.length = function(){ var count = -1; for(var i in
I want to call the Sleep function on ASM. So I wrote the following:
I wrote a linked list implementation for my java class earlier this year. This
I have written the following function which adds an event listener and registers the

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.