struct problem

i am doing a program that user can input the filename, and i can run in to count the word frequency using link list.
i have 5 list operation, like create, add, search, print, and delete.

BUT the main problem i faced is when doing the struct, i don't know how to define the structure? if i using char* in word, then i don't know how to get the string(word) into the link list when i using char* to define now....
anybody can help me. really don't know how to do

struct Node {
int count;
char* word;
struct Node* next;
struct Node* theList;
};
Just create a dynamic memory and store it in data into it, then simply point that address into 'word' variable.

1
2
3
4
5
6
7
//when adding new word
struct Node N1;
N1.word = (cahr*) malloc (128);
strcpy (N1.word, "String");

//when deleting existing word
delete N1.word;


Is there any problem for you to do this?
Alternatively, you can define word to be a string class rather than a char*. Use the strings class for strings unless you have a good reason not to.
i not really understand as just beginner. can u explain more?
so do u mean my original struct doesn't work?
and also in main function, when my program read a txt file, there are many string, when i created the fitst node successful, then in main, how can i put the string (getcontent) into a char* word?
is it yr ways? and struct can put in main??

int main(int argc, char** argv)
{
string getcontent;
ifstream openfile (argv[1]);
while (! openfile.eof())
{
openfile>>getcontent;
}
}
re: kbw

as i see many people said when using link list should prevent to use string. but i don't know why. is there any problem?

also is i don't know why i not work while using string, but char * is ok. is it because i use cygwin to compile??
i not really understand as just beginner. can u explain more?

I'll digress and explain strings.

The C and C++ Languages don't have a built in string type, but strings are provided thru their standard libraries.

In C, a string is an array of chars. For example:
 
char name[] = "wendy";
declares an array 6 chars long and assigned the characters: 'w', 'e', 'n', 'd', 'y', 0 to the content. Note that strings in C have a 0 or NULL to mark the end.

Here's another example:
 
char* place = "Rye";
This creates an array of 4 chars with "Rye", and declares a pointer to a char, and assign's the address of that string to place. So place point's to the string "Rye".

When you declare your struct as:
1
2
3
4
struct Node
{
    char* word;
};
you're saying a Node has a pointer to a string. You have the reponsibility of ensuring that that pointer points to something. If you're a beginner that's hard. If you're an expert that's hard to do correctly all the time.

An alternative is:
1
2
3
4
struct Node
{
    char word[24];
};
This says your Node has enough space for a string 23 chars long including the trailing NULL. If you have a short string like "hello", you'll be wasting the extra space, and you can't hold a string that exceeds your 23 character limit.

A better definition is:
1
2
3
4
struct Node
{
    string word;
};
In this case, the use of string takes care of all the mucky detail, and you can assign a string of any length to it. string is a C++ standard library class.

when my program read a txt file ... how can i put the string into a char* word?
If your Node's word is a string instead of a char*, you assign it.

I've put an example below. This just shows how you'd do the assignment. It doesn't help with your linked list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
#include <string>

using namespace std;

struct Node
{
    string word;
};

int main(int argc, char** argv)
{
    Node node;
    string getcontent;
    ifstream openfile(argv[1]);
    while (openfile)
    {
        openfile>>getcontent;
        node.word = getcontent;
    }
    return 0;
}
Last edited on
thanks for your information, it is very very useful, and help me a lot.
but in struct , seems string cannot be used, any suggestion?
string can be used just fine in a struct, it just means the struct is no longer a POD, so you can do raw read/writes or memcpy's on it (but you probably shouldn't do that anyway).
If your instructor insists that you don't use string in your Node, then we may as well face the use of char*.

Remember a char* is a pointer to char or an array of chars. So where do we make it point to?

When you declare a variable in a function like openfile and getcontent in the example above, they are said to be declared on "the stack". When you call a function, the parameters are pushed onto the stack along with the return address, then the function executes, and local variables are created at the new top of the stack. When the function returns, the local variables are destroyed and the stack is popped, returning you to sort of where you were to begin with.

There's also an area of memory called the heap where you can get some memory and return it when done. In C, you access this heap with malloc/free. In C++, you're encouraged to use new/delete.

I'll rewrite the example above using Node with char* and using new/delete.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Node
{
    char* word;
};

int main(int argc, char** argv)
{
    string getcontent;
    ifstream openfile(argv[1]);
    while (openfile)
    {
        openfile>>getcontent;

        Node node;
        node.word = new char[getcontent.size() + 1];  // allocate space for string
        strcpy(node.word, getcontent.c_str());  // set value
        cout << node.word << endl; // use value
        delete [] node.word;  // release space when done
    }
    return 0;
}
i succeeded to get the char point into the struct of add
but how to print out node.word = new char[getcontent.size() + 1]
the char array created by word char
Last edited on
but how to print out node.word
Look at the example.
If I got it right and you are working with files and counting words in it just use Perl. In Perl you can write something like that in around 5 lines.

http://www.perlmonks.org/index.pl?node=Tutorials
Topic archived. No new replies allowed.