Answer to Question #227757 in C for Delta

Question #227757

Have is a binary image consist of "0‟ and ".‟ where "0‟ means a black pixel and „.‟ means a white pixel. Store this image using one singly list per row . Finally show its mirror image.


Each node may contain an integer counter for counting number of following "0‟s or ".‟s.

Simply insert a node after dummy node of a row

Input:

20 32

00000000000000000000000000000000

00000.....0000.....0000......000

0000.00000000.00000.000.00000000

000.000000000.000000000.00000000

000.0000000000.....0000....00000

000.000000000000000.000.00000000

0000.00000000.00000.000.00000000

00000.....0000.....0000......000

00000000000000000000000000000000

00000000000000000000000000000000

0000000000000.000000.00000000000

000000000000..00000..00000000000

00000000000...0000...00000000000

0000000000000.000000.00000000000

0000000000000.000000.00000000000

0000000000000.000000.00000000000

0000000000000.000000.00000000000

000000000000...0000...0000000000

00000000000.....00.....000000000

00000000000000000000000000000000



1
Expert's answer
2021-08-20T01:09:44-0400
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>


typedef struct List
{
	char str[100];
	struct List* pNext;
}List;
void pushList(List** hd, const char* s)
{
	if ((*hd) == NULL)
	{
		(*hd) = (List*)malloc(sizeof(List));
		(*hd)->pNext = NULL;
		strcpy((*hd)->str, s);
	}
	else
	{
		List* p = (*hd);
		while (p->pNext)
		{
			p = p->pNext;
		}
		List* nw = (List*)malloc(sizeof(List));
		nw->pNext = NULL;
		strcpy(nw->str, s);
		p->pNext = nw;
	}
}
void PrintList(List* h)
{
	List* p = h;
	while (p)
	{
		char cur[100];
		strcpy(cur, p->str);
		for (int i = 0; i < strlen(cur); i++)
			if (cur[i] == '0')
				cur[i] = '.';
			else
				cur[i] = '0';
		printf("%s\n", cur);
		p = p->pNext;
	}
}
void clearList(List** h)
{
	List* pr = NULL;
	while ((*h)->pNext)
	{
		pr = (*h);
		(*h) = (*h)->pNext;
		free(pr);
	}
	free((*h));
}
int main()
{
	int row;
	int col;
	scanf("%i%i", &row, &col);
	List* hd = NULL;
	for (int i = 0; i < row; i++)
	{
		char cur[100];
		scanf("%s", &cur);
		pushList(&hd, cur);
	}
	printf("===================================================\n");
	PrintList(hd);
	clearList(&hd);
	return  0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS