生活随笔
收集整理的這篇文章主要介紹了
数据结构算法实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
前言
C++實現數據結構代碼,本文參考部分博主代碼,僅用于學習
一、線性表
1.順序表
代碼如下(示例):
關于數據結構實現與實踐可以參考
數據結構的實踐應用
#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
;
typedef struct {ElemType data
[MaxSize
];int length
;
}SqList
;
Status
InitList(SqList
&L
) {memset(L
.data
, 0, sizeof(L
));L
.length
= 0;return 0;
}
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
;
}
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
;
}
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
;
}
int LocateElem(SqList L
, ElemType e
) {int i
= 0;while (i
< L
.length
) {if (L
.data
[i
] == e
) {return i
+1;}i
++;}return 0;
}
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
;}}
}
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
;
}
void Creat(SqList
&L
) {int n
;bool flag
;cout
<< "輸入要創建的順序表長度: ";cin
>> n
;cout
<< endl
;cout
<< "輸入順序表元素: ";flag
= CreatList(L
, n
);if (flag
) {cout
<< "創建成功!" << endl
;CoutList(L
);}elsecout
<< "輸入長度非法,創建失敗" << endl
;}
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
);}
}
void Delete(SqList
&L
)
{int place
; bool flag
;cout
<< "請輸入要刪除的位置(從1開始):\n";cin
>> place
;flag
= ListDelete(L
, place
);if (flag
){cout
<< "刪除成功!!!\n";CoutList(L
);}
}
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.創建" << 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;
}
總結
以上是生活随笔為你收集整理的数据结构算法实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。