C++:Vector和List的实现

Vector的实现

//test.h
#pragma once#include <iostream>
#include <cstdio>
#include <string.h>
#include <assert.h>using namespace std;typedef int DataType;#define TESTHEADER printf("\n================%s===============\n", __FUNCTION__)

class Vector
{
public:Vector();Vector(const Vector& v);~Vector();Vector& operator = (Vector& v);void Swap(Vector& v);size_t Size()const;size_t Capacity()const;void Reserve(size_t n);void Resize(size_t n, DataType);DataType& operator[](size_t pos);void PushBack(DataType v);void PopBack();
private:void Expand(size_t n);DataType* _start;DataType* _finish;DataType* _endofstorage;
};//test.cc
/***构造函数**/
#include "test.h"Vector::Vector():_start(NULL),_finish(NULL),_endofstorage(NULL)
{
}/*** 拷贝构造函数**/
//v(v1)
Vector::Vector(const Vector& v)
{_start = new DataType[v.Size()];memcpy(_start, v._start, v.Size() * sizeof(DataType));_finish = _start + v.Size();_endofstorage = _start + v.Size();
}/*** 扩展空间**/
void Vector::Expand(size_t n)
{if(n > Capacity()){size_t size = Size();DataType* tmp = new DataType [n];if(_start != NULL){memcpy(tmp, _start, size * sizeof(DataType));delete [] _start;}_start = tmp;_finish = _start + size;_endofstorage = _start + n;}
}/*** 预先分配空间并且初始化**/
void Vector::Resize(size_t n, DataType value = DataType())
{if(n <= Size()){_finish = _start + n;_endofstorage = _start + n;}else{if(n > Capacity()){Expand(n);size_t i = 0;for(; i < Size(); ++i){_start[i] = value;}}//end if(n > Capacity())}//end else
}/*** 预先分配空间但是不进行初始化**/
void Vector::Reserve(size_t n)
{Expand(n);
}/*** 尾插一个元素**/void Vector::PushBack(DataType v)
{if(_finish == _endofstorage){size_t new_capacity = Capacity() == 0 ? 3 : (Capacity() * 2);Expand(new_capacity);}*_finish = v;++_finish;
}/*** 交换内置类型(成员变量)**/
void Vector::Swap(Vector& v)
{swap(_start, v._start);swap(_finish, v._finish);swap(_endofstorage, v._endofstorage);
}/*** 赋值**/
//v1 = v2
//operator = (v1, v2)
Vector& Vector::operator = (Vector& v)
{Swap(v);return *this;
}//v[pos]
DataType& Vector::operator[](size_t pos)
{assert(pos < Size());return _start[pos];
}/*** 析构函数**/
Vector::~Vector()
{if(_start){delete [] _start;_start = _finish = _endofstorage = NULL;}
}/*** 返回Vector的有效字符个数**/
size_t Vector:: Size()const
{return _finish - _start;
}/*** Vector的容量**/
size_t Vector::Capacity()const
{return _endofstorage - _start;
}/*** 尾删一个元素**/
void Vector::PopBack()
{--_finish;
}

List的实现

//list.h
#pragma once
#include <iostream>
#include <cstdio>
#include <assert.h>
using namespace std;typedef int DataType;
struct ListNode
{DataType _data;ListNode* _next;ListNode* _prev;ListNode(DataType x):_data(x),_next(NULL),_prev(NULL){}
};class List
{typedef ListNode Node;
public:List();//构造函数~List();//析构函数List(const List& l);//拷贝构造List& operator = (List l);//赋值构造函数void PushBack(DataType l);//尾插函数void PushFront(DataType l);//头插函数void PopBack();//尾删函数void PopFront();//头删函数void Insert(Node* pos, DataType l);//往pos处插入一个结点void Erase(Node* pos);//删除某个位置上的某个结点void Print();void Clean();//清空
private:Node* _head;
};//test.cc
#include "list.h"/*** 构造函数**/
List::List()
{_head = new Node(DataType());_head -> _next = _head;_head -> _prev = _head;
}void List:: Insert(Node* pos, DataType l)
{assert(pos);Node* prev= pos -> _prev;Node* next = pos;Node* new_node = new Node(l);//new_node VS next;new_node -> _next = next;next -> _prev = new_node;//new_node VS prevprev -> _next = new_node;new_node -> _prev = prev;
}void List::Erase(Node* pos)
{Node* next = pos;Node* prev = pos -> _prev;prev -> _next = next;next -> _prev = prev;
}void List::PushBack(DataType l)
{this -> Insert(_head, l);
}//l1(l2)
List::List(const List& l)
{_head = new Node(DataType());_head -> _prev = _head;_head -> _next = _head;Node* cur = l._head -> _next;while(cur != l._head){this -> PushBack(cur -> _data);cur = cur -> _next;}
}void List::PushFront(DataType l)
{Insert(_head -> _next, l);
}/*** 清空**/
void List::Clean()
{Node* cur = _head -> _next;while(cur != _head){Node* next = cur -> _next;delete cur;cur = next;}
}void List::Print()
{assert(_head -> _next != _head);Node* cur = _head -> _next;while(cur != _head){cout << cur -> _data << " ";cur = cur -> _next;}cout << endl;
}
List::~List()
{Clean();delete _head;
}void List::PopBack()
{Erase(_head -> _prev);
}//l1 = l2
List& List::operator = (List l)
{swap(this -> _head, l._head);return *this;
}void List::PopFront()
{Erase(_head -> _next);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/383908.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【数据结构】(面试题)使用两个栈实现一个队列(详细介绍)

http://blog.csdn.net/hanjing_1995/article/details/51539578 使用两个栈实现一个队列 思路一&#xff1a; 我们设定s1是入栈的&#xff0c;s2是出栈的。 入队列&#xff0c;直接压到s1即可 出队列&#xff0c;先把s1中的元素倒入到s2中&#xff0c;弹出s2中的栈顶元素&#x…

POJ 1006 Biorhythms

中国剩余定理的模板题 只是有一个问题就是求出来Xk*MR中的R比给定的日期还大&#xff0c;但是如果负数的整除就不是向下取整了&#xff0c;为了解决这个问题&#xff0c;我们将R减小M&#xff0c;这样总是正的&#xff0c;求出来的就没有什么问题。 #include <iostream>…

POJ 3696 欧拉函数+快速幂

题目的意思大概就是问是否存在一串全是8的数字是L的倍数 直接想没有什么想法&#xff0c;要想到用简洁的形式将这个数字表示出来&#xff0c;对于每一位都是8的数字我们可以用 X8*(10k-1)/9的形式表示出来&#xff0c;那么题目的意思就是求X使L|X&#xff0c;我们先处理一下8和…

两个栈实现一个队列,两个队列实现一个栈

http://blog.csdn.net/zw_1510/article/details/51927554 问题1&#xff1a;用两个栈实现一个队列&#xff0c;实现队列的push和delete操作 栈的特性是先进后出&#xff08;FILO&#xff09;,队列的特性是先进先出&#xff08;FIFO&#xff09;,在实现delete时&#xff0c;我们…

C++:String的写时拷贝

String的写时拷贝 //test.h #pragma once#include <iostream> #include <string.h> #include <cstdio> #include <assert.h> using namespace std;#define TESTHEADER printf("\n%s\n", __FUNCTION__) class String { public:String(const …

两个栈实现一个队列与两个队列实现一个栈

http://blog.csdn.net/z84616995z/article/details/19204529 两个栈实现一个队列&#xff1a; 原理方法&#xff1a;用一个栈为主栈&#xff0c;一个栈为辅助栈存放临时元素。 入队&#xff1a;将元素依次压入主栈 出队&#xff1a;先检测辅助栈是否为空&#xff0c;如果非空&a…

UVa11426——欧拉函数

发现对于gcd问题要多和欧拉函数联系在一起&#xff0c;虽然有时候并不是互质&#xff0c;但是我们知道有多少互质的然后根据互质的数目就能解决很多个gcd的问题 对于这道题目&#xff0c;题目要求的是所有数对的gcd的和&#xff0c;直接思考的话有难度。但是我们如果联想到欧拉…

C++:继承和多态

虚函数:只有类的成员函数才能定义为虚函数 虚函数 在类的成员函数前面加上一个 virtual 关键字, 此时这个成员函数就叫做虚函数 虚函数 当在子类中定义了一个与父类完全相同的虚函数的时候,此时就叫做子类的虚函数重写了父类的虚函数 构成多态的条件 派生类重写基类的虚函数…

POJ 1061扩展欧几里得

扩展欧几里得的模板题&#xff0c;需要注意的是为了得到一个最小正数解我们要使axbyc中的a,b都是正数 #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<cmath> #include<ctim…

C++::探索对象模型

前面我们已经知道, 在没有虚函数的时候, 对象的大小就是对应的成员变量的大小, 而成员函数不会占用对象的空间, 今天我们来讨论一下, 当类中定义了虚函数的时候, 此时对象的大小以及对象模型 非继承下的对象模型 class Base { public:virtual void func1(){cout << &qu…

auto_ptr

#include <iostream> #include <memory> using namespace std;class A { public:A(){cout<<"构造"<<endl;}~A(){cout<<"A析构"<<endl;}void fun(){cout<<"A::fun"<<endl;} };class PA { public…

POJ 2142——扩展欧几里得

题目是很裸的扩展欧几里得&#xff0c;但是对x,y有限制条件&#xff0c;要求所有x,y中abs(x)abs(y)最小&#xff0c;在这个条件下要求abs(a* x)abs(b* y)最小 显然我们需要用扩展欧几里得求得一组解&#xff0c;问题在于如何处理这组解以得到符合条件的值。 我是这样处理的&a…

C++::模板

模板的简单介绍 C中模板是为了能够使得函数或者类实现范型编程的目的, 同时C模板的出现是为了避免代码的冗余 举个例子 void Swap(int& a, int& b) {int tmp a;b a;a b; } void Swap(char& a, char& b) {char tmp a;b a;a b; } 上面的函数除了类型不…

Linux select TCP并发服务器与客户端编程

http://blog.csdn.net/szkbsgy/article/details/10558881 [cpp] view plaincopy <span style"font-size:18px;">服务端&#xff1a; #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/time.h> #i…

BZOJ - 2186 欧拉函数

题目的意思大概是求1~N!中和M&#xff01;互质的数的个数 因为对欧拉函数理解不够深刻所以我是分析得到结果的&#xff1a; 当N<M的时候显然符合要求的数的个数为0&#xff1b; 当N>M的时候我们要求的是1~N!中不含1 ~M的素因子的的数的个数&#xff0c;结合欧拉函数的…

多态相关概念

多态相关注意事项 所谓的多态就是指函数有多中状态, 在C中通常是通过父类指针指向子类对象的方法实现多态, 这样父类可以通过子类的类型调用不同的方法. 即实现一个接口多种方法, 多态的引用是为了实现接口复用 在 C中多态是通过虚函数来实现的. 子类通过对父类相关接口进行重…

模板实现栈队列以及链表

模板实现链表 //test.h #include <iostream> #include <cstdio> #include <assert.h> using namespace std;template <class T> struct ListNode {ListNode* _prev;ListNode* _next;T _data;ListNode(const T& x):_prev(NULL),_next(NULL),_data(…

基于Visual C++2013拆解世界五百强面试题--题5-自己实现strstr

http://blog.csdn.net/itcastcpp/article/details/12907371 请用C语言实现字符串的查找函数strstr&#xff0c; 找到则返回子字符串的地址&#xff0c;没有找到返回为空&#xff0c;请用数组操作与指针操作实现 看到题目想到最简单的方法就是母字符串和子字符串比较&#xff0c…

卡特兰数

卡特兰数的引入与n边形分成三角形的个数有关&#xff1a; 我们令f[n]表示n边形可以分成的三角形的个数&#xff0c;特殊的&#xff0c;令f[2]1 我们考虑以顶点1顶点的一个三角形&#xff0c;假设用的是n边形的k-k1边&#xff0c;那么这种情况的方案数就是f[k]∗f[n−k1]f[k]*…

软件测试相关概念

什么叫软件测试 软件测试就是测试产品没有错误,同时又证明软件是可以正确运行的 测试和调试的区别 调试一般都在开发期间 ,测试是伴随着整个软件的生命周期, 调试是发现程序中问题并且解决问题, 测试是发现程序中的缺陷 软件测试的目的和原则 目的:验证软件有没有问题 原…