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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:22:49+00:00 2026-05-21T07:22:49+00:00

I am doing this homework task where it requires me to take in a

  • 0

I am doing this homework task where it requires me to take in a massive string, and break this up into many substrings where each string is indicated by the ‘\n’ new line value inside the string, and store this into a linked list, for example:

string = "hello world\n i need help!!"

Would turn into:

string1 = "hello world\n"
string2 = "i need help!!"

I’ve written up this code, that breaks the string into substrings and stores them into individual nodes. The code itself, is quite ugly and needs more refinement, but I can’t even get to that point because somewhere weird seems to happen in the middle, where all the strings inside the linked list gets replaced by the last string I add to the linked list…

Here’s my code, please help if you can:

#define eq(A, B) ( A == B )

typedef struct list * link;
typedef char Item;

struct list {
    link next;
    Item *string;
};


void printlist (link ls);
link newLS (char text[]);
link newNode (char text[]);
void insertNode (link next, Item item[]);

link newLS (char text[]) {
    int i = 0;
    int j = 0;
    char temp[(strlen(text))];
    link new = NULL;

    while (text[i] != '\0') {
        temp[j] = text[i];
        if (text[i] == '\n' || text[i+1] == '\0') {
            temp[j+1] = '\0';
            j = -1;
            if (new == NULL) {
                new = newNode(temp);
                printf("new: %s", new->string);
            } else {
                insertNode(new, temp);
                printf("new: %s", new->next->string);
            }
        } 
        i++;
        j++;
    }
    printlist(new);
    return new;
}

link newNode (char text[]) {
    link new = malloc(sizeof(*new));
    assert(new != NULL);
    new->string = malloc ((strlen(text)) * sizeof(char));
    new->string = text;
    new->next = NULL;

    return new;
}

void insertNode (link ls, Item item[]) {
    assert (ls != NULL);
    assert (item != NULL);

    while (ls->next != NULL) {
        ls = ls->next;
    }
    ls->next = newNode(item);

}

int main(int argc, char **argv) {
    link ls;

    ls = newLS("1\n2\n3");
    return 0;
}

We must use this function:

link newLS (char text[]) 
  • 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-21T07:22:50+00:00Added an answer on May 21, 2026 at 7:22 am
    1. #define eq(A, B) ( A == B ) is not a good idea, an improvement will be to define it as #define eq(A, B) ( (A) == (B) )

    2. You allocate buffer, and then, not using it, but assigning another pointer to the pointer:

      new->string = malloc ((strlen(text)) * sizeof(char));
      new->string = text;
      

      instead, you should copy the data from the given pointer:

      new->string = malloc ((strlen(text) + 1) * sizeof(char));
      memcpy(new->string, text, strlen(text) + 1);
      

      moreover, when you’ll try to free the allocated memory you’ll get segmentation fault, because new->string is not pointing to the allocated area…

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

Sidebar

Related Questions

This one is a case of not doing your homework.:-) Apart from dynamic loading
I'm doing a basic homework assignment which looks like this: While input <> -1
any problems with doing this? int i = new StreamReader(file.txt).ReadToEnd().Split(new char[] {'\n'}).Length
This is NOT a homework question, actually I am doing this for fun. Here
So I've been doing this all night - can't quite understand my homework, and
So I've been doing this all night - can't quite understand my homework, and
I beg pardon for not doing homework on this issue and directly asking question
I'm doing this: return this.each(function(){ $.this.load('searchInterface.html',function(){ console.log('load'); //a lot of code }); $('#more_button').bind('click',more()); function
I have some code doing this : var changes = document.getElementsByName(from); for (var c=0;
I'm doing this system Stacked and I am creating the search function. And in

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.