跳过正文
  1. 学习笔记/
  2. Language/
  3. C++ Algorithm/
  4. C++ · 其他/

·2 分钟·
lyrumu
作者
lyrumu
目录

数据结构
#


语法
#


Class类
#

类似于Struct结构体的升级拓展版,先初步了解一下Class的基本语法吧.

用于操作和数据的封装,面向对象

大部分情况,Class内部只包含**数据和函数**

能够把近似的数据和函数归类整理,有助于避免屎山代码

Class内部可以再嵌套另一个Class,这里不展开

 1class 类名称{
 2private://以下内容私有,只有class内部可以调用和访问
 3    //这里暂时不讨论private,只以public为例
 4    string password;//比如密码私有
 5public://以下内容公开,class内外都可以调用和访问
 6    int age;
 7    string name;//属性,数据
 8    void print(){//普通函数
 9        cout<<age<<" "<<name<<endl;//用于做具体操作
10    }
11    类名称(int nianling,string xingming){//构造函数
12        //函数名称必须与类名称一样,不需要写返还的类型
13        //用于创造东西
14        age = nianling;
15        name = xingming;
16    }
17    类名称(){
18        age = 0;
19        name = "";
20    }
21};//别漏分号
22
23
24int main(){
25    //Class的类名称即是一个自定义的变量类型
26    //可以自行在main中调用
27    类名称 pastman;//无构造函数的初始化
28    pastman.age = 18;
29    pastman.name = "hth";
30    //调用构造函数创建对象
31    类名称 deadman(18,"hth");
32    //利用class创建了一个自定义类型的变量,同时调用构造函数进行了初始化
33    return 0;
34}

动态链表
#

一般都是推荐带上头结点的:

以几个字节存储空间的代价换来后面插入删除等逻辑的统一

前插入法:

(前插入法的结果会使数据与写入顺序相反)

每次的newnode要先后处理它的头和尾,也就是这个newnode的next要指向哪?又有谁的next指向这个newnode?

newnode->next = head->next;//

head->next = newnode;//

(两行代码顺序不能换)

 1#include<bits/stdc++.h>
 2using namespace std;
 3struct node{//结点
 4    int data;
 5    node *next;
 6}
 7node *createlist(){//链表创建
 8    node *head = new int();//创建头结点
 9    head->next = NULL;
10    while(1){
11        int value;
12        cin>>value;
13        if(value<=0)break;
14        node *newnode = new node();//创建新结点
15        newnode->data = value;//先把数据域处理掉
16        newnode->next = head->next;//处理新结点的头
17        head->next = newnode;//处理新结点的尾
18    }
19    return head;//返回头结点
20}

头结点

尾插入法:

 1#include<bits/stdc++.h>
 2using namespace std;
 3struct node{
 4    int data;
 5    node *next;
 6}
 7node *createlist(){
 8    node *head = new node();//创建头结点
 9    head->next = NULL;
10    node *tail = head;//需要再创建一个尾结点
11    while(1){
12        int value;
13        cin>>value;
14        if(value<=0)break;
15        node *newnode = new node();
16        newnode->data = value;//老样子先处理数据
17        tail->next = newnode;//处理newnode的尾
18        tail = newnode;//移动到newnode上
19        tail->next = NULL;//处理newnode的头
20    }
21    return head;
22}

这里只包括链表基本创建,还有链表打印,查找,插入,删除,以及内存释放,待学习


算法
#