[C++] MyList

时间:2023-02-21 15:53:31

完成作业型。。。保证无bug,完全没考虑效率。

#include <iostream>
using namespace std; #define DEBUG
#ifdef DEBUG
#define ASSERT(expr) { \
if (!(expr)) { \
cout << "=============== ASSERT(" << #expr << ") failed at line " << __LINE__ \
<< " in function " << __FUNCTION__ << "() " << endl; \
throw(); \
} \
}
#else
#define ASSERT(expr)
#endif template<typename T>
class MyList {
private:
// copy 'cnt' elements from 'src' to 'dest'
// ignore if (cnt == 0)
static void __copy(T* dest, const T* src, int cnt) {
ASSERT(cnt >= );
if (!cnt || src == dest) return; if (src < dest) {
for (int i = cnt-; i >= ; --i)
dest[i] = src[i];
} else {
for (int i = ; i < cnt; ++i)
dest[i] = src[i];
}
} // reverse [start, end]
static void __reverse(T* start, T* end) {
ASSERT(start <= end);
for (int i = ; i <= (end-start)/; ++i) {
T tmp = start[i];
start[i] = end[-i];
end[-i] = tmp;
}
} // qsort [start, end]
// increasingly if (less == true), decreasingly otherwise
static void __qsort(T* start_elem, T* end_elem, bool less) {
T *st = start_elem, *ed = end_elem;
if (st >= ed) return;
const T pivot = *st;
if (less) {
while (st < ed) {
while (st < ed && *ed >= pivot) --ed;
*st = *ed;
while (st < ed && *st <= pivot) ++st;
*ed = *st;
}
} else {
while (st < ed) {
while (st < ed && *ed <= pivot) --ed;
*st = *ed;
while (st < ed && *st >= pivot) ++st;
*ed = *st;
}
}
*st = pivot;
__qsort(start_elem, st-, less);
__qsort(ed+, end_elem, less);
} // deep clone the list form 'src' to 'dest'
static void __clone(MyList* dest, const MyList* src) {
ASSERT(dest && src && dest != src);
dest->zero();
if (! src->m_Size) return; // 'src' is empty dest->initialize(src->m_Size);
__copy(dest->m_List, src->m_List, src->m_Size);
} private:
int m_Size; // current element count
int m_MaxSize; // max element count
T* m_List; // buffer private:
// allocate memory for 16 elements if (m_List == NULL)
// double m_List otherwise
void double_space(void) {
ASSERT(m_Size == m_MaxSize);
if (! m_MaxSize) { // not allocated before
ASSERT(m_List == NULL);
m_MaxSize = ;
m_List = new T[m_MaxSize];
return;
} ASSERT(m_List != NULL);
T* newList = new T[m_MaxSize*];
__copy(newList, m_List, m_Size);
delete[] m_List;
m_List = newList;
m_MaxSize *= ;
} // initialize this with a specific size
void initialize(int size) {
ASSERT(size > );
this->m_Size = this->m_MaxSize = size;
this->m_List = new T[size];
} // set 'this' with all-zero
void zero() {
m_Size = m_MaxSize = ;
m_List = (T*)NULL;
} public:
// constructor: a new empty list
MyList() {
this->zero();
} // constructor: with 'item' repeating 'num' times
MyList(int num, const T& item) {
this->zero();
// ASSERT(num >= 0)
if (num <= ) return; this->initialize(num);
for (int i = ; i < num; ++i) {
m_List[i] = item;
}
return;
} // copy constructor: deep clone from a list
// attention: avoid cloning from itself
MyList(const MyList& lst) {
// ASSERT(this != &lst);
if (this != &lst) {
__clone(this, &lst);
}
} // constructor: with first 'len' elements in 'arr'
MyList(T* arr, int len) {
this->zero(); // ASSERT(len >= 0);
if (len <= ) return;
ASSERT(arr != NULL); this->initialize(len);
__copy(m_List, arr, len);
} // destroctor: free the buffer is allcated before
~MyList() {
if (! m_List) {
ASSERT(m_Size == && m_MaxSize == );
return;
}
delete[] m_List;
this->zero();
} // insert 'item' at the position of 'index'
void insert(int index, const T& item) {
// 'push()' when (index == m_Size)
ASSERT(index >= && index <= m_Size);
if (m_Size == m_MaxSize)
this->double_space(); // __copy: ignored if (m_Size == index)
__copy(m_List+index+, m_List+index, m_Size-index);
m_List[index] = item;
++m_Size;
} // clear current list
void clean() {
m_Size = ;
} // push 'item' to end of the list
void push(const T& item) {
this->insert(m_Size, item); // insert: 'index' = m_Size
} // pop from end of the list
T pop() {
ASSERT(m_Size > );
return m_List[--m_Size];
} // get element count
int get_size() const {
return m_Size;
} // erase elements indexed [start, end]
void erase(int start, int end) {
ASSERT(start >= && end < m_Size);
ASSERT(start <= end); // __copy: ignore if (end == m_Size-1)
__copy(m_List+start, m_List+end+, m_Size--end);
m_Size -= end-start+;
} // get the reference of element indexed 'index'
T& operator [](int index) const {
ASSERT(index >= && index < m_Size);
return m_List[index];
} // get the element indexed 'index'
T get_item(int index) const {
ASSERT(index >= && index < m_Size);
return m_List[index];
} // pick elements index [start, end] to form a new list
// if (end == -1), then pick from 'start' to the last element.
MyList get_item(int start, int end) const {
// specially treat when (end = -1)
if (end == -) {
end = m_Size-;
} // ASSERT(start <= end && start >= 0 && end < m_Size);
if (start <= end && start >= && end < m_Size) {
// if this is empty, 0 <= start <= end < m_Size = 0 is impossible
return MyList(m_List+start, end-start+);
}
return MyList(); // empty
} // count the element 'item' in the list
int count(const T& item) const {
int ret = ;
for (int i = ; i < m_Size; ++i) {
if (m_List[i] == item) ++ret;
}
return ret;
} // remove the element 'item' in the list (at the first present)
void remove(const T& item) {
for (int i = ; i < m_Size; ++i) {
if (m_List[i] == item) {
this->erase(i, i);
return;
}
}
} // like copy construtor, deep clone from 'lst'
// attention: avoid cloning from itself
MyList& operator =(const MyList& lst) {
if (this != &lst) { // a = a
__clone(this, &lst);
}
return *this;
} // add the element 'item' to the end of this list
MyList& operator +=(const T& item) {
this->push(item);
return *this;
} // add the list 'lst' to the end of this list
MyList& operator +=(const MyList& lst) {
MyList tmp = lst;
for (int i = ; i < tmp.m_Size; ++i) {
this->push(tmp.m_List[i]);
}
return *this;
} // sort current list
// increasingly if (less == true), decreasingly otherwise
void sort(bool less = true) {
if (! m_Size) return;
ASSERT(m_List != NULL); __qsort(m_List, m_List + m_Size-, less);
} // reverse current list
void reverse() {
if (! m_Size) return;
ASSERT(m_List != NULL); __reverse(m_List, m_List + m_Size-);
} // merge two list to a new list
template<typename _T>
friend MyList<_T> operator +(const MyList<_T>&, const MyList<_T>&); // merge a list and an element to a new list
template<typename _T>
friend MyList<_T> operator +(const MyList<_T>&, const _T&); // output the list to an ostream
template<typename _T>
friend ostream& operator <<(ostream&, const MyList<_T>&);
}; template<typename T>
MyList<T> operator +(const MyList<T>& lst1, const MyList<T>& lst2) {
MyList<T> ret = lst1;
ret += lst2;
return ret;
} template<typename T>
MyList<T> operator +(const MyList<T>& lst, const T& item) {
MyList<T> ret = lst;
ret += item;
return ret;
} template<typename T>
ostream& operator <<(ostream& os, const MyList<T>& lst) {
os << "[";
for (int i = ; i < lst.m_Size; ++i) {
if (i) os << ", ";
os << lst.m_List[i];
}
os << "]";
return os;
} int main()
{
MyList<int> a, b;
for(int i = ; i < ; ++i) {
a.push(i);
}
// a = [0, 1, 2, 3, 4] a[] = ; // a = [0, 1, 2, 15, 4]
a.sort(); // a = [0, 1, 2, 4, 15]
a.reverse(); // a = [15, 4, 2, 1, 0]
a += ; // a = [15, 4, 2, 1, 0, 12]
for(int i = ; i < a.get_size(); ++i) {
cout << a[i] << endl;
} b = a.get_item(, -); // b = [] *若start > end,返回空数组
b = a.get_item(, -); // b = [1, 0, 12]
a += b; // a = [15, 4, 2, 1, 0, 12, 1, 0, 12]
for(int i = ; i < a.get_size(); ++i)
cout << a.get_item(i) << endl;
cout << a.count() << endl; b.clean(); // b = []
cout << b.get_size() << endl; a.erase(, ); // a = [15, 4, 1, 0, 12]
b = a + a; // b = [15, 4, 1, 0, 12, 15, 4, 1, 0, 12]
b.insert(, ); // b = [15, 4, 1, 116, 0, 12, 15, 4, 1, 0, 12]
b.remove(); // b = [15, 1, 116, 0, 12, 15, 4, 1, 0, 12]
cout << b << endl; MyList<double> c(, 3.14);
for(int i = ; i < ; ++i)
c.push(1.1 * i);
cout << c.get_item(, ) << endl; return ;
}

