C + + implements linear table by class

Time:2020-10-18

This example for you to share the C + + class to achieve the specific code of linear table, for your reference, the specific content is as follows

The following figure shows the function definition implemented in standard C language

The following can be implemented in C + +. The first parameter is the pointer of this

List. H function

#pragma once
typedef int Elem;
class List
{
public:
 List(int size);
 ~List();
 Void clearlist(); // set the array length to 0
 Bool listempty(); // judge whether the array is empty
 Int listlength(); // get array length
 Bool GetElem (int i, elem * e); // finds the specified subscript element
 Int locateelem (elem * e); // finds the specified element
 Bool priorelem (elem * currentelem, elem * preelem); // find the precursor element of an element
 Bool nextelem (elem * currentelem, elem * nexterem); // find the successor elements of an element
 Void listtraverse(); // traverse the linear table and output elements
 Bool listinsert (int i, elem * e); // inserts an element at the specified location
 Bool ListDelete (int i, elem * e); // delete the element at the specified location
private:
 int *m_ Plist; // points to a block of memory
 int m_ Isize; // memory size
 int m_ Ilength; // the length of the array
};

Class implementation, list.cpp

#include<iostream>
#include "List.h"
using namespace std;
 
List::List(int size)
{
 m_iSize = size;
 m_pList = new Elem[m_iSize];
 m_iLength = 0;
}
 
 
List::~List()
{
 delete[] m_ Plist; // free array memory
 m_pList = NULL;
}
 
void List::ClearList()
{
 m_iLength = 0;
}
 
bool List::ListEmpty()
{
 return m_iLength == 0 ? true : false;
}
 
int List::ListLength()
{
 return m_iLength;
}
 
bool List::GetElem(int i, Elem *e)
{
 if (i < 0 || i >= m_iSize)
 {
 return false;
 }
 *e = m_pList[i];
 return true;
}
 
int List::LocateElem(Elem *e)
{
 for (int i = 0; i < m_iLength; i++)
 {
 if (m_pList[i] == *e)
 {
  return i;
 }
 }
 return -1;
}
 
bool List::PriorElem(Elem *currentElem, Elem *preElem)
{
 Int temp = locateelem (currentelem); // find the ordinal number of the element
 if (temp == -1)
 return false;
 else if (temp == 0)
 return false;
 else
 {
 *preElem = m_pList[temp - 1];
 return true;
 }
 
}
 
bool List::NextElem(Elem *currentElem, Elem *nextElem)
{
 Int temp = locateelem (currentelem); // find the ordinal number of the element
 if (temp == -1)
 return false;
 else if (temp == m_iLength - 1)
 return false;
 else
 {
 *nextElem = m_pList[temp + 1];
 return true;
 }
}
 
void List::ListTraverse()
{
 for (int i = 0; i < m_iLength; i++)
 {
 cout << m_pList[i] << endl;
 }
}
 
bool List::ListInsert(int i, Elem *e)
{
 if (i<0 || i>m_iLength)
 return false;
 for (int k=m_iLength-1;k>=i;k--)
 {
 m_pList[k + 1] = m_pList[k];
 }
 m_pList[i] = *e;
 m_iLength++;
 return true;
}
 
bool List::ListDelete(int i, Elem *e)
{
 if (i<0 || i>m_iLength)
 return false;
 *e = m_pList[i];
 for (int k = i + 1; k < m_iLength; k++)
 {
 m_pList[k - 1] = m_pList[k];
 } 
 m_iLength--;
 return true;
}

Test main program

#include<iostream>
#include "List.h"
using namespace std;
 
int main()
{
 Elem temp;
 Elem arry[11] = { 3,5,7,2,9,1,8 };
 List *list1 = new List(10);
 cout << "length:" << list1->ListLength() << endl;
 for (int i = 0; i < 7; i++)
 {
 list1->ListInsert(i, &arry[i]);
 }
 cout << "length:" << list1->ListLength() << endl;
 //Delete first element
 list1->ListDelete(0, &temp);
 cout << temp << endl;
 //Search precursor elements
 list1->PriorElem(&arry[4], &temp);
 cout << temp << endl;
 list1->NextElem(&arry[4], &temp);
 cout << temp << endl;
 list1->ListTraverse();
 delete list1;
  return 0;
}

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.

Recommended Today

Singularity iPhone version officially launched

Recently, I haven’t updated my short book, technology blog, CocoaChina, etc. I’ve been busy developing my own product singularity app. I hope to solve our technical problems in this high-quality “app ape” product, so that more people know the singularity. We dig high-quality Internet technology articles every day for you to recommend (currently, it supports […]