Sequence Table operations

Source: Internet
Author: User

[Cpp]
// SequenceList. h: interface for the CSequenceList class.
//
//////////////////////////////////////// //////////////////////////////
 
# If! Defined (afx_sequencelist_hsf-ecaed4fd_189e_4994_9843_bc7e9134caf81_encoded _)
# Define afx_sequencelist_hsf-ecaed4fd_189e_4994_9843_bc7e9134caf81_encoded _
 
# If _ MSC_VER> 1000
# Pragma once
# Endif // _ MSC_VER> 1000
 
# Define Max size 100
# Define LIST_INIT_SIZE 100
# Define LIST_INCREMENT 100
Typedef int DataType;
 
Typedef struct
{
DataType * data;
Int iLength;
Int iAllocatedSpace;
} SqList;
 
Class CSequenceList
{
Public:
SqList sqList;
CSequenceList ();
Virtual ~ CSequenceList ();
Void operator = (const CSequenceList listSeq)
{
SqList. data = listSeq. sqList. data;
SqList. iLength = listSeq. sqList. iLength;
SqList. iAllocatedSpace = listSeq. sqList. iAllocatedSpace;
}
BOOL InitList ();
BOOL Insert (int index, DataType elem );
BOOL Delete (int index );
DataType GetAt (int index );
BOOL DestroyList ();
BOOL IsEmpty ();
Int GetLength ();
Int Find (int from, DataType & elem );
Void Unique ();
CSequenceList MergeList (CSequenceList & listA );
Void Reverse ();
};
 
# Endif //! Defined (afx_sequencelist_hsf-ecaed4fd_189e_4994_9843_bc7e9134caf81_encoded _)
 
 
 
// SequenceList. cpp: implementation of the CSequenceList class.
//
//////////////////////////////////////// //////////////////////////////
 
# Include "stdafx. h"
# Include "DataStruct. h"
# Include "SequenceList. h"
 
# Ifdef _ DEBUG
# Undef THIS_FILE
Static char THIS_FILE [] =__ FILE __;
# Define new DEBUG_NEW
# Endif
 
//////////////////////////////////////// //////////////////////////////
// Construction/Destruction
//////////////////////////////////////// //////////////////////////////
 
CSequenceList: CSequenceList ()
{
 
}
 
CSequenceList ::~ CSequenceList ()
{
 
}
 
/*************************************** *********************************
Function Name: InitList
Author: Charles Tan)
Date:
For use: Initialize the sequence table and dynamically allocate space for the sequence table.
Parameters:
Returned value: Successful: true failed: false
**************************************** ********************************/
BOOL CSequenceList: InitList ()
{
SqList. data = (DataType *) malloc (LIST_INIT_SIZE * sizeof (DataType ));
// SqList. data = new DataType [LIST_INIT_SIZE];
If (sqList. data = NULL)
{
Return false;
}
SqList. iLength = 0;
SqList. iAllocatedSpace = LIST_INIT_SIZE;
 
Return true;
}
 
/*************************************** *********************************
Function Name: Insert
Author: Charles Tan)
Date:
For use: insert an element before the index in the sequence table
Shape Parameter: index starting from 0
Elements to be inserted by elem
Returned value: Successful: true failed: false
**************************************** ********************************/
BOOL CSequenceList: Insert (int index, DataType elem)
{
Int I;
DataType * newBase;
 
If (index <0 | index> sqList. iLength)
{
Return false;
}
 
If (sqList. iLength> = sqList. iAllocatedSpace)
{
NewBase = (DataType *) realloc (sqList. data, (sqList. iAllocatedSpace + LIST_INCREMENT) * sizeof (DataType ));
If (! NewBase)
{
Return false;
}
SqList. data = newBase;
SqList. iAllocatedSpace + = LIST_INCREMENT;
}
 
For (I = sqList. iLength; I> index; I --)
{
SqList. data [I] = sqList. data [I-1];
}
SqList. data [index] = elem;
SqList. iLength ++;
 
Return true;
}
 
/*************************************** *********************************
Function Name: Delete
Author: Charles Tan)
Date:
Usage: deletes the elements at the specified position in the sequence table.
Shape Parameter: index starting from 0
Returned value: Successful: true failed: false
**************************************** ********************************/
BOOL CSequenceList: Delete (int index)
{
Int I;
 
If (index <0 | index> sqList. iLength-1 | sqList. iLength = 0)
{
Return false;
}
 
For (I = index; I <sqList. iLength-1; I ++)
{
SqList. data [I] = sqList. data [I + 1];
}
SqList. data [sqList. iLength-1] = '\ 0 ';
SqList. iLength --;
 
Return true;
}
 
