As we all know, for the sequential table reversal and sorting is like an array, the data will be changed position on the line, because their data is continuous space storage, but for the list if you also only change the value of the order, then you do not understand the heart of the list, The characteristic of the list is to dynamically open up the free space and then based on the address of the previous node to find the next node, so that the concept of position in the chain is too unimportant, so for the list we want to reverse or order is to change the address stored in each node, the popular point is to change the pointer point, So I found a unified algorithm that reverses and sorts all the lists:
(The linked list is all with the head node linked list.)
Briefly describe the algorithm steps: 1. Disconnect the first node of the list from the back node (if the list of less than 1 nodes does not have to be reversed and sorted:>) the original linked list and the broken list are formed at this time.
2. For the reversal, can be broken out of the linked list of nodes one by one to insert the original linked list, for the sort, you can break out the linked list of nodes in a separate order to insert the original linked list, The so-called sequential insertion is the insertion of a number that is larger than its size (and can be found in front of a number smaller than itself, depending on how it is sorted), and this sequential insertion can be done with a single function.
The specific analysis and code are as follows: (in fact, it is necessary to reverse the circular link list completely, but for the sake of practice, we still achieve it)
1. Reversal and sequencing of single-linked list
/************ * Copyright (c) 2015,wk studios* filename:s_list.h* Co mpiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 2 5************************************************************ /#ifndef _list_h#define _list_h#include<iostream>using namespace std; #include <assert.h># Define Elemtype inttypedef struct node{elemtype data; struct Node *next;} node;typedef struct list{Node *first; Node *last; size_t size;} Mange;//1. Initialize single-linked list void initlist (mange *l);//2. Head interpolation bool Pushfront (mange *l,elemtype e);//3. show void Print (mange *l);//4. Find node* find_val (Mange *l,elemtype e) by value;//5. insert bool Insert_val in order (mange *l, elemtype x); reverse linked list bool//6 by position List (mange *l);//7. Sort list by position (small to large) bool Sort_pos_list (mange *l); #endif
/********************************************************************** * Copyright (c) 2015,wk Studios* filename:s _list.cpp* compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 2 5********************************************* /#include "List.h"//1. Initializes a single-linked list void initlist (mange *l) {node *s= (node*) malloc (sizeof (node)); ASSERT (s! = NULL); s->data=-1000000;//here can also put other information, here means no information s->next=null; l->first=l->last=s; l->size=0;} 2. Head interpolation bool Pushfront (mange *l,elemtype e) {node *s= (node*) malloc (sizeof (node)); assert (s! = NULL);s->data=e;s-> next=l->first->next; L->first->next=s;if (L->size = = 0) {l->last=s; }l->size++;return true;} 3. Show void Print (Mange *l) {if (l->first->next==null) {cout<< "EMPTY list! \ n "; return;} Node *p=l->first->next;while (p!= NULL) {cout<<p->data;if (p->next!=null) {cout<< "-";} p=p->next; }cout<< "\ n";} 4. Find node* Find_val by value (Mange *l,elEmtype e) {Node *p=l->first->next;while (p!=null && p->data!=e) {p=p->next;} return p;} 5. Insert bool Insert_val sequentially (mange *l, Elemtype x) {Node *p = Find_val (l,x); Node *s = (node *) malloc (sizeof (node)); assert (s! = null); s->data = X;p = L->first;while (P->next! = null) {if (x < ; P->next->data) break;p = P->next;} Insert the back of P s->next=p->next;p->next=s;if (p = = l->last) {l->last=s;} return true;} 6. Reverse the linked list by position bool Reverse_val_list (mange *l) {if (l->size<2) {cout<< "only 1 or 0 nodes, no reversal required! \ n "; return false;} Node *p=l->first->next; Node *q=p->next; l->last=p; p->next=null; while (q!=null) {p=q; Note the order and the loss of the back pointer q=q->next; Pushfront (L, p->data); }}//7. Sort list by location (small to Large) bool Sort_pos_list (mange *l) {if (l->size<2) {cout<< "only 1 or 0 nodes, no sorting required! \ n "; return true;} Node *p=l->first->next; Node *q=p->next; l->last=p; p->next=null; while (q!=null) {p=q;q=q->next; Insert_val (L,p->data); } return true;
/********************************************************************** * Copyright (c) 2015,wk Studios* filename:m ain.cpp* compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 2 5*********************************************** /#include "List.h" int main () {mange mylist; Initlist (&mylist); int select=1; Elemtype item;while (Select) {cout<< "****************************************************\n";cout<< "* [1] Pushfront [2]insert_val *\n ";cout<<" * [3]print [4]reverse_posi_list *\n]; cout& lt;< "* [5]sort_pos_list [0] Quit *\n";cout<< "********************************************* \ n ";cout<<" please input a number which be represent a operator you want to do: "; Cin>>select;switch (Sele CT) {case 1:cout<< ' please input number ' want to insert: (end of number is-1) '; while (cin>>item,item!=-1) {Pus Hfront (&mylist,item);} Break;casE 2:cout<< "Please input number want to insert: (end of number Is-1)", while (cin>>item,item!=-1) {Insert_val (&mylist,item);} Break;case 3:print (&mylist); Break;case 4:reverse_val_list (&mylist); Break;case 5:sort_pos_list (&mylist );break;default:cout<< "Thank you for using!\n"; return 0;}
2. Reversal and sequencing of single-cycle linked list
3. Reversal and sequencing of doubly linked list
4. Reversal and sequencing of double-cycle linked lists
Uncover the veil of reversal and sequencing of the list