我正在创建一个必须交换顺序的链表

问题描述 投票:0回答:1

我正在编写一个使用双向链表的程序。首先,它从输入中获取2个数字。第二个数字将在以后使用,但第一个数字n在函数中用于安排链接列表,以便它从n变为1,如n-> n-1-> n-2-> ... -> 1,同时使用next或prev在节点之间移动。然后另一个函数采用相同的链表并对其进行交换,因此它从1变为'n',但我不知道如何进行交换。我更改或尝试执行的所有操作均显示为细分错误,或返回相同列表而未进行任何更改。另外,这是我第一次必须使用此网站解决编码方面的任何问题,因此,如果我做错了事,请告诉我,这样我就可以避免犯任何错误

[这是我的计算机科学1课的家庭作业,应于2019年10月25日到期。我只需要帮助我解决无法解决的问题的代码部分。

typedef struct nod{
  int data;
  struct nod *next;
  struct nod *prev;
}soldier;
soldier *head = NULL;


soldier* create_soldier (int sequence);
soldier* create_reverse_circle(int n);
soldier* rearrange_circle(soldier *head);
void display(soldier *head);
int kill(soldier* head, int n, int k);

//dynamically allocates a solider node
soldier* create_soldier (int sequence){
  soldier *temp = (soldier *)malloc(sizeof(soldier));
  temp->data = sequence;
  temp->next = NULL;
  temp->prev = NULL;
  return temp;
}

//creates a list of soliders from n to 1
soldier* create_reverse_circle(int n){
  soldier *temp = NULL;
  soldier *t = NULL;
  int i;
  //printf("w");
  for(i = n; i > 0; i--){
    temp = create_soldier(i);

    if(head==NULL){
      head=temp;
      temp->next=head;
    }
    else{
      t=head;
      t->prev = t;
      while(t->next!=head){
        t=t->next;
      }
      t->next=temp;
      temp->prev = t;
      temp->next=head;
    }
  }

  return head;
}

 //rearranges the soliders in the list to go from 1 to n
 //the function I am having issues with
soldier* rearrange_circle(soldier* head){
  soldier *t = (soldier *)malloc(sizeof(soldier));
  soldier *temp = NULL;
  soldier *exc = head;
  int h = 0;
  int k = 1;

  //printf("\ntest 1:");  for test purposes
  while(exc != head){
    //tamp->data = k;
    temp = exc;
    temp->next = exc->prev;
    temp->prev = exc->next;

    if(h != 1){
      head = temp;
      temp->next = head;
      h = 1;
    }
    else{

      t = head;
      while(t->next != head)
        t=t->next;
      t->next = temp;
      temp->prev = t;
      head->next = t->next;
      temp->next = head;
    }

    exc = exc->next;
    temp = temp->next;
    //k++;
  }

  //printf("h = %d\n", h); also for test purposes


  return head;
}

//displays the head of the list
 void display(soldier* head){
  soldier *temp=head;
  while(temp->next != head){
    printf("%d->", temp->data);
    temp = temp->next;
  }
  printf("%d", temp->data);

}

用户应输入2个数字。第一个数字n确定循环,第二个数字k稍后使用。第一个函数create_reverse_circle应该取n并返回一个从n到1的双链表到主函数,以在显示函数中打印出来。然后,另一个函数Rearrange_circle获取该链接列表,将其顺序​​反转,从1到n,然后将其返回给main函数,以再次在display函数中进行打印(该函数只会打印出与第一次相同的结果,鉴于没有细分错误)。交换列表之后,由于解决了问题,因此我遗漏了一个int函数,应该使用链接列表N和第二个数字K删除函数中的所有其他节点,直到k + 1为剩余并返回该值以将其打印在主窗口中。

c linked-list doubly-linked-list
1个回答
0
投票

基于this,您可以通过以下方式反转列表:

/* Function to reverse a Doubly Linked List */
void reverse(soldier **head_ref)  
{  
    soldier *temp = NULL;  
    soldier *current = *head_ref;  

    /* swap next and prev for all nodes of  
    doubly linked list */
    while (current != NULL)  
    {  
        temp = current->prev;  
        current->prev = current->next;  
        current->next = temp;              
        current = current->prev;  
    }  

    /* Before changing the head, check for the cases like empty  
        list and list with only one node */
    if(temp != NULL )  
        *head_ref = temp->prev;  
} 

请注意,它不会创建一个新的要反转的列表,相反,它会破坏性地获取该列表并将其反转,为此,您还需要向其传递指向“指向头部的指针”的指针,然后您可以到达结果在“指向头部的指针”中(必须与传递指针的对象相同)。但是,如果您希望将原始列表保存在一个列表中,则可以先复制整个列表,然后反转该新列表。用于复制列表的代码,基于this

soldier *copy(soldier *start1)
{
    if(start1==NULL) return;
    soldier *temp=(soldier *) malloc(sizeof(soldier));
    temp->prev=start1->prev;
    temp->data=start1->data;
    temp->next=copy(start1->next);
    return temp;
}
© www.soinside.com 2019 - 2024. All rights reserved.