#include <iostream>
#include <cstring>
#include <string>
usingnamespace std;
int ReverseString(char *);
struct node {
node *prev;
char c;
node *next;
};
// detects if a word is a palindrome
void makeList(constchar candidate[], node* &head, node* &tail) {
// get the length of the c-string
int len = strlen(candidate);
node* first = NULL;
node* temp = NULL;
//make first link
first = new node;
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 node;
//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(node * head, node * tail) {
while(head != tail && head < tail)
{
if(head->c == tail->c)
{
head = head->next;
tail = tail->prev;
}
elsereturnfalse;
}
returntrue;
}
// reads in the list of words
// prints out word if it is a palendrome
int main() {
node *head = NULL;
node *tail = NULL;
char words[100];
cout << "Enter text : ";
cin.getline(words,100);
makeList(words, head, tail);
// use our function to check if it's a palindrome
if (isPalindrome(head, tail)==true) {
cout << "Forward: "<< words << endl;
cout << "Reverse: ";
ReverseString(words);
cout << endl;
cout << "It's a palindrome !"<<endl;
}
else{
cout << "Forward: "<< words << endl;
cout << "Reverse: ";
ReverseString(words);
cout << endl;
cout << "Not a palindrome !"<<endl;
}
cin.get();
return 0;
}
int ReverseString(char *rev){
if(*rev!='\0')
{
ReverseString(rev + 1);
putchar(*rev);//change this.
}
return 0;
}
So here is the code , done it , but can I ask if inside my structure
1 2 3 4 5
struct node {
node *prev;
char c;
node *next;
};
Instead I wan to cancel the node*prev as my question i should not add it inside . i should only have this only
1 2 3 4
struct node {
char c;
node *next;
};
SO my question is , how i create the *prev ? for all my code above those using it?
senior help . thanks
Was using a linked list part of your assignment? Because it doesn't really offer much in this situation and is completely unnecessary except in complicating what should be a simple solution, assuming all you're after is determining if your input is a palindrome.
Read word into string, check it against its own reverse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
string one;
node currentLetter; // set to starting letter
while (1)
{
one += currentLetter.c;
if (currentLetter.node == 0) // no more letters
{ break;}
else
{ currentLetter = currentLetter->next;
}
string reverse = one;
reverse(reverse.begin(), reverse.end());
if ( one == reverse)
{ cout << "Palindrome"; }
Doing it in O(N) time and O(1) space is more interesting. This is a common approach:
1. Reverse the second half of the list (the part of the list just after its mid-point)
2. Compare if the first half of the list to the second half
3. Undo step 1. to restore the original list
int size( const node* first )
{
int n = 0 ;
while(first) { ++n ; first = first->next ; }
return n ;
}
// get the node just after the mid point of the list
node* middle( node* first, int sz )
{
int n = sz/2 + sz%2 ;
while( n-- ) first = first->next ;
return first ;
}
node* reverse( node* first )
{
node* prev = nullptr ;
while(first)
{
node* next = first->next ;
first->next = prev ;
prev = first ;
first = next ;
}
return prev ;
}
bool is_palindrome( node* first )
{
bool palindrome = true ;
// reverse the subsequence just after the middle of the list
node* mid = reverse( middle( first, size(first) ) ) ;
// compare first half of list with the second half
for( node* p = mid ; p && palindrome ; first = first->next, p = p->next )
if( first->c != p->c ) palindrome = false ;
reverse( mid ) ; // restore the original list
return palindrome ;
}
// given a string, make a singly linked list of chars
// allocate nodes for each char in the string and link them up
// return the first node of the list (which holds the first char)
node* make_list( const std::string& str ) ;
// given the first node of a list, print out the sequence of chars in the list
void print_list( const node* first ) ;
Once you have done that, write a main() which accepts a string from the user, calls make_list() to create a list, and then calls print_list() to print out the list. Make sure that this is working correctly; and then we can move forward.