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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:20:33+00:00 2026-05-11T03:20:33+00:00

I came across this problem while preparing for an interview and curious to know

  • 0

I came across this problem while preparing for an interview and curious to know the diffrent ways it can be written. I found this at http://cslibrary.stanford.edu/103/ and have given the problem as it is.

here is a code to build the list {1,2,3}

struct node* BuildOneTwoThree() {     struct node* head = NULL;     struct node* second = NULL;     struct node* third = NULL;     head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap     second = malloc(sizeof(struct node));     third = malloc(sizeof(struct node));     head->data = 1; // setup first node     head->next = second; // note: pointer assignment rule     second->data = 2; // setup second node     second->next = third;     third->data = 3; // setup third link     third->next = NULL;     // At this point, the linked list referenced by 'head'     // matches the list in the drawing.     return head; } 

Q: Write the code with the smallest number of assignments (=) which will build the above memory structure. A: It requires 3 calls to malloc(). 3 int assignments (=) to setup the ints. 4 pointer assignments to setup head and the 3 next fields. With a little cleverness and knowledge of the C language, this can all be done with 7 assignment operations (=).

  • 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. 2026-05-11T03:20:34+00:00Added an answer on May 11, 2026 at 3:20 am

    I did it with six assignments. What do I get?

    struct node {     int data;     struct node * next; };  struct node * build_123() {     struct node * first = malloc(sizeof(*first));     struct node * second = malloc(sizeof(*second));     struct node * third = malloc(sizeof(*third));      assert(first && second && third);      *first = (struct node){ 1, second };     *second = (struct node){ 2, third };     *third = (struct node){ 3, NULL };      return first; } 

    Also, the exercise isn’t very useful. If I wanted to build a linked list from a known set of integers, I’d do something like this:

    struct node {     int data;     struct node * next; };  #define build_list(...) \     _build_list((sizeof((int []){ __VA_ARGS__ }))/(sizeof(int)), \     (int []){ __VA_ARGS__ })  struct node * _build_list(size_t count, int values[count]) {     struct node * next = NULL;      for(size_t i = count; i--; )     {         struct node * current = malloc(sizeof *current);         assert(current);         *current = (struct node){ values[i], next };         next = current;     }      return next; } 

    Then, you can build an arbitrary list with

    struct node * list = build_list(1, 2, 3); 

    Here’s another version using a single assignment, inspired by codelogic’s answer:

    struct node * build_123(void) {     struct node * list = malloc(sizeof(struct node [3]));     return memcpy(         list,         (struct node []){ { 1, list + 1 }, { 2, list + 2 }, { 3, NULL } },         sizeof(struct node [3])     ); } 

    Finally, I slightly modified MSN’s solution – now, there’s no assignment at all:

    struct node {     int data;     struct node * next; };  struct node * make_node(struct node * new_node, int data, struct node * next) {     return memcpy(new_node, &(struct node){ data, next }, sizeof(*new_node)); }  struct node * create_node(int data, struct node * next) {     return make_node(malloc(sizeof(struct node)), data, next); }  struct node * build_123(void) {     return create_node(1, create_node(2, create_node(3, NULL))); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 90k
  • Answers 90k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Where I'm at we return datasets, datatables, datarows, and datareaders… May 11, 2026 at 5:59 pm
  • Editorial Team
    Editorial Team added an answer Do you just want to call a parameterless constructor to… May 11, 2026 at 5:59 pm
  • Editorial Team
    Editorial Team added an answer You are trying to get the sections on request. But… May 11, 2026 at 5:59 pm

Related Questions

Yesterday, I asked this question and never really got an answer I was really
While in the final throws of upgrading MS-SQL Server 2005 Express Edition to MS-SQL
While investigating a reported problem with my site loading slowly, I came across an
Possible Duplicate: How to properly clean up Excel interop objects in C# I've read

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.