1 #include<iostream> 2 3 using namespace std; 4 #define swap(a,b) {int t;t = a;a = b;b = t;} 5 //节点类型的定义 6 typedef struct node 7 { 8 int data; 9 node *prior, *next; 10 }Node, *pNode; 11 12 void TwoWayBubble(pNode &L); 13 void CreateList(pNode &L, int n); 14 void Traverse(pNode L); 15 16 void TwoWayBubble (• pNode & L) . 17 { 18 is • pNode head = L; // head . 19 • pNode tail = NULL; // End 20 is • pNode L-P => Next; 21 is • pNode Q; 22 is BOOL Exchange = to true ; // if exchange , depending on whether the exchange takes place and finally determines whether the loop end 23 is 24 the while (Exchange) 25 { 26 is Exchange = to false ; 27 // large numbers sink to the bottom 28 the while (p-> Next = tail!) // left to right large number sink to the bottom 29 { 30 Q = p-> Next; 31 is IF (p-> Data> Q-> Data) 32 { 33 is Exchange = to true ; // occurred exchange 34 is the swap (p-> Data, Q-> Data); 35 P Q =; // P is moved backward 36 } 37 [ the else P = p-> Next; // not exchange occurs, backward 38 } 39 40 // decimal bubbler 41 is P = p-> Prior; 42 is the while (p- > prior! = head)// right to left decimal bubble 43 is { 44 is Q = p-> Prior; 45 IF (p-> Data <Q-> Data) 46 is { 47 Exchange = to true ; // occurred exchange 48 the swap (p-> Data, Q-> Data); 49 P = Q; // P move forward 50 } 51 is the else P = p-> Prior; // not exchange occurs, forward 52 is } 53 is } 54 is } 55 56 is voidCreateList (• pNode L &, int n-) 57 is { 58 // create a header node 59 L = new new the Node; // allocate space for a node; 60 // precursor successor are blank 61 is L-> Next = NULL; 62 is L -> = Prior NULL; 63 is • pNode P = L; // P points to the last node 64 int a; 65 for ( int I = 0 ; I <n-; I ++ ) 66 { 67 CIN >> a; 68 • pNode PNEW = new newThe Node; // allocate a new node space; 69 p-> Next = PNEW; // connect a node 70 pNew-> Prior = P; // precursor new node 71 is pNew-> Next = NULL; / / successor node of the new node 72 pNew-> Data = a; // range of the new node 73 is P = PNEW; // P update 74 } 75 } 76 77 void Traverse (• pNode L) 78 { 79 • pNode L-P = > Next; 80 the while (! P = NULL) 81 { 82 << p-COUT> Data << " " ; 83 P = p-> Next; 84 } 85 COUT << endl; 86 } 87 88 int main () 89 { 90 int n-; // number of data elements 91 is • pNode L; // doubly linked list 92 CIN >> n-; 93 CreateList (L, n-); // list construct 94 // Traverse (L); 95 TwoWayBubble (L); // bidirectional bubble 96 Traverse (L); //Traversing the list 97 return 0 ; 98 }
Doubly linked list bidirectional bubble sort c ++
Guess you like
Origin www.cnblogs.com/zhangqiling/p/12051410.html
Recommended
Ranking