Hi I am really close at getting this to work. The copy and reverse functions are the issue.
The copy only does the first and last node and non of the middle nodes. Seems like it is not moving along as it goes. How do it fix that?
Reverse is just totally messed up because the .previous node don't seem to be correct and I can't figure out why.
Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace me_Palindromic_Lists { class palindromicNode { // Dimention a new node private Node palindromicList; // Blank Constructor public palindromicNode() { palindromicList = null; } // Constructor for one character public palindromicNode(char c) { palindromicList = new Node(c); } // Constructor for a sting public palindromicNode(string s) { // Set a node to keep track Node Current = new Node(); // Keep track of the pervios node Node prevNode = new Node(); // Loop through each character in the string foreach (char c in s) { // Create a new node for the character Node palNode = new Node(c); palNode.previous = prevNode; // Check to see if the node list is blank, indicaing it is new if (palindromicList == null) // Set palnode as the new node palindromicList = palNode; else { //Make current the current Copy of the list Current = palindromicList; // Loop to the last node while (Current.next != null) Current = Current.next; } // Keep track of this node for next time prevNode = Current; // Set the next node as the new node Current.next = palNode; } } public int length { get { // Set up a node to keep track Node Current = palindromicList; // Dimention a counter to count the length int counter = 1; // Loop through each node while (Current.next != null) { // Increment the counter counter++; Current = Current.next; } // Return the counter return counter; } } // Override the ToSting public override string ToString() { // Dimention a place for the result String result = ""; // Create a note to keep track Node current = palindromicList; // Loop through each node while (current != null) { // Append the result with the next character result += current.nodeCharater; // Advance to the next node current = current.next; } // Return the result string return result; } public palindromicNode Copy() { // Dimention a node to keep track Node Current = palindromicList; Node previousNode = new Node(); // Dimention a new instance of the list palindromicNode copiedList = new palindromicNode(); // Loop through the current list while (Current != null) { // Create a new node Node newNode = new Node(Current.nodeCharater); // Set the previous node newNode.previous = previousNode; // Check if the new list is blank which indicates it is new and set equal to new node if (copiedList.palindromicList == null) { copiedList.palindromicList = newNode; } else { // Set new node as the next node copiedList.palindromicList.next = newNode; //copiedList.palindromicList = newNode; } // Remeber this as the previous node for next iteration previousNode = Current; // Advance the current node Current = Current.next; } // Return the new list return copiedList; } public palindromicNode Reverse() { // not complete palindromicNode nodeReverse = new palindromicNode(); Node Current = palindromicList; Node previousNode = new Node(); // Loop to the last node while (Current.next != null) Current = Current.next; while (Current != null) { Node reverseNode = new Node(Current.nodeCharater); reverseNode.previous = previousNode; if (nodeReverse.palindromicList == null) { nodeReverse.palindromicList = reverseNode; } else { nodeReverse.palindromicList.next = reverseNode; } previousNode = Current; Current = Current.previous; } return nodeReverse; } public bool equals(palindromicNode n) { bool result = true; Node Current = palindromicList; while (Current != null) { if (Current.nodeCharater != n.palindromicList.nodeCharater) { result = false; } Current = Current.next; n.palindromicList = n.palindromicList.next; } return result; } // Make a node class private class Node { // The character value public char nodeCharater; // Store the next node public Node next; // Store the last node public Node previous; // Constructor for a blank node. public Node() { nodeCharater = '\0'; next = null; previous = null; } //Contructor for a node with a character public Node(char character) { nodeCharater = character; next = null; previous = null; } } } }Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace me_Palindromic_Lists { class Program { static void Main(string[] args) { header(); StreamReader myStream = new StreamReader("data.txt"); string[] palindromeString = myStream.ReadLine().ToString().Split(' '); foreach (string item in palindromeString ) { palindromicNode palindrome = new palindromicNode(item); Console.WriteLine("Initial string: \t" + item); Console.WriteLine("String as a list: \t" + palindrome.ToString() ); palindromicNode copy = palindrome.Copy(); palindromicNode reverseCopy = palindrome.Reverse(); Console.WriteLine("Copy as a list: \t" + copy.ToString() ); Console.WriteLine("Copy Reversed: \t\t" + reverseCopy .ToString() ); if (copy.equals (reverseCopy )) { Console.WriteLine ("This is a palindrome"); } else { Console.WriteLine("This is not a palindrome"); } Console.WriteLine(); } Console.Read(); } static void header() { Console.WriteLine("Palindromic Lists\n"); Console.WriteLine("Author: Scott Merryfield"); Console.WriteLine("Class: c# GSD311"); Console.WriteLine("Assignment: Seminar 5 Individual Program"); Console.WriteLine("Date: March 20, 2012\n"); } } }


Reply With Quote