hi guys im creating a dictionary which would show the word and the meaning of the word
this is what i have done so far, i created a class called meaning which stores the meaning of the word

#ifndef MEANING
#define MEANING
#include<string>
class Meaning
{
public:
	Meaning();
	~Meaning(); 

// setter methods
	void setmeaning(string Meaning);
	void setNext(Meaning*);

// getter methods
	void printMeaning(); // display the meaning of a word on the screen
	string getMeaning(); // return the meaning of a word
	Meaning* getNext(); // get the next meaning for a word if it exists
private:
	string Meaning;
	Meaning* next;
};
#endif

and i created the single linked list class list.h

#ifndef LIST
#define LIST
#include<string>
#include"Meaning.h"

class List
{
public:
	List();
	~List();
	void isnertAtRear(string meaning);
	void printList() const;
	bool isEmpty() const;
private:
	meaning *head;
};
#endif

i was trying to create constructor for meaning.h class but it doesnt seem to be working at all i think im doing something wrong here im not very confident with constructors just concept of constructor seem un-understandable to me so i always get them wrong

#include<iostream>
#include<string>
#include"Meaning.h"

using namespace std;

Meaning::Meaning()
{
	Meaning = 0;
	next = NULL;
}
void Meaning::setMeaning(string w)
{
	Meaning = w;
}

void Meaning::setNext(Meaning* m)
{
	next = m;
}
int Meaning::getMeaning()
{
	return Meaning;
}
Meaning* Meaning::getNext()
{
	return next;
}

here is the definition file i created
IndDict.h

#ifndef IDT
#define IDT
#include<string>
#include"List.h"

const int MAX_TABLESZ = 23;

class IndDict
{
public:
	IndDict();
	~IndDict();


	int hashKey(string);
	void insert(string word, string meaning);
	List* lookUP(string word);
	void traverse();

private:
	struct{
		string word;
		List*meaningList;
	}dictionary[Max_TABLESZ];
};
#endif

and this is implementation file IndDict.cpp

#include<iostream>
#include<string>
#include"IndDict.h"
using namespace std;

IndDict::IndDict()
{
}

IndDict::hashKey(string word)
{
	int result = 0;
	for (int i=0; i < word.length(); i++)
	{			
	result = result + static_cast<int>(word[i]);
	}
	return (result % MAX_TABLESZ); 
}

IndDict::insert(string word, string meaaning)
{
  int i, home;

  i = hashKey(word);

    if (dictionary[i].word != "")
      {
    home = i;
    do
      {
        i = i + c;
        if(i >= MAX_TABLESZ)
          {
        i = i - MAX_TABLESZ;
          }
      }
    while ((dictionary[i].word != "") && (home != i));
      }
    if(dictionary[i].word == "")
      {
    dictionary[i].word = word;
        dictionary[i].meaningList->insertAtRear(meaning); 
      }
}

List* IndDict::lookUP(string word)
{
  int i, home;

  i = hashKey(word);

  if(dictionary[i].word != word)
    {
      home = i;
      do
        {
      i = i + c;
      if(i >= MAX_TABLESZ)
        {
          i = i - MAX_TABLESZ;
        }
    }
      while((dictionary[i].word != word) && (home != i));
    }
    if(dictionary[i].word == word)
      {
    return dictionary[i].meaningList;
      }
    else 
      {
    return NULL;
      }

}
 void traverse();
 {
 }

well what i really need help on is the last traverse function in IndDict.cpp file which should print out the non empty dictionary entries in alphabetical order. and it has to do that by using standard library "list" class to display the "word" and it meaning in alphabetical order or any other way as long as it displays in alphabetical order.

also im not sure how to make a constructor for IndDict.cpp
and if there is someone really kind could you tell me if there are any other error
thanks

Recommended Answers

All 2 Replies

anybody!!! hasn't anyone read this post? any help would be greatly appreciated

Do you know how to google? The code for traversing a linked list is an easy concept, the only easy thing I thought easy about linked lists.

node *temp = start_ptr;
//create a temp ptr to traverse your list

while(temp != NULL)
{

  if(whatever condition you need)
       cout<<"data"<<temp->data<<endl;

  temp = temp->next;
}
//Traverses the list till temp is null ie The end of the list!

There is nothing more to traversing a list for the purpose of printing data to the screen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.