Last active
August 29, 2015 14:02
-
-
Save duyet/96274e0fdc7a30219445 to your computer and use it in GitHub Desktop.
Function to reverse a linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* http://lvduit.com */ | |
#include <iostream> | |
#include <time.h> | |
using namespace std; | |
const int MIN = 1; | |
const int MAX = 50; | |
typedef struct dNode { | |
int value; | |
struct dNode * next; | |
} NODE; | |
typedef struct nList { | |
struct dNode * head, * tail; | |
} LIST; | |
// node function | |
inline NODE * createNode(int x); | |
// list function | |
inline LIST * createList(); | |
inline void printList(LIST * L); | |
// head function | |
inline void addHead(LIST * L, NODE * newNode); | |
inline void addHead(LIST * L, int x); | |
inline NODE * removeHead(LIST * L); | |
inline void deleteHead(LIST * L); | |
// tail function | |
inline void addTail(LIST * L, NODE * newNode); | |
inline void addTail(LIST * L, int x); | |
inline void deleteTail(LIST * L); | |
// addon function | |
inline void swap(int & a, int & b) { a ^= b ^= a ^= b; } | |
int getRand() { return rand() % (MAX - MIN + 1) + MIN; } | |
// list action | |
inline void reverse(LIST * L) { | |
NODE * next; | |
NODE * curr = L->head; | |
NODE * prev = NULL; | |
while (curr != NULL) { | |
next = curr->next; | |
curr->next = prev; | |
prev = curr; | |
curr = next; | |
} | |
L->head = prev; | |
} | |
// -------------------------------------------------------------------------------------------------- | |
int main() { | |
srand((unsigned int)time(0)); | |
LIST * L = createList(); | |
for (int i = 0; i < 20; i++) { | |
addTail(L, getRand()); // Tạo bộ ngẫu nhiên | |
} | |
printList(L); cout << endl; | |
reverse(L); // đảo | |
printList(L); | |
return 0; | |
} | |
// -------------------------------------------------------------------------------------------------- | |
inline NODE * createNode(int x) { | |
NODE * newNode = new NODE; | |
newNode->value = x; | |
newNode->next = NULL; | |
return newNode; | |
} | |
inline LIST * createList() { | |
LIST * newList = new LIST; | |
newList->head = newList->tail = NULL; | |
return newList; | |
} | |
inline void printList(LIST * L) { | |
NODE * currentNode = L->head; | |
while (currentNode != NULL) { | |
cout << currentNode->value << " "; | |
currentNode = currentNode->next; | |
} | |
} | |
inline void addHead(LIST * L, NODE * newNode) { | |
if (newNode == NULL) return; | |
if (L->head == NULL) { | |
L->head = L->tail = newNode; | |
return; | |
} | |
newNode->next = L->head; | |
L->head = newNode; | |
return; | |
} | |
inline void addHead(LIST * L, int x) { | |
NODE * newNode = createNode(x); | |
return addHead(L, newNode); | |
} | |
inline void addTail(LIST * L, NODE * newNode) { | |
if (newNode == NULL) { | |
return; | |
} | |
if (L->tail == NULL) { | |
L->head = L->tail = newNode; | |
} | |
L->tail->next = newNode; | |
L->tail = newNode; | |
return; | |
} | |
inline void addTail(LIST * L, int x) { | |
NODE * newNode = createNode(x); | |
if (newNode == NULL) { | |
return; | |
} | |
return addTail(L, newNode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment