简体   繁体   English

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

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

I'm writing a program that uses a doubly linked list.我正在编写一个使用双向链表的程序。 first, it takes 2 numbers from the input.首先,它从输入中获取 2 个数字。 the second number is to be used later but the first number, n, is used in a function to arrange the linked list so that it goes from n to 1 like n->n-1->n-2->...->1 while using next or prev to move between nodes.第二个数字稍后使用,但第一个数字 n 用于 function 以排列链表,使其从 n 到 1,如 n->n-1->n-2->... ->1 同时使用 next 或 prev 在节点之间移动。 Then another function takes the same linked list and swaps it around so it goes from 1 to 'n' but I can't figure out how to get it to swap.然后另一个 function 采用相同的链表并将其交换,因此它从 1 变为“n”,但我不知道如何让它交换。 Everything I change or try either shows up as a segmentation fault or returns the same list without changing anything.我更改或尝试的所有内容要么显示为分段错误,要么返回相同的列表而不更改任何内容。 Also, this is my first time having to use this website for any issues I have with coding so if I am doing something wrong, kindly let me know so I can avoid making any mistakes另外,这是我第一次使用这个网站来解决我在编码方面遇到的任何问题,所以如果我做错了什么,请告诉我,这样我可以避免犯任何错误

This is for a homework assignment in my computer science 1 class that is due 10/25/2019.这是我的计算机科学 1 class 中的家庭作业,该作业将于 2019 年 10 月 25 日到期。 I just need help on the part of the code that is giving me the issues I cannot fix.我只需要代码部分的帮助,这些代码给了我无法解决的问题。

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

}

The user is suppose to input 2 numbers.假设用户输入 2 个数字。 the first number, n, determines the loop and the second number, k, will be used later.第一个数字 n 确定循环,第二个数字 k 将在稍后使用。 the first function, create_reverse_circle, should take n and return a doubly linked list going from n to 1 to the main function to get printed in the display function.第一个 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, given that there isn't a segmentation fault). 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,假设没有分段错误)。 Once the list has been swapped, an int function that I've left out because I have it solved, should use the linked list, N and the second number, K, to remove all the other nodes in the function until k+1 is remaining and return that value to be printed in the main.一旦列表被交换,一个 int function 因为我已经解决了,所以应该使用链表 N 和第二个数字 K 来删除 function 中的所有其他节点,直到 k+1剩余并返回该值以打印在主文件中。

Based on this , you can reverse the list this way:基于,您可以通过以下方式反转列表:

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

Note that it does not make a new list which is reversed, instead it destructively takes the list and reverses it, also to do this you need to pass it a pointer to a "pointer to the head", and then you can reach the result in the "pointer to the head" (must be the same thing you passed the pointer of).请注意,它不会创建一个反转的新列表,而是破坏性地获取列表并将其反转,为此,您需要将指针传递给“指向头部的指针”,然后您可以达到结果在“指向头部的指针”中(必须与您传递的指针相同)。 If however you would like to keep the original list in one piece, you can copy the whole list first, then reverse that new list.但是,如果您想将原始列表保持在一个整体中,您可以先复制整个列表,然后反转该新列表。 Code for copying list, based on 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;
}

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

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