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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:30:24+00:00 2026-06-10T09:30:24+00:00

So I have a self-made double-linked-list implementation that is being used as a stand

  • 0

So I have a self-made double-linked-list implementation that is being used as a stand in for a queuer. (implemented in C, a language that I am admittedly weak in).

my typedef for the node:

typedef struct Node
{
    char *name;
    int data;
    int recurring;
    struct Node *next;
    struct Node *prev;
}node;

which says “a node has a name, a datapoint, whether it’s recurring or not and pointers to the previous and next nodes”

the insertion function like so

node * insertFromTail(node *tail, int data, int recurring, char *name)
{
    node *newNode;
    node *oldNext;
    node *origTail = tail;
    /*assume *pointer points to tail of list*/
    /*printf("tail data is %d\n", tail->data);
    printf("before loop\n");*/
    while(tail->prev != NULL && tail->data > data)
    {
        /*printf("inside while loop\n");*/
        tail = tail -> prev;
    }
    /*printf("after loop\n");*/
    /*if we are looking at a no item list or tail*/
    if(tail->next == NULL)
    {
        /*printf("pointer is tail\n");*/
        return insert(tail, data, recurring, name);
    }
    else /*tail pointer points at item before the point of insertion*/
    {
        /*printf("default case\n");
        printf("pointer data is %d\n", tail->data);*/
        oldNext = tail->next;
        newNode = (node *)malloc(sizeof(node));
        newNode->data = data;
        newNode->recurring = recurring;
        newNode->name = name;
        oldNext -> prev = newNode;
        newNode -> next = oldNext;
        tail -> next = newNode;
        newNode -> prev = tail;
        return origTail;
    }
}

with the internal insert

node * insert(node *tail, int data, int recurring, char *name)
{
        /* Allocate memory for the new node and put data in it.*/
        tail->next = (node *)malloc(sizeof(node));
        (tail->next)->prev = tail;
        tail = tail->next;
        tail->data = data;
        tail->recurring = recurring;
        tail->name = name;
        tail->next = NULL;
        return tail;
}

which is passed the tail of the list, the data point, the time at which the next item will recur at and the name of the item.

if we start with a node that is empty and has NULL prev and next references (a dummy node), and I add three unique nodes with a function called ADD that calls insertFromTail taking input from stdIn

int main()
{
    node *start,*temp,*tail;
    start = (node *)malloc(sizeof(node));
    temp = start = tail;
    temp->next = NULL;
    temp->prev = NULL;
    if(strcmp(command, "ADD") == 0)
    {
        scanf("%d",&argTime);
        scanf("%s",&argName);
        tail = insertFromTail(head, argTime, 0, *argName);
    }
}

with input as so:

INPUT: 
ADD 10 Gin
ADD 20 Vodka
ADD 30 Rum
PRINT

I would get an output of

OUTPUT:
Rum 10
Rum 20
Rum 30

This is an error, as the desired output would be
OUTPUT:
Gin 10
Vodka 20
Rum 30

I have a feeling it has to do with how the string is passed into the node, but as you can see, I’m stumped. This is the last thing left on the assignment and everything else is working perfectly, so I decided to ask here to see if anyone can nudge me on the right path. Thanks for your help in advance 🙂

P.S. Sorry for bad everything, I’m sleep deprived 🙁

  • 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-10T09:30:26+00:00Added an answer on June 10, 2026 at 9:30 am

    Short answer: you’ll need to duplicate that name:

    tail->name = strdup(name);
    

    Longer answer: at each iteration you’re storing the same pointer. You’re storing it and then the next time you’re writing to it again. So you end up with 3 identical pointers to whatever you input last.

    A simple fix is to duplicate the string and store a copy: precisely what strdup does. But if your implementation lacks strdup, you can try:

    tail->name = malloc(strlen(name) + 1);
    strcpy(tail->name, name);
    
    • Don’t forget to check for errors
    • Don’t forget to free the memory at some point
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a self made data structure (for example linked list) that works well,
I have a self-made Process Manager for my site, that simply parses the shell
I have a self-made framework/API and it does some database searches that can take
I have made an iPad application, in that I used navigation control, now in
Have this self-made slider: http://jsfiddle.net/wyc3P/4/ What it does: takes min and max values in
I have a small self made gallery which im still working on it: http://springbreak.enteratenorte.com
I have a problem with my self-made search page. There is 2 tables: **name
I have made an application (for my self) for feeds reading, using SyndicationFeed ,
I have a self-referential table in my database that looks sort of like above.
I have programmed a self-made concat function: char * concat (char * str1, char

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.