⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortedlinkedlist.cpp

📁 回顾基础
💻 CPP
字号:
//【例2.5】  建立排序的单链表。

#include <stdlib.h>                              //其中定义随机数函数rand()

//#include "SinglyLinkedList.h"                      //不带头结点的单链表
//#include "HSLinkedList.h"                        //带头结点的单链表
//#include "SortedHSLinkedList.h"                  //排序的单链表
//#include "CirHSLinkedList.h"                     //带头结点的循环单链表
//#include "HDoublyLinkedList.h"                   //双链表
//#include "CirHDoublyLinkedList.h"                //带头结点的循环双链表
#include "SortedCHDLinkedList.h"               //排序的带头结点的循环双链表

void random(int table[], int n)                  //将数组元素填充为非0随机数,n指定数组长度
{                                                //同例1.3
    int i=0;
    while (i<n)
    {
        int value = rand() % 100;                //随机数范围是0~99
        if (value!=0)
            table[i++] = value;
    }
}

int main()
{
    const int N=8;
    int table[N]={41,5,67,41,19,26,97,1};
//    random(table, N);                            //产生随机数
//    SinglyLinkedList<int> list(table, N);
//    HSLinkedList<int> list(table, N);
//    SortedHSLinkedList<int> list(table, N);
//    CirHSLinkedList<int> list(table, N);
//    HDoublyLinkedList<int> list(table, N);
//    CirHDoublyLinkedList<int> list(table, N); 
    SortedCHDLinkedList<int> list(table, N); 

/*    list.insert(0,0);
    list.insert(1,1);
    list.insert(-1,-1);
    list.insert(N+5,105);
    list.insert(200);
    list.print(); 

    int old; 
    if (list.remove(N+4,old))
        cout<<"remove("<<old<<")"<<endl;
*/

//    list.insert(0);
    cout<<list; 
    cout<<"remove("<<table[0]<<")="<<list.remove(table[0])<<","<<list; 
    list.printPrev(); 

    listb.
    return 0;
}

/*
程序运行结果如下:
 (1, 5, 19, 26, 41, 41, 67, 97)
remove(41)=1, (1, 5, 19, 26, 41, 67, 97)
listPrev: (97, 67, 41, 26, 19, 5, 1)
*/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -