C++实现静态顺序表类

       写了3个多小时,还是太慢了、太菜了!

C++实现静态顺序表类
图1 程序运行演示截图1

       StdAfx.h文件:

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_)
#define AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_

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

#include <stdc++.h> //万能头文件,非常好用!我用的是VC6.0,里面没有;
//因此手动将codeblocks的 stdc++.h 文件导入到 D:\software\Microsoft Visual Studio\VC98\Include 中

typedef int elementType;
const int maxn = 10000+13;
using namespace std;

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_)

       SeqList1.h文件:

// SeqList1.h: interface for the SeqList class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SEQLIST1_H__5F69CE41_7D8B_4396_BAAE_F849B1FD54D1__INCLUDED_)
#define AFX_SEQLIST1_H__5F69CE41_7D8B_4396_BAAE_F849B1FD54D1__INCLUDED_

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

class SeqList  
{
public:
	SeqList();
	virtual ~SeqList();
	void printList();
	int Length();
	int locate( elementType value );//返回第一个值对value的位置,没有则返回-1
	bool isEmpty();//判空
	bool isFull();//判满
	bool getElement( int pos, elementType& value );//获取pos位置的值
	bool insertList( int pos, elementType value );//在pos位置前插入value值
	bool insertList_1( elementType value );//在尾部插入value值
	bool deleteListNode( int pos, elementType& value );//按位置删除元素
	bool deleteListNode_1( int value );//按值删除元素
	bool deleteListNode_2( int value );//按值删除所有对应元素
private:
	elementType Arr[maxn];//存放表元素的数组
	size_t listSize;//记录当前顺序表的大小
};

#endif // !defined(AFX_SEQLIST1_H__5F69CE41_7D8B_4396_BAAE_F849B1FD54D1__INCLUDED_)

       SeqList1.cpp文件:

// SeqList1.cpp: implementation of the SeqList class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SeqList1.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SeqList::SeqList()
{
	listSize = 0;
}

SeqList::~SeqList()
{
	cout << this << " 顺序表已销毁!" << endl;
}

void SeqList::printList()
{
	int column = 0;
	for( int i = 0; i < listSize; i ++ )
	{
		cout<< setiosflags(ios::left) << setw(5) << Arr[i] << " ";
		if( ++ column % 10 == 0 )
			cout << endl;
	}
	cout << endl;
}

int SeqList::Length()
{
	return listSize;
}

int SeqList::locate( elementType value )
{
	for( int i = 0; i < listSize; i ++ )
		if( Arr[i] == value )
			return i + 1;
	return -1;
}

bool SeqList::isEmpty()
{
	return listSize == 0;
}

bool SeqList::isFull()
{
	return listSize == maxn;
}

bool SeqList::insertList( int pos, elementType value )
{
	if( isFull() )
	{
		cout << "顺序表已满!插入失败!" << endl;
		return false;
	}
	if( pos > listSize )
	{
		cout << "插入位置超过当前顺序表容量!插入失败!" << endl;
		return false;
	}
	if( pos <= 0 )
	{
		cout << "插入位置必须大于0!插入失败!" << endl;
		return false;
	}
	for( int i = listSize - 1; i >= pos - 1; i -- )
		Arr[ i + 1 ] = Arr[i];
	Arr[ pos - 1 ] = value;
	listSize ++;//一定不能少!
	return true;//一定不能少!
}

bool SeqList::insertList_1( elementType value )
{
	if( isFull() )
	{
		cout << "顺序表已满!插入失败!" << endl;
		return false;
	}
	Arr[ listSize ++ ] = value;
	return true;//一定不能少!
}

bool SeqList::deleteListNode( int pos, elementType& value )
{
	if( isEmpty() )
	{
		cout << "顺序表为空!删除失败!" << endl;
		return false;
	}
	if( pos > listSize )
	{
		cout << "删除位置大于表长!删除失败!" << endl;
		return false;
	}
	value = Arr[ pos - 1 ];
	for( int i = pos; i < listSize - 1; i ++ )
		Arr[ i - 1 ] = Arr[i];
	listSize --;//一定不能少!
	return true;//一定不能少!
}