[C++] MyList<T>的更多相关文章

  1. 【jq】c&num;零基础学习之路(5)自己编写简单的Mylist&lt&semi;T&gt&semi;

    public class MyList<T> where T : IComparable { private T[] array; private int count; public My ...

  2. 我的STL之旅 MyList

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> // ...

  3. MyList 泛型委托

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  4. 仿照ArrayList自己生成的MyList对象

    现在需要自己生成一个list集合,基本雷同ArrayList,不使用API的List接口. 实现如下: MyList的代码: public class MyList<T> { privat ...

  5. &num;学号 20175201张驰 《Java程序设计》第10周课下作业MyList

    参见附件,补充MyList.java的内容,提交运行结果截图(全屏) 课下推送代码到码云 public class MyList { public static void main(String [] ...

  6. List myList&equals;new ArrayList()的理解

    ArrayList不是继承List接口,是实现了List接口.你写成ArrayList arrayList = new ArrayList();这样不会有任何问题.和List list = new A ...

  7. 写一个MyList

    首先定义接口 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  8. 36&period;创建模板mylist

    node.h #pragma once //创建模板 template <class T> class Node { public: T t;//数据 Node *pNext;//指针域 ...

  9. Redis数据库

    Redis是k-v型数据库的典范,设计思想及数据结构实现都值得学习. 1.数据类型 value支持五种数据类型:1.字符串(strings)2.字符串列表(lists)3.字符串集合(sets)4.有 ...

随机推荐

  1. 利用js来实现文字的滚动(也就是我们常常见到的新闻版块中的公示公告)

    首先先看一下大致效果图(因为是动态的,在页面无法显示出来) 具体的实现代码如下: 1.首先是css代码: <style type="text/css"> body,ul ...

  2. 关于同一台机器上安装多个sql实例的连接方法

    由于客户需要在一台服务器上安装了两个sql服务器(一个sql2000,一个是sql2005,其实例名不同),默认的端口1433被先安装的sql2000使用,后来安装的的随机启用了一个3045端口.其中 ...

  3. Swift开发第六篇——操作运算符也可以重载&amp&semi; func 的参数修饰

    本篇分为两部分: 1.Swift 中重载操作运算符的使用 2.Swfit 中 func 的参数修饰 1.Swift 中重载操作运算符的使用 与别的语言不同,Swift 支持运算符的重载,运算符指的是“ ...

  4. Unity发送参数给iOSNative并响应

    unity想要给iOS客户端发送通知并相应.语言太苍白直接上代码. unity端创建两个C#文件 1.触发cs这个不用多说,大家估计都懂. using UnityEngine; using Syste ...

  5. 实现在线阅读pdf功能--php

    在网上找了很久,想要实现一个在线阅读word,pdf文件的功能,网上的资料很多,但是提到真正怎么实现的比较少.现在我来简单说明一下,我实现的过程. 我现在只能实现在线阅读pdf(将word等转换成pd ...

  6. Tomcat 对 HTTP 协议的实现(下)

    在<Tomcat 对 HTTP 协议的实现(上)>一文中,对请求的解析进行了分析,接下来对 Tomcat 生成响应的设计和实现继续分析.本文首发于(微信公众号:顿悟源码) 一般 Servl ...

  7. http 协议&lowbar;DNS&lowbar;域名解析 DNS 服务器&lowbar;内容分发网络 CDN&lowbar;缓存机制&lowbar;HTML5 浏览器存储技术&lowbar;cookie&lowbar;sessionStorage&lowbar;localStorage

    TCP/IP 协议族 是按层次去划分的 应用层    决定了向用户提供应用服务时通信的活动. FTP 协议(文件传输协议)DNS(域名协议)HTTP(超文本传输协议) 传输层    提供处于网络连接中 ...

  8. 20145236《网络对抗》Exp8 WEB基础实践

    20145236<网路对抗>Exp8 WEB基础实践 一.基础问题回答 什么是表单 表单在网页中主要负责数据采集功能 一个表单有三个基本组成部分: 表单标签 表单域:包含了文本框.密码框. ...

  9. Jersey 2&period;x 运行项目

    现在我们已经有可以可以运行的项目了,让我们队这个项目进行一些测试吧. 你需要运行下面的一些命令行: mvn clean test 这个命令将会对项目进行编译后运行单元测试. 你应该会看到和下面类似的输 ...

  10. Android Studio2&period;3&period;3卡在Building 'xxx' Gradle project info的解决方法

    Android Studio版本:V2.3.3 操作系统环境:Ubuntu14.04  64bit 新安装好Android Studio后,在创建新的项目时或者在导入他人的项目代码时,Android ...