avatar
Untitled

Guest 58 14th Mar, 2019

                                           
                         #include "Class.h"
#include <iostream>

list::list()
{
	first = last = NULL;
}

bool list::Add(int value)
{
	node* p = new node;
	p->value = value;
	p->next = NULL;

	if (first == NULL)
	{
		first = last = p;
	}
	else {
		last->next = p;
		last = p;
	}
	return true;
}

void list::getElements()
{
	node* it = first;
	while (it != NULL)
	{
		std::cout << it->value << " ";
		it = it->next;
	}
	std::cout << std::endl;
}

bool list::Del(int value)
{
	if (first == NULL) return 0;
	node* it = first;
	node* erase;

	if (it->value == value)
	{
		erase = it;
		first = it->next;
		delete erase;
		return true;
	}

	bool OK = false;
	while (it->next != NULL)
	{
		if (it->next == last && last->value == value)
		{
			it->next = NULL;
			erase = last;
			last = it;
			delete erase;
			return true;
		}
		if (it->next->value == value && OK == false)
		{
			erase = it->next;
			it->next = it->next->next;
			delete erase;
			OK = false;
			it = it->next;
		}
		else it = it->next;
	}
	if (OK == false) return false;
	else return true;
}
int  list::Count(int value)
{
	node* it = first;
	int counter = 0;
	while (it != NULL)
	{
		if (it->value == value)
		{
			counter++;
		}
		it = it->next;
	}
	if (counter == 0) return 0;
	else return counter;
}
bool list::Exists(int value)
{
	int c = Count(value);
	if (c == 0) return false;
	else return true;
}

int  list::GetCount()
{
	int counter = 0;
	node* it = first;
	while (it != NULL)
	{
		counter++;
		it = it->next;
	}
	return counter;
}
int* list::GetSortedArray()
{
	node* it1;
	node* it2;
	it1 = first;
	while (it1->next != NULL)
	{
		it2 = it1->next;
		while (it2!= NULL)
		{
			if (it1->value > it2->value)
			{
				int aux = it1->value;
				it1->value = it2->value;
				it2->value = aux;
			}
			it2 = it2->next;
		}
		it1 = it1->next;
	}
	int V[2000];
	int c = -1;
	it1 = first;
	while (it1 != NULL)
	{
		V[++c] = it1->value;
		it1 = it1->next;
	}
	return V;
}

//--------------------------------------------------------------------------------------------------------------------------------------
void tree::getElements()
{

}

tree::tree()
{
	root = NULL;
	for (int i = 0; i < 2000; i++)
		V[i] = 0;
}

void tree::SRD(treenode* it , int value)
{
	if (it == NULL)
		return;
	tree::SRD(it->st,value);
	if (it->value == value) exist++;
	c++;
	V[c] = it->value;
	tree::SRD(it->dr,value);
}

bool tree::Add(int value)
{
	treenode* p = new treenode;
	p->value = value;
	p->dr = NULL;
	p->st = NULL;

	if (root == NULL)
	{
		root = p;
		return true;
	}

	bool ok = false;
	treenode* it = root;
	while (ok == false)
	{
		if (value <= it->value)
		{
			if (it->st != NULL)
			{
				it = it->st;
				continue;
			}
			else
			{
				it->st = p;
				break;
			}
		}
		if (value > it->value)
		{
			if (it->dr != NULL)
			{
				it = it->dr;
				continue;
			}
			else
			{
				it->dr = p;
				break;
			}
		}
	}
	return true;
}
bool tree::Del(int value)
{
	if (root->value == value && root->dr == NULL && root->st == NULL)
	{
		root = NULL;
		return true;
	}

}
int tree::Count(int value)
{
	exist = 0;
	SRD(root, value);
	return exist;
}
bool tree::Exists(int value)
{
	exist = 0;
	SRD(root, value);
	if (exist == 0)
		return false;
	else return true;
}
// iungureanu@bitdifender.com //
int* tree::GetSortedArray()
{
	SRD(root,0);
	return V;
}
int  tree::GetCount()
{
	return c;
}
                      
                                       
Pentru a distribui această postare, copiați această adresă URL și trimiteți-o prietenilor
RAW Paste Data
Postări Recente
Ad Block 3