c++模板链表实现
2016-03-24 17:48
627 查看
简介:主要是利用模板实现链表的操作。模板的使用,使得程序的开发量大大地减少。
可以先定义一个链表LinkList,之后可以定义自己的类了(例如:Student类),使用时就可以这样调用了
LinkList<Student> L;
下面便是链表的实现:
LinkList.cpp
当真正使用的时候,只需要定义自己的类及其成员函数的实现,然后调用就ok了,很方便吧!
可以先定义一个链表LinkList,之后可以定义自己的类了(例如:Student类),使用时就可以这样调用了
LinkList<Student> L;
下面便是链表的实现:
LinkList.cpp
template<class Type> struct Node { Type data; struct Node *next; }; template<class Type> class LinkList { private: Node<Type> *head;//头指针 public: LinkList(); int InsertLinkList(); Node<Type> *GetLinkList(int pos); int DelLinkList(int pos); void PrintLinkList(); ~LinkList(); }; //创建链表 template<class Type> LinkList<Type>::LinkList() { head = new Node<Type>; head->next = NULL; } //链表插入 头插入 template<class Type> int LinkList<Type>::InsertLinkList() { Node<Type> *insertElement = new Node<Type>; insertElement->data.Input();//插入结点数据的输入 insertElement->next = head->next; head->next = insertElement; return 1; } //取链表结点 template<class Type> Node<Type>* LinkList<Type>::GetLinkList(int pos) { Node<Type> *p = head; int point = 0; while ((p != NULL) && (point < pos)) { p = p->next; point++; } return point == pos ? p : NULL; } //打印链表结点 template<class Type> void LinkList<Type>::PrintLinkList() { Node<Type> *p; p = head->next; if (p == NULL) cout << "表空!" << endl; while (p) { // cout << p->data <<setw(2); p->data.Print(); p = p->next; } cout << endl; } //删除链表结点 template<class Type> int LinkList<Type>::DelLinkList(int pos) { Node<Type> *p, *delElememt; int point = 1; p = GetLinkList(pos - 1); if (p == NULL) { cout << "position error!" << endl; return -1; } else if (p->next == NULL) { cout << "this position is not exist" << endl; return -1; } else { delElememt = p->next; p->next = delElememt->next; delete delElememt; return 1; } } template<class Type> LinkList<Type>::~LinkList() { Node<Type> *p, *cur; cur = head->next; while (1) { if (cur == NULL) break; else { p = cur; cur = cur->next; delete p; p = NULL; } } delete head; head = NULL; }student.cpp
class Stu { private: char *name; int id; public: Stu(); Stu(char *na, int ID); ~Stu(); void Input(); void Print(); }; #if 1 Stu::Stu() { name = NULL; } Stu::Stu(char *na, int ID) { name = new char[strlen(na) + 1]; strcpy(name, na); id = ID; } Stu::~Stu() { delete[]name; } void Stu::Input() { cout << "please input name and ID of student" << endl; name = new char[20]; cin >> name >> id; } void Stu::Print() { cout << "student:" << id << " name: " << name << endl; } #endif
void menu() { cout << "********************" << endl; cout << "1---------------插入" << endl; cout << "2---------------删除" << endl; cout << "3---------------显示" << endl; cout << "0---------------退出" << endl; cout << "********************" << endl; cout << "请选择" << endl; } int main(void) { LinkList<Stu> L; //例如,就像这样来调用 int choice; int m = 1; while (m) { menu(); cin >> choice; switch (choice) { case 1: { int ret = L.InsertLinkList(); if (ret == 1) { cout << "insert successful!" << endl; } else { cout << "insert failure!" << endl; } break; } case 2: { int pos; cout << "请输入要删除的位置!" << endl; cin >> pos; if (L.DelLinkList(pos) == 1) { cout << "delete successful!" << endl; } else { cout << "delete failure!" << endl; } break; } case 3: { cout << "信息:" << endl; L.PrintLinkList(); break; } case 0: { m = 0; break; } default: { cout << "输入错误,请重新输入!" << endl; break; } } } system("pause"); return 0; }
当真正使用的时候,只需要定义自己的类及其成员函数的实现,然后调用就ok了,很方便吧!
相关文章推荐
- C++作业2
- CBC和CTR解密模式——C++实现
- C++学习笔记2——对象数组与对象指针
- #ifdef条件选择应用!
- 通过C语言程序修改控制台的背景和前景颜色
- VC++图像存取总结
- C语言深度剖析读书笔记(一)
- C++ 的并行和异步
- C++标准库 vector排序
- C++面试出现频率最高的30道题目
- 使用c语言来扩展python模块
- c++作业2
- google c++ 编码规范
- C++ 引用和指针 const与#define #define与内联函数
- 我的C++第二次实验报告
- C++重新学习心得(1)(《C++Primer第四版》1-8章)
- (水题)坐电梯时间(problem G)
- C++ 容器及选用总结
- 国外找的一个base64转码的函数,简单好用。
- C++中的各种容器实现原理