C++ data structure linear table array implementation

  • 2020-05-24 05:53:12
  • OfStack

C++ data structure linear table - array implementation

Linear table array implementation, to achieve several core functions, language is C++, if you have better ideas and comments, welcome to leave a message ~~~


/* Author : Moyiii 
 *  Linear table array implementation, for learning purposes only, if of course  
 *  You can use it if you want.  
*/ 
 
#include<iostream> 
using namespace std; 
 
// The order sheet  
class SeqList 
{ 
public: 
  // Constructor, accept 1 Three default list sizes  
  SeqList(int size = MAX_LIST_SIZE); 
  // Destructor, release elems Occupied memory space  
  ~SeqList(); 
  // Clear the table  
  void clear(); 
  // Determine if the table is empty  
  bool isEmpty(); 
  // Gets the current number of elements in the table  
  int getLength(); 
  // In the first pos Insert before element position 1 A new element  
  bool insertElem(int pos, int elem); 
  // Delete the first pos An element  
  bool deleteElem(int pos); 
  // Print the elements in the table  
  void print(); 
  int *elems;// Table elements,  
private: 
  static const int MAX_LIST_SIZE; 
  int m_length;// The number of elements in the table  
  int m_size;// The current maximum length of the table  
}; 
 
SeqList :: SeqList(int size) 
{ 
  //size It cannot be less than zero, nor can it exceed the maximum length specified by the system  
  // Otherwise do truncation  
  if(size > MAX_LIST_SIZE) 
  { 
    m_size = MAX_LIST_SIZE; 
  } 
  else if(size < 0) 
  { 
    m_size = 0; 
  } 
  else 
  { 
    m_size = size; 
  } 
 
  elems = new int[m_size]; 
  m_length = 0; 
 
 
  if(!elems) 
  { 
    cout << "Space allocate failed!" << endl; 
  } 
} 
 
SeqList :: ~SeqList() 
{ 
  delete []elems; 
} 
 
void SeqList :: clear() 
{ 
  m_length = 0; 
} 
 
bool SeqList :: isEmpty() 
{ 
  if(m_length == 0) 
  { 
    return true; 
  } 
  else 
  { 
    return false; 
  } 
} 
 
int SeqList :: getLength() 
{ 
  return m_length; 
} 
 
bool SeqList :: insertElem(int pos, int elem) 
{ 
  if(m_length == m_size) 
  { 
    cout << "List is Full" << endl; 
    return false; 
  } 
 
  if(pos < 1 || pos > m_length + 1) 
  { 
    cout << "Over Bound!" << endl; 
    return false; 
  } 
 
  // The element moves back after insertion  
  for(int i = m_length; i >= pos - 1; --i) 
  { 
    elems[i+1] = elems[i]; 
  } 
 
  elems[pos-1] = elem; 
  m_length++; 
  return true; 
} 
 
bool SeqList :: deleteElem(int pos) 
{ 
  if(pos < 1 || pos > m_length) 
  { 
    return false; 
  } 
 
  for(int i = pos - 1; i <= m_length - 1; ++i) 
  { 
    elems[i] = elems[i+1]; 
  } 
 
  m_length--; 
  return false; 
} 
 
void SeqList :: print() 
{ 
  for(int i = 0; i < m_length; ++i) 
  { 
    cout << elems[i] << " "; 
  } 
  cout << endl; 
} 
 
// Initialize the  
const int SeqList :: MAX_LIST_SIZE = 100; 
 
int main() 
{ 
  SeqList myList; 
 
  for(int i = 1; i <= 10; ++i) 
  { 
    myList.insertElem(1,i); 
  } 
 
  myList.print(); 
 
  cout << "Length= " << myList.getLength() <<endl; 
 
  myList.deleteElem(5); 
 
  myList.print(); 
 
  cout << "Length= " << myList.getLength() <<endl; 
 
  myList.clear(); 
 
  cout << myList.isEmpty() << endl; 
 
  return 0; 
} 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: