[英]How to reverse a single-linked list in C?
對於我的班級,我的任務是建立一個鏈表,輸入數據並執行不同的功能。 我的代碼編譯正常,但是reversion和建設性的reversion函數不起作用。
如果我反轉列表並打印它,我只會返回第一個節點。 我似乎在某個地方忽略了這一點。 這是我的代碼:(如果您發現任何其他功能中的任何錯誤請告訴我)提前致謝!
#include <stdio.h>
#include <stdlib.h>
typedef struct DoubleNode{
struct DoubleNode* next;
double data;
} DoubleNode;
DoubleNode* insertFirst(DoubleNode*head, double c) {
DoubleNode* temp;
temp=malloc(sizeof(DoubleNode));
temp->data= c;
temp->next= head;
return temp;
}
void printList(DoubleNode*head) {
DoubleNode* cursor;
printf("(");
cursor=head;
while (cursor != NULL) {
printf("%fl", cursor->data);
printf(" ");
cursor=cursor->next;
}
printf(")");
}
DoubleNode* insertLast(DoubleNode *head, double d) {
DoubleNode *tmp, *cursor;
//trivial case: empty list
if (head==NULL)
{return insertFirst(head,d);
} else{
//general case: goto end
cursor=head;
while (cursor->next != NULL){
cursor =cursor->next;
}
tmp= malloc(sizeof(DoubleNode));
tmp->data = d;
tmp->next = NULL;
cursor->next=tmp;
return head;
}
}
DoubleNode* reverseDoubleListCon(DoubleNode*head) {
DoubleNode *temp, *res, *cell;
cell=head;
res=NULL;
while (cell!=NULL) {
temp=malloc(sizeof(DoubleNode));
temp->data=cell->data;
temp->next=res;
res=temp;
cell=cell->next;
}
return res;
}
void reverseDoubleList(DoubleNode*head) {
DoubleNode *chain, *revChain, *cell;
cell=head;
revChain=NULL;
while (cell!=NULL) {
chain=cell->next;
cell->next=revChain;
revChain=cell;
cell=chain;
}
head=revChain;
}
double get(DoubleNode*head, double n) {
DoubleNode *cursor;
cursor=head;
for (int i=0; i<n; i++) {
cursor=cursor->next;
}
return cursor->data;
}
DoubleNode* delete(DoubleNode*head, double n) {
DoubleNode *cursor, *rest;
cursor=head;
for (int i=0; i<n; i++) {
cursor=rest;
cursor=cursor->next;
}
rest->next=cursor->next;
free(cursor);
return head;
}
DoubleNode* insert(DoubleNode*head, double d, double n) {
DoubleNode *cursor, *temp, *rest;
cursor=head;
for (int i = 0; i<n-1; i++) {
rest=cursor;
cursor= cursor->next;
}
temp=malloc(sizeof(DoubleNode));
temp->data=d;
temp->next=cursor;
rest->next= temp;
return head;
}
int main(int argc, const char * argv[]) {
DoubleNode *head;
head=malloc(sizeof(DoubleNode));
for (double i=0.0; i <11.0; i++) {
head=insertLast(head, i);
}
printList(head);
reverseDoubleList(head);
printList(head);
reverseDoubleListCon(head);
printList(head);
return 0;
}
這對你的反向功能起作用。
void reverseDoubleList(DoubleNode **head) {
DoubleNode *prev, *curr, *next;
curr=*head;
prev=NULL;
while (curr!=NULL) {
next=curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head=prev;
}
然后在main中,使用reverseDoubleList(&head)
調用它。 之后打印列表以查看效果。
要創建單個鏈表的反向副本,請將一個元素一個接一個地復制,然后將其添加到反向列表的前面:
DoubleNode* reverseDoubleListCon( DoubleNode* head) {
DoubleNode *reverse = NULL;
while ( head )
{
DoubleNode *temp = malloc(sizeof(DoubleNode));
temp->data = head->data; // copy data to new element
head = head->next; // step one forward
temp->next = reverse; // successor of new element is first element of reverse list
reverse = temp; // head of reverse list is new element
}
return reverse;
}
DoubleNode* reverseHead = reverseDoubleListCon( head );
要反轉單個鏈表,請從列表中刪除第一個元素並將其添加到反向列表的前面:
DoubleNode* reverseDoubleList( DoubleNode* head) {
DoubleNode *reverse = NULL;
while ( head )
{
DoubleNode *temp = head; // next element is head of list
head = head->next; // step one forward
temp->next = reverse; // successor of next element is first element of reverse list
reverse = temp; // head of reverse list is next element
}
return reverse;
}
head = reverseDoubleList( head );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.