kat_stephens 0 Newbie Poster

I've started on an assignment and just can't get it right PLEASE help! The assignment is :Write a program that will do the following:

1.) Continuously prompt the user for words and store the words in a linked list.

Do not use an array or in any way pre-allocate any memory.

Use malloc() to request memory for each word as it is read.

2.) After the user has completed entering words, print the words in the

order that they were entered by the user. Then print them in the

reverse order from which they were entered.

After each word is printed in reverse, delete it from the linked list.

After the word is deleted, return the memory using free().

When the program ends the list should be empty.

Your program should allow for the possible storage of any number of words. Each word should allow a maximum size of 15 letters. Your program should make certain that this word length is not exceeded so that the program does not crash if longer words are entered by the user. Any word entered that is more than 15 letters should be discarded.

A sample run is shown below.

Sample Run

Enter a word: four

Do you have more words? yes or no: y

Enter a word: score

Do you have more words? yes or no: y

Enter a word: and

Do you have more words? yes or no: y

Enter a word: thiswordismuchtoobig

Do you have more words? yes or no: y

Enter a word: seven

Do you have more words? yes or no: y

Enter a word: years

Do you have more words? yes or no: y

Enter a word: ago

Do you have more words? yes or no: n

four

score

and

seven

years

ago

ago

years

seven

and

score

four

This is the code that I have now:

#include <stdio.h>
#include <string.h>
#define BIG 999
#define MAX 16

char getWord(char *word);

int main(void)
{
  char *tmpPtr=NULL;
  char *prevPtr=NULL;
  char *newPtr=NULL;
  char temp[MAX];
  
  struct node
  {
     char word[MAX];
     struct node *next;
  };
  
  struct node *list; /*list will point to the first node in the list*/
  
  /*Putting the first word in the list*/
  getWord(temp);
  list = (struct node *)malloc(sizeof(struct node));
  strcpy(list->word, temp);
  list->next = NULL;
  
  /*Getting the other words*/
  getWord(temp);
  newPtr = (struct node *)malloc(sizeof(struct node)); /*get the memory*/
  strcpy(newPtr->word, temp); /*load the node*/
  newPtr->next = NULL;
  
  /*Place the word in it's respective sorted position*/
  tmpPtr = list;
  prevPtr = NULL;
  while(strcmp(newPtr->word, tmpPtr->word) > 0)
  {
     prevPtr = tmpPtr;
     tmpPtr = tmpPtr->next;
     if(tmpPtr==NULL) /*End of the line*/
        break;
  }
  
  /*Insert the new node in sorted order*/
  if(tmpPtr==NULL)
  {    /*node goes at the end of the list*/
     prevPtr->next = newPtr;/*link new node to the current last node*/
  }
  else
     if(prevPtr==NULL)
     {    /*node goes at the front of the list*/    
        newPtr->next=tmpPtr;/*link new node to previous firs tnode*/
        list = newPtr;/*link list pointer to new node*/
     }
  else
  {   /*node goes before tmpPtr*/
      prevPtr->next=newPtr; /*link previous node to the new node*/
      newPtr->next=tmpPtr; /*link new node to next node*/
  }
  
  free(tmpPtr); /*tmpPtr is no longer need*/
  system("PAUSE");	
  return 0;
}


char getWord(char *word)
{
  char get[BIG];
  scanf("%s", get);
  while(strlen(get) !< MAX)
     printf("Error: Word is too long!");
     scanf("%s", get);
}