栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

利用静态成员构建链表

#include  
 
class Node 
{ 
public: 
  Node(int val, Node* next):val(val),next(next){} 
  //~Node(){cout<<"del "<val<<" "; 
    p = p->next; 
  } 
  cout<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  
#include  
using namespace std; 
 
template 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 Node* Node::head = 0; 
 
template void Node::showAll(){//打印全部节点的值 
  Node *p = head; 
  while (p) 
  { 
    cout<val<<" "; 
    p = p->next; 
  } 
  cout< void Node::insertHead(T val){//头插 
  Node *p = new Node(val, head); 
  head = p; 
} 
 
template void Node::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 void Node::delHead(){//删头 
  Node *p = head; 
  if (head) 
  { 
    head = head->next; 
    delete p; 
  } 
} 
 
template 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; 
} 
 
template void Node::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<<"姓名:"<val.showInfo(); 
    p = p->next; 
  } 
} 
 
void main(){ 
  for (int i = 1; i < 10; i++) 
  { 
    Node::insertTail(i);//这时Node称为一个用类模板生成的模板类 
    Node::insertTail(i / 10.0f); 
    Node::insertTail(i / 10.00); 
    Node::insertTail(Student("stu", i, 'F')); 
  } 
  Node::showAll(); 
  Node::showAll(); 
  Node::showAll(); 
  Node::showAll(); 
} 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/64482.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号