change by XOR

 avatar
unknown
c_cpp
3 years ago
1.3 kB
7
Indexable
#include <stdio.h>
#include <stdlib.h>
//#define INTEGER
#define STRUCT

#ifdef INTEGER
int main() {

	int arr[] = { 1,2,3,4,5,6,7,8 };

	int* prt1 = &arr[4];
	int* prt2 = &arr[6];

	printf("prima del tentato scambio %d %d \n\n", arr[4], arr[6]);
	int* tmp = prt1;

	*prt1 ^= *prt2;
	*prt2 ^= *prt1;
	*prt1 ^= *prt2;
	printf("%d %d dopo tentato scambio\n", arr[4], arr[6]);
}
#endif // INTEGER

#ifdef STRUCT
typedef struct {
	int key;
	double prio;
} HeapElem;

typedef struct {
	HeapElem* heap;
	int* pos; /*da non utilizzare per la risoluzione*/
	int n; /* quante coppie (chiave, prio) sono effettivamente presenti nello heap */
	int size; /* massimo numero di coppie (chiave, prio) che possono essere contenuti nello heap */
} MinHeap;

int main() {

	static MinHeap H;
	int i;
	H.heap = (HeapElem*)malloc(10 * sizeof(HeapElem));
	H.size = 10;
	H.n = 5;

	for ( i = 0; i < H.n; i++)
	{
		H.heap[i].prio = i+1;
		H.heap[i].key = 2 * i;
		
		printf(" %.2f,%d\t", H.heap[i].prio, H.heap[i].key);
	}
	HeapElem* father;
	int fPos, j = 3;
	HeapElem* child = &H.heap[i];
	if (j % 2 == 0)
	{
		j = j - 1;
	}
	fPos = j / 2;
	father = &H.heap[fPos];

	while (father->prio > child->prio)
	{
		


	}

}
#endif // STRUCT
Editor is loading...