連結串列的實現(c++)
阿新 • • 發佈:2018-11-17
連結串列的實現(c++)
連結串列的陣列實現
#include "stdafx.h" #include <iostream> using namespace std; class Alist { private: int maxsize; int listsize; int curr;//當前指向的物件 char* listarray; public: Alist(int size) { maxsize = size; listsize = curr = 0; listarray = new char[maxsize]; } ~Alist() { delete[] listarray; } void clear() { delete[] listarray; listsize = curr = 0; listarray = new char[maxsize]; } void insert(const char& it, int position) { for (int i = listsize; i > position - 1; i--) listarray[i] = listarray[i - 1]; listarray[position - 1] = it; listsize++; } void append(const char& it) { listarray[listsize++] = it; } void move(int position) { if (position > listsize) cout << "Out of range!" << endl; char it = listarray[position - 1]; for (int i = position - 1; i < listsize - 1; i++) listarray[i] = listarray[i + 1]; listsize--; } void getValue(int position) { cout << "The " << position << " element is " << listarray[position - 1]; } void length() const { cout << "The length of the list is " << listsize << endl; } void thelist() { for (int i = 0; i < listsize; i++) cout << listarray[i] << " "; cout << endl; } void ifempty() { if (listsize == 0) cout << "The list is empty." << endl; else cout << "The list is not empty." << endl; } void position(const char& it) { for (int i = 0; i < listsize; i++) { if (listarray[i] == it) cout << it << " is the " << i + 1 << " element" << endl; } cout << "a is the fisrt element" << endl; } }; int main() { Alist Alist1(10); Alist1.append('a'); Alist1.append('b'); Alist1.append('c'); Alist1.append('d'); Alist1.append('e'); Alist1.thelist(); Alist1.length(); Alist1.ifempty(); Alist1.getValue(3); Alist1.position('a'); Alist1.insert('f', 4); Alist1.thelist(); Alist1.move(3); Alist1.thelist(); int a; cin >> a; return 0; }