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.pswp.cn/news/383908.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383908.shtml
英文地址,請注明出處:http://en.pswp.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]*…

軟件測試相關概念

什么叫軟件測試 軟件測試就是測試產品沒有錯誤,同時又證明軟件是可以正確運行的 測試和調試的區別 調試一般都在開發期間 ,測試是伴隨著整個軟件的生命周期, 調試是發現程序中問題并且解決問題, 測試是發現程序中的缺陷 軟件測試的目的和原則 目的:驗證軟件有沒有問題 原…