M
mark.martinez2
I'm having some trouble with a linked list of strings. After I insert
a number of items, I call a print function and every node in the
linked list has the value from the last item I entered into the linked
list. Any ideas? Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct listNode { /* self-referential structure */
char *data;
int wordLength;
struct listNode *nextPtr;
};
typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
void insert(LISTNODEPTR *, char *, int);
void printList(LISTNODEPTR);
int isChar(char);
void resetString(void);
char word[50];
main()
{
FILE *fptr;
LISTNODEPTR startPtr = NULL;
int maxLine = 0;
char file_name[20];
int count = 0;
char c;
char d;
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);
int tempCount = 0;
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;
printf("%s\n",word);
}
/* if c and d are both characters */
else if ((isChar(c) == 1) && (isChar(d) == 1)) {
word[count] = c;
c = d;
count++;
printf("%s\n",word);
}
/* if c is character and d is space, return, tab, or new line */
else if ((isChar(c) == 1) && (isChar(d) == 0)) {
word[count] = c;
insert(&startPtr, word, count);
c = d;
count = 0;
printf("word: %s\n",word);
resetString();
}
/* if d is start of new word */
else if ((isChar(d) == 1) && (isChar(c) == 0)) {
c = d;
printf("%s\n",word);
}
}
fclose(fptr);
printList(startPtr);
}
void resetString(void) {
int i = 0;
for (i = 0;i < 50;i++) {
word = '\0';
}
}
int isChar(char c) {
if ((c >= 33) && (c <= 126)) {
return 1;
}
return 0;
}
/* insert into list */
void insert(LISTNODEPTR *sPtr, char *value, int length)
{
LISTNODEPTR newPtr, previousPtr, currentPtr;
newPtr = malloc(sizeof(LISTNODE));
if (newPtr != NULL) { /* is space available */
newPtr->data = value;
newPtr->wordLength = length;
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("%s 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("%s --> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
printf("NULL\n\n");
}
}
a number of items, I call a print function and every node in the
linked list has the value from the last item I entered into the linked
list. Any ideas? Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct listNode { /* self-referential structure */
char *data;
int wordLength;
struct listNode *nextPtr;
};
typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
void insert(LISTNODEPTR *, char *, int);
void printList(LISTNODEPTR);
int isChar(char);
void resetString(void);
char word[50];
main()
{
FILE *fptr;
LISTNODEPTR startPtr = NULL;
int maxLine = 0;
char file_name[20];
int count = 0;
char c;
char d;
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);
int tempCount = 0;
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;
printf("%s\n",word);
}
/* if c and d are both characters */
else if ((isChar(c) == 1) && (isChar(d) == 1)) {
word[count] = c;
c = d;
count++;
printf("%s\n",word);
}
/* if c is character and d is space, return, tab, or new line */
else if ((isChar(c) == 1) && (isChar(d) == 0)) {
word[count] = c;
insert(&startPtr, word, count);
c = d;
count = 0;
printf("word: %s\n",word);
resetString();
}
/* if d is start of new word */
else if ((isChar(d) == 1) && (isChar(c) == 0)) {
c = d;
printf("%s\n",word);
}
}
fclose(fptr);
printList(startPtr);
}
void resetString(void) {
int i = 0;
for (i = 0;i < 50;i++) {
word = '\0';
}
}
int isChar(char c) {
if ((c >= 33) && (c <= 126)) {
return 1;
}
return 0;
}
/* insert into list */
void insert(LISTNODEPTR *sPtr, char *value, int length)
{
LISTNODEPTR newPtr, previousPtr, currentPtr;
newPtr = malloc(sizeof(LISTNODE));
if (newPtr != NULL) { /* is space available */
newPtr->data = value;
newPtr->wordLength = length;
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("%s 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("%s --> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
printf("NULL\n\n");
}
}