int Q021_Merge_Two_Sorted_Lists(){
	printf("Test case 1\n");
	
	vector<int> test_in1{1, 3, 4};
	vector<int> test_in2{0};
	ListNode* test_l1 = createList(test_in1);
	printListNodes(test_l1);
	ListNode* test_l2 = createList(test_in2);
	printListNodes(test_l2);
	ListNode* res_test1 = mergeTwoLists(test_l1, test_l2);
	printListNodes(res_test1);
	releaseList(test_l1);
	releaseList(test_l2);


	return 0;
}
Example #2
0
File: stem.c Project: dentearl/stem
int main (int argc, char **argv)
{
    double scale = 1.;
    double width = 80.;
    double atom  = 1e-8; /* 1e-8 */
    double d;
    int n;
    gatherOptions(argc, argv, &scale, &width, &atom);
    doubleList_t *list = NULL;
    initList(list);
    while (scanf("%lf", &d) == 1) {
        addToList(&list, d);
    }
    double *array;
    n = countList(list);
    array = listToArray(list, n);
    releaseList(list);
    stem_leaf(array, n, scale, width, atom);
    free(array);
    return EXIT_SUCCESS;
}
Example #3
0
void main(int argc, char** argv)
{
	CvPoint2D32f srcQuad[4], dstQuad[4];
	CvMat* warp_matrix = cvCreateMat(3,3,CV_32FC1);
	float Z=1;
	/*cvNamedWindow("img", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("warp", CV_WINDOW_AUTOSIZE);*/

	dstQuad[0].x = 250; //src Top left
	dstQuad[0].y = 100;
	dstQuad[1].x = 430; //src Top right
	dstQuad[1].y = 115;
	dstQuad[2].x = 50; //src Bottom left
	dstQuad[2].y = 170;
	dstQuad[3].x = 630; //src Bot right
	dstQuad[3].y = 250;

	int lOff = 50, tOff = 150;
	srcQuad[0].x = tOff; //dst Top left
	srcQuad[0].y = lOff;
	srcQuad[1].x = 640-tOff; //dst Top right
	srcQuad[1].y = lOff;
	srcQuad[2].x = tOff; //dst Bottom left
	srcQuad[2].y = 480-lOff;
	srcQuad[3].x = 640-tOff; //dst Bot right
	srcQuad[3].y = 480-lOff;

	cvGetPerspectiveTransform(srcQuad, dstQuad,	warp_matrix);

	int ik=0, ni = 0, niX = 22-1;
	char names[22][25] = {
							"../../Data/6 Dec/009.jpg", 
							"../../Data/6 Dec/011.jpg",
							"../../Data/6 Dec/012.jpg",
							"../../Data/6 Dec/016.jpg",
							"../../Data/6 Dec/018.jpg",
							"../../Data/6 Dec/019.jpg",
							"../../Data/6 Dec/020.jpg",
							"../../Data/6 Dec/022.jpg",
							"../../Data/6 Dec/024.jpg",
							"../../Data/6 Dec/064.jpg",
							"../../Data/6 Dec/065.jpg",
							"../../Data/6 Dec/066.jpg",
							"../../Data/6 Dec/067.jpg",
							"../../Data/6 Dec/068.jpg",
							"../../Data/6 Dec/069.jpg",
							"../../Data/6 Dec/070.jpg",
							"../../Data/6 Dec/071.jpg",
							"../../Data/6 Dec/072.jpg",
							"../../Data/6 Dec/073.jpg",
							"../../Data/6 Dec/074.jpg",
							"../../Data/6 Dec/075.jpg",
							"../../Data/6 Dec/076.jpg"
						};
	int lwSum = 0, nopf = 0;
	//CvCapture *capture = cvCaptureFromCAM(0);
	/*double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
	IplImage* image = cvRetrieveFrame(capture);
	CvSize imgSize;
    imgSize.width = image->width;
    imgSize.height = image->height;
	CvVideoWriter *writer = cvCreateVideoWriter("out.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, imgSize);*/
	while(1)
	{
		//IplImage* img = cvQueryFrame(capture);
		IplImage* img = cvLoadImage( "../../Data/23 Jan/c.jpg", CV_LOAD_IMAGE_COLOR);
		//cvSaveImage(nameGen(ik++), img, 0);
		//cvShowImage("img", img);

		IplImage* warp_img = cvCloneImage(img);
		CV_MAT_ELEM(*warp_matrix, float, 2, 2) = Z;
		cvWarpPerspective(img, warp_img, warp_matrix, CV_INTER_LINEAR | CV_WARP_INVERSE_MAP | CV_WARP_FILL_OUTLIERS);
		//cvReleaseImage(&img);
		//cvWaitKey(0);

		IplImage* grayimg = cvCreateImage(cvGetSize(warp_img),IPL_DEPTH_8U,1);
		cvCvtColor( warp_img, grayimg, CV_RGB2GRAY );
		cvReleaseImage(&warp_img);
		
		cvSmooth(grayimg, grayimg, CV_GAUSSIAN, 3, 3, 0.0, 0.0);
		cvEqualizeHist(grayimg, grayimg);
		cvThreshold(grayimg, grayimg, PercentileThreshold(grayimg, 10.0), 255, CV_THRESH_BINARY);
		
		IplImage* finalimg = cvCreateImage(cvGetSize(grayimg),IPL_DEPTH_8U,3);
		CvMemStorage* line_storage=cvCreateMemStorage(0);

		CvSeq* results =  cvHoughLines2(grayimg,line_storage,CV_HOUGH_PROBABILISTIC,10,CV_PI/180*5,350,100,10);
		cvReleaseImage(&grayimg);

		double angle = 0.0, temp;
		double lengthSqd, wSum=0;
		CvPoint center = cvPoint(0, 0);
		for( int i = 0; i < results->total; i++ )
		{
			CvPoint* line = (CvPoint*)cvGetSeqElem(results,i);
			//lengthSqd = (line[0].x - line[1].x)*(line[0].x - line[1].x) + (line[0].y - line[1].y)*(line[0].y - line[1].y);
			wSum += 1;//lengthSqd;
			if(line[0].y > line[1].y)
				temp = atan((line[0].y - line[1].y + 0.0) / (line[0].x - line[1].x));
			else
				temp = atan((line[1].y - line[0].y + 0.0) / (line[1].x - line[0].x));
			if(temp < 0)
				angle += (90 + 180/3.14*temp)/* * lengthSqd*/;
			else
				angle += (180/3.14*temp - 90)/* * lengthSqd*/;
			center.x += (line[0].x + line[1].x)/2;
			center.y += (line[0].y + line[1].y)/2;
		}
		angle /= wSum;	// Angle Direction: Left == -ve and Right == +ve
						// Angle is calculated w.r.t Vertical
		//angle+=10;	// Angle Offset (Depends on camera's position)
		center.x /= results->total;
		center.y /= results->total;

		double m = (angle != 0) ? tan(CV_PI*(0.5-angle/180)) : 100000;	// 100000 represents a very large slope (near vertical)
		//m=-m;		// Slope Correction
		
		CvPoint leftCenter = cvPoint(0, 0), rightCenter = cvPoint(0, 0);
		double leftSlope = 0, rightSlope = 0, leftCount = 0, rightCount = 0;
		for( int i = 0; i < results->total; i++ )
		{
			CvPoint* line = (CvPoint*)cvGetSeqElem(results,i);
			CvPoint midPoint = cvPoint((line[0].x + line[1].x)/2, (line[0].y + line[1].y)/2);
			double L11 = (0-center.y + m*(0-center.x + 0.0))/m;
			double L22 = (midPoint.y-center.y + m*(midPoint.x-center.x + 0.0))/m;
			if(L11*L22 > 0)
			{
				leftCenter.x += midPoint.x;
				leftCenter.y += midPoint.y;
				leftSlope += -(line[1].y - line[0].y)/(line[1].x - line[0].x+0.0001);
				leftCount++;
			}
			else
			{
				rightCenter.x += midPoint.x;
				rightCenter.y += midPoint.y;
				rightSlope += -(line[1].y - line[0].y)/(line[1].x - line[0].x+0.0001);
				rightCount++;
			}
		}
		cvReleaseMemStorage(&line_storage);
		leftCenter.x /= leftCount;		leftCenter.y /= leftCount;		leftSlope /= leftCount;
		rightCenter.x /= rightCount;	rightCenter.y /= rightCount;	rightSlope /= rightCount;
		
		CvPoint botCenter = cvPoint(finalimg->width/2, finalimg->height);
		int dL = abs(botCenter.y-leftCenter.y + m * (botCenter.x-leftCenter.x)) / sqrt(m*m + 1);
		int dR = abs(botCenter.y-rightCenter.y + m * (botCenter.x-rightCenter.x)) / sqrt(m*m + 1);
		
		int lw = abs((leftCenter.y - rightCenter.y) + m*(leftCenter.x - rightCenter.x)) / sqrt(m*m + 1);
		lwSum += lw;
		nopf++;
		
		if(lw <= SINGLE_LANE_WIDTH)
		{
			double L11 = (0-leftCenter.y + m*(0-leftCenter.x + 0.0))/m;
			double L22 = (botCenter.y-leftCenter.y + m*(botCenter.x-leftCenter.x + 0.0))/m;
			if(L11*L22 < 0)
				dR = lwSum/nopf - dL;	// Only Left Lane is visible
			else
				dL = lwSum/nopf - dR;	// Only Right Lane is visible
		}
		
		//cvSaveImage("test.jpg", finalimg, 0);

		printf("Bot:\t(%d, %d, %.3f)\n", dL, (finalimg->height)/10, 90.0-angle);
		printf("Target:\t(%d, %d, %.3f)\n", (dL+dR)/2, (finalimg->height)*9/10, 90.0);

		location bot, target;
		bot.x = dL;		bot.y = (finalimg->height)/10;		bot.theta = 90.0-angle;
		target.x = (dL+dR)/2;	target.y = (finalimg->height)*9/10;	target.theta = 90.0;

		cvReleaseImage(&finalimg);

		list *ol = NULL, *cl = NULL;
		elem e,vare;
		e.l = bot;	e.g = 0;	e.h = 0;	e.id = UNDEFINED;

		int n = 15;
		elem* np = loadPosData(n);
	
		while(1)
		{
			cl = append(cl, e);
			//printList(cl);
			if(isNear(e.l, target))
				break;
			ol = update(ol, e, target, np, n);
			//printList(ol);
			e = findMin(ol);
			//printf("Min: (%.3f, %.3f, %.3f, %d)\n", e.l.x, e.l.y, e.l.theta, e.id);
			ol = detach(ol, e);
			//printList(ol);
			//getchar();
		}
		free(np);

		vare = e;
		printf("(%.3f, %.3f, %.3f) : %d\n", vare.l.x, vare.l.y, vare.l.theta, vare.id);
		while(!((abs(vare.l.x-bot.x) < 1.25) && (abs(vare.l.y-bot.y) < 1.25)))
		{
			vare=search(cl,vare.parent.x,vare.parent.y);
			if(vare.id != -1)
			{
				printf("(%.3f, %.3f, %.3f) : %d\n", vare.l.x, vare.l.y, vare.l.theta, vare.id);
				e = vare;
			}
		}
		printf("\n(%.3f, %.3f, %.3f) : %d\n", e.l.x, e.l.y, e.l.theta, e.id);
		//navCommand(10-e.id, e.id);

		releaseList(ol);
		releaseList(cl);
		
		getchar();
		int c = cvWaitKey(0);
		if(c == '4')
		{
			if(ni != 0)
				ni--;
		}
		else if(c == '6')
		{
			if(ni != niX)
				ni++;
		}
	}
}
Example #4
0
 PObject::~PObject()
 {
     releaseList();   // at destrution of class, release it's children from memory 
 }
Example #5
0
void releaseQueue(Queue *queue) {
    releaseList(queue->head);
    free(queue);
}
Example #6
0
void releaseStack(Stack *stack) {
    releaseList(stack->top);
    free(stack);
}