Single list in reverse order (first interpolation method implemented using)

Establishment of a single list // 
// Create a single chain may be head or tail interpolation interpolation. First interpolation means establishing a single linked list, always insert a new node to the current linked list header.
// list reverse head used in an interpolation existing.
. 1
#include <the iostream> 2 . 3 the using STD COUT ::; . 4 the using STD :: endl; . 5 . 6 struct Item { . 7 char C; . 8 Item * Next; . 9 }; 10 Item Routine1 * (* Item X) { . 11 Item PREV = NULL *, * Curr = X; 12 is the while (Curr) { 13 is Item Next * = curr-> Next; // subsequent node is assigned to the current node Next 14 curr-> next = prev; // modify the current node junction point subsequent prev 15 prev = Curr; // At this point forward movement prev assigned Curr 16 Curr = Next; // Curr assigned curr-> next time subsequent nodes stored pointer . 17 } 18 is return prev; // last prev points is moved to the last node curr, and curr-> Next == NULL; . 19 } 20 is 21 is void Routine2 (Item * X) { 22 is Curr = * Item X; 23 is the while (Curr) { 24 COUT << curr-> C << " " ; 25 Curr = curr-> Next; 26 } 27 cout << endl; 28 29 } 30 31 int main(void ){ 32 Item *x, 33 d = {'d', NULL}, 34 c = {'c', &d}, 35 b = {'b', &c}, 36 a = {'a', &b}; 37 Routine2(&a); 38 x = Routine1(&a); 39 Routine2(x); 40 return 0; 41 }
// output: a b c d
       d c b a

Guess you like

Origin www.cnblogs.com/Davirain/p/12168944.html