Demo entry 6749574

xyylc

   

Submitted by ssd on Jun 13, 2018 at 14:03
Language: C++. Code size: 1.8 kB.

#ifndef ARRAY_CPP
#define ARRAY_CPP

#include"Array.h"

template <class T>
Array<T>::Array(int size){
	if(size<=0)
		cout<<"Invalid Array Size!!!\n\n";
	else
		ArraySize = size;
		ArrayLength = 0;
		GetArray();
}

template <class T>
void Array<T>::GetArray(void){
	array = new T[ArraySize];
	if(array == NULL) cout<<"Error!!!\n\n";
}
template <class T>
boolean Array<T>::Insert(T& x,int i){
	if(ArrayLength==ArraySize||i>ArrayLength){
		cout<<"the Array is out of range!!!\n\n";
		return FALSE;
	}
	int m;
	for(m=ArrayLength-1;m>=i;m--){
		array[m+1] = array[m];
	}
	array[i]=x;
	ArrayLength++;
	return TRUE;
}

template <class T>
boolean Array<T>::Remove(int i){
	int m;
	if(ArrayLength ==0 ) {
		cout<<"array is empty!!!\n \n";
		return FALSE;
	}
	if(i<0||i>ArrayLength-1) return FALSE;
	else
		for(m=i;m<ArrayLength-1;m++){
			array[m] = array[m+1];
		}
	ArrayLength--;
	return TRUE;
}	

template <class T>
void Union(Array<T> &Va, Array<T> &Vb){
	int n = Va.GetLength();
	int m = Va.GetLength();
	for(int i=0; i<m; i++){
		T x = Vb.GetNode(i);
		int k = Va.Find(x);
		if(k == -1){
			Va.Insert(x, n);
			n++;
		}

	}
}

template <class T>
void Intersection(Array<T> &Va,Array<T> &Vb){
	int n = Va.GetLength();
	int m = Vb.GetLength();
	int i = 0;
	while(i<m){
		T x = Vb.GetNode(i);
		int k = Va.Find(x);
		if(k == -1){
			Vb.Remove(i);
			m--;
		}else{
			i++;
		}
			
	}
}

template <class T>
void Josephus(Array<T> &P, int n, int s, int m){
	int k = 1;
//	int m;
	for(int i=0;i<n;i++){
		//cin>>m;
		P.Insert(k,i);
		k++;
	}
	int s1 = s;
	for(int j = n; j>=1; j--){
		s1 = (s1 +(m-1))%j;
		if(s1 == 0) s1 =j;
		int w = P.GetNode(s1-1);
		P.Remove(s1-1);
		P.Insert(w,n-1);
	}
}
#endif

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).