C/C++ 数据结构---线性顺序存储数据:查询快,增删慢

   顺序存储结构

      顺序存储结构其主要使用的是数组来存储数据的, 所以它的特点是:查询快,增删慢!

 为什么增删慢呢?由于增删需要把插入的位置元素后移或者前移 所以效率就要慢了!当然如果是插入在末尾的话 就另当别论了!

插入的核心代码:

for (i = arrayList->length; i > pos; i--) {
//元素后移
arrayList->node[i] = arrayList->node[i - 1];
};
//插入元素
arrayList->node[pos] = (unsigned int)node;
arrayList->length ++;

删除的核心代码:

for (i = pos + 1; i < arrayList->length; i++) {
arrayList->node[i - 1] = arrayList->node[i];
}
arrayList->length--;


下面来看看头文件:

seqList.h

#ifndef _SEQ_LIST_
#define _SEQ_LIST_
typedef void SeqList;
typedef void SeqListNode;

SeqList* seqList_create(int capacity);
void seqList_destory(SeqList* list);
void seqList_clear(SeqList* list);
int seqList_length(SeqList* list);
int seqList_capacity(SeqList* list);
int seqList_insert(SeqList* list,SeqListNode* node,int pos);
SeqListNode* seqList_get(SeqList* list,int pos);
SeqListNode* seqList_delete(SeqList* list,int pos);
#endif
再来看看实现  重点是插入和删除的方法

seqList.c

#include <string.h>
#include "seqList.h"
typedef struct _tag_list {
	int length;//数组长度
	int capacity;//开辟的空间大小
	unsigned int* node;//存储指针的数组
}ArrayList;


SeqList* seqList_create(int capacity) {
	int ret = 0;
	ArrayList *list = NULL;
	list = (ArrayList*)malloc(sizeof(ArrayList));//分配内存空间
	if (list == NULL)
	{
		return NULL;
	}
	memset(list, 0, sizeof(ArrayList));
	list->node = (unsigned int *)malloc(sizeof(unsigned int)*capacity);//分配存储数组下标的内存空间
	if (list->node == NULL)
	{
		return NULL;
	}
	list->capacity = capacity;
	list->length = 0;
	return list;
}
//释放内存
void seqList_destory(SeqList* list) {
	ArrayList *arrayList;
	if (list == NULL) { return; }
	arrayList = (ArrayList*)list;
	if (arrayList->node == NULL)
	{
		return;
	}
	free(arrayList->node);
	free(arrayList);

}
//清理  回到初始状态
void seqList_clear(SeqList* list) {
	ArrayList *arrayList;
	if (list == NULL) { return; }
	arrayList = (ArrayList*)list;
	arrayList->length = 0;
}
//获取长度
int seqList_length(SeqList* list) {
	ArrayList *arrayList;
	if (list == NULL) { return -1; }
	arrayList = (ArrayList*)list;
	return arrayList->length;
}
//获取空间大小
int seqList_capacity(SeqList* list) {
	ArrayList *arrayList;
	if (list == NULL) { return -1; }
	arrayList = (ArrayList*)list;
	return arrayList->capacity;
}
//插入一个数据
int seqList_insert(SeqList* list, SeqListNode* node, int pos) {

	if (list == NULL || node == NULL || pos<0) {
		return -1;
	}
	int i = 0;
	ArrayList* arrayList = NULL;
	arrayList = (ArrayList*)list;
	if (arrayList->capacity <= arrayList->length) {//判断是否已满
		return -1;
	}
	if (pos >= arrayList->length)
	{
		pos = arrayList->length;
	}

	for (i = arrayList->length; i > pos; i--) {
		//元素后移
		arrayList->node[i] = arrayList->node[i - 1];
	};
	//插入元素
	arrayList->node[pos] = (unsigned int)node;
	arrayList->length ++;
	return 0;
}
SeqListNode* seqList_get(SeqList* list, int pos) {
	ArrayList* arrayList = NULL;
	SeqListNode* ret = NULL;
	arrayList = (ArrayList*)list;
	if (list == NULL || pos<0||pos>= arrayList->length)
	{
		return NULL;
	}
	ret= (SeqListNode*)arrayList->node[pos];
	return ret;
}
SeqListNode* seqList_delete(SeqList* list, int pos) {
	if (list == NULL || pos<0)
	{
		return NULL;
	}
	int i = 0;
	ArrayList *arrayList = NULL;
	SeqListNode *node = 0;
	arrayList = (ArrayList*)list;
	node = (SeqListNode*)arrayList->node[pos];//缓存pos的位置
	for (i = pos + 1; i < arrayList->length; i++) {
		arrayList->node[i - 1] = arrayList->node[i];
	}
	arrayList->length--;
	return node;
}
再来看看测试代码:

