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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:13:32+00:00 2026-06-04T17:13:32+00:00

I’m having a problem with a doubly linked list so I have two questions.

  • 0

I’m having a problem with a doubly linked list so I have two questions.

First, the description.

I’ve made one struct this way:

typedef struct team{
    char *name;
    char *teamPlace;
}Team;

And created my list this way:

typedef struct nodeTeam{
    int numberOfTeams;
    Team team;
    struct nodeTeam *next;
    struct nodeTeam *prev;
}NodeTeam;

So, my list will have an head and an tail. When I add some Team to my list, my head int numberOfTeams; will have the number of teams of my list. tail will contain the last element of my list and int numberOfTeams; after head will contain the Team ID.

My list will be created this way:

int createsListOfTeams(NodeTeam **head, NodeTeam **tail);
int addNodeTeamsSorted(NodeTeam *head, NodeTeam **tail, Team team);
void printListOfTeams(NodeTeam *listofTeams);

int main()
{
    NodeTeam *headTeams,*tailTeams;
    Team eq;
    /*Creates the doubly linked list*/
    if(createsListOfTeams(&headTeams,&tailTeams)){ /*See below this part of the code*/
        printf("\nError\n");
        return 0;
    }

    /*Teams are on a .txt file. The code for reading from a file is missing. It´s working ok so I believe it's not needed.
    After reading one line after another it will do this

    addNodeEquipasSorted(headTeams,tailTeams,eq);

    where eq is a `struct` with the team data.
    */

    /*Will print all the teams*/
    printListOfTeams(headTeams);

    return 0;
}

This is the code for creating the list:

/*Add the teams to the doubly linked list. At the end, all teams will be sorted by name*/
int createsListOfTeams(NodeTeam **head, NodeTeam **tail){
    (*head) = (NodeTeam *)malloc(sizeof(NodeTeam));

    if ((*head) == NULL){
        return -1;
    }
    (*head)->numberOfTeams = 0;
    strcpy((*head)->team.teamPlace,"");
    strcpy((*head)->team.name,"");
    (*head)->next = NULL;
    (*head)->prev = NULL;

    *tail = *head;
    return 0;
}

The code for adding (sorted by team name) a Team to my list is this:

/*Creates the doubly linked list*/
int addNodeTeamsSorted(NodeTeam  *head, NodeTeam  **tail, Team team){
    NodeTeam  *no, *aux;

    /*Memory alloc for a new node*/
    no = (NodeTeam*) malloc(sizeof(NodeTeam));
    if (no == NULL){
        return -1;
    }

    /*Updates the number of element of the list*/
    head->numberOfTeams++;

    /*Creates a copy of tail*/
    aux = (*tail);

    /*Puts team data on node*/
    no->team = team;

    /*to see if the list it's empty(no it's the first element of my list) or the last node as a name "smaler" then node*/
    if(head == *tail || strcmp((*tail)->team.name,no->team.name) <= 0)
    {
        if (head == *tail){
            no->numberOfTeams = 1;
        }
        else{
            no->numberOfTeams = head->numberOfTeams;
            (*tail)->numberOfTeams = no->numberOfTeams - 1;
        }
        no->next = (*tail)->next;
        no->prev = *tail;
        (no->prev)->next = no;
        (*tail) = no;
        aux = (*tail);
    }
    else{ /*If not the first element*/
        head = head->next; /*To advance to the first item after my head*/
        while(strcmp(head->team.name,no->team.name) < 0 && strcmp((*tail)->team.name,no->team.name) > 0 && head != *(tail)){
            head = head->next;
            (*tail) = (*tail)->prev;
        }
        if(strcmp(head->team.name,no->team.name) >= 0){
            no->next = head;
            no->prev = head->prev;
            head->prev = no;
            (no->prev)->next = no;
            no->numberOfTeams = (no->next)->numberOfTeams;
            (no->next)->numberOfTeams = no->numberOfTeams + 1;
            if((no->prev)->prev != NULL){
                (no->prev)->numberOfTeams = no->numberOfTeams - 1;
            }
        }
        else{
            no->next = (*tail)->next;
            no->prev = (*tail);
            no->numberOfTeams = (no->prev)->numberOfTeams + 1;
            (no->prev)->next = no;
            (no->next)->prev = no;
        }
    }

    /*Puts `tail` pointing to the right position*/
    if (aux != (*tail)){
        (*tail) = aux;
    }

    return 0;
}