/*************************************** *********************************
Function Name: GetAt
Author: Charles Tan)
Date:
For usage: return the elements at the specified position in the sequence table.
Shape Parameter: index starting from 0
Return Value:
**************************************** ********************************/
DataType CSequenceList: GetAt (int index)
{
If (index <0 | index> sqList. iLength-1)
{
Return false;
}
 
Return sqList. data [index];
}
 
/*************************************** *********************************
Function Name: DestroyList
Author: Charles Tan)
Date:
For use: destroys a sequence table.
Parameters:
Return Value:
**************************************** ********************************/
BOOL CSequenceList: DestroyList ()
{
If (sqList. data)
{
Free (sqList. data );
}
 
SqList. iLength = 0;
SqList. iAllocatedSpace = 0;
 
Return true;
}
 
/*************************************** *********************************
Function Name: IsEmpty
Author: Charles Tan)
Date:
Usage: determines whether the sequence table is empty.
Parameters:
Return Value:
**************************************** ********************************/
BOOL CSequenceList: IsEmpty ()
{
If (sqList. iLength = 0)
{
Return false;
}
 
Return true;
}
 
/*************************************** *********************************
Function Name: GetLength
Author: Charles Tan)
Date:
For use: returns the actual length of the sequence table.
Parameters:
Return Value:
**************************************** ********************************/
Int CSequenceList: GetLength ()
{
Return sqList. iLength;
}
 
 
/*************************************** *********************************
Function Name: Find
Author: Charles Tan)
Date:
For usage: searches for the elem element from the from in the sequence table, and returns the index of the element starting from 0.
Form parameter: from
Elements to be searched by elem
Returned value:-1 is returned if no value is found. If yes, the element index starting from 0 is returned.
**************************************** ********************************/
Int CSequenceList: Find (int from, DataType & elem)
{
Int I;
 
If (from <0 | from> sqList. iLength-1)
{
Return-1;
}
 
For (I = from; I <sqList. iLength; I ++)
{
If (sqList. data [I] = elem)
{
Return I;
}
}
 
Return-1;
}
 
/*************************************** *********************************
Function Name: Unique
Author: Charles Tan)
Date:
For use: delete duplicate elements in the sequence table
Parameters:
Return Value:
**************************************** ********************************/
Void CSequenceList: Unique ()
{
Int I, index;
 
For (I = 0; I <sqList. iLength-1; I ++)
{
Index = I + 1;
While (index = Find (index, sqList. data [I])> = 0)
{
Delete (index );
}
}
}
 
/*************************************** *********************************
Function Name: MergeList
Author: Charles Tan)
Date:
For use: merge two ordered tables
Parameters:
Return Value:
**************************************** ********************************/
CSequenceList: MergeList (CSequenceList & listA)
{
Int I, j, k;
CSequenceList listSeq;
ListSeq. InitList ();
 
I = j = k = 0;
While (I <sqList. iLength & j <listA. sqList. iLength)
{
If (sqList. data [I] <listA. sqList. data [j])
{
ListSeq. Insert (listSeq. sqList. iLength, sqList. data [I ++]);
}
Else
{
ListSeq. Insert (listSeq. sqList. iLength, listA. sqList. data [j ++]);
}
}
 
While (I <sqList. iLength)
{
ListSeq. Insert (listSeq. sqList. iLength, sqList. data [I ++]);
}
 
While (j <listA. sqList. iLength)
{
ListSeq. Insert (listSeq. sqList. iLength, listA. sqList. data [j ++]);
}
 
Return listSeq;
}
 
/*************************************** *********************************
Function Name: Reverse
Author: Charles Tan)
Date:
For use: reverse sequence table
Parameters:
Return Value:
**************************************** ********************************/
Void CSequenceList: Reverse ()
{
Int I;
DataType temp;
 
For (I = 0; I <sqList. iLength/2 + sqList. iLength % 2; I ++)
{
Temp = sqList. data [I];
SqList. data [I] = sqList. data [sqList. iLength-I-1];
SqList. data [sqList. iLength-I-1] = temp;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.