zoukankan      html  css  js  c++  java
  • 链表及其各种函数操作的实现方法

    代码中主要实现了下面四个操作:

    下面几种操作都是线性操作,算法复杂度都是O(n);

    1. 链表插入默认是按关键字大小插入链表的,所以最后得到的结果是从大到小排好序的,分三种情况(1)链表为空(2)插入的点最小,插在链表最前面;(3)插入链表中间;(4)插入链表结尾。
    2. 链表删除是在链表中找到要删除的关键字,然后删除该节点,如果有两个以上,只删一个。如果没有就返回。删除操作必须释放删除结点所申请的内存:
    3. 查找最大理论上就是最后一个,查找最小理论上就是第一个结点。但是我为了通用性写成了遍历整个链表查找最大和最小的结点。(如果找最小直接返回第一个节点算法复杂度为O(1))
    4. 打印链表,从头到尾打印链表。

    主函数中我一函数运行时间为种子生成了一个随机数组,然后一个一个插入链表。

      1 #include<iostream>
      2 #include<time.h>
      3 using namespace std;
      4 #define N 100
      5 struct list{
      6     int val;
      7     list* next;
      8 };
      9 list *temp=NULL,*head=NULL;
     10 int insertlist(int data){
     11     temp=new list;//临时结点保存要插入的值,
     12                   //每次重新申请空间,因为在外面定义,全局变量
     13                   //所以不用担心申请的空间在函数结束时被系统销毁
     14     temp->val=data;
     15     if(head==NULL){//如果链表中没有值的时候
     16         temp->next=NULL;
     17         head=temp;
     18         return 0;
     19     }
     20     list* node=new list;//记录用来与插入值对比的下一个节点,
     21                         //必须新申请空间,因为指向地址不停在变,
     22                         //不申请的话头指针所指向地址也跟着变
     23                         //局部变量,函数返回时自动销毁所申请内存
     24     list* nodelast;//记录node上一个节点,可以不用申请新内存
     25     node=head;
     26     while(node){
     27         if(data<head->val){//如果插入第一个
     28             temp->next=head;//把当前头节点的地址赋给临时变量的下一个
     29             head=temp;//把头指针换为新结点
     30             return 0;
     31         }
     32         else if(data<node->val){//如果在中间插入
     33             temp->next=node;
     34             nodelast->next=temp;//node上一个节点指向新插入的节点
     35             return 0;
     36         }
     37         else{
     38             nodelast=node;
     39             node=node->next;
     40         }
     41     }
     42     temp->next=NULL;//在最后插入
     43     nodelast->next=temp;
     44     return 0;
     45 }
     46 int deletelist(int data)
     47 {
     48     if(head==NULL)
     49     {
     50         cout<<"链表为空"<<endl;
     51         return 0;
     52     }
     53     if(head->val==data)
     54     {
     55         list *t=NULL;
     56         t=head;
     57         head=head->next;
     58         delete t;
     59         return 0;
     60     }
     61     temp=new list;
     62     temp=head;
     63     while(temp->next)
     64     {
     65         if(temp->next->val==data)
     66         {
     67             list *t=NULL;
     68             t=temp->next;
     69             temp->next=temp->next->next;
     70             delete t;
     71             return 0;
     72         }
     73         temp=temp->next;
     74     }
     75     cout<<"链表中没有"<<data<<endl;
     76     return 0;
     77 }
     78 int findmax()
     79 {
     80     int max=0;
     81     if(head==NULL)
     82     {
     83         cout<<"链表为空"<<endl;
     84         return 0;
     85     }
     86     temp=new list;
     87     temp=head;
     88     while(temp)
     89     {
     90         if(temp->val>max){
     91             max=temp->val;
     92         }
     93         temp=temp->next;
     94     }
     95     return max;
     96 }
     97 int findmin()
     98 {
     99     int min=65565;
    100     if(head==NULL)
    101     {
    102         cout<<"链表为空"<<endl;
    103         return 0;
    104     }
    105     temp=new list;
    106     temp=head;
    107     while(temp)
    108     {
    109         if(temp->val<min){
    110             min=temp->val;
    111         }
    112         temp=temp->next;
    113     }
    114     return min;
    115 }
    116 int printlist()
    117 {
    118     list* node=new list;//
    119     node=head;
    120     while(node){
    121         cout<<node->val;
    122         if(node->next)
    123             cout<<"->";
    124         node=node->next;
    125     }
    126     cout<<endl;
    127     return 0;
    128 }
    129 int main()
    130 {
    131     int number[N],j,t;
    132     for(int i=0;i<N;i++)
    133         number[i]=i;
    134     srand((unsigned)time(NULL));
    135     for(int i=0;i<N;i++)//随机生成1到N的数组
    136     {
    137         j=rand()%N;
    138         t=number[i];
    139         number[i]=number[j];
    140         number[j]=t;
    141     }
    142     cout<<"插入前:";
    143     for(int i=0;i<N;i++)
    144     {
    145         cout<<number[i];
    146         if(i<N)
    147             cout<<"->";
    148     }
    149     cout<<endl<<endl<<"插入后:";
    150     for(int i=0;i<N;i++)
    151     {
    152         insertlist(number[i]);
    153     }
    154     deletelist(99);
    155     deletelist(0);
    156     printlist();
    157     cout<<endl<<endl<<"链表中最大值为:"<<findmax()<<endl;
    158     cout<<endl<<endl<<"链表中最小值为:"<<findmin()<<endl;
    159     return 0;
    160 }
  • 相关阅读:
    一分钟搞懂列式与行式数据库(转)
    docker daemon 配置文件
    Docker-删除untagged docker images
    全栈JavaScript之路(十三)了解 ElementTraversal 规范
    static, readonly, const
    Timer与AlarmManager的差别
    计算客 (人人都有极客精神)爆力
    nginx 配置web 虚拟文件夹 而且codeIgniter,thinkphp 重定向url 地址
    单例模式之 懒汉模式普通版
    POJ 3468 A Simple Problem with Integers 【树状数组】
  • 原文地址:https://www.cnblogs.com/jhmu0613/p/6087128.html
Copyright © 2011-2022 走看看