Jump to content

Linked lists of strings (C)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Panserbjorn

Panserbjorn

    Newbie

  • Members
  • Pip
  • 2 posts
I'm working on a program to justify a block of text. The step I'm stuck on now (haven't gotten to the justification part), is reading in the text file into a linked list of strings. I'm 99% sure I have the code that identifies what is and isn't a string working, but when I print the linked list, I either get blanks or single non-regular characters in each spot. My weakness is pointers, and I suspect that my pointers are screwing this up royally. If you could take a look and help me out, I'd much appreciate it. I'm sorry if this is too much code to post on the forum (I'm new here).


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


struct listNode {  /* self-referential structure */

   char *data;

   struct listNode *nextPtr;

};


typedef struct listNode LISTNODE;

typedef LISTNODE *LISTNODEPTR;


void insert(LISTNODEPTR *, char *);

void printList(LISTNODEPTR);

int isChar(char);


main()

{


	FILE *fptr;

	LISTNODEPTR startPtr = NULL;

	int maxLine = 0;

	char file_name[20];

	int count = 0;

	char c;

	char d;

	char word[50];


	printf("Type in the name of the file containing the text: \n");

	scanf("%s",file_name);

	fptr=fopen(file_name,"r");


	int numChar; /* number of characters per line */


	printf("How many characters per line?: ");

	scanf("%d", &maxLine);


	c = fgetc(fptr);


	while (d = fgetc(fptr)) != EOF) {


			/* if two new lines in a row */

			if ((c == '\n') && (d == '\n')) {

			}


			/* if not two non-characters in a row */

			else if ((isChar(c) == 0) && (isChar(d) == 0)) {

				c = d;

			}

			/* if c and d are both characters */

			else if ((isChar(c) == 1) && (isChar(d) == 1)) {

				word[count] = c;

				c = d;

				count++;

			}


			/* if c is character and d is space, return, tab, or new line */

			else if ((isChar(c) == 1) && (isChar(d) == 0)) {

				word[count] = c;


				char *newword = malloc(sizeof(word));

				newword = word;

				insert(&startPtr, newword);


				c = d;

				count = 0;

			}



			/* if d is start of new word */

			else if ((isChar(d) == 1) && (isChar(c) == 0)) {

				c = d;

			}




	}


	fclose(fptr);


	printList(startPtr);

}



int isChar(char c) {

	if ((c >= 33) && (c <= 126)) {

		return 1;

	}

	return 0;

}


/* insert into list */

void insert(LISTNODEPTR *sPtr, char *value)

{

   LISTNODEPTR newPtr, previousPtr, currentPtr;


   newPtr = malloc(sizeof(LISTNODE));



   if (newPtr != NULL) {    /* is space available */

   	  newPtr->data = malloc(sizeof(value));

   	  newPtr->data = value;

      newPtr->nextPtr = NULL;


      previousPtr = NULL;

      currentPtr = *sPtr;


      while (currentPtr != NULL && value > currentPtr->data) {

         previousPtr = currentPtr;          /* walk to ...   */

         currentPtr = currentPtr->nextPtr;  /* ... next node */

      }


      if (previousPtr == NULL) {

         newPtr->nextPtr = *sPtr;

         *sPtr = newPtr;

      }

      else {

         previousPtr->nextPtr = newPtr;

         newPtr->nextPtr = currentPtr;

      }

   }

   else

      printf("%c not inserted. No memory available.\n", value);

}


/* Print the list */

void printList(LISTNODEPTR currentPtr)

{

   if (currentPtr == NULL)

      printf("List is empty.\n\n");

   else {

      printf("The list is:\n");


      while (currentPtr != NULL) {

         printf("%c --> ", currentPtr->data);

         currentPtr = currentPtr->nextPtr;

      }


      printf("NULL\n\n");

   }

}



#2
Panserbjorn

Panserbjorn

    Newbie

  • Members
  • Pip
  • 2 posts
Well, I figured out why I was getting odd results for the linked list.. the printList command was printing %c instead of %s. However, new problem. The test file I'm using starts with this:

A knowledge and appreciation

When I print the linked list, it starts with the word knowledge, but misses the A. Any ideas?