全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

利用C++简单实现顺序表和单链表的示例代码

本文主要给大家介绍了关于C++实现顺序表和单链表的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍:

一、顺序表示例代码:

#include <assert.h>
#include <iostream>
using namespace std;

typedef int Datatype;

class SeqList
{
public:
 SeqList()
  :_array(NULL)
  ,_size(0)
  ,_capacity(0)
 {
 }

 SeqList(const SeqList& s)
 {
  _array = (Datatype*)malloc(s._size*(sizeof(Datatype)));
  memcpy(_array, s._array, s._size*(sizeof(Datatype)));
  _size = _capacity = s._size;
 }

 SeqList& operator=(SeqList& s)
 {
  free(_array);
  Swap(s);
  return *this;
 }

 void Swap(SeqList& s)
 {
  _array = s._array;
  _size = s._size;
  _capacity = s._capacity;
 }

 ~SeqList()
 {
  if (_array)
  {
   free(_array);
   _array = NULL;
   _size = _capacity = 0;
  }
 }

 void Print()
 {
  for (size_t i = 0; i < _size; i++)
  {
   cout << _array[i] << " ";
  }
  cout << endl;
 }

 void CheckCapcacity()
 {
  if (_size == _capacity)
  {
   _capacity = 2 * _capacity + 3;
   _array = (Datatype*)realloc(_array, _capacity*sizeof(Datatype));
   assert(_array);
  }
 }

 //后插
 void PushBack(Datatype x)
 {
  Insert(_size, x);
 }

 //前插
 void PushFront(Datatype x)
 {
  Insert(0, x);
 }

 //删除最后一个
 void PopBack()
 {
  Erase(_size);
 }

 //删除第一个
 void PopFront()
 {
  Erase(0);
 }

 //[]运算符重载
 Datatype& operator[](size_t pos)
 {
  assert(pos < _size);
  return _array[pos];
 }

 //pos位置前插入x
 void Insert(size_t pos, Datatype x)
 {
  assert(pos <= _size);
  CheckCapcacity();
  int end = (int)_size - 1;
  if (pos == 0)
  {
   while (end >= 0)
   {
    _array[end + 1] = _array[end];
    end--;
   }
   _array[0] = x;
  }
  else
  {
   while (end >= (int)pos)
   {
    _array[end + 1] = _array[end];
    end--;
   }
   _array[pos] = x;
  }
  _size++;
 }

 //删除pos位置的元素
 void Erase(size_t pos)
 {
  assert(pos < _size);
  //popfront的实现
  if (_size > 0)
  {
   if (pos == 0)
   {
    int end = 0;
    while (end < (int)_size - 1)
    {
     _array[end] = _array[end + 1];
     end++;
    }
    _size--;
   }
   //popback的实现
   else if (pos == _size)
   {
    _size--;
   }
   //erase
   else
   {
    int end = pos;
    while (end < (int)_size - 1)
    {
     _array[end] = _array[end + 1];
     end++;
    }
    _size--;
   }
  }
  return; 
 }

private:
 Datatype* _array;
 size_t _size;
 size_t _capacity;
};

二、单链表(不含头结点)示例代码

#include <iostream>
#include <assert.h>
using namespace std;

typedef int DataType;

struct SListNode
{
 SListNode* _next;
 DataType _data;

 SListNode(DataType x)
  :_data(x)
  , _next(NULL)
 {}
};

typedef SListNode Node;

class SList
{
public:
 SList()
  :_head(NULL)
  , _tail(NULL)
 {}

 SList(const SList& s)
  :_head(NULL)
  ,_tail(NULL)
 {
  Copy(s);
 }

 SList& operator=(const SList& s)
 {
  Destroy();
  Copy(s);
  return *this;
 }

 ~SList()
 {
  Destroy();
 }


 void Copy(const SList& s)
 {
  Node* cur = s._head;
  while (cur)
  {
   PushBack(cur->_data);
   cur = cur->_next;
  }
 }

 void Destroy()
 {
  Node* cur = _head;
  while (_head != NULL)
  {
   cur = _head;
   _head = cur->_next;
   delete cur;
  }
  _head = _tail = NULL;
 }

 void PushBack(DataType x)
 {
  if ((_head == NULL)&&(_tail == NULL))
  {
   _head = _tail = new Node(x);
  }
  else
  {
   _tail->_next = new Node(x);
   _tail = _tail->_next;
  }
 }

 void PopBack()
 {
  if (_head == NULL)
  {
   return;
  }
  else if (_head ->_next == NULL)
  {
   delete _head;
   _head = _tail = NULL;
  }
  else
  {
   Node* tmp = _head;
   while (tmp->_next->_next != NULL)
   {
    tmp = tmp->_next;
   }
   _tail = tmp;
   tmp->_next = NULL;
  }  
 }

 void PushFront(DataType x)
 {
  if ((_head == NULL) && (_tail == NULL))
  {
   _head = _tail = new Node(x);
  }
  else
  {
   Node* tmp = new Node(x);
   tmp->_next = _head;
   _head = tmp;
  }
 }

