繁体   English   中英

我正在制作一个必须交换顺序的链表

[英]I'm making a linked list that has to swap it's order

我正在编写一个使用双向链表的程序。 首先,它从输入中获取 2 个数字。 第二个数字稍后使用,但第一个数字 n 用于 function 以排列链表,使其从 n 到 1,如 n->n-1->n-2->... ->1 同时使用 next 或 prev 在节点之间移动。 然后另一个 function 采用相同的链表并将其交换,因此它从 1 变为“n”,但我不知道如何让它交换。 我更改或尝试的所有内容要么显示为分段错误,要么返回相同的列表而不更改任何内容。 另外,这是我第一次使用这个网站来解决我在编码方面遇到的任何问题,所以如果我做错了什么,请告诉我,这样我可以避免犯任何错误

这是我的计算机科学 1 class 中的家庭作业,该作业将于 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 将在稍后使用。 第一个 function,create_reverse_circle,应该取 n 并将一个从 n 到 1 的双向链表返回到主 function 以在显示 ZC1C425268E68385D1AB4Z5074 中打印。 Then, another function, rearrange_circle, takes that linked list, reverses the order so it goes from 1 to n, and returns it to main function to get printed again in the display function (which only prints the same result it did the first time,假设没有分段错误)。 一旦列表被交换,一个 int function 因为我已经解决了,所以应该使用链表 N 和第二个数字 K 来删除 function 中的所有其他节点,直到 k+1剩余并返回该值以打印在主文件中。

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

/* 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;  
} 

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

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM