第一、单链表的定义和操作
#include <iostream> using namespace std; template <typename T> struct Node { T data; Node* next; }; template <typename T> class SingleLinkList { public: SingleLinkList() { head = new Node<T>(); head->next = NULL; } ~SingleLinkList() { Node<T> *p; while (head) { p = head; head = head->next; delete p; } head = NULL; } void createLinkList(int n) { std::cout << "you create single linklist with "<<n<<" node" << std::endl; Node<T>* s, *p; p = head; for (int i = 0; i < n; i++) { s = new Node<T>(); cout << "please enter the " << i << " number" << endl; std::cin >> s->data; s->next = NULL; p->next = s; p = s; } } bool insertNode(T data) { Node<T>* p = head; while (p->next!=NULL) { p = p->next; } Node<T> *n = new Node<T>(); n->data = data; n->next = NULL; p->next = n; return true; } bool insertNode(int i, T data) { Node<T>* p = head; int j; for (j = 1; j <= i-1; j++) { p = p->next; if (p==NULL) { break; } } if (p==NULL&&j<(i-1)) { std::cout << "The index:"<<i<<" is not found" << std::endl; return false; } Node<T>* node = new Node<T>(); node->data = data; node->next = p->next; p->next = node; return true; } bool deleteNode(int i) { Node<T>*f = head; Node<T>*s = head; int j; for (j=1; j <= i; j++) { s = f; f = f->next; if (f==NULL) { break; } } if (j<i+1) { std::cout << "The index:" << i << " is not found" << std::endl; return false; } s->next = f->next; delete f; return true; } T getElement(int i) { Node<T> *p = head; int j; for (j = 1; j <= i; j++) { p = p->next; if (p==NULL) { break; } } if (j<i && p==NULL) { std::cout << "The index:" << i << " is not found" << std::endl; } return p->data; } int findNode(T value) { Node<T> *p = head; int i = 0; while (p!=NULL) { if (p->data==value) { break; } p = p->next; i++; } if (p==NULL) { return -1; } return i; } void printAll() { Node<T>*p = head->next; while (p!=NULL) { std::cout << p->data << " "; p = p->next; } cout << endl; } int lenght() { Node<T>*p = head->next; int j = 0; while (p!=NULL) { p = p->next; j++; } return j; } bool clearLink() { Node<T>* current = head->next; while (head->next!=NULL) { current = head->next; head->next = current->next; delete current; } return true; } bool isEmpty() { return head->next == NULL; } private: Node<T>* head; };
第二、控制台演示
#include "pch.h" #include <iostream> #include "SingleLinkList.h" using namespace std; int main() { SingleLinkList<int> sLinkList; cout << "1:createLinkList(int n);" << endl; cout << "2:insertNode(T data);" << endl; cout << "3:insertNode(int i, T data);" << endl; cout << "4:int findNode(T value)" << endl; cout << "5:int printAll()" << endl; cout << "6:deleteNode(int i)" << endl; cout << "7: T getElement(int i)" << endl; cout << "8: clearLink()" << endl; cout << "10:exit;" << endl; int cmd=0; do { cout << "please tap cmd" << endl; cin >> cmd; switch (cmd) { case 1: { cout << "please enter the number of nodes you want to create" << endl; int nNum = 0; cin >> nNum; sLinkList.createLinkList(nNum); } break; case 2: cout << "please enter data " << endl; int data; cin >> data; sLinkList.insertNode(data); break; case 3: cout << "please enter index and data,example 4 10" << endl; int d, i; cin >> i >> d; sLinkList.insertNode(i, d); break; case 4: { cout << "please enter data you want to search" << endl; int searchData; cin >> searchData; int index = sLinkList.findNode(searchData); cout << "search index:" << index << endl; } break; case 5: sLinkList.printAll(); break; case 6: cout << "please enter index you want to delete" << endl; int delIndex; cin >> delIndex; sLinkList.deleteNode(delIndex); break; case 7: cout << "please enter index" << endl; int getIndex; cin >> getIndex; { int dataElement = sLinkList.getElement(getIndex); cout << "value:" << dataElement << endl; } break; case 8: sLinkList.clearLink(); break; default: break; } } while (cmd != 10); return 0; }