 void PopFront()
 {
  if (_head == NULL)
  {
   return;
  }
  Node* cur = _head;
  _head = _head->_next;
  delete cur;
 }

 Node* Find(DataType x)
 {
  Node* tmp = _head;
  while (tmp)
  {
   if (tmp->_data == x)
    return tmp;
   tmp = tmp->_next;
  }
  return NULL;
 }

 // 插入一个节点在pos的前面
 void Insert(Node* pos, DataType x)
 {
  assert(pos);
  if (pos == 0)
  {
   PushFront(x);
  }
  else
  {
   Node* cur = _head;
   while (cur->_next != pos)
   {
    cur = cur->_next;
   }
   Node* tmp = new Node(x);
   tmp->_next = pos;
   cur->_next = tmp;
  }
 }

 void Erase(Node* pos)
 {
  assert(pos);
  if (pos == 0)
  {
   PopFront();
  }
  else if (pos->_next == NULL)
  {
   PopBack();
  }
  else
  {
   Node* cur = _head;
   while (cur->_next != pos)
   {
    cur = cur->_next;
   }
   Node* tmp = cur->_next;
   cur->_next = tmp->_next;
   delete tmp;
  }
 }

 void Print()
 {
  Node* tmp = _head;
  while (tmp != NULL)
  {
   cout <<tmp->_data << "->";
   tmp= tmp->_next;
  }
  cout <<"NULL"<<endl;
 }

private:
 Node* _head;
 Node* _tail;
};

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持


# c  # 单链表  # 实现顺序表  # c语言单链表  # C++顺序表的基本操作实现  # C++实现数据结构的顺序表详解  # C++顺序表实现图书管理系统  # C++实现基于静态数组的顺序表  # C++实现动态顺序表(vector)  # C++实现动态顺序表  # C++实现顺序表的常用操作(插入删出查找输出)  # C++实现顺序表的方法  # C++超详细分析顺序表  # 链表  # 相关内容  # 第一个  # 给大家  # 不含  # 这篇文章  # 谢谢大家  # 多说  # 运算符  # 有疑问  # operator  # public  # SeqList  # Datatype  # const  # typedef  # int  # amp  # memcpy  # sizeof 


相关文章: 韩国服务器如何优化跨境访问实现高效连接?  北京制作网站的公司排名,北京三快科技有限公司是做什么?北京三快科技?  如何在Golang中处理模块冲突_解决依赖版本不兼容问题  最好的网站制作公司,网购哪个网站口碑最好,推荐几个?谢谢?  制作电商网页,电商供应链怎么做?  微信小程序制作网站有哪些,微信小程序需要做网站吗?  建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析  制作网站外包平台,自动化接单网站有哪些?  如何做静态网页,sublimetext3.0制作静态网页?  c# Task.ConfigureAwait(true) 在什么场景下是必须的  建站上传速度慢?如何优化加速网站加载效率?  ,有什么在线背英语单词效率比较高的网站?  安徽网站建设与外贸建站服务专业定制方案  建站之星手机一键生成:多端自适应+小程序开发快速建站指南  如何快速搭建高效香港服务器网站?  网站制作费用多少钱,一个网站的运营,需要哪些费用?  如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法  深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?  如何在IIS中配置站点IP、端口及主机头?  巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成  TestNG的testng.xml配置文件怎么写  代购小票制作网站有哪些,购物小票的简要说明?  免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?  制作网站建设的公司有哪些,网站建设比较好的公司都有哪些?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?  如何高效利用200m空间完成建站?  建站之星安装模板失败:服务器环境不兼容?  网站按钮制作软件,如何实现网页中按钮的自动点击?  如何在Tomcat中配置并部署网站项目?  Swift中循环语句中的转移语句 break 和 continue  如何选择网络建站服务器?高效建站必看指南  如何自定义建站之星模板颜色并下载新样式?  宝盒自助建站智能生成技巧:SEO优化与关键词设置指南  公司网站设计制作厂家,怎么创建自己的一个网站?  大连网站设计制作招聘信息,大连投诉网站有哪些?  网站制作新手教程,新手建设一个网站需要注意些什么?  如何高效完成自助建站业务培训?  较简单的网站制作软件有哪些,手机版网页制作用什么软件?  网站图片在线制作软件,怎么在图片上做链接?  微信小程序 input输入框控件详解及实例(多种示例)  开源网站制作软件,开源网站什么意思?    详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)  如何在阿里云域名上完成建站全流程?  北京建设网站制作公司,北京古代建筑博物馆预约官网?  公司网站制作费用多少,为公司建立一个网站需要哪些费用?  如何零基础开发自助建站系统?完整教程解析  香港服务器租用每月最低只需15元?  宝塔Windows建站如何避免显示默认IIS页面?  如何在IIS7中新建站点?详细步骤解析 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。