bool SeqList::deleteListNode_1( int value )
{
	if( isEmpty() )
	{
		cout << "顺序表为空!删除失败!" << endl;
		return false;
	}
	if( locate(value) == -1 )
	{
		cout << "表中无此元素!删除失败!" << endl;
		return false;
	}
	int index = locate(value);
	for( int i = index - 1; i < listSize; i ++ )
		Arr[i] = Arr[ i + 1 ];
	listSize --;//一定不能少!否则会出现已失效的位置仍占有先前元素的错误!
	return true;//一定不能少!
	/*精简版如下!
	void delete(int A[],int key,int& n) 
	{ 
		int i,j; 
		for(i=0;i<n&&A[i]-key;i++); //查找key值元素 
		if(i>=n) 
			cout<<"not found"<<endl; 
		else
		{ 
			for(j=i;j<n-1;A[j]=A[j+1],j++);//若找到,将该元素后边的值向前覆盖 
			--n;//数组长度减1 
		} 
	}
	--------------------- 
	作者:castle_kao 
	来源:**** 
	原文:https://blog.****.net/castle_kao/article/details/53487610?utm_source=copy 
	版权声明:本文为博主原创文章,转载请附上博文链接!
	*/
}

bool SeqList::deleteListNode_2( int value )
{
	if( isEmpty() )
	{
		cout << "顺序表为空!删除失败!" << endl;
		return false;
	}
	if( locate(value) == -1 )
	{
		cout << "表中无此元素!删除失败!" << endl;
		return false;
	}
	int cnt = 0;
	for( int i = 0; i < listSize; i ++ )
		if( Arr[i] == value )
			cnt ++;
	while( cnt -- )
	{
		int pos = locate(value), data;
		deleteListNode( pos, data );
	}
	return true;
}

       SeqList.cpp(测试函数)文件:

// SeqList.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "SeqList1.h"

int main(int argc, char* argv[])
{
	ios::sync_with_stdio(false);
	freopen( "x1.in", "r", stdin );
	//freopen( "x1.out", "w", stdout );
	//printf( "%d\n", (bool)-1 );
	SeqList L1;
	if( L1.isEmpty() )
	{
		cout << "空表!" << endl;
	}
	int n;
	cin >> n;
	for( int i = 0; i < n; i ++ )
	{
		int num;
		cin >> num;
		L1.insertList_1(num);
	}	
	cout << "当前表长为:" << L1.Length() << endl;
	L1.printList();
	L1.insertList( 5, -1 );
	cout << "当前表长为:" << L1.Length() << endl;
	L1.printList();
	int data;
	L1.deleteListNode( 4, data );
	cout << "值为 " << data << " 的元素已删除!" << endl;
	

	L1.deleteListNode_1(6);
	cout << L1.Length() << endl;
	L1.printList();

	L1.deleteListNode_1(7);
	cout << "当前表长为:" << L1.Length() << endl;
	L1.printList();

	int delKey = 2;
	L1.deleteListNode_2(delKey);
	cout << "所有值为" << delKey << "元素删除后," << "当前表长为:" << L1.Length() << endl;
	L1.printList();

	SeqList L2;
	if( L2.isEmpty() )
	{
		cout << "空表!" << endl;
	}
	for( int j = 0; j < maxn; j ++ )
	{
		L2.insertList_1( j + 1 );
	}
	if( L2.isFull() )
	{
		cout << "表满!" << endl;
	}
	cout << "当前表长为:" << L2.Length() << endl;
	L2.printList();//改为	L1.printList();  会有意想不到的效果!
	
	return 0;
}

 

C++实现静态顺序表类
图2 程序运行演示截图2