revsindoulist.txt

来自「It is an ebook about data structures,mai」· 文本 代码 · 共 54 行

TXT
54
字号
How do you reverse a singly linked list? How do you reverse a doubly linked list? Write a C program to do the same. 

Discuss it!          


              

                     /* SINGLY LINKED LIST */ 

#include <stdio.h> 
#include <stdlib.h> 

struct linkedList{ 
    int element; 
    struct linkedList *next; 
}; 

typedef struct linkedList* List; 

List reverseList(List L) 
{ 
    List tmp, previous=NULL; 

    while(L){ 
        tmp = L->next; 
        L->next = previous; 
        previous = L; 
        L = tmp; 
    } 
    L = previous; 
    return L; 
     
} 

List recursiveReverse(List L) 
{ 
    List first, rest; 
    if(!L) 
        return NULL; 
    first = L; 
    rest = L->next; 
    if(!rest) 
        return NULL; 
    rest = recursiveReverse(rest); 
    first->next->next = first; 
    first->next = NULL; 
    L=rest; 

    return L; 
} 


 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?