This implementation mainly adopts the exchange pointer method, with a single-chain table attached and its related implementation.
# Include <stdio. h> struct node; typedef struct node * ptrtonode; typedef ptrtonode list; typedef ptrtonode position; typedef int elementtype; struct node {elementtype element; position next;}; List initlist () {list l = (list) malloc (sizeof (struct node); L-> next = NULL; return l;} void insertelement (list l) {int element; while (scanf ("% d", & element) {position TMP = (position) malloc (sizeof (struct node); TMP-> element = Element; TMP-> next = L-> next; L-> next = TMP ;}} void printlist (list l) {position P = L-> next; printf ("L->"); While (P! = NULL) {printf ("% d->", p-> element); P = p-> next;} printf ("null \ n ");} int get_length (list l) {int I = 0; position P = L-> next; while (P! = NULL) {I ++; P = p-> next;} return I;} void bubblepoint (list l) {position pre; position cur; position next; int I, j; I = get_length (l); printf ("length = % d \ n", I); While (I! = 1) {pre = L; cur = L-> next; next = cur-> next; j = I; I --; while (J! = 1) {J --; If (cur-> element> next-> element) {cur-> next = Next-> next; pre-> next = next; next-> next = cur; Pre = next; next = cur-> next;} else {pre = cur; cur = next; next = Next-> next ;}}}} int main (INT argc, char ** argv) {list l; L = initlist (l); insertelement (l); printlist (l); bubblepoint (L ); printlist (l );}
The execution result is as follows: