plz help - a program that should be really easy (linked list) 
Author Message
 plz help - a program that should be really easy (linked list)

This is an exercise I have to do using a linked list.  But I can't get it to
work.  It's supposed to read numbers from a file"addFile" (eg. 12  32  5
14), and add them to the linked list in ascending order, then print it to
the screen.  Then it reads numbers from a file called "removeFile" (eg. 12
5  14), and removes those numbers from the linked list, then prints the list
to the screen again.  But it says "list empty" every time.  It should have
been so easy, but it's not working???  Thanks for any help you can give
me...

#include <stdio.h>
#include <stdlib.h>

typedef struct numberNode NODE;

struct numberNode
{
   int number;
   NODE *nextPtr;

Quote:
};

void addToList(NODE *, int);
void removeFromList(NODE *, int);
void printList(NODE *, char *);
void readAddFile(NODE * );
void readRemoveFile(NODE * );
NODE * initializeList();

main()
{
 NODE *startPtr;
 startPtr = initializeList();
 printList( startPtr, "before adding any numbers" );
 readAddFile(startPtr);
 printList( startPtr, "after adding some numbers" );
 readRemoveFile ( startPtr);
 printList ( startPtr, "after removing some numbers ");

Quote:
}

void readAddFile (NODE * startPtr)
{
 FILE * addFile;
 int number;

 if (addFile = fopen ( "addFile", "r"))
 {
  while ( !feof( addFile))
  {
   fscanf(addFile, "%d", &number);
   addToList (startPtr, number);
  }
 }

Quote:
}

void readRemoveFile (NODE * startPtr)
{
 FILE * removeFile;
 int number;
 if ( removeFile = fopen ( "removeFile", "r"))
 {
  while ( !feof( removeFile))
  {
   fscanf(removeFile, "%d", number);
   removeFromList (startPtr, number);
  }
 }

Quote:
}

void addToList(NODE * startPtr, int newNumber )
{
 NODE *newPtr, *searchPtr, *trailingPtr;
 newPtr = (NODE *)malloc(sizeof(NODE));

 newPtr -> number = newNumber;
 newPtr -> nextPtr = NULL;

 trailingPtr = NULL;
 searchPtr = startPtr;

 while (searchPtr != NULL && newNumber > searchPtr -> number) {
  trailingPtr = searchPtr;
  searchPtr = searchPtr -> nextPtr;
 }

 if (trailingPtr == NULL) {
  newPtr -> nextPtr = startPtr;
  startPtr = newPtr;
 }
 else {
  trailingPtr -> nextPtr = newPtr;
  newPtr -> nextPtr = searchPtr;
 }

Quote:
}

void removeFromList(NODE * startPtr, int delNumber)
{
 NODE  *searchPtr, *trailingPtr, tempPtr;

 if (delNumber == startPtr -> number) {
  tempPtr = *startPtr;
  startPtr = startPtr -> nextPtr;
  free(tempPtr);
 }
 else {
  trailingPtr = startPtr;
  searchPtr = startPtr -> nextPtr;

  while (searchPtr != NULL && searchPtr -> number != delNumber) {
   trailingPtr = searchPtr;
   searchPtr = searchPtr -> nextPtr;
  }

  if (searchPtr != NULL) {
   tempPtr = searchPtr;
   trailingPtr -> nextPtr = searchPtr -> nextPtr;
   free(tempPtr);
  }
 }

Quote:
}

void printList(NODE *startPtr, char * message)
{

 NODE *p = startPtr -> nextPtr;
 printf( "\n\n %s \n\n", message);
    if (p == NULL)
   printf("\nThe list is empty \n");
  else
   while (p != NULL)
     {
       printf(" %d  ",p -> number);
       p = p -> nextPtr;
     }

Quote:
}

NODE * initializeList()
{
  NODE *startPtr;
  startPtr = (NODE *)malloc(sizeof( NODE));
  startPtr -> nextPtr = NULL;
  return startPtr;
Quote:
}



Wed, 19 Mar 2003 03:00:00 GMT  
 plz help - a program that should be really easy (linked list)


