#include<iostream> #include<cstdlib> #include<cstring> using namespace std; struct Node{ int date; Node* next; }; class List{ private: Node *head; public: List() { head = new Node; head->date = 0; head->next = NULL; } ~List() { delete head; } int Length(); bool IsEmpty(); void Insert(int x,int k); void Delete(int k); void ClearList(); void Output(); }; int List::Length() { Node *q = head->next; int k = 0; while (q) { k++; q = q->next; } return k; } bool List::IsEmpty(){ return head->next == NULL; } void List::Insert(int x, int s) { if (x < 1) { cout<<"NO"<<endl; exit(1); } int k = 0; Node *q = head; while (q&& k < x-1) { k++; q = q->next; } if (!q) { cout<<"NO"<<endl; exit(1); } Node *p = new Node; p->date = s; p->next = q->next; q->next = p; } void List::Delete(int x) { if (x < 1) { cout<<"NO"<<endl; exit(1); } int k = 0; Node *q = head; while (q&& k < x-1) { k++; q = q->next; } if (q->next == NULL) { cout<<"NO"<<endl; exit(1); } Node *p = q->next; q->next = p->next; } void List::ClearList() { Node *q = head->next; while (q) { Node *p = q; q = q->next; delete p; } head->next = NULL; } void List::Output() { Node *q = head->next; while (q) { cout<<q->date<<" "; q = q->next; } cout<<endl; } int main() { List l; l.Insert(1,2); l.Insert(2,3); l.Insert(3,4); l.Insert(4,5); l.Output(); l.Delete(2); l.Output(); cout<<l.Length(); return 0; }