顺序表的实现
一:c语言实现
test.h
#pragma once
typedef struct SeqList
{
int* array;
int capacity;
int size;
}SeqList;
//初始化
void SeqListInit(SeqList* seq);
//销毁
void SeqListDestroy(SeqList* seq);
//头插
void SeqListpushFront(SeqList* seq,int val);
//尾插
void SeqListpushBack(SeqList* seq,int val);
//头删
void SeqListPopFront(SeqList* seq);
//尾删
void SeqListPopBack(SeqList* seq);
//下标插
void SeqListInsert(SeqList* seq, int index, int val);
//下标删
void SeqListErase(SeqList* seq, int index);
//打印
void SeqListprint(SeqList* seq);
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include"test.h"
#include<assert.h>
#define INIT_CAPACITY 100
static void ensurecapacity(SeqList* seq) {
if (seq->size < seq->capacity) {
return;
}
else {
int newcapacity = 2 * seq->capacity;
int* newarray = (int*)malloc(newcapacity * sizeof(int));
for (int i = 0;i < seq->size;i++) {
newarray[i] =seq->array[i];
}
free(seq->array);
seq->array = newarray;
}
}
//初始化
void SeqListInit(SeqList* seq) {
seq->array = (int*)malloc(sizeof(int) * INIT_CAPACITY);
seq->capacity = INIT_CAPACITY;
seq->size = 0;
}
//销毁
void SeqListDestroy(SeqList* seq) {
free(seq->array);
seq->capacity = 0;
seq->size = 0;
}
//头插
void SeqListpushFront(SeqList* seq, int val) {
for (int i = seq->size - 1;i >= 0;i--) {
seq->array[i + 1] = seq->array[i];
}
seq->array[0]=val;
seq->size++;
}
//尾插
void SeqListpushBack(SeqList* seq, int val) {
seq->array[seq->size]=val;
seq->size++;
}
//头删
void SeqListPopFront(SeqList* seq) {
assert(seq->size > 0);
for (int i = 0;i < seq->size - 1;i++) {
seq->array[i] = seq->array[i + 1];
}
seq->size--;
}
//尾删
void SeqListPopBack(SeqList* seq) {
assert(seq->size > 0);
seq->size--;
}
//下标插
void SeqListInsert(SeqList* seq, int index, int val) {
if (index < 0 || index>seq->size) {
printf("下标不合法");
return;
}
ensurecapacity(seq);
for (int i = seq->size;i > index;i--) {
seq->array[i] = seq->array[i-1];
}
seq->array[index] = val;
seq->size++;
}
//下标删
void SeqListErase(SeqList* seq, int index) {
assert(seq->size > 0);
for (int i = index;i < seq->size - 1;i++) {
seq->array[i] = seq->array[i + 1];
}
seq->size--;
}
//打印
void SeqListprint(SeqList* seq) {
for (int i = 0;i < seq->size;i++) {
printf("%d ",seq->array[i]);
}
printf("\n");
}
main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"test.h"
int main() {
SeqList seqList;
SeqListInit(&seqList);
SeqListpushFront(&seqList, 1);
SeqListpushFront(&seqList, 2);
SeqListpushFront(&seqList, 3);
SeqListpushFront(&seqList, 4);
SeqListprint(&seqList);
SeqListpushBack(&seqList, 10);
SeqListpushBack(&seqList, 20);
SeqListpushBack(&seqList, 30);
SeqListpushBack(&seqList, 40);
SeqListprint(&seqList);
SeqListInsert(&seqList, 4,0);
SeqListprint(&seqList);
SeqListErase(&seqList, 5);
SeqListprint(&seqList);
SeqListPopFront(&seqList);
SeqListprint(&seqList);
SeqListPopBack(&seqList);
SeqListprint(&seqList);
system("pause");
return 0;
}
二:c++实现(模板)
test.h
#pragma once
#include<iostream>
using namespace std;
#define MAX_SIZE 10
template<class Type>
class SeqList {
private:
Type* array;
size_t capacity;
size_t size;
public:
~SeqList();
SeqList(size_t sz=MAX_SIZE);
bool isfull()const;
bool isempty()const;
void SeqListPushFront(const Type& val);//头插
void SeqListPushBack(const Type &val);//尾插
void SeqListPopFront();//头删
void SeqListPopBack();//尾删
void SeqListprint();//打印
void SeqListInsert(int index, const Type& val);//按位置插入
void SeqListerase(int index);//按位置删除
int Find(const Type& val);//按值查找
void delete_val(const Type& val);//按值删除
int length();//求长度
void reserve();//反转
void sort();//排序
void clear();
};
template<class Type>
SeqList<Type>::~SeqList() {
delete[]array;
capacity = 0;
size = 0;
}
template<class Type>
SeqList<Type>::SeqList(size_t sz) {//分配空间
capacity = sz > MAX_SIZE? sz : MAX_SIZE;
array = new int[capacity];
size = 0;
}
template<class Type>
bool SeqList<Type>::isfull()const {
return size >= capacity;
}
template<class Type>
bool SeqList<Type>::isempty()const {
return size==0;
}
template<class Type>
void SeqList<Type>::SeqListPushFront(const Type& val) {
if (isfull()) {
cout << "顺序表已满,不可插入" << endl;
}
else {
for (int i = size;i >0;i--) {
array[i] = array[i-1];
}
array[0] = val;
size++;
}
}
template<class Type>
void SeqList<Type>::SeqListPushBack(const Type& val) {
if (isfull()) {
cout << "顺序表已满,不可插入" << endl;
}
array[size++] = val;
}
template<class Type>
void SeqList<Type>::SeqListPopFront() {
if (isempty()) {
cout << "顺序表为空,不可删除" << endl;
}
for (int i = 0;i < size ;i++) {
array[i] = array[i + 1];
}
size--;
}
template<class Type>
void SeqList<Type>::SeqListPopBack() {
if (isempty()) {
cout << "顺序表为空,不可删除" << endl;
}
size--;
}
template<class Type>
void SeqList<Type>::SeqListprint() {
for (int i = 0;i < size;i++) {
cout << array[i] << " ";
}
cout << endl;
}
template<class Type>
void SeqList<Type>::SeqListInsert(int index, const Type& val) {
if (index<0 || index>size) {
cout << " 插入位置非法" << endl;
}
if (isfull()) {
cout << "顺序表已满,不可插入" << endl;
}
for (int i = size;i > index;i--) {
array[i] = array[i - 1];
}
array[index] = val;
size++;
}
template<class Type>
void SeqList<Type>::SeqListerase(int index) {
if (isempty()) {
cout << " 顺序表为空,不可删除" << endl;
}
for (int i = index;i < size;i++) {
array[i] = array[i + 1];
}
size--;
}
template<class Type>
int SeqList<Type>::Find(const Type& val) {
for (int i = 0;i < size;i++) {
if (array[i] == val)
{
cout << i << endl;
return i;
}
}
cout << "顺序表中没有该数" << endl;
return -1;
}
template<class Type>
void SeqList<Type>::delete_val(const Type& val) {
for (int i = 0;i < size;i++) {
if (array[i] == val) {
return SeqListerase(i);
}
}
return;
}
template<class Type>
int SeqList<Type>::length() {
cout << size << endl;
return size;
}
template<class Type>
void SeqList<Type>::reserve() {
int m = size - 1;
for (int i = 0;i <=((size-1)/ 2);i++) {
int temp = array[i];
array[i] = array[m];
array[m] = temp;
m--;
}
}
template<class Type>
void SeqList<Type>::sort() {
for (int i = 0;i < size;i++) {
for (int j = 0;j < size - i - 1;j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
template<class Type>
void SeqList<Type>::clear() {
while (size) {
array[size--] =NULL;
}
}
main.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>
#include"test.h"
int main() {
SeqList<int> seqList;
seqList.SeqListPushFront(1);
seqList.SeqListPushFront(2);
seqList.SeqListPushFront(3);
seqList.SeqListPushFront(4);
seqList.SeqListprint();
seqList.SeqListPushBack(10);
seqList.SeqListPushBack(20);
seqList.SeqListPushBack(30);
seqList.SeqListPushBack(40);
seqList.SeqListprint();
seqList.SeqListPopFront();
seqList.SeqListPopFront();
seqList.SeqListprint();
seqList.SeqListPopBack();
seqList.SeqListprint();
seqList.SeqListInsert(0, 0);
seqList.SeqListprint();
seqList.SeqListerase(5);
seqList.SeqListprint();
seqList.Find(1);
seqList.Find(100);
seqList.delete_val(1);
seqList.SeqListprint();
seqList.length();
seqList.reserve();
seqList.SeqListprint();
seqList.reserve();
seqList.SeqListprint();
seqList.clear();
seqList.SeqListprint();
seqList.length();
return 0;
}
三:c++实现
test.h
#pragma once
class SeqList {
private:
int* array;
int capacity;
int size;
public:
SeqList();
~SeqList();
void ensurecapacity();
void SeqListPushFront(int val);
void SeqListPushBack(int val);
void SeqListPopFront();
void SeqListPopBack();
void SeqlistInsert(int index, int val);
void SeqListErase(int index);
void SeqListprint();
};
test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#define INIT_CAPAICTY 100
#include"test.h"
SeqList::SeqList() {
array = new int[INIT_CAPAICTY];
capacity = INIT_CAPAICTY;
size = 0;
}
SeqList::~SeqList() {
delete[]array;
capacity = 0;
size = 0;
}
void SeqList::ensurecapacity() {
if (size < capacity) {
return;
}
else {
int newcapacity = 2 * capacity;
int* newarray = new int[newcapacity];
for (int i = 0;i < size;i++) {
newarray[i] = array[i];
}
delete[]array;
array = newarray;
}
}
void SeqList::SeqListPushFront(int val) {
for (int i = size-1;i >=0;i--) {
array[i+1] = array[i];
}
array[0] = val;
size++;
}
void SeqList::SeqListPushBack(int val) {
array[size] = val;
size++;
}
void SeqList::SeqListPopFront() {
for (int i = 0;i<size;i++) {
array[i] = array[i+1];
}
size--;
}
void SeqList::SeqListPopBack() {
size--;
}
void SeqList::SeqlistInsert(int index, int val) {
if (index<0 || index>size) {
cout << "下标不合法" << endl;
}
ensurecapacity();
for (int i = size;i > index;i--) {
array[i] = array[i-1];
}
array[index] = val;
size++;
}
void SeqList::SeqListErase(int index) {
for (int i = index;i < size;i++) {
array[i] = array[i + 1];
}
size--;
}
void SeqList::SeqListprint() {
for (int i = 0;i < size;i++) {
cout << array[i]<<" ";
}
cout << endl;
}
main.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"test.h"
#include<stdlib.h>
int main() {
SeqList seqList;
seqList.SeqListPushFront(1);
seqList.SeqListPushFront(2);
seqList.SeqListPushFront(3);
seqList.SeqListPushFront(4);
seqList.SeqListprint();
seqList.SeqListPushBack(10);
seqList.SeqListPushBack(20);
seqList.SeqListPushBack(30);
seqList.SeqListPushBack(40);
seqList.SeqListprint();
seqList.SeqListPopFront();
seqList.SeqListprint();
seqList.SeqListPopBack();
seqList.SeqListprint();
seqList.SeqlistInsert(0, 0);
seqList.SeqListprint();
seqList.SeqListErase(8);
seqList.SeqListprint();
system("pause");
return 0;
}