Implementation of a halfchange function that connects the second half of the list received in the c language list structure to the first half and the first half to the second half.

Asked 2 years ago, Updated 2 years ago, 30 views

Prerequisite
I asked because I was having a problem with the list structure of the c language and faced a problem I didn't understand.

What do you want to do
I would like to implement a halfchange function that connects the second half of the received list to the first half and the first half to the second half.I was able to make the list I received only in the second half, but I cannot proceed from there.Below is the code I created, the correct output, and my output.

Affected Source Code

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

US>structure node {
    care element;
    structure node*next;
};

structure node*initlist(){
  structure node*n;
  n = (struct node*) malloc (size of (struct node);
  n - > next = NULL;
  return n;
}

void insert(struct node*p, charx){
  structure node*n;
  n=(struct node*)malloc(sizeof(struct node)));
  n->element=x;
  n - > next = NULL;
  while(p->next!=NULL)
    p=p->next;
  p->next=n;
}

void printlist(struct node*p){
    p=p->next;
    while(p){
        putchar(p->element);
        p=p->next;
    }
    putchar('\n');
}

void halfchange (struct node*p) {
  structure node*q;
  inti = 0;
  intj;
  intk;
  q = p;
  while(p){
    p=p->next;
    i++;
  }
  i--;
  p=q;
  for(j=1;j<=i/2;j++){
    delete(p);
  }
}
void delete(struct node*p) {
  if(p->next!=NULL){
    p->next=p->next->next;
  }
}

int main(intargc, char*argv[]){
    structure node*list, *head;
    char*p;

    if(argc<2)
        exit(-1);

    list = initlist();
    p = argv[1];
    for(;*p;p++){
        insert(list,*p);
    }

    halfchange (list);
    printlist(list);

    for(;list;){
        head=list;
        list = list->next;
        free(head);
    }
    return 0;
}

Correct Output

[]$./q5-2tokyo
kyoto
[ ] $./q5-2 yuyakekoyeake
koyakeyuyake

My output

[]$./q5-2tokyo
kyo
[ ] $./q5-2 yuyakekoyeake
koyake

c

2022-09-30 10:26

1 Answers

The content of the reference article shown in the comment has been resolved, but it is explained just in case.

The main problem with the source code of the question is delete(p) the first half of the list starting from the beginning of the list in the following parts.

for(j=1;j<=i/2;j++){
    delete(p);
  }

If you delete the data, it will be lost, so you cannot reconnect it.

Then use the parameters passed to the working pointer, and the number of nodes and the second half position calculation are slightly redundant.

If you combine the source code of the question with the reference article shown in the comment, and include a rough description in the source comment:

void halfchange(struct node*p){
    // Invalid parameter or number of nodes pointing to NULL = 1 return
    if(!p||!p->next||!p->next->return;

    // *q is not for temporary evacuation, but for final node acquisition, and *r is for new final node acquisition.
    structure node*q,*r;
    inti,j,k;

    // Get current last node and number of nodes
    q = p;
    for(i=0;q->next!=NULL;i++){
        q = q - > next;
    }

    // Get a new final node after processing
    k = i/2;
    r = p;
    for(j=0;j<k;j++){
        r=r->next;
    }

    // cut the list in half and move it back and forth
    q->next=p->next;// Connect the current first node to the current last node
    p->next=r->next;// Set the new first node next to the new last node
    r ->next=NULL;// Clears the next pointer of the new last node to the last node
}


2022-09-30 10:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.