#define  _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include<string.h>
#include "seqList.h"

typedef struct mac {
	int pir;
	char name[64];

}Mac;


void main() {
	int i = 0;
	Mac m1,m2,m3;
	m1.pir = 3200;
	strcpy(m1.name,"m1");
	m2.pir = 3456;
	strcpy(m2.name, "m2");
	m3.pir = 1900;
	strcpy(m3.name, "m3");
	SeqList* list=seqList_create(10);
	seqList_insert(list, &m1, 0);
	seqList_insert(list,&m2, 0);
	seqList_insert(list,&m3,0);
	for ( i = 0; i <seqList_length(list); i++)
	{
		Mac* node=(Mac*)seqList_get(list,i);
		printf("pir=%d\n", node->pir); 
		printf("name=%s\n", node->name);
	}
	//删掉一个元素
	seqList_delete(list,0);
	printf("---------------\n");
	for (i = 0; i <seqList_length(list); i++)
	{
		Mac* node = (Mac*)seqList_get(list, i);
		printf("pir=%d\n", node->pir);
		printf("name=%s\n", node->name);

	seqList_destory(list);
	printf("helloWord\n");
	system("pause");
}

再来看看C++的实现:


#pragma once
template<typename T>
class CircleList2
{
public:
	CircleList2(int cap);
	~CircleList2();
public:
	T get(int pos);
	int insert(T &t,int pos);
	int dele(int pos);
	int getLen();
private:
	T *ts;
	int len;
};



#include "CircleList2.h"


template<typename T>
CircleList2<T>::CircleList2(int cap)
{
	ts = new T[cap];
	this->len = 0;
}

template<typename T>
CircleList2<T>::~CircleList2()
{
	delete[]ts;
	ts = nullptr;
}



template<typename T>
T CircleList2<T>::get(int pos) {

	return ts[pos];
}


template<typename T>
int CircleList2<T>::insert(T & t, int pos)
{
	int i = 0;
	for ( i = len; i <pos ; i--)
	{
		ts[i] = ts[i - 1];
	}
	ts[i] = t;
	this->len++;
	return 0;
}

template<typename T>
int CircleList2<T>::dele(int pos)
{
	
	for (int i = len; i <pos ; i--)
	{
		ts[i] = ts[i + 1];
	}
len--;

	return 0;
}

template<typename T>
int CircleList2<T>::getLen()
{
	return len;
}


测试文件:


#include<iostream>
using namespace std;
#include"CircleList2.cpp"

struct Teacher
{
	int age;

};

void main() {
	Teacher t1,t2;
	t1.age = 12;
	t2.age = 14;
	CircleList2<Teacher> list(10);
	list.insert(t1,0);
	list.insert(t2, 0);
	for (int i = 0; i < list.getLen(); i++)
	{
		Teacher t=list.get(i);
		cout << "age==" << t .age<<endl;
	}
	cin.get();
}




你可能感兴趣的:(C/C++ 数据结构---线性顺序存储数据:查询快,增删慢)