首页 > 代码库 > 【C++/数据结构】顺序表的基本操作

【C++/数据结构】顺序表的基本操作

<span style="font-size:18px;"><strong>#pragma once
#include <iostream>
using namespace std;

typedef enum { FALSE, TRUE }Status;

template<class Type>
class SeqList
{
public:
	SeqList(int sz = DefaultSize)
	{
		capacity = sz > DefaultSize ?

sz : DefaultSize; base = new Type[capacity]; size = 0; } ~SeqList() { destroy(); /* delete []base; base = NULL; capacity = size = 0;*/ } public: Status Inc() { Type *newbase = new Type[capacity + INC_SIZE]; if (newbase == NULL) { return FALSE; } memcpy(newbase, base, sizeof(Type)*size); delete[]base; base = newbase; capacity += INC_SIZE; return TRUE; } Status merge(SeqList<Type> <1, SeqList<Type> <2) { int i = 0; int j = 0; int k = 0; while (i < lt1.size && j < lt2.size) { if (lt1.base[i] > lt2.base[j]) { base[k++] = lt2.base[j++]; } else { base[k++] = lt1.base[i++]; } } while(i < lt1.size) { base[k++] = lt1.base[i++]; } while (j < lt2.size) { base[k++] = lt2.base[j++]; } size = lt1.size + lt2.size; return TRUE; } Status IsFull() const { if (size >= capacity) { return TRUE; } else { return FALSE; } } Status Empty() const { if (size == 0) { return TRUE; } else { return FALSE; } } Status push_back(const Type& x) { if (IsFull() && !Inc()) { cout << "IsFull!不能尾插" << x << endl; return FALSE; } base[size++] = x; return TRUE; } Status push_front(const Type& x) { if (IsFull() && !Inc()) { cout << "IsFull!不能头插" << x << endl; return FALSE; } for (int i = size; i > 0; --i) { base[i] = base[i - 1]; } base[0] = x; size++; return TRUE; } Status pop_back() { if (Empty()) { cout << "Empty!不能尾删" << endl; return FALSE; } size--; return TRUE; } Status pop_front() { if (Empty()) { cout << "Empty!不能头删" << endl; return FALSE; } for (int i = 0; i < size; ++i) { base[i] = base[i + 1]; } size--; return TRUE; } Status insert_val(const Type& x) { if (IsFull() && !Inc()) { cout << "IsFull!不能按值插入" << x << endl; return FALSE; } int i = 0; while (base[i] < x && i < size) { i++; } insert_pos(i, x); return TRUE; } Status insert_pos(int pos, const Type& x) { if (IsFull() && !Inc()) { cout << "IsFull! 不能按位置插入" << endl; return FALSE; } if (pos < 0 || pos > size) { return FALSE; } for (int i = 0; i < size; ++i) { if (i == pos) { for (int j = size; j > i; --j) { base[j] = base[j - 1]; } } } base[pos] = x; size++; return TRUE; } int find(const Type& x) { if (size == 0) { return -1; } for (int i = 0; i < size; ++i) { if (base[i] == x) { return i; } } } Status delete_pos(int pos) { if (pos < 0 || pos > size) { return FALSE; } for (int i = 0; i < size; ++i) { if (i == pos) { for (int j = i; j < size; ++j) { base[j] = base[j + 1]; } break; } } size--; return TRUE; } Status delete_val(const Type& x) { int key = find(x); if (key == -1) { return FALSE; } delete_pos(key); return TRUE; /*if(Empty()) { cout << "Empty!" << endl; return FALSE; } for(int i = 0; i < size; ++i) { if(base[i] == x) { for(int j = i; j < size; ++j) { base[j] = base[j+1]; } break; } } size--; return TRUE;*/ } void sort() { if (size == 0 || size == 1) { return; } for (int i = 0; i < size - 1; ++i) { for (int j = 0; j < size - i - 1; ++j) { if (base[j] > base[j + 1]) { int tmp = base[j]; base[j] = base[j + 1]; base[j + 1] = tmp; } } } } void resver() { if (size == 0 || size == 1) { return; } for (int i = 0, j = size - 1; i < j; ++i, --j) { int tmp = base[i]; base[i] = base[j]; base[j] = tmp; } } int length() { return size; } void clear() { size = 0; } Status destroy() { if (base == NULL) { return FALSE; } delete[]base; base = NULL; size = capacity = 0; return TRUE; } Status modify_val(const Type& x, const Type& y) { int key = find(x); if (key == -1) { return FALSE; } modify_pos(key, y); return TRUE; } Status modify_pos(int pos, const Type& x) { if (pos < 0 || pos > size) { return FALSE; } for (int i = 0; i < size; ++i) { if (i == pos) { base[i] = x; } } return TRUE; } void show_list() { for (int i = 0; i < size; ++i) { cout << base[i] << ‘ ‘; } cout << endl; } private: enum {DefaultSize = 20, INC_SIZE = 3}; Type *base; int capacity; int size; };</strong></span>


