线性表--顺序实现

/*
线性表:顺序实现 
*/

#include 
#include 
using namespace std;

#define MaxSize 1000  // 宏定义 
int A[MaxSize];
int n;

// 在loc位置插入元素item 
void InsertList(int &n, int loc, int item)
{
	if (n == MaxSize)
	{
		cout << "线性表已满!" << endl;
		return; 
	}
	if (loc < 0 || loc > n)
	{
		cout << "插入位置不正确!" << endl;
		return; 
	}
	for (int i=n-1; i>=loc; i--)
	{
		A[i+1] = A[i]; 
	}
	A[loc] = item;
	n++;
}

// 删除loc处的元素 
void DeleteList(int &n, int loc) 
{
	if (loc < 0 || loc >= n)
	{
		cout << "删除位置不正确!" << endl ;
		return;
	}
	for (int i=loc; i<=n-2; i++)
	{
		A[i] = A[i+1];
	}
	n--;
}

// 线性表排序 
void SortList()
{
	sort(A,A+n);
}

// 输出提示 
void OutputHint()
{
	cout << "############  操作选项  ############" << endl;
	cout << "0:help" << endl; 
	cout << "1:遍历线性表" << endl; 
	cout << "2:插入元素" << endl; 
	cout << "3:删除元素" << endl;
	cout << "4:查找元素" << endl; 
	cout << "5:元素排序" << endl; 
	cout << "############  操作选项  ############" << endl;
	
}

// 遍历线性表 
void TraverseList()
{
	if (n == 0)
	{
		cout << "线性表为空!" << endl;
		return; 
	}
	for (int i=0; i> choice;
		switch (choice)
		{
			case 0:
				OutputHint();
				break;
			case 1:
				TraverseList();
				break;
			case 2:
				cout << "请输入要插入的位置(当前范围是[0-" << n << "]):";
				cin >> loc;
				cout << "请输入元素值:";
				cin >> item;
				InsertList(n,loc,item);
				break; 
			case 3:
				cout << "请输入要删除的位置(当前元素个数:" << n << "):";
				cin >> loc;
				DeleteList(n,loc);
				break;
			case 4:
				cout << "请输入要查找的元素:" ;
				cin >> item;
				Locate(item);
				break; 
			case 5:
				SortList();
				break;
			default :
				cout << "您输入的操作序号有误,请重新输入!" << endl; 
		}
		cout << "请输入操作序号:" ; 
	}
}

你可能感兴趣的:(数据结构)