順序表相關操作

// SequenceList.h: interface for the CSequenceList class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SEQUENCELIST_H__ECAED4FD_189E_4994_9843_BC7E9134CAF8__INCLUDED_)
#define AFX_SEQUENCELIST_H__ECAED4FD_189E_4994_9843_BC7E9134CAF8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define MAXSIZE 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_H__ECAED4FD_189E_4994_9843_BC7E9134CAF8__INCLUDED_)



// 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()
{

}

/************************************************************************
函數名:  InitList
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  初始化順序表,爲爲順序表動態分配空間
形參數:  
返回值:  成功:true  失敗: 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;
}

/************************************************************************
函數名:  Insert
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  往順序表中index之前插入插入元素
形參數:  index 從0開始的索引
         elem 要插入的元素
返回值:  成功:true  失敗: 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;
}

/************************************************************************
函數名:  Delete
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  刪除順序表中指定位置的元素
形參數:  index 從0開始的索引
返回值:  成功:true  失敗: 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;
}

/************************************************************************
函數名:  GetAt
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  返回順序表中指定位置的元素
形參數:  index 從0開始的索引
返回值:                                             
************************************************************************/
DataType CSequenceList::GetAt(int index)
{
	if (index < 0 || index > sqList.iLength - 1)
	{
		return false;
	}

	return sqList.data[index];
}

/************************************************************************
函數名:  DestroyList
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  摧毀順序表
形參數:  
返回值:                                             
************************************************************************/
BOOL CSequenceList::DestroyList()
{
	if (sqList.data)
	{
		free(sqList.data);
	}

	sqList.iLength = 0;
	sqList.iAllocatedSpace = 0;

	return true;
}

/************************************************************************
函數名:  IsEmpty
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  判斷順序表是否爲空
形參數:  
返回值:                                             
************************************************************************/
BOOL CSequenceList::IsEmpty()
{
	if (sqList.iLength == 0)
	{
		return false;
	}

	return true;
}

/************************************************************************
函數名:  GetLength
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  返回順序表的實際長度
形參數:  
返回值:                                             
************************************************************************/
int CSequenceList::GetLength()
{
	return sqList.iLength;
}


/************************************************************************
函數名:  Find
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  在順序表中從from開始查找元素elem,找到則返回從0開始的元素索引
形參數:  from 開始查找位置
         elem 要查找的元素
返回值:  沒有找到則返回-1,找到則返回從0開始的元素索引                                            
************************************************************************/
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;
}

/************************************************************************
函數名:  Unique
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  將順序表中的重複元素刪除
形參數:  
返回值:                                              
************************************************************************/
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);
		}
	}
}

/************************************************************************
函數名:  MergeList
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  將兩個有序表合併
形參數:  
返回值:                                              
************************************************************************/
CSequenceList 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;
}

/************************************************************************
函數名:  Reverse
作  者:	 譚友亮(Charles Tan)
日  期:	 2013-4-12
作  用:  逆轉順序表
形參數:  
返回值:                                              
************************************************************************/
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;
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章