Advertisement
Guest User

Untitled

a guest
Sep 14th, 2011
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int LINE_BUFFER_SIZE = 256;
  7.  
  8. struct CharLinkD {
  9.  
  10. CharLinkD *prev;
  11. char c;
  12. CharLinkD *next;
  13.  
  14. };
  15.  
  16. // detects if a word is a palindrome
  17. void makeList(const char candidate[], CharLinkD* &head, CharLinkD* &tail) {
  18.  
  19.         // get the length of the c-string
  20.         int len = strlen(candidate);
  21.  
  22.  
  23.         CharLinkD* first = NULL;
  24.         CharLinkD* temp = NULL;
  25.  
  26.         //make first link
  27.         first = new CharLinkD;
  28.         first->prev = NULL;
  29.         first->next = NULL;
  30.         first->c = candidate[0];
  31.  
  32.         //make head and tail point to it
  33.         head = first;
  34.         tail = first;
  35.  
  36.         //make the list
  37.  
  38.         for(int i = 1; i < len; i++)
  39.         {
  40.                 //make a new link for each character left
  41.                 temp = new CharLinkD;
  42.  
  43.                 //make a link that goes back to tail
  44.                 temp->prev = tail;
  45.  
  46.                 //link forward is null
  47.                 temp->next=NULL;
  48.  
  49.                 temp->c = candidate[i];
  50.  
  51.                 //set up link backward to end of list
  52.                 tail->next = temp;
  53.  
  54.                 //new tail is now last element
  55.                 tail = temp;
  56.         }
  57. }
  58.  
  59. bool isPalindrome(CharLinkD * head, CharLinkD * tail) {
  60.         bool answer = true;
  61.  
  62.         //YOUR CODE HERE
  63.  
  64.         return answer;
  65. }
  66.  
  67. // reads in the list of words
  68. // prints out word if it is a palendrome
  69. int main() {
  70.  
  71.         CharLinkD *head = NULL;
  72.         CharLinkD *tail = NULL;
  73.  
  74.         // open a file for READING
  75.         ifstream infile("words");
  76.  
  77.         // create a buffer to hold a line of text from the file
  78.         char line[LINE_BUFFER_SIZE];
  79.  
  80.         // while not at the end of file
  81.         while (!infile.eof()) {
  82.                 // get a line of the file into the buffer
  83.                 infile.getline(line, LINE_BUFFER_SIZE);
  84.  
  85.  
  86.                 makeList(line, head, tail);
  87.                 // use our function to check if it's a palindrome
  88.                 if (isPalindrome(head, tail)) {
  89.                         cout << line << endl;
  90.                 }
  91.         }
  92.         infile.close();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement