pointers with linkList

I really would like to understand what's going on with this code if someone has any idea about it pls help out. what does every line say?!!!!

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

//make first link
first = new CharLinkD;
first->prev = NULL;
first->next = NULL;
first->c = candidate[0];
closed account (3hM2Nwbp)
Firstly, I'll say that

A) It's difficult to say based on what code you've shown
B) The code violates common-sense encapsulation
C) It's a bit redundant

1
2
3
4
5
6
7
8
CharLinkD* first = NULL;
CharLinkD* temp = NULL;

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



Line 1: Create a null pointer to a CharLinkD object
Line 2: Create a null pointer to a CharLinkD object
Line 5: Create a new CharLinkD object and assign it to `first`
Line 6: Access the `prev` field of `first` and assign it NULL
Line 7: Access the `next` field of `first` and assign it NULL
Line 8: Access the `c` field of `first` and assign it `candidate[0]`
I'm assuming that you already knew that, but that's all that I can say about that code.


Gripes about the code (and reasons why):

1) `prev`, `next`, and `c` are publicly accessible
-- Users of the code can easily break a list object by changing what one of the nodes are pointing to. The art of effective programming includes absolutely restricting what the end-users shouldn't do as opposed to politely warning them not to.

2) `prev`, and `next` should be set NULL in the `CharLinkD` constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class CharLinkD
{
  public:
    CharLinkD(void) : prev(NULL), next(NULL)
    {
    }
  private:
    CharLinkD* prev;
    CharLinkD* next;
};

int main()
{

  CharLinkD dummy; // <-- prev / next are set NULL in the constructor

  /* As Opposed To */

  CharLinkD dummy;
  dummy.next = NULL;
  dummy.prev = NULL;

  return 0;
}


3) Need to see more code for a more detailed explanation.
Last edited on

I really really appreciate your helpful Luc Lieber. I just wish one thing on my whole life is that to be professional like you in C++. By the way 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.

here is the code:
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
94
#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);


	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;

	//YOUR CODE HERE

	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();
}
Topic archived. No new replies allowed.