线性顺序表
来源:互联网 发布:linux c 守护进程 编辑:程序博客网 时间:2024/06/06 08:30
线性顺序表c++实现:
#include <iostream>using namespace std;namespace sequence { //线性表顺序存储 const int kListSize = 100; typedef int DataType; typedef struct { DataType data[kListSize]; int length; }SeqList; void InitList(SeqList *seq_list) { memset(seq_list, 0, sizeof(SeqList)); } int ListLength(const SeqList &seq_list) { if (seq_list.length > kListSize) { return 0; } return seq_list.length; } DataType GetNode(const SeqList &seq_list, int pos) { if (pos < 1 || pos > ListLength(seq_list)) { throw "位置无效"; } return seq_list.data[pos - 1]; } int LocateNode(const SeqList &seq_list, DataType elem) { for (int pos = 0; pos < ListLength(seq_list); ++pos) { if (seq_list.data[pos] == elem) { return ++pos; } } return 0; } void InsertList(SeqList *seq_list, int pos, DataType elem) { if (pos < 1 || pos > ListLength(*seq_list) + 1) { std::cout << "插入元素的位置无效" << endl; return; } int length = ListLength(*seq_list); if (length >= kListSize) { std::cout << "栈已经满" << endl; return; } for (int index = length - 1; index >= pos - 1; --index) { seq_list->data[index + 1] = seq_list->data[index]; } seq_list->data[pos - 1] = elem; seq_list->length++; } void DeleteList(SeqList *seq_list, int pos) { if (pos < 1 || pos > ListLength(*seq_list)) { std::cout << "删除元素位置无效" << endl; return; } int length = ListLength(*seq_list); if (0 == length) { std::cout << "容器为空" << endl; return; } for (int index = pos; index < length; ++index) { seq_list->data[index - 1] = seq_list->data[index]; } seq_list->length--; } //去除重复的元素 void Purge(SeqList *seq_list) { for (int pos = 1; pos <= ListLength(*seq_list); ++pos) { int elem1 = GetNode(*seq_list, pos); for (int nest_pos = pos + 1; nest_pos <= ListLength(*seq_list); ++nest_pos) { int elem2 = GetNode(*seq_list, nest_pos); if (elem1 == elem2) { DeleteList(seq_list, nest_pos); } } } } //反转元素 void Reverse(SeqList *seq_list) { if (0 == ListLength(*seq_list)) { cout << "元素为空"; return; } int mid = ListLength(*seq_list) / 2; for (int index = 0; index < mid; ++index) { int temp = seq_list->data[index]; seq_list->data[index] = seq_list->data[ListLength(*seq_list) - index - 1]; seq_list->data[ListLength(*seq_list) - index - 1] = temp; } }}void PrintList(const sequence::SeqList &seq_list) { cout << "{"; for (int index = 0; index < sequence::ListLength(seq_list); index++) { sequence::DataType elem = sequence::GetNode(seq_list, index + 1); cout << elem << " "; } cout << "}" << endl;}int main() { sequence::SeqList seq_list; sequence::InitList(&seq_list); sequence::InsertList(&seq_list, 1, 10); sequence::InsertList(&seq_list, 2, 20); sequence::InsertList(&seq_list, 3, 30); sequence::InsertList(&seq_list, 2, 40); cout << "打印原始list:" << endl; PrintList(seq_list); cout << "删除第2位置" << endl; sequence::DeleteList(&seq_list, 2); PrintList(seq_list); cout << "获得元素20的位置" << endl; int index = sequence::LocateNode(seq_list, 20); cout << "位置为:" << index << endl; cout << "去除重复的元素" << endl; sequence::InsertList(&seq_list, 4, 50); sequence::InsertList(&seq_list, 5, 20); sequence::InsertList(&seq_list, 6, 60); sequence::Purge(&seq_list); PrintList(seq_list); cout << "反转元素" << endl; sequence::Reverse(&seq_list); PrintList(seq_list); return 0;
0 0
- 线性表-顺序表
- 线性表--- 顺序表
- 线性表顺序表
- 线性表->顺序表
- 线性表- 顺序表
- 线性表--顺序表
- 线性表---顺序表
- 线性表--顺序表
- 线性表---顺序表
- 【线性表】顺序表
- 线性表--顺序表
- 线性表(顺序表)
- 线性表---顺序表
- 线性表--顺序表
- 线性表-顺序表
- 线性表-顺序表
- 数据结构--顺序线性表
- 线性表顺序存贮
- 计算机网络基础(二)
- 树的子结构
- VC提升进程权限
- 12 Tomcat编码问题03 header部分
- 在安卓开发中使用正则表达式,高效率开发
- 线性顺序表
- Java基础语法总结
- [LeetCode] 126. Word Ladder II
- POJ 3414 Pots【BFS】
- 老罗的获奖感言及经验转载
- 深入分析ArrayBlockingQueue
- Binary Search Tree Iterato
- Java(发牌机器)
- 136. Single Number