sorting

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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
Last edited on
First the Meaning attribute is a string not a pointer. Second, use the initializer list of the constructor. Third, just use 0. The NULL macro is not really necessary and is ill-advised. Lastly change the name of the string attribute. Using the class name for a variable name seems confusing.

1
2
3
Meaning::Meaning() : next(0), name()
{
}


I'd recommend using std::list rather than writing your own list. Don't reinvent the wheel. Your program is hard enough to write as it is.
http://cplusplus.com/reference/stl/list/

The constructor for IndDict is fairly simple. It only has 1 class attribute which is the array. You need to use memset to zero initialize the array or provide a simple constructor for the struct type just like you do for the meaning class. It would just initialize the pointer to zero and call the default constructor of the string attribute.

Last edited on
thanks for your reply,
at this point am not very sure i understood where i made a mistake by your explanation.
First the Meaning attribute is a string not a pointer. Second, use the initializer list of the constructor. Third, just use 0. The NULL macro is not really necessary and is ill-advised. Lastly change the name of the string attribute. Using the class name for a variable name seems confusing.


thanks for the tip about std::list im reading up on it i might be able to work it out.

and i got to do the constructor for IndDict
1
2
3
4
5
6
7
8
IndDicT::IndDicT()
{
  for(int i=0; i < MAX_TABLESZ; i++)
    {
      dictionary[i].word = "";
    }
}

do you think this looks about right?
thanks for your help
Last edited on
Topic archived. No new replies allowed.