C++数据结构——顺序表
以下代码可以作为一个顺序表的模板,从顺序表的初始化创建到增删改查,都有详细的过程,供学习参考。
#include<iostream>
#include<stdio.h>
using namespace std;
#define elemType int
struct SequentialList
{
elemType *elements;
int size;
int capacity;
};
//初始化
void initializeList(SequentialList *list,int capacity)
{
list->elements = new elemType[capacity];
list->size = 0;
list->capacity = capacity;
}
//销毁
void destroyList(SequentialList *list)
{
delete[] list->elements;
}
//当前大小
int size(SequentialList *list)
{
return list->size;
}
//判断是否为空
bool isEmpty(SequentialList *list)
{
return list->size == 0;
}
//顺序表插入
void insert(SequentialList *list,int index,elemType elements)
{
if(index < 0 || index > list->size){
throw std::invalid_argument("Invalid index");
}
//扩容
if(list->size == list->capacity){
int newCapacity = list->capacity * 2;
elemType *newElements = new elemType[newCapacity];
for(int i = 0; i < list->size; ++i){
newElements[i] = list->elements[i];
}
delete[] list->elements;
list->elements = newElements; //指向新的内存空间
list->capacity = newCapacity;
}
//从后往前遍历
for(int i = list->size; i > index; --i){
list->elements[i] = list->elements[i-1];
}
list->elements[index] = elements;
list->size ++;
}
//删除元素
void deleteElement(SequentialList *list,int index)
{
if(index < 0 || index >= list->size){
throw std::invalid_argument("Invalid index");
}
//从前往后遍历
for(int i = index; i < list->size - 1; ++i){
list->elements[i] = list->elements[i+1];
}
list->size --;
}
//找到指定元素
int findElement(SequentialList *list,elemType element)
{
for(int i = 0; i < list->size; ++i){
if(list->elements[i] == element){
return i;
}
}
return -1;
}
//获取元素
elemType getElement(SequentialList *list,int index)
{
if(index < 0 || index >= list->size){
throw std::invalid_argument("Invalid index");
}
return list->elements[index];
}
//修改元素
void upDateElement(SequentialList *list,int index,elemType value)
{
if(index < 0 || index >= list->size){
throw std::invalid_argument("Invalid index");
}
list->elements[index] = value;
}
int main()
{
SequentialList myList;
initializeList(&myList,10);
for(int i = 0; i < 10; ++i){
insert(&myList, i, i*10);
}
std::cout << "Size:" << size(&myList) << std::endl;
std::cout << "is empty:" << isEmpty(&myList) << std::endl;
std::cout << "顺序表元素:" ;
for(int i = 0;i < size(&myList); ++i){
std::cout << getElement(&myList,i) << " ";
}
cout << endl;
std::cout << "删除第五个后:" ;
deleteElement(&myList,5);
for(int i = 0;i < size(&myList); ++i){
std::cout << getElement(&myList,i) << " ";
}
cout << endl;
std::cout << "修改第一个后:" ;
upDateElement(&myList,1,1122);
for(int i = 0;i < size(&myList); ++i){
std::cout << getElement(&myList,i) << " ";
}
cout << endl;
std::cout << "找到元素并修改:" ;
int idx = findElement(&myList,20);
upDateElement(&myList,idx,520);
for(int i = 0;i < size(&myList); ++i){
std::cout << getElement(&myList,i) << " ";
}
cout << endl;
std::cout <<"从最后插入元素: ";
insert(&myList,9,100);
for(int i = 0;i < size(&myList); ++i){
std::cout << getElement(&myList,i) << " ";
}
cout << endl;
std::cout <<"获取第五个元素: ";
std::cout << getElement(&myList,5) << std::endl;
destroyList(&myList);
return 0;
}
运行结果: