Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Oct 17th, 2004, 7:47 AM   #1
sbhatta
Newbie
 
Join Date: Oct 2004
Posts: 3
Rep Power: 0 sbhatta is on a distinguished road
ok guys.... here is the problem. I have to use Double linked list. Absolutely no arrays to store file datas.

Teh program is supposed to check between two text files and find the lines that are unique (i.e the lines that appear only in file 1 but not anywhere in file 2)

Follwoing is my code. I am not sure where I am making the mistake. If you look at the check() fuction, you will notice that it is where I am supposed to check each of the lines (pNode3 contains it) with all the lines (pNode2 contains it). And then if it is not found, then I call display3() function which basically displays the datas..... Please take a look at check routine


Also I cant use any unix commands like diff and only standard liabraries.....
I think I have very small logic error. It compiles fine. If you copy and paste in your unix..., you may be able to see that I am itterating between all the lines of file 2 with each lines of file 1.

Assumption: Each of the word is not more than 100char
* Also, I find difference until the smallest number of lines in files
* Also I find difference until the smallest length of line
The text files I am using are:

The Way that can be told of is not the eternal Way;
The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
The Named is the mother of all things.
Therefore let there always be non-being,
 *so we may see their subtlety,
And let there always be being,
 *so we may see their outcome.
The two are the same,
But after they are produced,
 *they have different names.
The Nameless is the origin of Heaven and Earth;
The named is the mother of all things.

Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
so we may see their outcome.
The two are the same,
But after they are produced,
they have different names.
They both may be called deep and profound.
Deeper and more profound,
The door of all subtleties!
Here is the code
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <wchar.h>
#include <string.h>

#define FALSE 0
#define TRUE 1
#define MAXWORDS 100

#define NullChar '\0'

/*structure of the double linked list*/
struct NODE2 
{
 struct NODE2 *pNext;
 struct NODE2 *pPrev;
 char* nData;
}*pHead, *pTail, *pNode,*pHead2, *pTail2, *pNode2,*pHead3,*pTail3,*pNode3;

struct COUNT
{
 int lineNo; /*line no*/
 int words; /*total word count*/
}file, file2;

/*PROTOTYPES */
void midFunct(char *ch1);
void adddll2(char *ch2);
void addll3(char *ch2);
void display(struct NODE2 *pNode, struct NODE2 *pNode2);
int strcomp(const char *src1,const char *src2);
void blankDiff(char * a , char * b);
int getword(char *word, int lim, FILE * instream);
void check(struct NODE2* pNode2,struct NODE2* pNode3,char *newlinetest);
void display3(struct NODE2 *pNode3);
void removeMe(struct NODE2* pNode3);
    
    
/*man page for mdiff*/
void help()
{
 printf("Usage is: mdiff [option] filespec1 filespec2\n");
 printf("Options:\n");
 printf(" -help\t \n");
 printf(" -b\t \n");
 printf(" -c\t.\n");
 printf(" -i\t \n");
 printf(" -q\t \n");
 printf(" -v No diff\t \n");
 printf("filespec2 can be a drive or directory name.\n");
}



int main(int argc, char **argv)
{
  
 int opt;
 /* options -> the inputs that user has */
 extern int getopt(int argc, char *argv[], char *optionS);
 while ((opt = getopt(argc, argv, "bciqv")) != EOF) 
  {
   switch (opt) 
	{
	case '?':
  printf("Invalid command line option\n");
  help();
  return(1);
	case 'b':
  blankDiff(argv[2], argv[3]);
  break;
	case 'c':
  break;
	case 'i':
  break;
	case 'q':
  break;
	case 'v':
  break;
	}
  }
 return EXIT_SUCCESS;
}



void midFunct(char *ch1)
{
  
 pNode = malloc (sizeof (struct NODE2));
 pNode->nData = ch1;

  
 if (pHead == NULL) {
  pHead = pNode;
  pNode->pPrev = NULL;
 }
 else {
  pTail->pNext = pNode;
  pNode->pPrev = pTail;
 }
 pTail = pNode;
 pNode->pNext = NULL;
}



void adddll2(char *ch2)
{
 pNode2 = malloc (sizeof (struct NODE2));
 pNode2->nData = ch2;
 if (pHead2 == NULL) {
  pHead2 = pNode2;
  pNode2->pPrev = NULL;
 }
 else {
  pTail2->pNext = pNode2;
  pNode2->pPrev = pTail2;
 }
  
 pTail2 = pNode2;
 pNode2->pNext = NULL;


}


void display3(struct NODE2 * pNode3)
{
 pNode3=pHead3;
 while(pNode3)
  {
   printf("%s",pNode3->nData);
   pNode3=pNode3->pNext;
  }
}

/*test stub only*/
/*test function to display link lists*/
void display(struct NODE2 * pNode, struct NODE2 *pNode2)
   /* struct NODE *pNode3)*/
{
 pNode=pHead;
 while(pNode)
  {
   printf("<%s>",pNode->nData);
   pNode=pNode->pNext;
  }
 pNode2=pHead2;
 while(pNode2)
  {
   printf("[%s]",pNode2->nData);
   pNode2=pNode2->pNext;
  }
}

int strcomp ( const char *_src1, const char *_src2 ) 
{
 while ( *_src1 == *_src2++ ) 
  if ( *_src1++ == '\0' ) 
   return 0;
 /* while ( *_src1 == *_src2++ ) */
  
 if ( *_src1 == '\0' )  
  return -1;
  
 if ( *--_src2 == '\0' ) 
  return 1;
  
 return ( ( ( unsigned char ) *_src1 ) - ( ( unsigned char ) *_src2 ) );
}
/* int strcomp ( const char *_src1, const char *_src2 ) */

