1. 程式人生 > >C++ 順序表實現

C++ 順序表實現

動態 struct als 順序表 ios 查找 isp tdi cas

線性表就是字面上的意思,

技術分享

順序表是線性表基於數組的一種實現,

“順序”這個名字怎麽來的並不清楚,可以勉強解釋為“存儲地址是連續、順序的”。

另外兩種線性表實現分別是“基於鏈表”和“散列存儲”。

順序表可以是動態的,也可以是靜態的,

“靜態”就是一開始就知道表容量,並且這個容量在之後無法被更改;

“動態”就是能夠動態的改變表的容量,實現上基於動態內存分配。

基於數組的靜態版本:

#ifndef SEQLIST_H
#define
SEQLIST_H const int MAX_SIZE = 512; template <class T> class SeqList { private: T node[MAX_SIZE]; int size = 0; public: int getSize() const { return size; } bool add(const T& x); bool insert(int i, const T& x); bool remove(int i); int search(T& x); T
& getNode(int i); }; #endif template <class T> bool SeqList<T>::add(const T& x) { if (size <= MAX_SIZE) { ++size; node[size-1] = x; return true; } return false; } template <class T> bool SeqList<T>::insert(int
i, const T& x) { if (size <= MAX_SIZE && i >= 0 && i <= size) { ++size; for (int k = size-1; k != i; --k) { node[k] = node[k-1]; } node[i] = x; return true; } return false; } template <class T> bool SeqList<T>::remove(int i) { if (i >= 0 && i <= size) { for (int k = i; k != size; ++k) { node[k] = node[k+1]; } --size; return true; } return false; } template <class T> int SeqList<T>::search(T& x) { for (int i = 0; i != size; ++i) { if (node[i] == x) return i; } return -1; } template <class T> T& SeqList<T>::getNode(int i){ if (i >= 0 && i < size) { return
node[i]; } }; #ifndef STUDENT_H #define STUDENT_H #include <string> #include <stdio.h> struct Student { int no = 0; std::string name; std::string grade; double goal = 0.0; Student() = default; Student(int arg1, const std::string& arg2, const std::string& arg3, double arg4): no(arg1), name(arg2), grade(arg3), goal(arg4) {} void display() const { printf("%d %s %s %.2f\n", no, name.c_str(), grade.c_str(), goal); } bool operator==(const Student &c1) { if (c1.name == this->name || c1.no == this->no && c1.grade == this->grade) return true; return false; } }; #endif #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string> using namespace std; int main() { SeqList<Student> list; Student s1(1, "張三", "2005", 70); Student s2(2, "李四", "2004", 65); Student s3(3, "王五", "2005", 80); Student s4(4, "馬六", "2006", 90); Student s5(5, "黃榮", "2004", 85); list.add(s1); list.add(s2); printf("step-a\n"); for (int i = 0; i != list.getSize(); ++i) { list.getNode(i).display(); } list.insert(0, s3); list.insert(2, s4); list.insert(4, s5); printf("step-b\n"); for (int i = 0; i != list.getSize(); ++i) { list.getNode(i).display(); } list.remove(0); list.remove(3); printf("step-c\n"); for (int i = 0; i != list.getSize(); ++i) { list.getNode(i).display(); } printf("step-d\n"); if (list.search(s5) == -1) { printf("不存在\n"); } else { s5.display(); } Student s6(6, "郭靖", "2004", 85); if (list.search(s6) == -1) { printf("不存在\n"); } else { s6.display(); } printf("step-e\n"); for (int i = 0; i != list.getSize(); ++i) { list.getNode(i).display(); } printf("step-f\n"); system("pause"); while (true) { system("cls"); cout << "#####WINDOWS##EDITION#####################" << endl; for (int i = 0; i != list.getSize(); ++i) { list.getNode(i).display(); } cout << "########################MAXSIZE=512#########" << endl; cout << "請選擇您的操作1增2刪3改4查:" << endl; int order = 0; cin >> order; if (order < 1 || order > 4) { cout << "無效命令!\n" << endl; } else { switch (order) { case 1: { cout << "請輸入學生的學號和姓名:" << endl; int no = 0; string name; cin >> no >> name; cout << "請輸入學生的學級和分數:" << endl; string grade; double goal = 0.0; cin >> grade >> goal; Student s(no, name, grade, goal); list.add(s); break; } case 2: { cout << "請輸入學生座號:" << endl; int num; cin >> num; if (!list.remove(num)) { cout << "不好意思,這個學生不存在" << endl; } break; } case 3: { cout << "請輸入需要修改的學生的座號:" << endl; int num; cin >> num; cout << "您要修改的是" << list.getNode(num).name << "...,請輸入新數據以覆蓋原始數據:" << endl; cin >> list.getNode(num).no >> list.getNode(num).name >> list.getNode(num).grade >> list.getNode(num).goal; break; } case 4: { cout << "請輸入需要查找的學生姓名:" << endl; string name; cin >> name; Student s; s.name = name; if (list.search(s) == -1) { cout << "抱歉,沒找到這名同學。" << endl; } else { cout << "該同學信息如下:" << endl; list.getNode((list.search(s))).display(); } break; } } } system("pause"); } return 0; }

常量成員函數的設置有些小問題待修改。。

C++ 順序表實現