zoukankan      html  css  js  c++  java
  • 链表的插入排序

      

     1 #include <cstring>
     2 #include <cstdio>
     3 using namespace std;
     4 
     5 typedef int type;
     6 
     7 template<class T>
     8 struct Node {
     9     T data;
    10     Node *next;
    11     Node<T>() {
    12         data = 0;
    13         next = NULL;
    14     }
    15 };
    16 
    17 Node<type> *head = new Node<type>();
    18 
    19 template<class T> 
    20 void add(Node<T> *n0) {
    21     Node<T> *pr = head;
    22     Node<T> *now = head -> next;
    23     while(now) {
    24         if(now -> data > n0 -> data) {
    25             n0 -> next = now;
    26             pr -> next = n0;
    27             return ;
    28         }
    29         pr = now;
    30         now = now -> next;
    31     }
    32     pr -> next = n0;
    33 }
    34 
    35 template<class T>
    36 void print() {
    37     Node<T> *p = head -> next;
    38     while(p) {
    39         printf("%d ", p -> data);
    40         p = p -> next;
    41     } puts("");
    42 }
    43 
    44 
    45 int main() {
    46     int n;
    47     type x;
    48     while(cin >> n) {
    49         cin >> x;
    50         Node<type> *p = new Node<type>();
    51         p -> data = x;
    52         p -> next = NULL;
    53         head -> next = p;
    54         for(int i = 1; i < n; i++) {
    55             cin >> x;
    56             Node<type> *n0 = new Node<type>();
    57             n0 -> data = x;
    58             add<type>(n0);
    59         }
    60         print<type>();
    61     }
    62 }
    View Code
  • 相关阅读:
    WLC-Download 3-party CA to WLC
    WLC-生成CSR操作
    MSE-初始化MSE
    Nexus-产品认识
    Nexus-配置VDC
    Nexus-VDC(Virtual Device Context)
    Nexus-FEX基础配置
    Nexus-配置vPC 实验三
    Nexus-配置vPC 实验二
    Nexus-配置vPC 实验一
  • 原文地址:https://www.cnblogs.com/zhanzhao/p/4810988.html
Copyright © 2011-2022 走看看