龙空技术网

神级程序员倾囊相助教你C++编程模板库线性表线性存储实现实现

趣味C语言CPP 47

前言:

现时大家对“c语言线性表实现”大概比较关切,同学们都想要剖析一些“c语言线性表实现”的相关内容。那么小编在网络上收集了一些对于“c语言线性表实现””的相关知识,希望兄弟们能喜欢,兄弟们一起来了解一下吧!

<span style="font-size:18px;"><strong>SeqList.h</strong></span>

#pragma once

template<typename T>

class SeqList

{

public:

SeqList(int capacity);

~SeqList(void);

public:

int SeqList_Length();

int SeqList_Capacity();

int SeqList_Insert(T &t, int pos);

int SeqList_Get(T &t, int pos);

int SeqList_Delete(T &t, int pos);

private:

int length;

int capacity;

T *pArray; //数组

};

<span style="font-size:18px;"><strong>

</strong></span>

<span style="font-size:18px;"><strong>SeqList.cpp</strong></span>

#include<iostream>

#include"SeqList.h"

usingnamespace std;

template<typename T>

SeqList<T>::SeqList(int capacity)

{

//T *pArry;

//pArray = new char[10];

pArray = new T[capacity];

this->capacity = capacity;

this->length = 0;

}

template<typename T>

SeqList<T>::~SeqList(void)

{

delete[] pArray;

pArray = NULL;

length = 0;

capacity = 0;

}

template<typename T>

int SeqList<T>::SeqList_Length()

{

returnthis->length;

}

template<typename T>

int SeqList<T>::SeqList_Capacity()

{

returnthis->capacity;

}

template<typename T>

int SeqList<T>::SeqList_Insert(T &t, int pos)

{

int i;

if ( pos< 0)

{

return -1;

}

for (i = length; i>pos; i--)

{

pArray[i] = pArray[i - 1];

}

pArray[i] = t;

this->length++;

return 0;

}

template<typename T>

int SeqList<T>::SeqList_Get(T &t,int pos)

{

if (pos< 0)

{

return -1;

}

t = pArray[pos];

return 0;

}

template<typename T>

int SeqList<T>::SeqList_Delete(T &t,int pos)

{

if ( pos< 0)

{

return -1;

}

t = pArray[pos];

for (int i = pos + 1; i < len; i++)

{

pArray[i - 1] = pArray[i];

}

len--;

return 0;

}

。在分享的同时我自己也推荐一个学C/C++的学习群496926338无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!群内有很多干货和技术分享!

<span style="font-size:18px;"><strong>Test.cpp</strong></span>

#include<iostream>

#include"SeqList.cpp"

usingnamespace std;

struct Teacher

{

int age;

};

void main11()

{

SeqList<int> list(10);

int a = 1;

int b = 2;

int c = 3;

list.SeqList_Insert(a, 0);

list.SeqList_Insert(b, 0);

list.SeqList_Insert(c, 0);

for (int i = 0; i < list.SeqList_Length(); i++)

{

int t;

list.SeqList_Get(t, i);

cout << t << endl;

}

system("pause;");

}

void main()

{

Teacher t1, t2, t3;

t1.age = 1;

t2.age = 2;

t3.age = 3;

SeqList<Teacher> list(10);

list.SeqList_Insert(t1, 0);

list.SeqList_Insert(t2, 0);

list.SeqList_Insert(t3, 0);

for (int i = 0; i < list.SeqList_Length(); i++)

{

Teacher t3;

list.SeqList_Get(t3, i);

cout << t3.age << endl;

}

system("pause;");

}

喜欢的朋友请转发收藏评论关注一波~么么哒

随着软件开发经验的增长,我们将会学会更多。 尽管看起来是常识,我始终坚信了解这些原则可以帮助你识别这些模式并做出反应。

标签: #c语言线性表实现