Pretty interesting problem about linked list. This program is about autobuses. Each autobus is given by brand, registration number, number of seats, kilometres and year when the bus was made. The program is organised as menu and when you hit number it calls some function. Problematic function: it should NOT PASS autobuses with the same registration number but this function(int povtorenie()) doesn't work properly if you type equal registration numbers on positions different from 1 and 2.

int povtorenie(struct node* call)
{
	char* a=new char[20];
	strcpy(a,call->regnomer);
	call = call->next;
	while(call!=NULL)
	{
		if(strcmp(call->regnomer,a)==0)
			return 1;
		else
			return 0;
		call = call->next;
	}
	return 1;
}

The menu from which we call functions:

int main()
 {
	SetConsoleCP(1251);
	SetConsoleOutputCP(1251);
	Node *head;
	Node *nov;
	head=NULL;
	int k=1;
	bool l=1;
	do
	{
		char p;
	printf("\n--------------------------------------------------");
	printf("\n 1.Entering elements");
	printf("\n 2.Show elements without sorting");
    printf("\n 3.Show elements with sorting");
	printf("\n 4.Delete elements");
	printf("\n 5.Search in the list");
	printf("\n 6.Sort searching results");
	printf("\n 0.Exit");
	printf("\n--------------------------------------------------\n");
	p=getche();
	if(p=='1')
	{
		do
		{	nov=newElement(stdin);	 	
		head=addToHead(head,nov);
		if(k>1)
		{
			if(povtorenie(head))
			{
				printf("\nThis element is already in!\n");
				head=triene(head,k);
			}
		}
	  	printf("Next element?Y/N:");
		k++;
		} while(toupper(getche())=='Y');
	}
	else if(p=='2')
	printlist(head);
	else if(p=='3')
	{
	sortirane(head);
    printf("\n After sorting:");
    printlist(head);
	}
		else if(p=='4')
	repeatdelete(head);
	else if(p=='5')
		{
		printf("\nEnter something for search:");
		find(head);
	}
	else if(p=='6')
	{printf("\nThe bus with most kilometres is:");
	sravnenie(head);
	printf("\nThe bus with minimum kilometres is:");
	sravnenie2(head);}
	else if(p=='0')
    l=0;
	}while(l);
	return 0;
}

the loop should be something like this. And do not dynamically allocate array a because it is causing you a memory leak (your program doesn't delete[] it). The array size is small enough that dynamic allocation isn't necessary anyway on most modern operating systems such as MS-Windows and *nix.

int povtorenie(struct node* call)
{
	char a[20];
	strcpy(a,call->regnomer);
	call = call->next;
	while(call!=NULL)
	{
		if(strcmp(call->regnomer,a)==0)
			return 1;
		call = call->next;
	}
	return 0;
}

Edited 6 Years Ago by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.