Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
Update list.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
SaptakBhoumik authored Jan 26, 2023
1 parent f25f99c commit 84009be
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ class list{
this->m_capacity=other.m_capacity;
}
~list(){
delete[] m_data;
if(m_data!=nullptr){
delete [] m_data;
m_data=nullptr;
}
}
list<T>& operator=(const list<T>& other){
if(this!=&other){
delete[] m_data;
if(m_data!=nullptr){
delete[] m_data;
m_data=nullptr;
}
m_data=new T[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
Expand All @@ -50,14 +56,20 @@ class list{
}
list<T>& operator=(list<T>&& other){
if(this!=&other){
delete[] m_data;
if(m_data!=nullptr){
delete[] m_data;
m_data=nullptr;
}
m_data=new T[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
for(size_t i=0;i<m_size;i++){
m_data[i]=other.m_data[i];
}
delete[] other.m_data;
if(m_data!=nullptr){
delete[] other.m_data;
other.m_data=nullptr;
}
}
return *this;
}
Expand Down Expand Up @@ -106,7 +118,10 @@ class list{
for(size_t i=0;i<m_size;i++){
new_data[i]=m_data[i];
}
delete[] m_data;
if(m_data!=nullptr){
delete[] m_data;
m_data=nullptr;
}
m_data=new_data;
}
for (size_t i = 0; i < other.m_size; i++)
Expand All @@ -128,7 +143,10 @@ class list{
for(size_t i=0;i<m_size;i++){
new_data[i]=m_data[i];
}
delete[] m_data;
if(m_data!=nullptr){
delete[] m_data;
m_data=nullptr;
}
m_data=new_data;
}
m_size++;
Expand All @@ -142,4 +160,4 @@ class list{
}
};
}
#endif
#endif

0 comments on commit 84009be

Please sign in to comment.