[英]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.