11_7
面向对象三大特性:封装 继承 多态
**1.**c语言:数据的生命周期和进程对等,一般存在于全局变量,不存在于函数中
数据:所有的数据都会生成符号
代码:最终会生成指令,指令最终会生成二进制
.text指令段,.data段,函数体和函数名都在虚拟地址空间
函数名会生成符号,函数名里面有函数体
int fun()
{
static int ssss = 0;//数据段
ssss++;
printf("%d ", ssss);//函数体生成指令
int a = 10;
int b = 20;
return 0;
}
**2.**C++就是将C语言代码封装起来
C语言单链表增删改查
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int val;
struct Node*next;
};
void init(struct Node** list_head)//初始化函数
{
//申请头节点
*list_head = (struct Node*)malloc(sizeof(struct Node));
if (NULL == *list_head)
{
printf("error\n");
return;
}
(*list_head)->next = NULL;
}
void destroy(struct Node** list)
{
struct Node* head = *list;
struct Node* p;
while (head->next != NULL)
{
p = head->next;
head->next = p->next;
free(p);
}
free(head);
*list = NULL;
}
struct Node* get_node(int val)//增添元素
{
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
if (0)
{
}
p->val = val;
p->next = NULL;
return p;
}
void insert(struct Node* list, int pos_val, int val)//插入元素
{
struct Node* p = list->next;
//for (; p != NULL && p->val != pos_val; p = p->next);
for (; p != NULL; p = p->next)
{
if (p->val = pos_val)
{
struct Node* tmp = get_node(val);
tmp->next = p->next;
p->next = tmp;
}
}
}
void delet(struct Node* list, int pos_val)//删除元素
{
struct Node* p = list;
while (p->next != NULL)
{
if (p->next->val == pos_val)
{
struct Node* tmp = p->next;
p->next = tmp->next;
free(tmp);
}
p = p->next;
}
}
struct Node* select(struct Node* list, int val)//查找
{
struct Node* p = list->next;
while (p != NULL)
{
if (p->val == val)
{
return p;
}
p = p->next;
}
//没有找到数值,返回空值
return NULL;
}
void modify(struct Node* list, int pos_val, int val)//修改元素
{
struct Node* p = list->next;
while (p != NULL)
{
if (p->val == pos_val)
{
p->val = val;
}
p = p->next;
}
}
void push_back(struct Node* list,int val)//尾插
{
struct Node* p = list;
while (p->next != NULL)
{
p = p ->next;
}
struct Node* tmp = get_node(val);
p->next = tmp;
}
void push_front(struct Node* list,int val)//头插
{
struct Node* tmp = get_node(val);
tmp->next = list->next;
list->next = tmp;
}
void pop_back(struct Node* list)//尾删
{
struct Node* p = list;
if (p->next == NULL)
{
return;
}
while (p->next != NULL && p->next->next != NULL)//走到最后一个节点的前一个节点
{
p = p->next;
}
free(p->next);
p->next = NULL;
}
void pop_front(struct Node* list)//头删
{
if (list->next == NULL)
{
return;
}
struct Node* p = list->next;
list->next = p->next;
free(p);
}
int back(struct Node* list)//返回最后一个元素
{
if (list->next == NULL)
{
return -1;
}
struct Node* p = list->next;
while (p->next != NULL)
{
p = p->next;
}
return p->val;
}
int front(struct Node* list)//返回第一个元素
{
if (list->next == NULL)
{
return -1;
}
return list->next->val;
}
int main()
{
struct Node* list_head;//头指针
init(&list_head);
return 0;
}
C++中的链表实现
#ifndef LIST_H
#define LIST_H
#include<iostream>
using namespace std;
template<typename T>//模板那可以存放任何类型
class List
{
public:
struct Node
{
T val;
struct Node* next;
};
typedef Node Type_Node;
List(const List& src)//拷贝构造
{
}
List& operator=(const List& src)//等号运算符重载
{
return *this;
}
int& operator[](int pos)
{
}
List()
{
//申请头节点
_head = (struct Node*)malloc(sizeof(struct Node));
if (NULL == _head)
{
printf("error\n");
return;
}
_head->next = NULL;
}
~List()
{
struct Node* p;
while (_head->next != NULL)
{
p = _head->next;
_head->next = p->next;
free(p);
}
free(_head);
_head = NULL;
}
struct Node* get_node(const T& val)//增添元素
{
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
if (0)
{
}
p->val = val;
p->next = NULL;
return p;
}
void insert(int pos_val, const T& val)//插入元素
{
struct Node* p = _head->next;
for (; p != NULL; p = p->next)
{
if (p->val = pos_val)
{
struct Node* tmp = get_node(val);
tmp->next = p->next;
p->next = tmp;
}
}
}
void delet(int pos_val)//删除元素
{
struct Node* p = _head;
while (p->next != NULL)
{
if (p->next->val == pos_val)
{
struct Node* tmp = p->next;
p->next = tmp->next;
free(tmp);
}
p = p->next;
}
}
struct Node* select(const T& val)//查找
{
struct Node* p = _head->next;
while (p != NULL)
{
if (p->val == val)
{
return p;
}
p = p->next;
}
//没有找到数值,返回空值
return NULL;
}
void modify(int pos_val, const T& val)//修改元素
{
struct Node* p = _head->next;
while (p != NULL)
{
if (p->val == pos_val)
{
p->val = val;
}
p = p->next;
}
}
void push_back(const T& val)//尾插
{
struct Node* p = _head;
while (p->next != NULL)
{
p = p->next;
}
struct Node* tmp = get_node(val);
p->next = tmp;
}
void push_front(const T& val)//头插
{
struct Node* tmp = get_node(val);
tmp->next = _head->next;
_head->next = tmp;
}
void pop_back()//尾删
{
struct Node* p = _head;
if (p->next == NULL)
{
return;
}
while (p->next != NULL && p->next->next != NULL)//走到最后一个节点的前一个节点
{
p = p->next;
}
free(p->next);
p->next = NULL;
}
void pop_front()//头删
{
if (_head->next == NULL)
{
return;
}
struct Node* p = _head->next;
_head->next = p->next;
free(p);
}
T back()//返回最后一个元素
{
if (_head->next == NULL)
{
return -1;
}
struct Node* p = _head->next;
while (p->next != NULL)
{
p = p->next;
}
return p->val;
}
T front()//返回第一个元素
{
if (_head->next == NULL)
{
return -1;
}
return _head->next->val;
}
private:
struct Node;
Node* _head;//头指针
};
int main()
{
List<int> list1;
list1.push_back(1);
List<int>::Node* p = NULL;
List<int> list2 = list1;
List<int> list3;
list3 = list1;//等号运算符重载
return 0;
}
#endif