I am not too familiar with pointers and structs (I've been reading about it but it's still not clicking completely). This assignment involves writing missing code for functions and if someone could help me out with them, greatly appreciated. I'm definitely trying to learn this so some form of explanation would also be very helpful! thanks again!
Code
/*
* Topics: Multidimensional Arrays, Pointers, Dynamic Allocation, Structs
*
*/
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
/* Globally defined constants
*/
const int MAX_WORD_SIZE = 20;
/* Words structure
*
* This has three member variables list: which will contain a list
* of cstrings, size: the max number of words the list can store and
* num_words: the number of cstrings in list.
*/
struct Words {
unsigned int num_words; // Number of words currently in list
unsigned int max_words; // The total size allocated to the list.
char **list; // The list storing the words
};
/* Function prototypes:
* To be implemented
*/
Words * newList(const unsigned int max_words);
Words * newList(const char words[]);
int deleteList(Words *p_w);
int printList(Words *p_w);
int appendList(Words *p_w, const char words[]);
int appendList(Words *dst, const Words *src);
char* findWord(const Words *p_w, const char word[]);
int removeWord(Words *p_w, const char word[]);
/* Main function
*
* Note the newList and deleteList functions must be implemented before appendList so the
* calls to appendList are initally commented out.
* Your code should work with the main below. Including the appendList functionality that is initially commented out.
*/
int main()
{
Words *pokemon, *tb;
char *word;
int ok=0;
string poke="charmeleon mewtwo pikachu charmander squirtle caterpie raichu butterfree abra kadabra alakazam slowpoke spoink"; //declaring a string
char team_a[]="rayquaza mew snorlax"; //creating array c_string
string team_b="weavile shuckle ampharos toxicroak mrmime";
cout << poke.c_str() << endl;
pokemon=newList(5); // Words * newList(const unsigned int max_words);
ok=deleteList(pokemon); // int deleteList(Words *p_w);
cout << "OK? "<< ok << endl;
pokemon=newList(poke.c_str()); // Words * newList(const char words[]);
tb=newList(team_a); // Words * newList(const char words[]);
cout << "*pokemon"<< endl;
printList(pokemon); // int printList(Words *p_w);
cout << "*tb"<< endl;
printList(tb); // int printList(Words *p_w);
/*
//
// Note the newList and deleteList functions must be implemented before appendList so the
// calls to appendList are initally commented out.
ok=appendList(tb,team_b.c_str()); // int appendList(Words *p_w, const char words[]);
cout << "*tb"<< endl;
printList(tb); // int printList(Words *p_w);
ok=appendList(pokemon,tb); // int appendList(Words *dst, const Words *src);
cout << "*pokemon"<< endl;
printList(pokemon); // int printList(Words *p_w);
word=findWord(pokemon,"charmeleon"); // char* findWord(const Words *p_w, const char word[]);
cout << "*word "<< *word << " word " << word << " address " << &word << endl;
ok=removeWord(pokemon,"squirtle"); // int removeWord(Words *p_w, const char word[]);
cout << "*pokemon"<< endl;
printList(pokemon); // int printList(Words *p_w);
*/
return 0;
}
/* Function: newList
*
*
* Takes the max_words of the list and creates a new Words
* with the given max_words without assigning any words, sets
* num_words to zero. returns the new list to the calling function.
* max_words must be greater than zero, otherwise newList returns
* NULL.
*/
Words * newList(const unsigned int max_words)
{
if(max_words < 1)
return NULL;
// Allocate list
Words * p_w = new Words;
// FUNCTION CODE GOES HERE
return p_w;
}
/* Function: newList
*
* Overloaded version of newList, this one takes a character
* array with words separated by spaces, for example:
*
* "charmeleon mewtwo pikachu charmander squirtle caterpie raichu butterfree abra kadabra alakazam slowpoke spoink"
*
* has four words. This newList will allocate enough space
* for the words, then the list will be initialized to those
* words and returns the list to the calling function. If
* words is empty, newList does nothing and returns NULL.
*/
Words * newList(const char words[])
{
// duplicate the words to handle literal input
char *dup = strdup(words);
int len = strlen(dup);
//Can't allocate zero memory
if(len == 0)
return NULL;
Words *p_w = NULL;
// FUNCTION CODE GOES HERE
return p_w;
}
/* Function: deleteList
*
* Takes a dynamically allocated Words and deletes
* the list and then the Words itself, returns 0.
* If the Words is NULL deleteList does nothing and
* returns -1;
*/
int deleteList(Words *p_w)
{
if(p_w == NULL)
return -1;
// FUNCTION CODE GOES HERE
return 0;
}
/* Function: printList
*
* Prints the words in the Words on a single Line
* with a single space between words. There is no
* space after the last word, but there is a newline,
* returns 0 for successful print. If p_w is NULL,
* printList does nothing returns -1.
*/
int printList(Words *p_w)
{
if(p_w == NULL)
return -1;
// FUNCTION CODE GOES HERE
return 0;
}
/* Function: appendList
*
* Takes a Words and a character array consisting of words
* separated by spaces, for example:
*
* "charmander squirtle caterpie raichu"
*
* Contains four words. appendList will take the Words
* and append these words to list. If the Words does not
* have enough space, appendList will dynamically allocate
* more space to list to allow these new words to fit.
* Returns the number of words appended. If words is empty
* or if p_w is NULL then appendList does nothing and returns -1.
*/
int appendList(Words *p_w, const char words[])
{
// duplicate the words to handle literal input
char *dup = strdup(words);
int len = strlen(dup);
if(p_w == NULL || len == 0)
return -1;
int word_count = 0;
// FUNCTION CODE GOES HERE
return word_count;
}
/* Function: appendList
*
* Takes two Wordss, appends the contents of src
* to dst. If dst does not have enough space
* appendList should dynamically allocate additional space
* then append the contents of src to dst, returns
* 0 on success. If either Words is NULL appendList does
* nothing and returns -1.
*/
int appendList(Words *dst, const Words *src)
{
if(dst == NULL || src == NULL)
return -1;
// FUNCTION CODE GOES HERE
return 0;
}
/* Function: findWord
*
* Finds the first occurrence of the word in the Words
* returns a pointer to that word in the list. Otherwise
* returns NULL. If p_w is NULL return NULL
*/
char* findWord(const Words *p_w, const char word[])
{
// FUNCTION CODE GOES HERE
return NULL;
}
/* Function: removeWord
*
* If p_w is null, returns -1. Otherwise, searches for every
* occurrence of word[], and removes that word of the list, returns
* the number of words removed.
*
*/
int removeWord(Words *p_w, const char word[])
{
int num_removed = 0;
if(p_w == NULL)
return -1;
else
{
num_removed++;
}
return num_removed;
}
/*in c++, the arrow operator is used to access number variables and function when using a pointer
to an object and the dot operator when accesing objects directly
example
Words *pokemon, poke;
pokemon ->list //pointer acess
*(pokemon) ->list//pointer access
poke.list // object access
/*/