on a one-way list

Asked 1 years ago, Updated 1 years ago, 40 views

The following statement is a program that receives 5 numeric entries and returns a value as a numberlist.
I'm studying two-way lists.This code is a one-way list

I'd like to avoid outputting commas only in the last while loop, but what should I do with the least amount of space calculation?

For the time being, I tried to create an address for the structure end and connect it when i=4, but it didn't work.

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

structure number {
  int num;
  structure number * next;
};

int main() {
  structure number *p, *head, *end;
  end=(structure number*)malloc(sizeof(structure number)));
  inti;
  printf("input5number:");
  for(i=0;i<5;i++){
    if(i==0){
      p=(structure number*)malloc(sizeof(structure number)));
      head=p;
      if(i==4){
        end = p;
        p - > next = NULL;
      }
    } else {
    p->next=(structure number*) malloc(sizeof(structure number));
    p=p->next;
    }
    scanf("%d", & p->num);
    p - > next = NULL;
  }
  p=head;
  printf("number list:");
  while(p!=NULL){
    if(p==end){
      printf("%d", p->num);
      p=p->next;
    } else {
    printf("%d,", p->num);
    p=p->next;
    }
  }
  printf("\n");
  return 0;
}

c

2022-09-30 21:43

4 Answers

That's what I think.

  • Let's initialize the variable

    structure number*p, *head, *end;
    
  • This is not required.

    end=(structure number*)malloc(sizeof(structure number)));
    
  • If
  • i only runs when 0, it is meaningless to determine if i is 4.
    Therefore, if(p==end){ determination in the while loop is not always successful.

     if(i==0){
        ...
        if(i==4){
            end = p;
            p - > next = NULL;
        }
    }
    
  • for loops are redundant in many other ways.

Let's initialize the variables.

structure number*p, *head, *end;

This is not required.

end=(structure number*)malloc(sizeof(structure number)));

There is no point in determining whether i is 4 when i is 0.
Therefore, if(p==end){ determination in the while loop is not always successful.

 if(i==0){
    ...
    if(i==4){
        end = p;
        p - > next = NULL;
    }
}

for loops are redundant in many other ways.

To sum up, it is as follows:

int main(){
    structure number *p = NULL, *head = NULL, *end = NULL; // variable initializes
    // end=(struct number*)malloc(sizeof(struct number))); // This is not required
    inti;
    printf("input5number:");
    for(i=0;i<5;i++){
        // Organize operations in the loop
        p=(structure number*)malloc(sizeof(structure number)));
        p - > next = NULL;
        if(i==0){
            head=p;
        }
        else{
            end->next=p;
        }
        end = p;
        scanf("%d", & p->num);
    }
    p=head;
    printf("number list:");
    while(p!=NULL){
        // If it's close to the original process, click here.
        if(p==end){
            printf("%d", p->num);
        }
        else{
            printf("%d,", p->num);
        }
        // Or @metropolis, let's do the following in your comment.
        // printf("%d%s", p->num, (p->next?", ":");
        p=p->next;
    }
    printf("\n");
    return 0;
}


2022-09-30 21:43

This method is used when you want to separate data with any symbol.
 Leave the initial value of the delimiter as an empty string, and set the delimiter to the desired symbol after the first output.

The specific code is as follows:I used the code of Renya Gaikon.

#include<stdio.h>
# include <stdlib.h>
US>typedef structure tNumber}
    int current;
    structNumber*next;
} TNumber;
int main(const int argc, const char*argv[])
{
    enum {Max=5};
    TNumber*ptr=(TNumber*)malloc(sizeof(TNumber)));
    TNumber*head=ptr;
    for(size_tidx=0; Max>idx;++idx){
        scanf("%d", & ptr->current);
        ptr->next=(Max-1!=idx)?(TNumber*) malloc(sizeof(TNumber))—NULL;
        ptr = ptr->next;
    }
    ptr = head;
    fprintf(stderr, "number list:");
    char*dlmt=";//★Added
    while(NULL!=ptr){
        fprintf(stderr, "%s%d", dlmt, ptr->current); // ★ change
        dlmt=", "; // ★ Add
        ptr = ptr->next;
    }
    fprintf(stderr, "\n"); // ★ Add
    return EXIT_SUCCESS;
}

The space calculation at the point "Do not output commas only in the last while loop" is O(1).It is constant regardless of the number of data.
Other than the one introduced here, O(1) is another way.
I thought using the term "space calculation" in this case was a bit exaggerated.


2022-09-30 21:43

I answered earlier, but I have reviewed how to write the entire code.
主なKey points of review の

  • Memory per item
    I've tried to get all the memory I need to use.
    Securing a large number of smaller areas in malloc is disadvantageous both in terms of speed performance and in termsHundreds of orders are not a problem, but thousands of orders are a concern.

  • How to write comparison operators
    variable operator constant

    When the compiler and static checks were poor, I thought that the constant operator variable was also written as "yes", but I thought it would be better to correct it because it is natural for humans to read from left to right.
     When you read NULL!=ptr, your gaze moves to the variable ptr, and then to NULL, which makes you tired.

US>Each item of memory
I've tried to get all the memory I need to use.
Securing a large number of smaller areas in malloc is disadvantageous both in terms of speed performance and in termsHundreds of orders are not a problem, but thousands of orders are a concern.

How to Write Comparison Operators
variable operator constant

When the compiler and static checks were poor, I thought that the constant operator variable was also written as "yes", but I thought it would be better to correct it because it is natural for humans to read from left to right.
 When you read NULL!=ptr, your gaze moves to the variable ptr, and then to NULL, which makes you tired.

#include<stdio.h>
# include <stdlib.h>
typeef structure TNumber TNumber;
structure TNumber {
    int value;
    TNumber*next;
};
#define INPUT_DATA_NUM(5)
int main(const int argc, const char*argv[])
{
    TNumber*data=(TNumber*)malloc(sizeof(TNumber)*INPUT_DATA_NUM);
    for(inti=0;i<INPUT_DATA_NUM;i++){
        data[i].next=&data[i+1];
        scanf("%d", & data[i].value);
    }
    data[INPUT_DATA_NUM-1].next=NULL;

    fprintf(stderr, "number list:");
    char*dlmt="";
    while(data!=NULL){
        fprintf(stderr, "%s%d", dlmt, data->value);
        dlmt=", ";
        data=data->next;
    }m
    fprintf(stderr, "\n");
    return 0;
}

When you write a program in your business (when you reuse it), you hesitate to limit the amount of heap (malloc) you use.mm Because the address may change depending on the process used, such as mmap or shared memory.The TNumber link should be an integer variable, not a pointer.デ It is also my real intention that it is troublesome to calculate the address when debugging.That's another explanation.

[Version using integer type variables for link]

#include<stdio.h>
# include <stdlib.h>
typeef structure TNumber TNumber;
structure TNumber {
    int value;
    int next;
};
#define INPUT_DATA_NUM(5)
#define DATA_NONE(-1)
int main(const int argc, const char*argv[])
{
    TNumber*data=(TNumber*)malloc(sizeof(TNumber)*INPUT_DATA_NUM);
    for(inti=0;i<INPUT_DATA_NUM;i++){
        data[i].next=i+1;// The following data:
        scanf("%d", & data[i].value);
    }
    data[INPUT_DATA_NUM-1].next=DATA_NONE;

    fprintf(stderr, "number list:");
    TNumber* current=data;
    char*dlmt="";
    for(;;){
        fprintf(stderr, "%s%d", dlmt, current->value);
        dlmt=", ";
        if(current->next==DATA_NONE){
            break;
        }
        current=&data [current->next];
    }
    fprintf(stderr, "\n");
    return 0;
}


2022-09-30 21:43

This answer is based on answer by kunif and answer by


2022-09-30 21:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.