溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++如何實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)的順序表

發(fā)布時間:2021-11-16 17:13:58 來源:億速云 閱讀:127 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)C++如何實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)的順序表的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

    代碼

    1.SeqList.h

    #ifndef SEQLIST_H
    #define SEQLIST_H
    #include<iostream>
    using namespace std;
    template<class T,int MAXSIZE>
    class SeqList
    {
    	T data[MAXSIZE];
    	int length;
    public:
    	SeqList();
    	SeqList(T a[],int n);
    	~SeqList();
    	int ListLength();
    	T Get(int pos);
    	int Locate(T item);
    	void SeqPrint();
    	void Insert(int i, T item);
    	T Delete(int i);
    };
    #endif

    2.SeqList.cpp

    #define _CRT_SECURE_NO_WARNINGS   1
    #include"SeqList.h"
    template<class T, int MAXSIZE>
    SeqList<T,MAXSIZE>::SeqList()
    {
    	length = 0;
    }
    template<class T, int MAXSIZE>
    SeqList<T, MAXSIZE>::SeqList(T a[], int n)
    {
    	if (n < MAXSIZE)
    	{
    		length = n;
    		for (int i = 0; i < n; i++)
    		{
    			data[i] = a[i];
    		}
    	}
    	else
    	{
    		cerr << "您的數(shù)據(jù)已經(jīng)超過范圍,系統(tǒng)無法繼續(xù)工作" << endl;
    		exit(-1);
    	}
    }
    template<class T, int MAXSIZE>
    SeqList<T, MAXSIZE>::~SeqList()
    {
    }
    template<class T, int MAXSIZE>
    int SeqList<T, MAXSIZE>::ListLength()
    {
    	return length;
    }
    template<class T, int MAXSIZE>
    T SeqList<T, MAXSIZE>::Get(int pos)
    {
    	if (pos > length || pos < 0)
    	{
    		cerr << "您要查找的位置不存在,系統(tǒng)無法繼續(xù)為您服務(wù)" << endl;
    		exit(-1);
    	}
    	else
    	{
    		return data[pos - 1];
    	}
    }
    template<class T, int MAXSIZE>
    int SeqList<T, MAXSIZE>::Locate(T item)
    {
    	for (int i = 0; i < length; i++)
    	{
    		if (data[i] == item)
    			return i + 1;
    	}
    	return -1;
    }
    template<class T, int MAXSIZE>
    void SeqList<T, MAXSIZE>::SeqPrint()
    {
    	for (int i = 0; i < length; i++)
    	{
    		cout << data[i] << "  ";
    	}
    	cout << endl;
    }
    template<class T, int MAXSIZE>
    void SeqList<T, MAXSIZE>::Insert(int i, T item)
    {
    	if (length < MAXSIZE)
    	{
    		for (int j = length - 1; j>=i - 1; j--)
    		{
    			data[j + 1] = data[j];
    		}
    		data[i - 1] = item;
    		length++;
    	}
    	else
    	{
    		cerr << "抱歉,當(dāng)前已經(jīng)達(dá)到系統(tǒng)最大的儲存,無法為您插入" << endl;
    		exit(-1);
    	}
    }
    template<class T, int MAXSIZE>
    T SeqList<T, MAXSIZE>::Delete(int i)
    {
    	if (length == 0)
    	{
    		cerr << "當(dāng)前無可刪除元素" << endl;
    		exit(-1);
    	}
    	if (i<1 || i>length)
    	{
    		cerr << "該位置非法" << endl;
    		exit(-1);
    	}
    	T x = data[i - 1];
    	for (int j = i; j < length; j++)
    	{
    		data[j - 1] = data[j];
    	}
    	length--;
    	return x;
    }

    3.test.cpp

    #define _CRT_SECURE_NO_WARNINGS   1
    #include"SeqList.cpp"
    #include<iostream>
    using namespace std;
    void menu()
    {
    	cout << "|------------------------------------|" << endl;
    	cout << "|----------- 歡迎來到順序表 ---------|" << endl;
    	cout << "|---------------1.插入---------------|" << endl;
    	cout << "|---------------2.刪除---------------|" << endl;
    	cout << "|---------------3.求長---------------|" << endl;
    	cout << "|---------------4.取值---------------|" << endl;
    	cout << "|---------------5.定位---------------|" << endl;
    	cout << "|---------------6.打印---------------|" << endl;
    	cout << "|---------------0.退出---------------|" << endl;
    	cout << "|------------------------------------|" << endl;
    }
    int main()
    {
    	int *a;
    	int n;
    	cout << "請輸入您要構(gòu)造的順序表的長度" << endl;
    	cin >> n;
    	a = new int[n];
    	cout << "請輸入該順序表中的每一個元素" << endl;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> a[i];
    	}
    	SeqList<int, 20>seq(a, n);
    	cout << "現(xiàn)在開始我們的程序之旅" << endl;
    	int input=0;
    	do
    	{
    		menu();
    		cout << "輸入您要進(jìn)行的操作的編號" << endl;
    		cin >> input;
    		switch (input)
    		{
    		case 1:
    			cout << "請輸入您要插入的位置和數(shù)值" << endl;
    			int pos;
    			int value;
    			cin >> pos;
    			cin >> value;
    			seq.Insert(pos,value);
    			break;
    		case 2:
    			cout << "請輸入您要刪除的位置" << endl;
    			int pos1;
    			cin >> pos1;
    			cout << "您刪除的元素的值為:";
    			cout << seq.Delete(pos1) << endl;
    			break;
    		case 3:
    			cout << "您的順序表當(dāng)前的長度為:" << seq.ListLength() << endl;
    			break;
    		case 4:
    			cout << "請輸入您要查找的位置" << endl;
    			int pos2;
    			cin >> pos2;
    			cout << "您查找的元素的值為:";
    			cout << seq.Get(pos2) << endl;;
    			break;
    		case 5:
    			cout << "請輸入您要查找的元素" << endl;
    			int item;
    			cin >> item;
    			cout << "您查找的元素的位置為:";
    			cout << seq.Locate(item) << endl;;
    			break;
    		case 6:
    			cout << "當(dāng)前順序表如下:" << endl;
    			seq.SeqPrint();
    			break;
    		case 0:
    			cout << "程序退出,感謝使用" << endl;
    			exit(-1);
    			break;
    		default :
    			cout << "您的輸入有誤,請重新選擇" << endl;
    		}
    	} while (input);
    	return 0;
    }

    感謝各位的閱讀!關(guān)于“C++如何實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)的順序表”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    c++
    AI