Esempio n. 1
0
 ListNode *mergelist(vector<ListNode *> &lists, int start, int end)
 {
     if(start == end) return lists[start];
     int mid = (start + end)/2;
     ListNode *head1 = mergelist(lists, start, mid);
     ListNode *head2 = mergelist(lists, mid+1, end);
     merge2Lists(head1, head2);
 }
Esempio n. 2
0
/**
 * wordmergepost(linkedlist *currlist)
 *
 * 			Execute post tasks for sort on file names
 * @list1:	Pointer to linked list head.
 *
 *		When merging word listings we needs to perform
 *		some clean up on the file lists
 */
void wordmergepost(linkedlist *currlist)
{
	/*sort the files after a merge*/
	((word_str*)currlist->data)->files =
			mergesort(((word_str*)currlist->data)->files,filenmcmp);
	/*merge any files that are the same*/
	((word_str*)currlist->data)->files =
			mergelist(((word_str*)currlist->data)->files,filenmcmp,filemergepost);
}
Esempio n. 3
0
/*Main function pass command line arg for each of the files to parse*/
int main ( int argc, char *argv[] ) {
	int i;
	linkedlist *wlist = NULL;
	file_str *filestr;
	FILE *fp;
	char *linebuf;
	unsigned int buflen;
	char *word;
	word_str *wordstr;
	int linenum;
	int *linenump;

	if(argc <= 1)
	{
		printf("Sorry no files were given!");
		return(-1);
	}


	/*iterate through the files*/
	for(i=1;i<argc;i++)
	{
		linenum = 0;
		fp = fopen(argv[i],"r");
		if(fp == NULL)
		{
			printf("Bad filename detected\n");
			return(-1);
		}
		/*reading each line*/
		while(!feof(fp))
		{
			linenum++;
			/*allocate some mem for the line read*/
			buflen = BUFLEN;
			linebuf = (char*)malloc(sizeof(char)*buflen);
			if(linebuf == NULL)
			{
				printf("Could not allocate mem to read line from file!\n");
				return(-1);
			}
			fgets(linebuf,sizeof(char)*BUFLEN,fp);
			/*if there was not enough buffer length tack
			 *on some more and read more of the file
			 */

			while(1)
			{
				if(!(feof(fp)||(linebuf[strlen(linebuf)-1]=='\n')))
				{
					buflen += BUFLEN-1;
					linebuf = (char*)realloc(linebuf,sizeof(char)*buflen);
					if(linebuf == NULL)
					{
						/*The line was too long to read
						 *With some refactoring this operation could be split
						 *into multiple parsing.  Issue is insuring a word is not
						 *cut off
						 */
						printf("Could not allocate mem to read line from file!\n");
						return(-1);
					}
				}else{
					break;
				}
				if(fgets(&linebuf[buflen-BUFLEN],sizeof(char)*BUFLEN,fp)==NULL)
				{
					/*we reached eof and no data was read make sure that word
					 *is cleared nice
					 */
					word[0]='\n';
				}
			}
			/*break up the line into words based on delimeter string*/
			word = strtok(linebuf, WORDDEL);
			while(word != NULL &&!feof(fp))
			{
				/*see if the line was blank, skip it*/
				if(linebuf[0]=='\n')
					break;
				/*we dont want the 'word' and 'word\n' to be different*/
				if(word[strlen(word)-1]=='\n')
				{
					word[strlen(word)-1] = '\0';
				}
				/*record information about the file*/

				filestr = (file_str*)malloc(sizeof(file_str));
				if(filestr == NULL)
				{
					printf("Could not allocate mem to hold list for file name!\n");
					return(-1);
				}
				filestr->filename=(char*)malloc(sizeof(char)*strlen(argv[i]));
				if(filestr->filename == NULL)
				{
					printf("Could not allocate mem to store file name!\n");
					return(-1);
				}
				strcpy(filestr->filename,argv[i]);
				linenump = (int*)malloc(sizeof(int));
				*linenump = linenum;
				filestr->lines = list_start((void*)linenump);
				/*record information about the word*/
				wordstr = (word_str*)malloc(sizeof(word_str));
				if(wordstr == NULL)
				{
					printf("Could not allocate mem for word list storage!\n");
					return(-1);
				}
				wordstr->word = (char*)malloc(sizeof(char)*strlen(word));
				if(wordstr->word == NULL)
				{
					printf("Could not allocate mem to store word!\n");
					return(-1);
				}
				strcpy(wordstr->word,word);
				/*create the linked list of files for the word*/
				wordstr->files = list_start(filestr);
				/*Add the word to the linkedlist, determine if the list is new*/
				if(wlist==NULL)
					wlist = list_start(wordstr);
				else
					wlist = list_add(wlist,wordstr);
				/*clear out the word*/
				word = strtok(NULL, WORDDEL);
			}
			/*free all of the memmory that was allocated for the line*/
			free(linebuf);
		}
		/*close the file for the next file*/
		fclose(fp);
	}
	/*sort all of the words*/
	wlist = mergesort(wlist,wordcmp);
	/*combine the file linkedlists for dup words*/
	wlist = mergelist(wlist,wordcmp,wordmergepost);
	/*Desplay formated index*/
	display(wlist);
	return (0);
}
Esempio n. 4
0
 ListNode *mergeKLists(vector<ListNode *> &lists) {
     if(lists.empty()) return NULL;
     int n = lists.size();
     return mergelist(lists, 0, n-1);
 }