Quote:
>This is an exercise I have to do using a linked list.  But I can't get it to
>work.  It's supposed to read numbers from a file"addFile" (eg. 12  32  5
>14), and add them to the linked list in ascending order, then print it to
>the screen.  Then it reads numbers from a file called "removeFile" (eg. 12
>5  14), and removes those numbers from the linked list, then prints the list
>to the screen again.  But it says "list empty" every time.  It should have
>been so easy, but it's not working???  Thanks for any help you can give
>me...

>#include <stdio.h>
>#include <stdlib.h>

>typedef struct numberNode NODE;

>struct numberNode
>{
>   int number;
>   NODE *nextPtr;
>};

>void addToList(NODE *, int);
>void removeFromList(NODE *, int);
>void printList(NODE *, char *);
>void readAddFile(NODE * );
>void readRemoveFile(NODE * );
>NODE * initializeList();

>main()
>{
> NODE *startPtr;
> startPtr = initializeList();
> printList( startPtr, "before adding any numbers" );
> readAddFile(startPtr);
> printList( startPtr, "after adding some numbers" );
> readRemoveFile ( startPtr);
> printList ( startPtr, "after removing some numbers ");
>}
snip
>void addToList(NODE * startPtr, int newNumber )
>{
> NODE *newPtr, *searchPtr, *trailingPtr;
> newPtr = (NODE *)malloc(sizeof(NODE));

> newPtr -> number = newNumber;
> newPtr -> nextPtr = NULL;

> trailingPtr = NULL;
> searchPtr = startPtr;

> while (searchPtr != NULL && newNumber > searchPtr -> number) {
>  trailingPtr = searchPtr;
>  searchPtr = searchPtr -> nextPtr;
> }

> if (trailingPtr == NULL) {
>  newPtr -> nextPtr = startPtr;
>  startPtr = newPtr;
> }
> else {
>  trailingPtr -> nextPtr = newPtr;
>  newPtr -> nextPtr = searchPtr;
> }
>}

snip
In addToList, startPtr is a parameter.  That means that it is a copy
of the variable passed by the calling function.  When you update it,
you only update this copy.  When you exit, the update is lost.
Consequently startPtr in main is never updated and you are always
pointing at your "dummy" struct that has nextPtr set to NULL.

You should change all your functions that modify the list to return a
new value for startPtr as initializeList does.

You have an additional problem in the use of the dummy struct.  When
adding the first real struct to the list, searchPtr->number is
uninitialized.  Trying to compare it to newNumber is not a good idea.
The popular approach is to do away with initializeList and simply set
startPtr in main to NULL.  All the functions to which you pass
startPtr should then check to see if it is NULL and perform the
appropriate empty list processing if so.  Once you know startPtr is
not NULL, you can then loop through the structs until ->nextPtr is
NULL.

<<Remove the del for email>>



Thu, 20 Mar 2003 08:07:25 GMT  
 plz help - a program that should be really easy (linked list)
...

Quote:
> if (addFile = fopen ( "addFile", "r"))
> {
>  while ( !feof( addFile))
>  {
>   fscanf(addFile, "%d", &number);
>   addToList (startPtr, number);
>  }
> }

In addition to the other corrections you've gotten, the above is
not a correct way to read a file until the end.  See question
12.2 of the comp.lang.c FAQ, which is at
http://www.eskimo.com/~scs/C-faq/top.html , for a discussion of
this.  [Briefly: feof() doesn't return true until after fscanf
has failed; failure to check return value of fscanf() results in
extra node in list].

--

Lucent Technologies Software Products Group



Fri, 21 Mar 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. help...easy linked list question

2. URGENT ! HELP NEWBIE plz - easy dialog question

3. Link List to Link List, HELP friends

4. I have it kinda but I am really confused.PLease HElp

5. LNK2001 unresolved external symbol (I really am in trouble here, please help)

6. Really dumb Linked List Q

7. really having trouble with linked lists

8. Really Easy Help needed Managed c++ syntax for declaring array of unsigned long

9. link list of a linked list (chained links?)

10. help with linked list program (ll.c)

11. help with linked list program (ll.c)

12. help for a new program on linked lists

 

 
Powered by phpBB® Forum Software