欢迎来到入门教程网!

C语言

当前位置:主页 > 软件编程 > C语言 >

C++利用静态成员或类模板构建链表的方法讲解

来源:本站原创|时间:2020-01-10|栏目:C语言|点击:

直接上代码了,说明看注释就可以:

利用静态成员构建链表

#include <IOSTREAM.H> 
 
class Node 
{ 
public: 
  Node(int val, Node* next):val(val),next(next){} 
  //~Node(){cout<<"del "<<val<<endl;} 
  static void showAll();//打印全部节点的值 
  static void insertHead(int);//头插 
  static void insertTail(int);//尾插 
  static void delHead();//删头 
  static void delTail();//删尾 
  static void clear();//清空 
protected: 
  int val; 
  Node *next; 
  static Node *head; 
private: 
}; 
 
Node* Node::head = 0; 
 
void Node::showAll(){//打印全部节点的值 
  Node *p = head; 
  while (p) 
  { 
    cout<<p->val<<" "; 
    p = p->next; 
  } 
  cout<<endl; 
} 
 
void Node::insertHead(int val){//头插 
  Node *p = new Node(val, head); 
  head = p; 
} 
 
void Node::insertTail(int val){//尾插 
  Node *p = new Node(val, 0); 
  if (!head) 
  { 
    head = p; 
    return; 
  } 
  Node *q = head; 
  while (q->next) 
  { 
    q = q->next; 
  } 
  q->next = p; 
} 
 
void Node::delHead(){//删头 
  Node *p = head; 
  if (head) 
  { 
    head = head->next; 
    delete p; 
  } 
} 
 
void Node::delTail(){//删尾 
  if (!head) 
  { 
    return; 
  } 
  if (!(head->next)) 
  { 
    delete(head); 
    head = NULL; 
    return; 
  } 
  Node *p = head; 
  while (p->next->next) 
  { 
    p = p->next; 
  } 
  delete(p->next); 
  p->next = NULL; 
} 
 
void Node::clear(){//清空 
  Node *p = head; 
  Node *q = 0; 
  head = 0; 
  while (p) 
  { 
    q = p; 
    p = p->next; 
    delete q; 
  } 
} 
 
void main(){ 
  Node::delHead(); 
  Node::delTail(); 
  Node::insertTail(2); 
  Node::delTail(); 
  for (int i = 0; i < 10; i++) 
  { 
    Node::insertTail(i + 1); 
  } 
  Node::delTail(); 
  Node::showAll(); 
} 

利用类模板构建链表
这有点类似于list<>:

#include <iostream> 
#include <string> 
using namespace std; 
 
template<class T> class Node//创建一个类模板,一个可以放入任何类型节点的链表 
{ 
public: 
  Node(T val, Node* next):val(val),next(next){} 
  static void showAll();//打印全部节点的值 
  static void insertHead(T);//头插 
  static void insertTail(T);//尾插 
  static void delHead();//删头 
  static void delTail();//删尾 
  static void clear();//清空 
protected: 
  T val; 
  Node *next; 
  static Node *head; 
private: 
}; 
 
template<class T> Node<T>* Node<T>::head = 0; 
 
template<class T> void Node<T>::showAll(){//打印全部节点的值 
  Node *p = head; 
  while (p) 
  { 
    cout<<p->val<<" "; 
    p = p->next; 
  } 
  cout<<endl; 
} 
 
template<class T> void Node<T>::insertHead(T val){//头插 
  Node *p = new Node(val, head); 
  head = p; 
} 
 
template<class T> void Node<T>::insertTail(T val){//尾插 
  Node *p = new Node(val, 0); 
  if (!head) 
  { 
    head = p; 
    return; 
  } 
  Node *q = head; 
  while (q->next) 
  { 
    q = q->next; 
  } 
  q->next = p; 
} 
 
template<class T> void Node<T>::delHead(){//删头 
  Node *p = head; 
  if (head) 
  { 
    head = head->next; 
    delete p; 
  } 
} 
 
template<class T> void Node<T>::delTail(){//删尾 
  if (!head) 
  { 
    return; 
  } 
  if (!(head->next)) 
  { 
    delete(head); 
    head = NULL; 
    return; 
  } 
  Node *p = head; 
  while (p->next->next) 
  { 
    p = p->next; 
  } 
  delete(p->next); 
  p->next = NULL; 
} 
 
template<class T> void Node<T>::clear(){//清空 
  Node *p = head; 
  Node *q = 0; 
  head = 0; 
  while (p) 
  { 
    q = p; 
    p = p->next; 
    delete q; 
  } 
} 
 
class Student//创建一个自定义的学生类 
{ 
public: 
  Student(string name, int age,char sex):name(name), age(age), sex(sex){} 
  void showInfo(){ 
    cout<<"姓名:"<<name<<" 年龄:"<<age<<" 性别:"<<sex<<endl; 
  } 
protected: 
  string name; 
  int age; 
  char sex; 
private: 
}; 
 
void Node<Student>::showAll(){//学生类节点和其他基本数据类型不同,不能直接用<<输出,所以重载showAll() 
  Node *p = head; 
  while (p) 
  { 
    p->val.showInfo(); 
    p = p->next; 
  } 
} 
 
void main(){ 
  for (int i = 1; i < 10; i++) 
  { 
    Node<int>::insertTail(i);//这时Node<int>称为一个用类模板生成的模板类 
    Node<float>::insertTail(i / 10.0f); 
    Node<double>::insertTail(i / 10.00); 
    Node<Student>::insertTail(Student("stu", i, 'F')); 
  } 
  Node<int>::showAll(); 
  Node<float>::showAll(); 
  Node<double>::showAll(); 
  Node<Student>::showAll(); 
} 

上一篇:C++实现简单的职工管理系统实训代码

栏    目:C语言

下一篇:C/C++实现字符串模糊匹配

本文标题:C++利用静态成员或类模板构建链表的方法讲解

本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/2382.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有