Just Explain

Hi guys
I would like to understand the code and from where i can start.
I need write a function that goes through a linked list and see if the words in the list are palindromes. So, I need to go through the letters in the word one way and compare that with going through the letters of the word the opposite way and if they match then the word is a palindrome and I return true.

This function takes the head and the tail of the doubly linked list and returns true if the string is a palindrome, false otherwise.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

const int LINE_BUFFER_SIZE = 256;

struct CharLinkD {

CharLinkD *prev;
char c;
CharLinkD *next;

};

// detects if a word is a palindrome
void makeList(const char candidate[], CharLinkD* &head, CharLinkD* &tail) {

	// get the length of the c-string
	int len = strlen(candidate);     // why is the program use this


	CharLinkD* first = NULL;
	CharLinkD* temp = NULL;

	//make first link
	first = new CharLinkD;
	first->prev = NULL;
	first->next = NULL;
	first->c = candidate[0];

	//make head and tail point to it
	head = first;
	tail = first;

	//make the list

	for(int i = 1; i < len; i++)
	{
		//make a new link for each character left
		temp = new CharLinkD;

		//make a link that goes back to tail
		temp->prev = tail;

		//link forward is null
		temp->next=NULL;

		temp->c = candidate[i];

		//set up link backward to end of list
		tail->next = temp;

		//new tail is now last element
		tail = temp;
	}
}

bool isPalindrome(CharLinkD * head, CharLinkD * tail) {
	bool answer = true;

	

	return answer;
}

// reads in the list of words
// prints out word if it is a palendrome
int main() {

	CharLinkD *head = NULL;
	CharLinkD *tail = NULL;

	// open a file for READING
	ifstream infile("words");

	// create a buffer to hold a line of text from the file
	char line[LINE_BUFFER_SIZE];

	// while not at the end of file
	while (!infile.eof()) {
		// get a line of the file into the buffer
		infile.getline(line, LINE_BUFFER_SIZE);


		makeList(line, head, tail);
		// use our function to check if it's a palindrome
		if (isPalindrome(head, tail)) {
			cout << line << endl;
		}
	}
	infile.close();
}
pls pls I just want to understand it that's all. why no one can help about it
hey I am still waiting to someone who can just give some an explanation to this code
no expert around here to tell me :(
You're gonna have to give me a bit. I need to break it down so I can post the answers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const int LINE_BUFFER_SIZE = 256;

struct CharLinkD {

CharLinkD *prev;
char c;
CharLinkD *next;

};

int main() {

	CharLinkD *head = NULL;
	CharLinkD *tail = NULL;


Here two of the above structures are being made ('head' and 'tail'). They will hold the values of the link list.

1
2
3
4
5
6
7
8
9
10
// open a file for READING
	ifstream infile("words");

	// create a buffer to hold a line of text from the file
	char line[LINE_BUFFER_SIZE];

	// while not at the end of file
	while (!infile.eof()) {
		// get a line of the file into the buffer
		infile.getline(line, LINE_BUFFER_SIZE);


Here we open a file called "words" which is in the same directory as the executable file. Then we define a character array of 256 characters. This is to hold any value read in from the file.
We then begin a loop that cycles through every line in the text file to pick up a possible palindrome and test it in the next functions.

1
2
3
4
5
6
makeList(line, head, tail);

void makeList(const char candidate[], CharLinkD* &head, CharLinkD* &tail) {

	// get the length of the c-string
	int len = strlen(candidate);     // why is the program use this 

Now we enter the makeList Function. It takes as arguements a character array (c-string) and the head and tail of a Double Link List Structure.
the strlen function is from the c-string library included at the top of the program. It counts the characters up to the first NULL terminating character and returns that as the value. It store that in the int for later use in this function.

1
2
3
4
5
6
7
8
9
10
11
12
	CharLinkD* first = NULL;
	CharLinkD* temp = NULL;

	//make first link
	first = new CharLinkD;
	first->prev = NULL;
	first->next = NULL;
	first->c = candidate[0];

	//make head and tail point to it
	head = first;
	tail = first;

This makes two more double linked lists then makes the first character of the read in value into the structure. Then sets it as the first in the double linked list.
1
2
3
	//make the list

	for(int i = 1; i < len; i++)

A loop. Here is the use of len from above as the condition to stop creating more members of the double linked list once we used up all the characters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	{
		//make a new link for each character left
		temp = new CharLinkD;

		//make a link that goes back to tail
		temp->prev = tail;

		//link forward is null
		temp->next=NULL;

		temp->c = candidate[i];

		//set up link backward to end of list
		tail->next = temp;

		//new tail is now last element
		tail = temp;
	}
}


Here is simple.
We make a new element of the double linked list.
wow : ) it was very helpful man I really appreciate your efforts.
to me it is a big help.
Topic archived. No new replies allowed.