On my .txt file I have this data:

E team;E team place
J team;J team place
G team;G team place
F team;F team place
L team;L team place
A team;A team place
H team;H team place
O team;O team place
K team;K team place
P team;P team place
N team;N team place
B team;B team place
C team;C team place
M team;M team place
D team;D team place
I team;I team place

And this is the output.

---------------------------------------------------------
|                     List of Teams                     |
---------------------------------------------------------
|        Number of Teams       |                     16 | no 00740ff0 | prev 00000000 | next 00741240 |
--------------------------------------------------------
|  ID  |       Team Name       |        Team Place      |
--------------------------------------------------------
|    1 | A team                | A team place           | no 00741240 | prev 00740ff0 | next 00741450 |
|    2 | B team                | B team place           | no 00741450 | prev 00741240 | next 007436b0 |
|    3 | C team                | C team place           | no 007436b0 | prev 00741450 | next 00743760 |
|    4 | D team                | D team place           | no 00743760 | prev 007436b0 | next 00741088 |
|    5 | E team                | E team place           | no 00741088 | prev 00743760 | next 00741190 |
|    2 | F team                | F team place           | no 00741190 | prev 00741088 | next 00741138 |
|    3 | G team                | G team place           | no 00741138 | prev 00741190 | next 00741298 |
|    4 | H team                | H team place           | no 00741298 | prev 00741138 | next 007437b8 |
|    5 | I team                | I team place           | no 007437b8 | prev 00741298 | next 007410e0 |
|    4 | J team                | J team place           | no 007410e0 | prev 007437b8 | next 00741348 |
|    5 | K team                | K team place           | no 00741348 | prev 007410e0 | next 007411e8 |
|    7 | L team                | L team place           | no 007411e8 | prev 00741348 | next 00743708 |
|    8 | M team                | M team place           | no 00743708 | prev 007411e8 | next 007413f8 |
|    8 | N team                | N team place           | no 007413f8 | prev 00743708 | next 007412f0 |
|    9 | O team                | O team place           | no 007412f0 | prev 007413f8 | next 007413a0 |
|   10 | P team                | P team place           | no 007413a0 | prev 007412f0 | next 00000000 |
--------------------------------------------------------

With this output I can see that my teams are being added sorted by name to my list. The debbug that I made printing the memory addresses is showing that all is ok. The problem is with the team ID. It´s int numberOfTeams;

So, finally after all of this text, these are my questions:

Question 1 What can I do to solve my teams ID, ie, after inserting a new Team to my list, the ID is updated to the right ID.

Question 2 Altough my addNodeTeamsSorted is working with the ID exeption, I believe the algoryth it’s to “chopy”. Can you recommend some optimizations?

Thanks

  • 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-04T17:13:34+00:00Added an answer on June 4, 2026 at 5:13 pm

    What can I do to solve my teams ID name, ie, after inserting a new Team to my list, the ID is updated to the right ID.

    This is an illegal memory write:

    (*head) = (NodeTeam *)malloc(sizeof(NodeTeam));
    
    if ((*head) == NULL){
        return -1;
    }
    (*head)->numberOfTeams = 0;
    strcpy((*head)->team.teamPlace,"");   <<------- here, and
    strcpy((*head)->team.name,"");        <<------- here
    

    teamPlace and name are unitialised char*, so the strcpy() will writing to somewhere that it should not. You need to allocate memory for these or define them as fixed sized arrays (if possible).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
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.