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

C++中实现队列类链式存储与栈类链式存储的代码示例

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

C++中实现队列类链式存储与栈类链式存储的代码示例

队列类链式存储

代码:
linkqueue.hpp 

// 队列类 
 
#pragma once 
 
#include "linklist.hpp" 
 
template  
class linkQueue 
{ 
public: 
  linkQueue(); 
  ~linkQueue(); 
public: 
  int clear(); 
  int append(T &t); 
  int retieve(T &t); 
  int header(T &t); 
  int length(); 
protected: 
  linkList *m_list; 
}; 
 
template  
linkQueue::linkQueue() 
{ 
  m_list = new linkList < T > ; 
} 
 
template  
linkQueue::~linkQueue() 
{ 
  clear(); 
  delete m_list; 
  m_list = NULL; 
} 
 
template  
int linkQueue::clear() 
{ 
  T t; 
  while (m_list->getLen() > 0) { 
    m_list->del(0, t); 
  } 
  return 0; 
} 
 
template  
int linkQueue::append(T &t) 
{ 
  return m_list->insert(t, m_list->getLen()); 
} 
 
template  
int linkQueue::retieve(T &t) 
{ 
  return m_list->del(m_list->getLen() - 1, t); 
} 
 
template  
int linkQueue::header(T &t) 
{ 
  return m_list->get(0, t); 
} 
 
template  
int linkQueue::length() 
{ 
  return m_list->getLen(); 
} 

main.cpp 

// 队列类测试程序 
 
#include  
#include  
#include "linkqueue.hpp" 
 
using namespace std; 
 
struct Student 
{ 
  char name[32]; 
  int age; 
}; 
 
void play() 
{ 
  Student s1, s2, s3; 
  s1.age = 21; 
  s2.age = 22; 
  s3.age = 23; 
 
  linkQueue lq; // 创建队列 
  lq.append(s1); // 入队列 
  lq.append(s2); 
  lq.append(s3); 
 
  Student tmp; 
  lq.header(tmp); 
  cout << "header of queue: " << tmp.age << endl; 
  cout << "length of queue: " << lq.length() << endl; 
 
  while (lq.length() > 0) { 
    lq.retieve(tmp); 
    cout << tmp.age << " "; 
  } 
  cout << endl; 
 
  lq.clear(); 
 
} 
 
int main() 
{ 
  play(); 
 
  return 0; 
} 


栈类链式存储

linkstack.hpp 

// 栈类 
 
#pragma once 
 
#include "linklist.hpp" 
 
template  
class linkStack 
{ 
public: 
  linkStack(); 
  ~linkStack(); 
public: 
  int clear(); 
  int push(T &t); 
  int pop(T &t); 
  int top(T &t); 
  int size(); 
protected: 
  linkList *m_list; 
}; 
 
template  
linkStack::linkStack() 
{ 
  m_list = new linkList < T > ; 
} 
 
template  
linkStack::~linkStack() 
{ 
  clear(); 
  delete m_list; 
  m_list = NULL; 
} 
 
template  
int linkStack::clear() 
{ 
  T t; 
  while (m_list->getLen() > 0) { 
    m_list->del(0, t); 
  } 
 
  return 0; 
} 
 
template  
int linkStack::push(T &t) 
{ 
  return m_list->insert(t, 0); 
} 
 
template  
int linkStack::pop(T &t) 
{ 
  return m_list->del(0, t); 
} 
 
template  
int linkStack::top(T &t) 
{ 
  return m_list->get(0, t); 
} 
 
template  
int linkStack::size() 
{ 
  return m_list->getLen(); 
} 

main.cpp 

// 链式存储栈类的测试程序 
 
#include  
#include  
#include "linkstack.hpp" 
 
using namespace std; 
 
struct Student 
{ 
  char name[32]; 
  int age; 
}; 
 
void play() 
{ 
  Student s1, s2, s3; 
  s1.age = 21; 
  s2.age = 22; 
  s3.age = 23; 
 
  linkStack ls; // 创建栈 
 
  // 入栈 
  ls.push(s1); 
  ls.push(s2); 
  ls.push(s3); 
 
  // 获取栈顶元素 
  Student tmp; 
  ls.top(tmp); 
  cout << "top of stack: " << tmp.age << endl; 
  cout << "size of stack: " << ls.size() << endl; 
 
  // 出栈 
  while (ls.size() > 0) { 
    ls.pop(tmp); 
  } 
 
  ls.clear(); 
 
} 
 
int main() 
{ 
  play(); 
 
  return 0; 
} 

linklist.h 

// 链表类 
 
#pragma once 
 
#include  
#include  
using namespace std; 
 
template  
struct Node 
{ 
  T t; 
  Node *next; 
}; 
 
template  
class linkList 
{ 
public: 
  linkList(); 
  ~linkList(); 
 
public: 
  int clear(); 
  int insert(T &t, int pos); 
  int get(int pos, T &t); 
  int del(int pos, T &t); 
  int getLen(); 
 
protected: 
  Node *header; 
  int length; 
}; 
 
template  
linkList::linkList() 
{ 
  header = new Node < T > ; 
  header->next = NULL; 
  length = 0; 
} 
 
template  
linkList::~linkList() 
{ 
  Node *tmp = NULL; 
 
  while (header) { 
    tmp = header->next; 
    delete header; 
    header = tmp; 
  } 
} 
 
template  
int linkList::clear() 
{ 
  ~linkList(); 
  linkList(); 
  return 0; 
} 
 
template  
int linkList::insert(T &t, int pos) 
{ 
  Node *cur = NULL; 
 
  // 对pos的容错处理 
  if (pos >= length) { 
    pos = length; 
  } 
 
  cur = header; 
  for (int i = 0; i < pos; ++i) { 
    cur = cur->next; 
  } 
 
  // 把上层应用的t结点缓存到容器中 
  Node *node = new Node < T > ; 
  node->next = NULL; 
  node->t = t; // 把t缓存到容器中 
 
  node->next = cur->next; 
  cur->next = node; 
 
  ++length; 
 
  return 0; 
} 
 
template  
int linkList::get(int pos, T &t) 
{ 
  Node *cur = NULL; 
 
  if (pos >= length) { 
    return -1; 
  } 
 
  cur = header; 
  for (int i = 0; i < pos; ++i) { 
    cur = cur->next; 
  } 
 
  t = cur->next->t; // 把pos位置的结点赋值给t 
 
  return 0; 
} 
 
template  
int linkList::del(int pos, T &t) 
{ 
  Node *cur = NULL; 
 
  if (pos >= length) { 
    return -1; 
  } 
 
  cur = header; 
  for (int i = 0; i < pos; ++i) { 
    cur = cur->next; 
  } 
  Node *ret = NULL; 
  ret = cur->next; 
  t = ret->t; // 把缓存的结点给上层应用t 
 
  // 删除操作 
  cur->next = ret->next; 
  --length; 
  delete ret; // 注意释放内存,因为insert的时候new Node 
 
  return 0; 
} 
 
template  
int linkList::getLen() 
{ 
  return length; 
} 

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

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

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