• 用链表排序,并删除指定数字


    #include<stdio.h>
    #include<iostream>
    #include<stdlib.h>
    using namespace std;
    struct data
    {
     int vt;
     struct data *next;
    };
    data *head;
    data* insert(data* headt,int N)
    { //printf("<1> ");
     data *listnode,*nodet;
     listnode=(data*)malloc(sizeof(data*));
     nodet=headt;
     listnode->vt=N;
     listnode->next=NULL;
     if(headt->vt>listnode->vt)
     { //printf("<2> ");
      listnode->next=nodet;
      headt=listnode;
      return headt;
     }
     else
     { while(nodet->next!=NULL)
      {if(nodet->vt<=listnode->vt&&nodet->next->vt>listnode->vt)
      { //printf("<3> ");
       listnode->next=nodet->next;
       nodet->next=listnode;
       break;
      }
      nodet=nodet->next;
      //printf("<7> ");
     //for(int i=1;i<=100000000;i++);
     /*data *ppt;
     ppt=headt;
     while(ppt!=NULL)
     { 
      cout<<ppt->vt<<' ';
      ppt=ppt->next;
     }
     cout<<endl;*/
      }
      //printf("<6> ");
      if(nodet->next==NULL)
       {//printf("<4> ");
       nodet->next=listnode;
       }
     }
     //free(listnode);
     //printf("<5> ");
     return headt;
    }
    data* remove(data* headt,int N)
    {
     data *nodet;
     nodet=headt;
     while(headt->vt==N)
     {
      nodet=headt->next;
      free(headt);
      headt=nodet;
     }
     while(nodet->next!=NULL)
      {
       if(nodet->next->vt==N)
       {
        data *pt;pt=nodet->next;
        nodet->next=nodet->next->next;
        free(pt);
       }
       else
        nodet=nodet->next;
      }
     return headt;
    }
    void output(data* headt)
    { //cout<<"1>>"<<endl;
     data *ppt;
     ppt=headt;
     while(ppt!=NULL)
     { 
      cout<<ppt->vt<<' ';
      ppt=ppt->next;
     }
     cout<<endl;
    }
    int main()
    {
     int m,n,mi;
     head=(data*)malloc(sizeof(data*));
     cin>>n;
     cin>>m;
     head->vt=m;head->next=NULL;
     for(int i=1;i<=n-1;i++)
      {
       cin>>m;
       head=insert(head,m);
      }
     output(head);
     cout<<"输入删除的数:";
     cin>>mi;
     head=remove(head,mi);
     output(head);
     data *pt,*pm;
     pt=head;pm=pt->next;
     while(pt!=NULL)
     {
      free(pt);
      pt=pm;
      if(pm!=NULL) pm=pm->next ;
     }
     return 0;
    }
     
    特别注意:代码最后删除所有申请的空间;
    中间函数基本上就是添加结点删除结点的模板了;
  • 相关阅读:
    金融系列4《PUTKEY指令》
    数据分析≠Hadoop+NoSQL,不妨先看完善现有技术的10条捷径(分享)
    ASP.NET对HTML元素进行权限控制(三)
    ASP.NET对HTML元素进行权限控制(二)
    ASP.NET对HTML元素进行权限控制(一)
    作弊控制——心态
    SQL多表连接
    ASP.NET Repeater嵌套Repeater实现菜单加载
    ASP.NET从数据库中取出数据,有数据的复选框为选中
    ASP.NET——拒绝访问。 (异常来自HRESULT:0x80070005 (E_ACCESSDENIED))
  • 原文地址:https://www.cnblogs.com/xcsj/p/12002798.html
Copyright © 2020-2023  润新知