Boobs C++ Moar Boobs Linked Lists - Click here -

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!
Last edited on
Your problem has little to do with the linked list - what you want to do is have an interactive prompt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::string line;
while((std::cout << "Do you want to `add`, `list`, `delete`, or `exit`?") && std::getline(std::cin, line))
{
    if(line == "add")
    {
        //
    }
    else if(line == "list")
    {
        //
    }
    else if(line == "delete")
    {
        //
    }
    else if(line == "exit")
    {
        break;
    }
    else std::cout << "Unknown option; ";
}
Fantastic, thank you. Any insight as to making the input, or myChar, a variable?
You would do it the same way as always.
What if I wanted to make a method in the header file to do it?
Then just call the function? I'm not sure where the confusion is.
Would you mind showing me how you'd do it? If it wouldn't take too much time. I'm really new to this.
I already did - just put your code on lines 6, 10, and 14 of my code snippet.
Still can't get anywhere with this. Any code to get me going in the right direction would be appreciated.
Ignoring my example, would you be able to prompt the user and add inputted information to the linked list?
Well, what I cannot figure out is how to add or remove characters by adding or removing a node from the linked list. So I need to create a method in the header file that allows me to do that, and that's what I'm having trouble with.
You already have a member function to insert a node, you are only missing a member function to remove a node.
Yeah, but I don't know how to make that function insert a user-selected variable.
You don't need to make that function do anything differently than it already does. You need to get input from the user, and then call the function with that input being passed as a parameter.
Here's what I have so far to add a character. It works but I'd like to be able to repeatedly add, can you tweak it for me?

1
2
3
4
5
6
7
8
char x;
cout << "Input Letter: ";
cin >> x; 
head.Insert(x);
head.Display();
head.HowMany(x);
count = head.HowMany(x);
cout << "There are " << count << " instances of " << x;

Click this link and think about what line 6 is for:
http://www.cplusplus.com/forum/beginner/150531/#msg785441
I'm not so worried about if else statements, I can figure those out. It's creating the methods in the header file and using pointers with the linked list that is troubling me. I managed to get it so I can repeatedly add characters to the list but that's all my program does.
Topic archived. No new replies allowed.