开发者

Build the list {1,2,3} with the minimum assignment statements in C [closed]

开发者 https://www.devze.com 2023-01-23 00:35 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

Rewrite this to minimize the assignment statements

/*
 Build the list {1, 2, 3} in the heap and store
 its head pointer in a local stack variable.
 Returns the head pointer to the caller.
*/
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 hea
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 poin开发者_运维问答t, the linked list referenced by "head"
// matches the list in the drawing.
return head;
}

From http://cslibrary.stanford.edu/


node *head;
node **next= &head;

int next_value= 1;
while (next_value<=3)
{
    *next= malloc(sizeof(struct node));
    (*next)->data= next_value;
    next= &(*next)->next;
    ++next_value;
}

*next= 0;


Generalizing:

node* buildNodeList(unsigned int n)
{
    node * head;
    node * prev;
    if (n == 0)
    {
        return 0;
    }

    prev = 0;
    while (n > 0)
    {
        head = (node*)malloc(sizeof(node));
        head->data = n;
        head->next = prev;
        prev = head;
        --n;
    }

    return head;
}


More general:

#include <stdarg.h>

node *BuildList(int len, ...)
{
    va_list args;
    node *head, **p = &head;
    va_start(args, len);
    while(len--) {
        *p = malloc(sizeof(node));
        (*p)->data = va_arg(args, int);
        p = &(*p)->next;
    }
    *p = 0;
    va_end(args);
    return head;
}


Two assignments; complete compilable code follows :-)

#include <stdio.h>
#include <stdlib.h>

struct node {
  int data;
  struct node *next;
};

struct node *newnode(int val, int nextval) {
  struct node *x = malloc(sizeof *x);  /* not assignment: initialization */
  if (x == NULL) {
    fprintf(stderr, "oops, no memory\n");
    exit(EXIT_FAILURE);
  }
  x->data = val;                                           /* assignments: 1 */
  x->next = (nextval < 4)                                  /* assignments: 2 */
          ? (newnode(nextval, nextval+1))
          : (NULL);
  return x;
}

int main(void) {
  struct node *head = newnode(1, 2); /* not assignment: initialization */
  struct node *curr;

  /* building of the list (3 elements with `data` values from 1 to 3) done */
  /* the assignments after this line are not used to BUILD the list */

  /* use nodes */
  curr = head;
  while (curr) {
    printf(" %d ==>", curr->data);
    curr = curr->next;
  }

  /* free structures */
  curr = head;
  while (curr) {
    struct node *tmp = curr->next;
    free(curr);
    curr = tmp;
  }

  return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消