
-----------------------------------
jin
Sun Nov 26, 2006 12:35 am

structs, pointers and linked list
-----------------------------------
Hey i am using a struct and forming a linked list when i output it i get the element and then a weird symbol followed by w.

Struct


struct WordListNode
{
  char *word;
  int count;
  WordListNode *next ;
};


Adding in list


void addInOrder(WordListNode *&head, char data[])
{
  WordListNode *previous = NULL ;
  WordListNode *current  = head ;
  
  // notice use of short-circuit evaluation in next line
  while (current != NULL && strcmp (current->word, data)next ;
  }
  
  WordListNode *newNode = new WordListNode ;
  newNode->word = new char [strlen(data)+1] ;
  strcpy(newNode->word, data) ;
  
  if (previous == NULL) // we're adding at the head of the list
  {
    newNode->next = head ;
    head = newNode ;
  }
  else // this works in the middle or end of list
  {
    newNode->next = current ;
    previous->next = newNode ;
  }
}


Thanks

-----------------------------------
md
Sun Nov 26, 2006 1:31 am


-----------------------------------
Except for the ugly C strings (std::string is your friend) I see nothing wrong with this code. Can you post all of your code? That would make debugging easier.

-----------------------------------
jin
Sun Nov 26, 2006 1:41 am


-----------------------------------

// Importing Libraries

# include 
# include 
# include 
# include 

using namespace std;

// Global Variable declaration

struct WordListNode
{
  char *word;
  int count;
  WordListNode *next ;
};

const int MAX_WORD_LENGTH = 25;
int Number_Words;
int Number_Words_Repeated;

// Function declaration
void Remove_Punctuation (char key [MAX_WORD_LENGTH + 1]);
void Read (WordListNode *&head, char file[]);
void Change_Case (char word [MAX_WORD_LENGTH + 1]);
void printList(WordListNode *head);
void addInOrder(WordListNode *&head, char data[]);

//////////////////////////////////////////////////////////////////////////////
/////////////////////////////   MAIN METHOD    //////////////////////////////
int main ()
{

    // Variable declaration

      WordListNode *head = NULL ;

    char key [MAX_WORD_LENGTH + 1];
    int result;

    // Function Calls
    char file[] = "test.txt";

    Read (head,  file);
    printList(head);

    cin >> result;
    return 0;
}

void printList(WordListNode *head)
{
  WordListNode *current = head ;
  
  cout next != NULL) cout next ;
  }
  cout word, data)next ;
  }
  
  WordListNode *newNode = new WordListNode ;
  newNode->word = new char [strlen(data)+1] ;
  strcpy(newNode->word, data) ;

  if (previous == NULL) // we're adding at the head of the list
  {
    newNode->next = head ;
    head = newNode ;
  }
  else // this works in the middle or end of list
  {
    newNode->next = current ;
    previous->next = newNode ;
  }
}


-----------------------------------
md
Sun Nov 26, 2006 11:02 am


-----------------------------------
Oh boy...

So g++ gives me the following warnings:

test.c: In function 'int main()':
test.c:39: warning: unused variable 'key'
test.c: In function 'void Remove_Punctuation(char*)':
test.c:72: warning: comparison between signed and unsigned integer expressions
test.c:76: warning: comparison between signed and unsigned integer expressions
test.c:85: warning: comparison between signed and unsigned integer expressions
test.c: In function 'void Change_Case(char*)':
test.c:100: warning: comparison between signed and unsigned integer expressions

While they are not serious, you may want to look into them. Second, since you are using C++ I HIGHLY recommend using std::string instead of C strings.

The problem with your code seems to stem from Read(). When you read your words you do not terminate your C string properly (you wouldn't have to with std::string!). Aside from that and some rather bizzare logic I can't see anything immediately wrong.

There are also a whole lot of code formatting things and code inconsistencies that should be fixed... ;-)

Because I am in a good mood... here ya go :P

// Importing Libraries

#include 
#include 
#include 
#include 


// Global Variable declaration

struct WordListNode
{
	std::string word;
	WordListNode *next ;
};

struct WordList
{
	unsigned short n_words;
	WordListNode *head;
};


void RemovePunctuation(std::string what);
void Read (WordList &list, std::string file);
void PrintList(WordList list);
void AddInOrder(WordList &list, std::string data);

int main ()
{
	WordList list;

    Read(list,  "test.txt");
    PrintList(list);

    return 0;
}

void PrintList(WordList list)
{
	WordListNode *current = list.head;

	std::cout next != NULL)
			std::cout next;
	}
	std::cout 