zoukankan      html  css  js  c++  java
  • 暑假自学(5)

    链表:

    链表开头:

    struct list
    {
    int data;
    struct list *next;
    };
    typedef struct list single;

    创建链表节点的流程:

    (1)给当前的每个节点的数据结构配置定量的空间大小
        struct list *node = malloc(sizeof(struct list));
    (2)清节点数据(由于结构体变量在未初始化的时候,数据是脏的)
        memset(node,0,sizeof(struct list));
    (3)给节点初始化数据
        node->id = data ; 
    (4)将该节点的指针域设置为NULL
        node->next = NULL ;
    链表的原理是指针移动,在限制条件下依次移动寻找目标节点进行操作。

    //链表的创建插入删除遍历。

    #include<iostream>
    #include<stdlib.h>
    #include<string.h>
    using namespace std;
    struct list
    {
    int data;
    struct list *next;
    };
    typedef struct list single;
    class Lianbiao
    {
    private:

    public:
    single *createnode(int id)
    {
    single *p = NULL ;
    p = (single *)malloc(sizeof(single));
    if(p == NULL)
    {
    cout<<"error!"<<endl;
    }
    memset(p,0,sizeof(single));
    p->data = id;
    p->next = NULL ;
    return p;
    }
    void Weicha(single *p,single *new_)//L single
    {
    /*single *p;*/
    while(NULL!=p->next)
    {
    p = p->next;
    }
    p->next = new_;
    }
    void Toucha(single *p,single *new_)
    {
    new_->next = p->next;
    p->next = new_;
    }
    void Bianli(single *p)
    {
    p = p->next;
    while(NULL!=p->next)
    {
    cout<<p->data<<" ";
    p = p->next;
    }
    cout<<p->data<<endl;
    }
    int Delete(single *p,int id)
    {
    single *prev = NULL;
    while(NULL!=p->next)
    {
    prev = p;
    p = p->next;
    if(p->data == id)
    {
    if(p->next!=NULL)
    {
    prev->next = p->next;
    free(p);
    }
    else
    {
    prev->next = NULL;
    free(p);
    }
    return 0;
    }
    }
    cout<<"无需要删除的节点"<<endl;
    return -1;
    }
    void Cha(single *p,single *new_,int n)
    {
    while(n>1)
    {
    p = p->next;
    n--;
    }
    new_->next = p->next;
    p->next = new_;
    }

    };

    void Begin()
    {
    cout<<"请选择操作:"<<endl;
    cout<<"1 创建"<<endl<<"2 尾插"<<endl<<"3 头插"<<endl<<"4 遍历"<<endl<<"5 删除"<<endl;
    }
    int main()
    {
    Lianbiao a;
    single *header = a.createnode(0);
    int i;
    int num;
    for(i=0;i<10;i++)
    {
    cout<<"输入数字"<<endl;
    cin>>num;
    a.Weicha(header,a.createnode(num));
    }
    a.Toucha(header,a.createnode(50));
    a.Cha(header,a.createnode(100),4);
    a.Bianli(header);
    a.Delete(header,5);
    return 0;
    }

    明天将小学期代码进行画图整合,尽快弄完小学期上交进行java学习

  • 相关阅读:
    我的学习之路_第八章_map集合
    我的学习之路_第七章_list集合,set集合
    我的学习之路_第六章_迭代器,泛型
    我的学习之路_第五章_Data,正则
    我的学习之路_第四章_异常
    我的学习之路_第三章_匿名内部类
    我的学习之路_第二章_接口/多态
    如何使用电脑上的谷歌浏览器来调试安卓手机上的移动端页面
    跨域之jsonp
    跨域之同源策略 Same-origin policy
  • 原文地址:https://www.cnblogs.com/buxiang-Christina/p/13281614.html
Copyright © 2011-2022 走看看