• 链表的基本操作源代码


    //链表的基本操含有对链表的建立,插入,删除,查询,输出等基本操作

    /在此特别提醒链表的基础尤为重要,在这里重写此代码能够更加深刻的理解了链表的空间的申请,

    /头节点的建立以及头指针的重要和尾节点存在的必要性

    /这个源代码以链表的创建为核心,为了怕麻烦就没有进行优化,但是创建和输出是这个代码的核心。

    /希望这个能帮助到初学者,在这有不懂得地方可以看我的随笔,链表的整表的创建,那里有更加详细的介绍

    /在此附上图:

    一、初始化界面

    二、创建链表

    三、输出所创建的链表中元素

    源代码:

    //先建立一个完整的链表,然后对其进行增删改查
    //在这里我们采用尾插入的方式来对其进行建立链表

    #include <iostream>
    #include <stdlib.h>
    #include <malloc.h>
    using namespace std;
    typedef int DataType;
    //声明节点
    typedef struct node{
    DataType data;
    struct node *next;
    }SLNode;
    //创建头指针
    SLNode *InitiateHead(SLNode*phead){
    phead = (SLNode*)malloc(sizeof(SLNode));
    phead->next = NULL;
    return phead;
    }
    //创建链表
    SLNode * Create_List(SLNode *head){//头节点开始的申请空间
    SLNode *r,*p;
    int x;
    char c ;
    if(head !=NULL){
    free(head);
    head = NULL;
    }
    head = (SLNode*)malloc(sizeof(SLNode));
    head->data=x;
    if(head !=NULL){
    r = head;
    cout<<"空间成功申请!"<<endl;
    }
    cout<<"创建链表:"<<endl;
    while(c !=NULL||c!='n'){
    cout<<"请输入:";cin>>x;
    p = (SLNode*)malloc(sizeof(SLNode));
    p->data = x;
    r->next = p;
    r = p;
    cout<<"是否继续输入:Y/N"<<endl;
    cin>>c;
    if(c == 'Y'||c == 'y'){
    continue;
    }else{
    r->next= NULL;
    cout<<"首地址1:"<<head<<endl;///
    cout<<"链表创建完成!"<<endl;
    return head;
    }

    }
    }
    //链表的插入
    int ListInsert(SLNode *head,int i ,DataType x){
    SLNode *p,*q;
    p = head->next;
    int j = 0;
    while(p->next!= NULL && j < i-1){
    p = p->next;
    j++;
    }
    if(j != i -1){
    cout<<"插入位置参数错误!"<<endl;
    return 0;
    }
    q = (SLNode *)malloc(sizeof(SLNode));
    q->data= x;
    q->next= p->next;
    p->next = q;
    cout<<"插入成功 !"<<endl;
    return 1;
    }
    //链表的删除
    int ListDelete(SLNode *head,int i ,DataType *x){
    SLNode *p ,*s;
    int j;
    p = head->next;
    j = 0;
    while( p ->next !=NULL && j<i -1){
    p = p->next;
    j++;
    }
    if(j != i -1){
    cout <<"删除位置参数错误!"<<endl;
    return 0;
    }
    s = p->next;
    *x = s->data;
    p->next = p->next->next;
    free(s);
    cout<<"删除成功!"<<endl;
    return 1;
    }
    //查询获取元素
    int ListGet(SLNode *head,int i ,DataType *x){
    SLNode *p ;
    int j;
    p = head->next;
    j = 0;
    while(p->next!= NULL &&j <i){
    p = p->next;
    j++;

    }
    if(j != i){
    cout<<"取元素 位置参数错误!"<<endl;
    return 0;
    }
    *x = p->data;
    cout<<"查找成功!"<<endl;
    return 1;

    }
    void OutPut_List(SLNode *phead){
    SLNode *p;
    // cout<<"首地址3:"<<phead<<endl;///
    //cout<<"首地址4:"<<phead->next<<endl;
    p = phead->next->next;
    //cout<<"data:"<<p->data<<endl;
    int i = 1;
    while(p!=NULL){
    cout<<"第"<<i<<"一个元素:"<<p->data<<endl;
    i++;
    p = p->next;
    }
    }
    //释放内存,避免内存的泄漏
    void Destroy(SLNode *head){
    SLNode *p = head ;
    SLNode *p1;
    while(p != NULL){

    p1 = p;
    p = p->next;
    free(p1);

    }
    head = NULL;

    cout<<"释放内存成功!"<<endl; 

    }
    void menu(){
    cout<<"_________________________________________________________"<<endl;
    cout<<"|*******************************************************|"<<endl;
    cout<<"|*****************欢迎进入菜单选项*.********************|"<<endl;
    cout<<"|*****************进入初始化阶段请稍后....**************|"<<endl;
    cout<<"|*****************0、清屏 **************|"<<endl;
    cout<<"|*****************1、创建链表 **************|"<<endl;
    cout<<"|*****************2、输出链表 **************|"<<endl;
    cout<<"|*****************3、链表的插入 **************|"<<endl;
    cout<<"|*****************4、链表的删除 **************|"<<endl;
    cout<<"|*****************5、链表的查询 **************|"<<endl;
    cout<<"|*****************6、释放内存空间 **************|"<<endl;
    cout<<"|*******************************************************|"<<endl;
    cout<<"_________________________________________________________"<<endl;
    }
    void operation(SLNode *phead){
    SLNode *p,*p1;
    int n,i;
    int input;
    cout<<"请选择:"<<endl;
    cin>>input;
    //p1=InitiateHead(phead);//头指针
    switch(input){
    case 0:system("CLS");menu();operation(phead);
    case 1:p1=Create_List(p);
    /* cout<<"首地址2:"<<p1<<endl;//此为创建链表后返回的头节点的地址,头节点不存任何东西*/
    phead->next=p1; phead->next=p1;menu();operation(phead); break;
    case 2:cout<<"输出已创建好的链表中的数据:"<<endl;OutPut_List(phead);menu();operation(phead);break;
    case 3:cout<<"请输入要插入的元素:";cin>>n;cout<<endl;cout<<"请输入要插入的位置";cin>>i;
    ListInsert(phead,i ,n);menu();operation(phead);menu();operation(phead);break;
    case 4:cout<<"请输入要删除的元素:";cin>>n;cout<<endl;cout<<"请输入要删除的位置";cin>>i;
    ListDelete(phead,i ,&n);menu();operation(phead);menu();operation(phead);break;
    case 5:cout<<"请输入要查询的元素:";cin>>n;cout<<endl;cout<<"请输入要查询的位置";cin>>i;
    ListGet(phead,i ,&n);menu();operation(phead);menu();operation(phead);break;
    case 6:cout<<"释放存储空间:"; Destroy(phead);break;
    default :cout<<"请重新选择!"<<endl; menu();operation(phead);

    }
    }

    int main(){
    SLNode *phead;
    phead=InitiateHead(phead);
    cout<<"首地址2.1:"<<phead<<endl;//此为头指针的地址
    menu();
    operation(phead);

    return 0;

    }

  • 相关阅读:
    痞子衡嵌入式:恩智浦i.MX RTxxx系列MCU特性那些事(1)- 概览
    痞子衡嵌入式:16MB以上NOR Flash使用不当可能会造成软复位后i.MXRT无法正常启动
    《痞子衡嵌入式半月刊》 第 12 期
    不能错过的分布式ID生成器(Leaf ),好用的一批!
    实用!一键生成数据库文档,堪称数据库界的Swagger
    安排上了!PC人脸识别登录,出乎意料的简单
    又被逼着优化代码,这次我干掉了出入参 Log日志
    图文并茂,带你认识 JVM 运行时数据区
    一文说通C#中的异步编程补遗
    一文说通C#中的异步编程
  • 原文地址:https://www.cnblogs.com/qpxv/p/3733274.html
Copyright © 2020-2023  润新知