• 单链表 list


      1 #include<iostream>
      2 using namespace std;
      3 
      4 struct ListNode
      5 {
      6     int num;
      7     char name[20];
      8     ListNode *link;
      9 };
     10 
     11 ListNode *first,*last;
     12 
     13 void Create()//建立一个链表
     14 {
     15     ListNode *p,*q;
     16     int i;
     17     q=first;//新节点要把节点设为空
     18     while(1)
     19     {
     20         p=new ListNode;
     21         cout<<"输入学号:";
     22         cin>>p->num;
     23         cout<<"输入姓名拼音:";
     24         cin>>p->name;
     25         p->link=q->link;
     26         q->link=p;
     27         q=p;
     28         cout<<"是否继续?(1/0)";
     29         cin>>i;
     30         if(i==0)
     31         {
     32             cout<<"链表建立完毕。"<<endl;
     33             break;
     34         }
     35     }
     36     last=p;
     37 }
     38 
     39 void Output()//输出
     40 {
     41     ListNode *p=first->link;
     42     while(p!=NULL)
     43     {
     44         cout<<"学号:"<<p->num<<":";
     45         cout<<"姓名:"<<p->name<<endl;
     46         p=p->link;
     47     }
     48 }
     49 
     50 ListNode* FindIndex(const int i)//查找第i个元素
     51 {
     52     if(i<=-1) return first;
     53     ListNode *p=first->link;
     54     int j=0;
     55     while(p!=NULL&&j<i)
     56     {
     57         if(p->num==i) break;
     58         p=p->link;
     59         j++;
     60     }
     61     return p;
     62 }
     63 
     64 ListNode* Insert(int valua,int i)//插入一个值
     65 {
     66     ListNode *p,*q;
     67     q=new ListNode;//申请一个新节点
     68     p=FindIndex(i-1);//找到插入位置的前一位置,在它的后方插入VALUA
     69     if(p==NULL) return NULL;
     70     q->link=p->link;
     71     q->num=valua;
     72     p->link=q;
     73     if(q->link==NULL)
     74         last=q;
     75     return q;
     76 }
     77 
     78 void RemoveAfter(ListNode *cl)//删除CL节点之后的一个节点
     79 {
     80     ListNode *newlink=cl->link;
     81     if(newlink!=NULL)
     82     {
     83         cl->link=newlink->link;
     84         delete newlink;
     85     }
     86 }
     87 
     88 void RemoveCur(ListNode *cl)//删除单个节点
     89 {
     90     ListNode *p=first;
     91     while(p->link!=cl)
     92     {
     93         p=p->link;
     94     }
     95     p->link=cl->link;
     96     delete cl;
     97 }
     98 
     99 void length()//计算链表长度
    100 {
    101     int len=0;
    102     ListNode *p=first->link;
    103     while(p!=NULL)
    104     {
    105         p=p->link;
    106         len++;
    107     }
    108     cout<<"链表长度为:"<<len<<endl;
    109 }
    110 
    111 
    112 
    113 void main()
    114 {
    115     first=new ListNode;
    116     first->link=NULL;
    117     
    118     Create();
    119     Output();
    120 
    121     int m;
    122     cout<<"查找学号:";
    123     cin>>m;
    124     ListNode *n=FindIndex(m);
    125     cout<<n->num<<":"<<n->name<<endl;
    126 
    127     int del;
    128     cout<<"需要删除的学号:";
    129     cin>>del;
    130     ListNode *delet=FindIndex(del);
    131     RemoveCur(delet);
    132 
    133     Output();
    134     length();
    135 }
  • 相关阅读:
    ps中套索工具怎么使用的方法
    动态加载css方法实现和深入解析
    深入React事件系统(React点击空白部分隐藏弹出层;React阻止事件冒泡失效)
    近期项目中用到的一些自己写的或者整理而成的前端效果干货(********************************)
    Vue.js自定义指令的用法与实例
    vue初探
    React Router基础使用
    javascript常用积累
    js动画(三)
    基于jQuery的ajax系列之用FormData实现页面无刷新上传
  • 原文地址:https://www.cnblogs.com/coder2012/p/2708832.html
Copyright © 2020-2023  润新知