Now that I have your attention, I need some much appreciated help if somebody has a few minutes.
I'm working with linked lists right now and I have a program that stores a letter in a series of nodes then displays the list of characters.
What I need the program to do is be able to have letters added to the list by the user, as well as be able to delete any particular letter. Finally, after the program displays the list it must count the instances of each letter and display that value as well, but I think I have this part working.
Here is the code so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//main.cpp
#include "def0514.h"
#include "node0601.h"
int main()
{
Node head('a');
head.Insert('b');
head.Insert('c');
head.Insert('b');
head.Insert('b');
int count = head.HowMany('a');
cout << "There are " << count << " instances of a\n";
count = head.HowMany('b');
cout << "There are " << count << " instances of b\n";
cout << "\n\nHere's the entire list: ";
head.Display();
cout << endl;
return 0;
}
|
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
|
//def0514.h
#ifndef DEFINED
#define DEFINED
#include <iostream>
using namespace std;
const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
const int minPos = 2;
const int maxPos = 10;
const int minLetters = 2;
const int maxLetters = 26;
#define DEBUG
#ifndef DEBUG
#define ASSERT(x)
#else
#define ASSERT(x) \
if (! (x)) \
{ \
cout << "Error!! Assert " << #x << " failed\n"; \
cout << " on line " << _LINE_ << "\n"; \
cout << " in file " << _FILE_ << "\n"; \
}
#endif
#endif
|
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
|
//node0601.cpp
#include <iostream>
using namespace std;
#include "node0601.h"
Node::Node(char c):
myChar(c),nextNode(0)
{
}
Node::~Node()
{
if ( nextNode )
delete nextNode;
}
void Node::Display() const
{
cout << myChar;
if ( nextNode )
nextNode->Display();
}
int Node::HowMany(char theChar) const
{
int myCount = 0;
if ( myChar == theChar )
myCount++;
if ( nextNode )
return myCount + nextNode->HowMany(theChar);
else
return myCount;
}
void Node::Insert(char theChar)
{
if ( ! nextNode )
nextNode = new Node(theChar);
else
nextNode->Insert(theChar);
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
//node0601.h
class Node
{
public:
Node(char c);
~Node();
void Display () const;
int HowMany (char c) const;
void Insert (char c);
private:
char GetChar ();
Node * GetNext ();
char myChar;
Node * nextNode;
};
|
If you run this you'll notice that it only displays the list abcbb and states the instances of a and b. So again, I need to be able to add characters to this list as well as delete them, then display the list with the number of instances of each letter.
I don't expect anyone to write this program for me, but if anyone has some advice on how to approach this or a little snippet of code to set me off in the right direction I would really appreciate it. My teacher is a total deadbeat, this is his last semester before retirement and he hasn't taught us anything at all so I'm really at a loss on this one. Thanks!