<span style="font-size:18px;"><strong>#include "SeqList.h"

void main()
{
	SeqList<int> It1;
	SeqList<int> It2;
	SeqList<int> mylist;

	int select = 1;
	int pos;
	int item;
	system("Color 0d");
	while (select)
	{
		cout << "************************************" << endl;
		cout << "* [0]  quit_system [1] push_back   *" << endl;
		cout << "* [2]  push_front  [3] show_list   *" << endl;
		cout << "* [4]  pop_back    [5] pop_front   *" << endl;
		cout << "* [6]  insert_val  [7] insert_pos  *" << endl;
		cout << "* [8]  find        [9] delete_pos  *" << endl;
		cout << "* [10] delete_val [11] sort        *" << endl;
		cout << "* [12] resver     [13] length      *" << endl;
		cout << "* [14] clear      [15] destroy     *" << endl;
		cout << "* [16] modify_val [17] modify_pos  *" << endl;
		cout << "* [18] merge                       *" << endl;
		cout << "************************************" << endl;
		cout << "请选择:>";
		cin >> select;
		switch (select)
		{
		case 1:
			cout << "请输入要插入的数据(-1结束):>";
			while (cin >> item, item != -1)
			{
				mylist.push_back(item);
			}
			break;
		case 2:
			cout << "请输入要插入的数据(-1结束):>";
			while (cin >> item, item != -1)
			{
				mylist.push_front(item);
			}
			break;
		case 3:
			system("cls");
			mylist.show_list();
			system("pause");
			break;
		case 4:
			mylist.pop_back();
			break;
		case 5:
			mylist.pop_front();
			break;
		case 6:
			cout << "请输入要插入的值:>";
			cin >> item;
			mylist.insert_val(item);
			break;
		case 7:
			cout << "请输入要插入的值的下标:>";
			cin >> pos;
			cout << "请输入要插入的值";
			cin >> item;
			mylist.insert_pos(pos, item);
			break;
		case 8:
			cout << "请输入要查找的值:>";
			cin >> item;
			cout << "该值下标为:" << mylist.find(item) << endl;
			break;
		case 9:
			cout << "请输入要删除的值的下标:>";
			cin >> item;
			mylist.delete_pos(item);
			break;
		case 10:
			cout << "请输入要删除的值:>";
			cin >> item;
			mylist.delete_val(item);
			break;
		case 11:
			mylist.sort();
			break;
		case 12:
			mylist.resver();
			break;
		case 13:
			cout << "线性表的长度为:" << mylist.length() << endl;
			break;
		case 14:
			mylist.clear();
			break;
		case 15:
			mylist.destroy();
			break;
		case 16:
			cout << "请输入要改动的值:>";
			cin >> item;
			cout << "请输入改动后的值:>";
			cin >> pos;
			mylist.modify_val(item, pos);
			break;
		case 17:
			cout << "请输入要改动的值的下标:>";
			cin >> pos;
			cout << "请输入改动后的值:>";
			cin >> item;
			mylist.modify_pos(pos, item);
			break;
		case 18:
			for (int i = 1; i < 10; i += 2)
			{
				It1.push_back(i);
			}
			for (int i = 2; i <= 10; i += 2)
			{
				It2.push_back(i);
			}
			mylist.merge(It1, It2);
			mylist.show_list();
		default:
			break;
		}
	}
}

	</strong></span>

执行结果例如以下:

技术分享




【C++/数据结构】顺序表的基本操作