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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:28:47+00:00 2026-05-26T11:28:47+00:00

Here is my implementation of count: int count(node *start) { static int l ;

  • 0

Here is my implementation of count:

int count(node *start)
{
    static int l ;
    node *current;            /* Node for travelling the linked list*/
    current=start;
    if(current->next!=start)
    {
        l = 1 + count ( current->next ) ;
        return ( l ) ;
    }


    else
    {
        return(1);
    }
}

Here is a fragment of main function where I am calling it:

void main()
{
    node *head;
printf ( "Length of linked list = %d", count ( head ) ) ;
}

Here is the structure:

struct cirdoublelinklist
{
    struct cirdoublelinklist *prev;  /** Stores address of previous node **/
    int value;                   /** stores value **/
    struct cirdoublelinklist *next;  /** stores address of next node **/
};

/** Redefining list as node **/
  typedef struct cirdoublelinklist node;

On running and trying to see the length of a list, it crashes with out of bound memory. Please help me with this, I have been working on this for a long time now.

Method to add the first node:

void initialize(node *start)
{
    start->prev=start;
    printf("\nEnter Value\n");
    scanf("%d",&start->value);
    start->next=start;
}

Method to add subsequent nodes after the location specified:

void insert_after(node *start)
{
    int num;                  /* value for inserting a node */
    int flag=0;
    node *newnode;            /* New inputed node*/
    node *current;            /* Node for travelling the linked list*/
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the value after which you want to insert a node\n");
    scanf("%d",&num);
    init(newnode);
    current=start;
    while(current->next!=start)
    {

        if(current->value==num)
        {
            newnode->next=current->next;
            current->next->prev=newnode;
            current->next=newnode;
            newnode->prev=current;
            flag=1;
        }
        current=current->next;
    }
    if(flag==0 && current->next==start && current->value==num)
    {
        /***  Insertion checking for last node  ***/
        newnode->next=current->next;     /* Start is being copied */
        current->next->prev=newnode;
        current->next=newnode;
        newnode->prev=current;
        flag=1;
    }
    if(flag==0 && current->next==NULL)
        printf("\nNo match found\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-26T11:28:48+00:00Added an answer on May 26, 2026 at 11:28 am

    Every time you call count, it has a new start, so current->next!=start is always comparing a node to its successor, which will only ever end if the list has length 1. What you most likely want to do is have two functions:

    int count(node *start)
    {
        if(start == NULL)
            return 0;
        return count_helper(start, start);
    }
    
    int count_helper(node *start, node *current)
    {
        static int l;
        if(current->next!=start)
        {
            l = 1 + count (start, current->next);
            return ( l ) ;
        }
        else
        {
            return(1);
        }
    }
    

    As others have mentioned, the static variable is not necessary. A better way of writing what I have called count_helper would be:

    int count_helper(node *start, node *current)
    {
        if(current->next!=start)
        {
            return 1 + count (start, current->next);
        }
        else
        {
            return 1;
        }
    }
    

    Finally, a more efficient implementation would be non-recursive:

    int count(node *start)
    {
        if(start == NULL)
            return 0;
        node *current = start->next;
        int c = 1;
        while(current != start)
        {
            c++;
            current = current->next;
        }
        return c;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a Clone() implementation for my class: MyClass^ Clone(){ return gcnew MyClass(this->member1, this->member2); }
I am trying to build my own implementation of a linked list in C++.
I try to implement the word count example by myself, here's my implementation of
Here is my implementation in summary 1) all DAOs implemented using HibernateDAO support/ @Transational
I'm having some difficulties getting the TabActivity to work. Here's the implementation of the
Here is my short implementation of Russian Peasant Multiplication . How can it be
Here is my perceptron implementation in ANSI C: #include <stdio.h> #include <stdlib.h> #include <math.h>
Here is a very basic class for handling sessions on App Engine: Lightweight implementation
I am missing something embarrassingly basic here in my Merge Sort implementation: # include
Im looking to make my own implementation of these Identicons or Gravatars found here

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.