日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构算法实现

發(fā)布時間:2023/12/31 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构算法实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 前言
  • 一、線性表
    • 1.順序表


前言

C++實現(xiàn)數(shù)據(jù)結(jié)構(gòu)代碼,本文參考部分博主代碼,僅用于學(xué)習


一、線性表

1.順序表

代碼如下(示例):

關(guān)于數(shù)據(jù)結(jié)構(gòu)實現(xiàn)與實踐可以參考
數(shù)據(jù)結(jié)構(gòu)的實踐應(yīng)用

/* Project: sequence_list(數(shù)據(jù)結(jié)構(gòu)-順序表) Date: 2018/09/12 20191012修改 添加Reverse 20200819修改 添加ClearList Author: Frank Yu CreateList(SqList &L,int n) 參數(shù):順序表L,順序表長度n 功能:創(chuàng)建長度為的順序表 時間復(fù)雜度:O(n) InitList(SqList &L) 參數(shù):順序表L 功能:初始化 時間復(fù)雜度:O(1) InsertList(SqList &L,int i,ElemType e) 參數(shù):順序表L,位置i,元素e 功能:位置i處插入元素e 時間復(fù)雜度:O(n) ListDelete(SqList &L,int i) 參數(shù):順序表L,位置i 功能:刪除位置i處元素 時間復(fù)雜度:O(n) LocateElem(SqList L,ElemType e) 參數(shù):順序表L,元素e 功能:返回第一個等于e的元素的位置 時間復(fù)雜度:O(n) Reverse(SqList &L) 參數(shù):順序表L 倒置函數(shù) 將原順序表直接倒置 PrintList(SqList L) 參數(shù):順序表L 功能:遍歷L,并輸出 SplitSort(SqList &L) 參數(shù):順序表L 功能:分開奇偶,并分開排序 ClearList(SqList &L) 參數(shù):順序表L 功能:清空順序表 */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<iomanip> #include<iostream> #define MaxSize 100 #define ElemType int #define Status int using namespace std; //順序表數(shù)據(jù)結(jié)構(gòu) typedef struct {ElemType data[MaxSize];int length; }SqList;//初始化順序表,構(gòu)造一個空的順序表 Status InitList(SqList &L) {memset(L.data, 0, sizeof(L));L.length = 0;return 0; }//創(chuàng)建順序表函數(shù),初始化前m個數(shù)據(jù) bool CreatList(SqList &L, int m) {if (m<0 || m>MaxSize)return false;for (int i = 0; i < m; i++) {cin >> L.data[i];L.length++;}return true; } //插入函數(shù),在位置i開始插入數(shù)據(jù)e 1<=i<=length+1 bool InsertList(SqList &L, int i,ElemType e) {if (i<1 || i>L.length+1) {cout << "插入位置無效" << endl;return false;}if (L.length >= MaxSize) {cout << "當前存儲空間已滿" << endl;return false;}for (int j = L.length; j >= i; j--) {L.data[j] = L.data[j-1];}L.data[i-1] = e;L.length++;return true; } //刪除函數(shù),刪除第i個的元素,其余元素依次移動 1<=i<=L.length+1 bool ListDelete(SqList &L, int i) {if (i<1 || i>L.length) {cout << "位置無效" << endl;return false;}for (int j = i; j < L.length - 1; j++) {L.data[j - 1] = L.data[j];}L.length--;return true; } //查找函數(shù) 按位置從小到大查找第一個值等于e的元素 并返回位置 int LocateElem(SqList L, ElemType e) {int i = 0;while (i < L.length) {if (L.data[i] == e) {return i+1;}i++;}return 0; } //倒置函數(shù) 將原順序表直接倒置 void Reverse(SqList &L) {if (L.length) {for (int i = 0; i < L.length - 1 - i; i++) {int t = L.data[i];L.data[i] = L.data[L.length - 1 - i];L.data[L.length - 1 - i] = t;}} } //對順序表進行排序 冒泡排序?qū)崿F(xiàn) void ListSort(SqList &L) {if (L.length) {int temp,flag;for (int i = 0; i < L.length - 1; i++) {flag = 0;for (int j = 0; j < L.length -1- i; j++) {if (L.data[j + 1] < L.data[j]) {temp = L.data[j + 1];L.data[j + 1] = L.data[j];L.data[j] = temp;flag = 1;}}if (flag == 0)break;}} } //清空順序表 void ClearList(SqList &L) {L.length = 0; }//輸出當前順序表的元素 void CoutList(SqList L) {cout << "當前順序表的元素為:" << endl;int len = 0;while (len < L.length) {cout << setw(4) << L.data[len];len++;}cout << endl; } //創(chuàng)建順序表函數(shù) void Creat(SqList &L) {int n;bool flag;cout << "輸入要創(chuàng)建的順序表長度: ";cin >> n;cout << endl;cout << "輸入順序表元素: ";flag = CreatList(L, n);if (flag) {cout << "創(chuàng)建成功!" << endl;CoutList(L);}elsecout << "輸入長度非法,創(chuàng)建失敗" << endl;} //插入功能函數(shù) 調(diào)用InsertList完成順序表元素插入 調(diào)用PrintList函數(shù)顯示插入成功后的結(jié)果 void Insert(SqList &L) {int place; ElemType e; bool flag;cout << "請輸入要插入的位置(從1開始)及元素:\n";cin >> place >> e;flag = InsertList(L, place, e);if (flag){cout << "插入成功!";CoutList(L);} } //刪除功能函數(shù) 調(diào)用ListDelete函數(shù)完成順序表的刪除 調(diào)用PrintList函數(shù)顯示插入成功后的結(jié)果 void Delete(SqList &L) {int place; bool flag;cout << "請輸入要刪除的位置(從1開始):\n";cin >> place;flag = ListDelete(L, place);if (flag){cout << "刪除成功!!!\n";CoutList(L);} } //查找功能函數(shù) 調(diào)用LocateElem查找元素 void Search(SqList L) {ElemType e; int flag;cout << "請輸入要查找的值:\n";cin >> e;flag = LocateElem(L, e);if (flag){cout << "該元素位置為:" << flag << endl;}elsecout << "未找到該元素!\n"; } void menu() {cout << setw(6) << "1.創(chuàng)建" << setw(6) << "2.插入" << endl;cout << setw(6) << "3.刪除" << setw(6) << "4.查找" << endl;cout << setw(6) << "5.倒置" << setw(6) << "6.排序" << endl;cout << setw(6) << "7.清空" << setw(6) << "8.退出" << endl;cout << setw(6) << "9.輸出" << endl; } int main() {SqList L;int choice;InitList(L);while (1) {menu();cout << "輸入選項:";cin >> choice;if (choice == 8)break;switch (choice){case 1:Creat(L); break;case 2:Insert(L); break;case 3:Delete(L); break;case 4:Search(L); break;case 5:Reverse(L); break;case 6:ListSort(L); break;case 7:ClearList(L); break;case 9:CoutList(L); break;default:cout << "輸入錯誤!" << endl;}}return 0; }

總結(jié)

以上是生活随笔為你收集整理的数据结构算法实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。