基于C++类模板的链表
2016-11-07 23:28
302 查看
一、基于C++类模板定义一个链表,管理学生成绩信息( 学号和成绩),进行排序后再输出。
程序结果
二、编写程序,从键盘输入若干整形数据,以单链表的形式存储并按升序排列,再向其中插入任一整形数据,重新按照升序方式输出。
结果
#include #include using namespace std; template class list{ struct node{ T data; node *next; } *head, *tail; public: list(){ head = tail = NULL; }; void Insert(T *item){ node *pn; pn = new node; pn->next + head; pn->data = *item; head = pn; if (tail == NULL){ tail = pn; } }; void Append(T *item){ node *pn; pn = new node; pn->next = NULL; pn->data = *item; if (tail == NULL){ head = tail = pn; } else{ tail->next = pn; tail = pn; } }; T Get(){ if (head == NULL){ return 0; } T tmp = head->data; node *pn = head; if (head->next == NULL){ head = tail = NULL; } else{ head = head->next; } delete(pn); return tmp; } }; class Student{ public: int num; int score; }; int main(){ Student s; listlink1; listlink2; for (int i = 0; i < 5; i++){ cin >> s.num >> s.score; link2.Insert(&s); link1.Append(&s.score); } cout<<"result"<
程序结果
二、编写程序,从键盘输入若干整形数据,以单链表的形式存储并按升序排列,再向其中插入任一整形数据,重新按照升序方式输出。
#include using namespace std; class List{ protected: int length; public: int GetLength(){ return length; } virtual int Get(int i) = 0; virtual bool Set(int x, int i) = 0; virtual void MakeEmpty() = 0; virtual bool Insert(int value, int i) = 0; virtual int Remove(int i) = 0; virtual int RemoveValue(int value) = 0; }; class Node{ public: Node(){ next = NULL; } Node(const int &item, Node *next1 = NULL){ data = item; next = next1; } int data; Node *next; }; class Linklist :public List{ protected: Node *head; public: Node *GetHead(){ return head; } Node *GetNext(Node &n){ if (n.next == head){ n.next->next; } else{ return n.next; } } int Get(int i){ Node *p = Find(i); return p->data; } bool Set(int x, int i){ Node *p = Find(i); if (p == NULL || p == head){ return false; } else{ p->data = x; } return true; } Node *Find(int i){ if (i<0 || i>length){ return NULL; } if (i=0){ return head; } Node *p = head->next; int j = 1; while(p!=NULL&&j<1){ p = p->next; j++; } return p; } Node *FindValue(int value){ Node *p = head->next; int i = 1; while (i++ <= length&&p->data!=value){ p = p->next; } return p; } void MakeEmpty(){ Node *q = head->next; int i = 1; while (i++ <= length){ head->next=q->next; delete q; q = head->next; } length = 0; } virtual bool Insert(int value, int i) = 0; virtual int Remove(int i) = 0; virtual int RemoveValue(int value) = 0; }; class Singlelist:public Linklist{ public: Singlelist(){ head = new Node; length = 0; } Singlelist(Singlelist &l){ Copy(l); } ~Singlelist(){ MakeEmpty(); delete head; } Singlelist &Copy(Singlelist &l){ Node *p, *q, *r; head = NULL; length = l.length; if (!l.head){ return *this; } head = new Node; if (!l.head){ return *this; } head->data = (l.head)->data; head-> next = NULL; r = NULL; p = head; q = l.head->next; while (q){ r = new Node; if (!r){ return *this; } r->data = q->data; r->next = NULL; p->next = r; p = p->next; q = q->next; } return *this; } bool Insert(int value, int i){ Node *p = Find(i - 1); if (p == NULL){ return false; } Node *newnode = new Node(value, p->next); p->next = newnode; length++; return true; } int Remove(int i){ Node *p = Find(i - 1),*q; q = p->next; p->next = q->next; int value = q->data; delete q; length--; return value; } int RemoveValue(int value){ Node *p = head, *q; while (p->next != NULL&&p->next->data != value){ p = p->next; } q = p->next; p->next = q->next; delete q; length--; return value; } Singlelist &operator=(Singlelist &l){ if (head){ MakeEmpty(); } Copy(l); return *this; } friend ostream &operator << (ostream &out, Singlelist &l){ Node *p = l.head->next; while (p){ out << p->data << " "; p->next; } out << endl; return out; } void sort(){ Node *tmp = head; while (tmp != NULL){ Node *q = tmp->next; while (q != NULL){ if (q->data > tmp->data){ int r = tmp->data; tmp->data = q->data; q->data = r; } } tmp = tmp->next; } } }; int main(){ Singlelist a; int i,t,x,length; cout << "input length"<> length; cout << "input data" << endl; for (i = 0; i <= length; i++){ cin >> t; a.Insert(t, i); } a.sort(); cout << "after sort" << endl; cout << a << endl; cout << "input another one" << endl; cin >> x; a.Insert(x, length++); cout << "after sort" << endl; cout<
结果
相关文章推荐
- 基于模板、全局特化、局部特化实现的链表
- C++类模板实现单链表
- C++类模板实现双链表
- 模板容器类及迭代器的实现二(基于链表)
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- 双向链表的C++类模板实现
- 基于C++模板 单链表基本操作
- 数据结构之链表 (C++类模板实现)
- C++类模板实现双链表
- 基于空项目模板创建使用Owin来host的WebApi项目
- 二叉树 链表实现 模板
- [置顶] 基于gnu-arm-linux的LPC2220的简单工程模板
- 收集的一个响应式基于bootstrap3的Color Admin 1.7后台模板
- 基于链表的栈和队列
- C++类模板的成员函数模板写法
- 网上流传的 基于MySQL 5.6和5.7的配置文件模板
- 模板实现双向链表
- C++模板实现动态顺序表(更深层次的深浅拷贝)与基于顺序表的简单栈的实现
- 创建Zigbee Z-Stack 3.0.1 BSP(三)--创建基于UltraIot工程模板
- Python之自动生成解析ini文件的C++类(基于libiniparser.a)