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.