2 Replies - 728 Views - Last Post: 11 January 2009 - 04:22 PM Rate Topic: -----

#1 milos_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 08

finding if there is one list in another

Posted 10 January 2009 - 03:14 PM

Hello all,
I am trying to make the program which will test if there is list1 in the list2...this is the code that i have made until now but for some reason program always turns back values like there is no L1 in L2

thanks in advance for help

#include <stdio.h>
#include <stdlib.h>

struct cvor{
	int br;
	struct cvor* sl;
};

void ubaci(struct cvor **lista, int x){				 // function for entering new data in list
	struct cvor* novi;
	if ((novi = (cvor*)malloc(sizeof(cvor))) == NULL){
		printf("Error\n");exit(1);}
	novi->br = x;
	novi->sl = *lista;
	*lista = novi;
}

void proveri(struct cvor *L1, struct cvor *L2){		// function for testing if there is L1 in L2
	struct cvor *tmp1,*tmp2;
	tmp1=L1;
	tmp2=L2;

	while(tmp2->sl!=NULL){
		if(tmp1->br == tmp2->br){
			tmp1=tmp1->sl;
			tmp2=tmp2->sl;}
		if(tmp1->sl==NULL) break;
		tmp1=L1;
		tmp2=tmp2->sl;
	}
	if(tmp1->sl==NULL)
		printf("There is L1 in L2");
	else
		printf("L1 does not exist in L2");
}

void ispisi_listu(struct cvor* lista){				  // function for printing list
	if (lista != NULL){
		ispisi_listu(lista->sl);
		printf("%d ", lista->br);
	}
}

void oslobodi_listu(struct cvor* lista){				// function to free allocated memmory
	while (lista){
		struct cvor* tmp = lista->sl;
		free(lista);
		lista = tmp;
	}
}

main(){
	struct cvor *L1=NULL,*L2=NULL,*L = NULL;
	int i,t,k,x;
	for (i = 1; i<5; i++){
		printf("Enter %d number in first list: ", i);
		scanf("%d",&k);
		ubaci( &L1, k);}
	for (t = 1; t<10; t++){
		printf("Enter %d number in second list: ", t);
		scanf("%d",&x);
		ubaci( &L2, x);}
	ispisi_listu(L1);
	putchar('\n');
	ispisi_listu(L2);
	putchar('\n');

	proveri(L1,L2);

	oslobodi_listu(L1);
	oslobodi_listu(L2);
}




Is This A Good Question/Topic? 0
  • +

Replies To: finding if there is one list in another

#2 TheMagnitude  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 144
  • Joined: 12-January 08

Re: finding if there is one list in another

Posted 11 January 2009 - 02:13 PM

Why have you used such strange names for your functions and variables.

Are they in a different language?

I would suggest changing them to english since the majority of people on this board are english, and perhaps then you would get some help.
Was This Post Helpful? 0
  • +
  • -

#3 milos_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 08

Re: finding if there is one list in another

Posted 11 January 2009 - 04:22 PM

View PostTheMagnitude, on 11 Jan, 2009 - 01:13 PM, said:

Why have you used such strange names for your functions and variables.

Are they in a different language?

I would suggest changing them to english since the majority of people on this board are english, and perhaps then you would get some help.

Thanks for that hint about language in code. I will try next time to put in English words.
So i figure it out whats wrong with my function. Instead of IF there is another while and now everything works fine.
void check(struct node *L1, struct node *L2){  
	struct node *tmp1,*tmp2;
	tmp1=L1;
	tmp2=L2;
	while(tmp2->sl!=NULL){
		while( tmp2->br==tmp1->br && tmp1->sl!=NULL ){
			tmp1=tmp1->sl;
			tmp2=tmp2->sl;
			}
		tmp2=tmp2->sl;
	}
	if(tmp1->sl==NULL)
		printf("There is L1 in L2");
	else
		printf("L1 does not exist in L2\n");
}



Milos
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1