Fully implement linear table in C++ (not objects)

// linear.cpp -- implementation of the linear table
// Linear table of type int
#include <iostream>
#include <cstdlib>
typedef int ElemType;
const int MAX_LIST_SIZE = 100;


struct Sqlist
{
	int *element; 		// position of base address
 	int listlen; 		// current linear table length
 	int listsize; 		// maximum size of linear table
};


void InitList(Sqlist &L) 			//Build an empty linear list L
{
	L.element = new ElemType[MAX_LIST_SIZE];
	if (!L.element)
		exit(EXIT_FAILURE);
	L.listlen = 0;
	L.listsize = MAX_LIST_SIZE;
}


void DestroyList(Sqlist &L) 				// Destroy the linear table L
{
	delete [] L.element;
	L.element = NULL;
}


void ClearList(Sqlist &L)				// 重置L
{
	for (int i =0; i <= L.listlen-1;i++)
		L.element[i] = 0;
	L.listlen = 0;
}


bool Listempty(Sqlist L) 						// Return 1 if L is an empty table
{
	return !L.listlen;
}


ElemType ListLength(Sqlist L) 						// Returns the length of L
{
	return L.listlen;
}


void GetElem(Sqlist L, int i ,ElemType &e) 				// Set e to the value of the i-th element in the table
{
	e = L.element[i - 1];
}


bool LocateElem(Sqlist L, ElemType e) 					// Determine if there is an element greater than e
{
	bool result = false;
	for (int i=0; i <= L.listlen - 1;i++)
		if (L.element[i] > e)
			result = true;
	return result;
}


void PriorElem(Sqlist L, ElemType cur_e, ElemType &pre_e) 	// Determine whether cur_e is an element of L, if it is and not the first, return its predecessor to e; if not, do not operate
{
	int i;
	bool p = false;
	for ( i = 0; i <= L.listlen - 1;i++)
		if (cur_e == L.element[i]){
			p = true;
			break;
			if (i && p)
		pre_e = L.element[i-1];
}


void NextElem(Sqlist L, ElemType cur_e, ElemType &next_e) // returns the next element of cur_e
{
	int i;
	bool p = false;
	for (i = 0; i<= L.listlen - 1;i++)
		if (cur_e == L.element[i]){
			p = true;
			break;
		}
	if (i != L.listlen - 1 && p)
		next_e = L.element[i + 1];
}


void ListInsert(Sqlist &L, int i, ElemType e) 			// add an element to position i
{
	if (i < 1 || i > L.listlen + 1){
		std::cout << "i is not in the range!";
		return;
	}
	for (int j = L.listlen; j >= i;j--)
		L.element[j] = L.element[j - 1];
	L.element[i - 1] = e;
	L.listlen++;
}


void ListDelete(Sqlist &L, int i, ElemType &e) 			// delete the i-th element in L and save it to e
{
	if (i < 1 || i > L.listlen + 1){
		std::cout << "i is not in the range!";
		return;
	}
	e = L.element[i - 1];
	for (int j = i - 1; j <= L.listlen - 2;j++)
		L.element[j] = L.element[j + 1];
	L.listlen--;
}


intmain()
{
	using namespace std;
	Sqlist L;
	InitList(L);
	bool p;
	for (int i = 1;i <= 50;i++)
		ListInsert(L, i, i*i);
	int len = ListLength(L);
	 ElemType e;
	ListDelete(L, 30, e);
	cout << "the original 30th number: " << e << endl;
	GetElem(L, 30, e);
	cout << "Later: " << e << endl;
	e = 999;
	p = LocateElem(L, e);
	cout << "is there a number bigger than 999?" << p << endl;
	PriorElem(L, 49, e);
	cout << "the number before 49 is: " << e << endl;
	NextElem(L, 49, e);
	cout << "the number after 49 is : " << e << endl;
	p = Listempty(L);
	cout << "Is list empty?" << p << endl;
	ClearList(L);
	p = Listempty(L);
	cout << "Now is list empty?" << p << endl;
	DestroyList(L);
	cout << "销毁后指针指向:" << L.element << endl;
	return 0;
}

The final program result is:

the original 30th number: 900

Later: 961

is there a number bigger than 999?1

the number before 49 is: 36

the number after 49 is : 64

Is list empty?0

Now is list empty?1

After destruction, the pointer points to: 00000000

Please press any key to continue . . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324600426&siteId=291194637