1 头文件 SeqList.h
- //SeqList.h
- #pragma once
- #define _CRT_SECURE_NO_WARNINGS
- #include
- #include
- using namespace std;
- class SeqList
- {
- public:
- //初始化
- SeqList();
- //销毁
- ~SeqList();
- //头插
- void PushFront(int data);
- //头删
- void PopFront();
- //尾插
- void PushBack(int data);
- //尾删
- void PopBack();
- //打印
- void Print();
- //寻找
- int* Find(int data);
- //插入
- void Insert(int pos, int data);
- //清除
- void Erase(int data);
- //检查容量
- void Check();
- private:
- //动态数组
- int* ptr;
- //数组索引
- int index;
- //数组容量
- int capacity;
- };
2 源文件
2.1 SeqList.cpp(函数)
- //SeqList.cpp
- #include "SeqList.h"
- //练习顺序表
- //初始化
- SeqList::SeqList():ptr(nullptr) //创建变量时即给指针赋值为空指针
- {
- index = 0;
- capacity = 0;
- }
- //销毁
- SeqList::~SeqList()
- {
- if (ptr!=nullptr)
- {
- delete[]ptr;
- ptr = nullptr;
- capacity = 0;
- index = 0;
- cout << "~SeqList Destroyed" << endl;
- }
- }
- //头插
- void SeqList::PushFront(int data)
- {
- Check();
- for (int i = index - 1; i >= 0; i--)
- {
- ptr[i + 1] = ptr[i];
- }
- ptr[0] = data;
- index++;
- }
- //头删
- void SeqList::PopFront()
- {
- for (int i = 1; i < index; i++)
- {
- ptr[i - 1] = ptr[i];
- }
- index--;
- }
- //尾插
- void SeqList::PushBack(int data)
- {
- Check();
- assert(ptr);
- ptr[index++] = data;
- }
- //尾删
- void SeqList::PopBack()
- {
- assert(index >= 0);
- index--;
- }
- //打印
- void SeqList::Print()
- {
- for (int i = 0; i < index; i++)
- {
- cout << ptr[i] << " ";
- }
- cout << endl;
- }
- //寻找
- int* SeqList::Find(int data)
- {
- for (int i = 0; i < index; i++)
- {
- if (ptr[i] == data)
- {
- return ptr + i;
- }
- }
- return nullptr;
- }
- //插入
- void SeqList::Insert(int pos, int data)
- {
- assert(pos > 0);
- Check();
- for (int i = index - 1; i >= pos - 1; i--)
- {
- ptr[i + 1] = ptr[i];
- }
- ptr[pos-1] = data;
- index++;
- }
- //清除
- void SeqList::Erase(int data)
- {
- int flag = 1;
- for (int i = 0; i < index; i++)
- {
- if (ptr[i] == data)
- {
- for (int j = i; j
- {
- ptr[j] = ptr[j + 1];
- }
- index--;
- flag = 0;
- }
- }
- if (flag)
- {
- cout << "该元素不存在" << endl;
- }
- }
- //检查容量
- void SeqList::Check()
- {
- if (ptr == nullptr || index == capacity)
- {
- int new_capcaity = (ptr == nullptr ? 4 : capacity * 2);
- //转移数据
- if (capacity != new_capcaity && capacity != 0)
- {
- int* temp = new int[new_capcaity];
- for (int i = 0; i < index; i++)
- {
- temp[i] = ptr[i];
- }
- delete[] ptr;
- ptr = temp;
- assert(ptr);
- capacity = new_capcaity;
- return;
- }
- ptr = new int[new_capcaity];
- capacity = new_capcaity;
- assert(ptr);
- }
- }
2.2 test.cpp(测试,入口)
- //test.cpp
- #include"SeqList.h"
- int main()
- {
- SeqList a;
- a.PushBack(1);
- a.PushBack(2);
- a.PushBack(3);
- a.PushBack(4);
- a.PushBack(5);
- a.PushBack(6);
- a.PushBack(1);
- a.Print();
- a.PopBack();
- a.PopBack();
- a.PopBack();
- a.PopBack();
- a.Print();
- a.PushFront(1);
- a.PushFront(2);
- a.PushFront(9);
- a.Print();
- a.PopFront();
- a.Print();
- a.Insert(3, 7);
- cout<
Find(2)< - cout<
Find(8)< - a.Print();
- a.Insert(1, 8);
- a.Print();
- a.Erase(1);
- a.Print();
- a.Erase(1);
- return 0;
- }
-
相关阅读:
Proteus仿真--1602LCD显示仿手机键盘按键字符(仿真文件+程序)
【Go语言】(一)环境搭建与了解VScode工具
语音信号处理-基础(一):声学基础知识
五分钟掌握NineData:新手任务速成指南
ArcgisPro3.0.1中文安装包下载及安装教程
ApiFox衔接前后端开发人员,提升沟通效率实践
eclipse配置maven,安装lombok,导入和创建springboot项目
【Vue】了解这些类和样式绑定就够了!
数据指标显示,目前还没到牛市顶点 2021-04-24
【题解】洛谷 P9658 Laser Trap
-
原文地址:https://blog.csdn.net/Crazy_Rabbits/article/details/136607074