void adddll3(char *ch3)
{
 pNode3 = malloc (sizeof (struct NODE2));
 pNode3->nData = ch3;
 if (pHead3 == NULL) {
  pHead3 = pNode3;
  pNode3->pPrev = NULL;
 }
 else {
  pTail3->pNext = pNode3;
  pNode3->pPrev = pTail3;
 }
 pTail3 = pNode3;
 pNode3->pNext = NULL;
  
}


/* getword: get next word or character from input */
int getword(char *word, int lim, FILE * instream)
{
 int c;
 char *w = word;
 while ((isspace(c = fgetc(instream))) && (c !='\n'))
  ;
  
 if (c != EOF)
  *w++ = c;
  
 if (!isalpha(c) ) {
  *w = NullChar;
  return c;
 }
  
 for (; --lim > 0; w++)
  if (!isalnum(*w = fgetc(instream))) {
   ungetc(*w,instream);
   break;
  }
  
 *w = NullChar;
 if (c == EOF)
  return EOF;
 return word[0];
}

 
void blankDiff(char* a, char* b)
{
 char *array1;
 char *array2;
 FILE *instream;
 FILE *instream2;
 instream=fopen(a,"r");
 instream2=fopen(b,"r");
 char *c, *d;
  
 if(instream==NULL){
  c=strcpy("\\",a);
  instream=fopen(c,"r");
 }
 if(instream2==NULL){
  d=strcpy("\\",b);
  instream2=fopen(d,"r");
 }
 a=c;
 b=d;

 if((instream == NULL) || (instream2 == NULL))
  {
   printf ("File Open Error!");
   exit(99);
  }
 int d2, k2;
 file.words=file2.words=0;
 file.lineNo=file2.lineNo=0;

 while((d2 != -1) || (k2 != -1))
  {
   array1=malloc(100*sizeof(array1));
   array2=malloc(100*sizeof(array2));
   d2=getword(array1,MAXWORDS,instream);
   k2=getword(array2,MAXWORDS,instream2);
   midFunct(array1);
   adddll2(array2);
   file.words = file.words +1;
   file2.words=file2.words+1;
   if(d2 == '\n' )
	file.lineNo=file.lineNo +1;
   if(k2 == '\n')
	file2.lineNo=file.lineNo +1;
  }
  
 char *array3=malloc(100*sizeof(array3));
  
 char *newlinetest=malloc(100*sizeof(char));
 strcpy(newlinetest,"\n");

 char *spacetest=malloc(100*sizeof(char));
 strcpy(spacetest,"");

 pNode=pHead;
 pNode2=pHead2;
 pNode3=pHead3;

 register int temptest=1;

 while(pNode!=NULL)
  {
   
   while(pNode3!=pHead3){
	if(pNode3 !=NULL){
  pNode3=pNode3->pPrev;
  removeMe(pNode3->pNext);

	}
 
   }
   free(pNode3);
   free(pHead3);
   free(pTail3);
   while(temptest != 0)
	{
  temptest=strcomp(pNode->nData,newlinetest);
  array3=pNode->nData;
  adddll3(array3);
  pNode=pNode->pNext;
 
  if(pNode==NULL)
   break;
	}

   check(pNode2,pNode3,newlinetest);

   temptest=1;
  }

}
void removeMe(struct NODE2* pNode3)
{
 if(pNode3==pHead3)
  {

   return;
  }
  
 if(pNode3->pPrev == NULL)
  pHead3=pNode3->pNext;
 else
  pNode3->pPrev->pNext=pNode3->pNext;

 if(pNode3->pNext == NULL)
  pTail3=pNode3->pPrev;
 else
  pNode3->pNext->pPrev=pNode3->pPrev;
 free(pNode3);
}


void check(struct NODE2* pNode2,struct NODE2 *pNode3,char *newlinetest)
{
 int compare,yes,check2;
 int length;
 yes=0;

 if(file2.lineNo > file.lineNo)
  length=file.lineNo;
 else 
  length=file2.lineNo;
 
  
 pNode3=pHead3;
 pNode2=pHead2;
  


 while(pNode2!=NULL)
  {

   if(strcomp(pNode2->nData, newlinetest)==0)
	check2=0;
	
   if(pNode3 == NULL)
	{
  break;
	}
   
   length--;
   
   if(length==0){
	check2=0;
	break;
   }
   
   compare=strcomp(pNode2->nData,pNode3->nData);

   if(compare !=0){
	(check2)++;
   }
   if((pNode3 !=NULL) && (strcomp(pNode2->nData,newlinetest) ==0))
	{
  while(strcomp(pNode3->nData,newlinetest)!=0)
   pNode3=pNode3->pNext;
	}
   
   if((pNode3==NULL) && (strcomp(pNode2->nData,newlinetest)!=0))
	{
  while(strcomp(pNode2->nData,newlinetest)!=0)
   pNode2=pNode2->pNext;
	}

   if(strcomp(pNode3->nData,newlinetest)==0){
	if(check2==0){
  yes=1;
	}
	pNode3=pHead3;
   }

   pNode2=pNode2->pNext;
   pNode3=pNode3->pNext;
 if(yes==1){
  display3(pNode3);   
  }
  

 }
  
}
sbhatta is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:58 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC