Handling Structure Pointers for Bidirectional Lists

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

The code below is a program that receives five numbers as input, outputs them as numberlists in the order they are received, and then outputs numberlists in reverse order.

I'm not sure if NULL is handled badly or if the bidirectional list itself is wrong.

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

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

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

c

2022-09-30 21:45

2 Answers

Note: What is the original problem?
There are two mistakes in the main book.

  • Failed to process the final dummy data in an attempt to attach dummy data with invalid data (num=NULL) to start/finalize the list.
  • When attempting to reverse-order the last valid data, start with dummy data.

    Start (dummy)<=>Data1<=>...<=>Data5<=>Final (not dummy)<=>Back to top
    

When attempting to reverse-order the last valid data, start with dummy data.

Start (dummy)<=>Data1<=>...<=>Data5<=>Final (not dummy)<=>Back to top

Apart from the comma problem, there are two things you can do to get the list done with minimal modification.

  • for loop last=p; followed by last->num=NULL;.
  • Change the start pointer setting p=last; in reverse order to p=last->prev;.

I think it's probably a redundant and funny process to find a way to determine the end of the list and get to the dummy data and try to create a process for that.
It seems to be made with the circulation list in mind.
Revised: This is what you would think if you were to recreate it briefly

    Initialize the
  • variable.Also, head is not required, and last is not required at this time.

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

    start=(structure number*)malloc(sizeof(structure number)));
    last=(structure number*)malloc(sizeof(structure number)));
    head=(struct number*)malloc(sizeof(struct number)));
    head->next=head;
    head->prev=head;
    head->num = NULL;
    
  • forYou should consider what needs to be done and configured in the loop, and how to speed up and reduce the order.I didn't point it out in the one-way list question, but the loop counter i is only used within for, so it can be shortened.
    Use the sauce for details.

  • This is also not required.

    last->next=head;
    
  • while(p->num!=NULL){ loop while num is not NULL(=0) because dummy data is assumed, the pointer reverts to valid data.

  • The display is concise in @metropolis's handling of the one-way list.

Let's initialize the variables.Also, head is not required, and last is not required at this time.

structure number*p, *head, *start, *last;

This is not required.

start=(structure number*)malloc(sizeof(structure number)));
last=(structure number*)malloc(sizeof(structure number)));
head=(struct number*)malloc(sizeof(struct number)));
head->next=head;
head->prev=head;
head->num = NULL;

for You should consider what needs to be done and configured in the loop, and how to do it quickly and less.I didn't point it out in the one-way list question, but the loop counter i is only used within for, so it can be shortened.
Use the sauce for details.

This is also not required.

last->next=head;

If num in while(p->num!=NULL){ is not NULL(=0), the decision to loop while num is not NULL ( 00) is assumed to be dummy data, so change the pointer back to valid data.

As for the display, @metropolis's handling of the one-way list is simple.

To sum up, it is as follows:

int main(){
    structure number *p = NULL, *start = NULL; // Initialize as much as necessary
    printf("input5number:");
    for(inti=0;i<5;i++){
        // Organize variable declarations and in-loop operations
        p=(structure number*)malloc(sizeof(structure number)));
        if(i==0){
            start = p;
            // be initially referring to oneself on a circular list
            p->prev=p;
            p->next=p;
        }
        else{
            // Insert at the end of the circular list
            p->prev = start->prev;
            p->next=start;
            start->prev=p;
            p->prev->next=p;
        }
        int result=scanf_s("%d", & p->num);// Replace old scanf
    }
    // --After that, the display process of the data entered --
    p=start;
    printf("number list:");
    do{
        printf("%d%s", p->num, (p->next!=start?", ":"));
        p=p->next;
    } while(p!=start);
    printf("\n");
    structure number * last = start->prev;// To simplify the end determination of reverse order display
    p = last;
    printf("number list:");
    do{
        printf("%d%s", p->num, (p->prev!=last?", ":"));
        p=p->prev;
    } while(p!=last);
    printf("\n");
    return 0;
}

See this article
Consolidated List 1 (unidirectional and linear) | Programming Place Plus Algorithm and Data Structure Edition [Data Structure] Chapter 3
Coupled List 2 (Bidirectional/Circulation) | Programming Place Plus Algorithm and Data Structure Edition [Data Structure] Chapter 4


2022-09-30 21:45

This answer is based on Answer from Kunif and Akiraejiri from the Answer from


2022-09-30 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.