#include<iostream> using namespace std; template<typename T> class List { public: struct Node { T value; Node* next; }; List() { while ( head == 0) { head=new Node; } head->next=0; } bool Create() { Node* x; Node* p; T v; char a; p=new Node; if (p!=0) { p=head; cout << "Please input the elements by space"<<endl; while(v != '\n') { cin >> v; x=new Node; p->next=x; x->value=v; x->next=0; p=x; } return true; } return false; } bool Insert() { Node* p; Node* temp; Node* x; int k, i; T v; p=new Node; temp=new Node; x=new Node; p=head; cout << "please input the element's location" << endl; cin >> k; if (k>0) { for (i=0; i<k-1; i++,p=p->next) { if (p == 0) return false; } cout << "please input the element's value" << endl; cin >> v; x->value=v; if( p->next == 0) { p->next=x; x->next=0; } else { temp=p->next; p->next=x; x->next=temp; } return true; } return false; } bool Delete() { Node* p; Node* temp; int k, i; p=new Node; temp=new Node; p=head; cout << "please input the element's location" << endl; cin >> k; if (p->next!=0 && k>0) { for(i=0; i<k-1; i++,p=p->next); { if (p->next == 0) return false; } temp=p->next; p->next=p->next->next; delete temp; return true; } return false; } void Display() { Node* p; if( head->next != 0) { for(p=head->next; p!=0; p=p->next) cout<<p->value<<" "; cout<<endl; } else cout << "empty!"<<endl; } private: Node* head; }; void menu(); int main() { bool res; int i; List<char> a; menu(); while(1) { cin >> i; switch(i) { case 1: { res=a.Create(); if (res == false) cout << "failed" << endl; else cout<< "succeed" << endl; break; } case 2: { res=a.Delete(); if (res == false) cout << "failed" << endl; else cout<< "succeed" << endl; break; } case 3: { res=a.Insert(); if (res == false) cout << "failed" << endl; else cout<< "succeed" << endl; break; } case 4: { a.Display(); break; } case 5: { return 0; } default:break; } } } void menu() { cout << "please input a number to choose your operation" << endl; cout << "1: create a list" << endl << "2: delete an element by location" << endl; cout << "3: insert an element by location" << endl << "4: display the latest list" << endl; cout << "5: quit the programm" << endl; }