写到了链表相关知识,感觉只要会了以下代码就可以,记录一下
#include<iostream> #include<cstdlib> using namespace std; template <typename T> class CNode { public: T m_data; // 表示数据域 CNode<T> *next; // 表示指针域,存储下一个节点的位置 }; template <typename T> class CList:public CNode<T>{ public: CNode<T> *header; CList(); void Menu(); void Insert(T);//插入到开头 void Insert(T,int); void InsertE(T); //插入到结尾 int GetLen(); void Delete(T); void Print(); int Search(T); }; template <typename T> CList<T>::CList(){ this -> header = (CNode<T> *)malloc(sizeof(CNode<T>)); //申请新的存储空间 header->m_data=-1; //数据用不到 赋个-1 header->next=NULL; //指针域指向空 } template <typename T> void CList<T>::Menu(){ cout << "========================" << endl; cout << "请输入你要选择的功能!" << endl; cout << " 1.创建链表" << endl; cout << " 2.插入节点" << endl; cout << " 3.删除节点" << endl; cout << " 4.遍历输出" << endl; cout << " 5.查询节点" << endl; cout << " 6.退出功能" << endl; cout << "请选择:" ; int chose; cin >> chose; switch(chose) { case 1: cout << "请输入初始要创建的节点个数!" <<endl; int cou,date; cin >> cou; for(int i=0;i<cou;i++) { cout << "请输入节点的数据!" << endl; cin >> date; InsertE(date); } Menu(); break; case 2: cout << "输入1插入到开头。输入2插入到尾部,输入3自定义插入!" <<endl; int icho,idate; cin >> icho; cout << "请输入节点的数据!" << endl; cin >> idate; switch(icho) { case 1: Insert(idate); break; case 2: InsertE(idate); break; case 3: cout << "请输入要插入的位置!" << endl; int pos; cin >> pos; Insert(idate,pos); } Menu(); break; case 3: cout << "请输入要删除的节点的数据内容!" << endl; int idate3; cin >> idate3; Delete(idate3); Menu(); break; case 4: Print(); Menu(); break; case 5: cout << "请输入要查询的节点的数据内容!" << endl; int idate5; cin >> idate5; cout << "要查询的节点位于第" << Search(idate5) << "个!" << endl; Menu(); break; case 6: cout << "再见!" <<endl; break; default: cout << "请正确输入!" << endl; Menu(); break; } } template <typename T> void CList<T>::Insert(T temp){ CNode<T> *newnode=(CNode<T> *)malloc(sizeof(CNode<T>)); newnode->m_data=temp; newnode->next=header->next; header->next=newnode; } template <typename T> void CList<T>::Insert(T temp,int loc){ if(loc<1||loc>GetLen()+1) { cout << "请正确输入要插入的位置!" << endl; Insert(temp,loc); } int tempcount=0; CNode<T> *newnode=this->header; CNode<T> *ptemp=(CNode<T> *)malloc(sizeof(CNode<T>)); while(tempcount<loc-1)//找到要插入的位置 { tempcount++; newnode=newnode->next; } ptemp->m_data=temp; //插入 ptemp->next=newnode->next; newnode->next=ptemp; } template <typename T> void CList<T>::InsertE(T sea){ CNode<T> *newnode=(CNode<T> *)malloc(sizeof(CNode<T>)); newnode->next=NULL; newnode->m_data=sea; CNode<T> *temp=this->header; if(temp==NULL) temp=newnode; else { while(temp->next) { temp=temp->next; } } temp->next=newnode; } template <typename T> int CList<T>::GetLen(){ int count=0; CNode<T> *temp; temp=this->header->next; while(temp) { count++; temp=temp->next; } return count; } template <typename T> void CList<T>::Delete(T temp){ CNode<T> *PCurr; PCurr=this->header; while(true) //寻找要删除的目标 { if(temp==PCurr->next->m_data) break; else PCurr=PCurr->next; } CNode<T> *Ptemp=PCurr; //目标前一个 PCurr=PCurr->next; //目标 Ptemp->next=PCurr->next; free(PCurr); //释放空间 } template <typename T> void CList<T>::Print(){ CNode<T> *temp=this->header->next; while(temp) { cout << temp->m_data << endl; temp=temp->next; } } template <typename T> int CList<T>::Search(T sea){ CNode<T> *PCurr; PCurr=this->header; int count=1; while(true) //寻找要查询的目标 { if(sea==PCurr->next->m_data) break; else { count++; PCurr=PCurr->next; } } return count; } int main() { CList<int> text; text.Menu(); }
原创文章,作者:1402239773,如若转载,请注明出处:https://blog.ytso.com/273214.html