1、顺序存储
//List.h #ifndef LIST_H_ #define LIST_H_ #define MAXSIZE 100 typedef struct _Poly { int a; int n; bool operator == (_Poly e) { if (a == e.a && n == e.n) return true; else return false; } }Poly; #define ElemType Poly typedef struct _Node { ElemType data[MAXSIZE]; int last; }Node; class List { private: Node *data; public: List(); ~List(); void Insert(ElemType e, int index); void Remove(int index); int Find(ElemType e); ElemType FindKth(int k); int Length(); }; #endif //List.cpp #include "List.h" #include <iostream> List::List() { data = new Node(); data->last = -1; } List::~List() { delete data; } void List::Insert(ElemType e, int index) { if (data->last == MAXSIZE - 1) { std::cout << "表满了" << std::endl; return; } if (index < 0 || index >(data->last + 1)) { std::cout << "添加位置不合法" << std::endl; return; } for (int i = data->last; i >= index; i--) { data->data[i + 1] = data->data[i]; } data->data[index] = e; data->last++; } void List::Remove(int index) { if (index < 0 || index > data->last) { std::cout << "删除位置不和法" << std::endl; return; } for (int i = index; i < data->last; i++) { data->data[i] = data->data[i + 1]; } data->last--; } int List::Find(ElemType e) { for (int i = 0; i < data->last; i++) { if (data->data[i] == e) return i; } return -1; } ElemType List::FindKth(int k) { if (k < 0 || k > data->last) { std::cout << "查找位置不合法" << std::endl; } return data->data[k]; } int List::Length() { return (data->last + 1); }
2、链式存储
//PList.h #ifndef PLIST_H_ #define PLIST_H_ #define MAXSIZE 100 typedef struct _Poly { int a; int n; bool operator == (_Poly e) { if (a == e.a && n == e.n) return true; else return false; } }Poly; #define ElemType Poly typedef struct _PNode { ElemType data; _PNode* next; }PNode; class PList { private: PNode *head; public: PList(); ~PList(); void Insert(ElemType e, int index); void Remove(int index); int Find(ElemType e); ElemType FindKth(int k); int Length(); }; #endif //PList.cpp #include "PList.h" #include <iostream> PList::PList() { head = new PNode(); head->next = NULL; } PList::~PList() { delete head; } void PList::Insert(ElemType e, int index) { PNode *p = head; PNode *s = new PNode(); s->data = e; s->next = NULL; if (index > Length()) std::cout << "插入位置错误" << std::endl; int i = 0; while (p && i < index) { p = p->next; i++; } s->next = p->next; p->next = s; } void PList::Remove(int index) { PNode *p = head; PNode *s = new PNode(); if (index > Length()) std::cout << "移除位置错误" << std::endl; int i = 0; while (p && i < index) { p = p->next; i++; } s = p->next; p->next = s->next; delete s; } int PList::Find(ElemType e) { PNode *p = head; int i = 0; while (p) { if (p->data == e) return i; i++; p = p->next; } return -1; } ElemType PList::FindKth(int k) { PNode *p = head; if (k < 0 || k > Length()) { std::cout << "查找位置不合法" << std::endl; } int i = 0; while (p && i < k) { p = p->next; i++; } return p->next->data; } int PList::Length() { PNode *p = head; int i = 0; while (p) { i++; p = p->next; } return i; }
3、测试用例
//main.cpp #include <iostream> #include <stdlib.h> #include "PList.h" using namespace std; int main() { PList L; for (int i = 0; i < 5; i++) { Poly t = { i, i }; L.Insert(t, i); }; cout << "last:" << L.Length() << endl; Poly e = { 2, 2 }; cout << "Find return:" << L.Find(e) << endl; for (int i = 0; i < 4; i++) { cout << L.FindKth(i).a << "X" << L.FindKth(i).n << " + "; } cout << L.FindKth(4).a << "X" << L.FindKth(4).n << endl; system("pause"); return 0; }
时间: 2024-10-15 21:37:48