Stack/Queue Compare

U

unified

Ok, I'm working on a program that is supposed to compare each letter
of a string that is put into a stack and a queue. It is supposed to
tell whether or not a word is a palindrome or not. (a palindrome is a
word that spells the same thing backwards/forewards). I have gotten
this much so far:
using System;
using System.Collections;

namespace StackTest
{
public class Tester
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Enter in a string");
string s1=Console.ReadLine();
Console.WriteLine(" ");
Stack st=new Stack();
Queue q1=new Queue();

for(int i=0; i<s1.Length; i++)
{
q1.Enqueue(s1.Substring(i,1));
if(i==s1.Length-1)
{
for(i=0; i<s1.Length; i++)
{
char y=Convert.ToChar(q1.Dequeue());
Console.WriteLine(y);
}
Console.WriteLine(" ");
}
}
for(int i=0; i<s1.Length; i++)
{
st.Push(s1.Substring(i,1));
if(i==s1.Length-1)
{
for(i=0; i<s1.Length; i++)
{
Char x=Convert.ToChar(st.Pop());
Console.WriteLine(x);
}
}
}
string qu=Convert.ToString(q1);
string sta=Convert.ToString(st);
if(qu==sta)
{
Console.WriteLine("PALINDROME");
}
if(qu!=sta)
{
Console.WriteLine("NOT A PALINDROME");
}
}
}
}

Every string that i enter has the output of "NOT A PALINDROME" I have
played with this for a while now and am just getting tired of it. I
dont know what i have that i dont need, or what i need that i dont
have.. but any help would be appreciated.
 
J

Josh [MS.Com]

The _biggest_ problem here is that Convert.ToString(q1) is returning:
"System.Collections.Queue",
and Convert.ToString(st) is returning: "System.Collections.Stack"...

Sounds like a pretty lame excerise for queues and stacks - don't really
wanna think to much about it myself... :)

Here's my outline for it tho;

1. Load the string forwards into the Queue and Stack
for (int i = 0; i < inString.Length; i++)
{
queue.Enqueue(inString);
stack.Push(inString);
}

2. Then loop thru the count of the queue (or the stack) and see if we have a
palindrome

for (int i = 0; i > queue.Count; i++)
{
if ((string)queue.Dequeue() == (string)stack.Pop())
continue;
else
Console.WriteLine("NOT A PALINDROME");
}

Console.WriteLine(" PALINDROME");


The Lesson here my friend is that QUEUES are FIFO (First in First Out) and
STACKS are LIFO (Last In First Out).
So when you first pop off the stack, your getting the last letter of the
word, when you first dequeue from the queue you're getting the first letter.

Hope it helps, have fun...

Josh
Microsoft.com Tools


unified said:
Ok, I'm working on a program that is supposed to compare each letter
of a string that is put into a stack and a queue. It is supposed to
tell whether or not a word is a palindrome or not. (a palindrome is a
word that spells the same thing backwards/forewards). I have gotten
this much so far:
using System;
using System.Collections;

namespace StackTest
{
public class Tester
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Enter in a string");
string s1=Console.ReadLine();
Console.WriteLine(" ");
Stack st=new Stack();
Queue q1=new Queue();

for(int i=0; i<s1.Length; i++)
{
q1.Enqueue(s1.Substring(i,1));
if(i==s1.Length-1)
{
for(i=0; i<s1.Length; i++)
{
char y=Convert.ToChar(q1.Dequeue());
Console.WriteLine(y);
}
Console.WriteLine(" ");
}
}
for(int i=0; i<s1.Length; i++)
{
st.Push(s1.Substring(i,1));
if(i==s1.Length-1)
{
for(i=0; i<s1.Length; i++)
{
Char x=Convert.ToChar(st.Pop());
Console.WriteLine(x);
}
}
}
string qu=Convert.ToString(q1);
string sta=Convert.ToString(st);
if(qu==sta)
{
Console.WriteLine("PALINDROME");
}
if(qu!=sta)
{
Console.WriteLine("NOT A PALINDROME");
}
}
}
}

Every string that i enter has the output of "NOT A PALINDROME" I have
played with this for a while now and am just getting tired of it. I
dont know what i have that i dont need, or what i need that i dont
have.. but any help would be appreciated.



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
J

Justin Rogers

This looks like a homework question, but here goes anway. The basic concepts
behind a stack are that
things go on top and then pop off in LIFO or last-in first-out order. The
concept of a queue details a
FIFO or first-in first-out order. Now using this assumption, once the user has
pumped all of their characters
onto the queue/stack combo you should be able to compare them in order until the
end of the queue/stack to
determine palindrome consistency. For instance:

using System;
using System.Collections;
using System.Collections.Specialized;

public class Palindrome {
private Stack stack;
private Queue queue;
private bool palindrome = true;

public Palindrome(string possible) {
this.stack = new Stack(possible.Length);
this.queue = new Queue(possible.Length);

for(int i = 0; i < possible.Length; i++) {
stack.Push(possible.Substring(i, 1));
queue.Enqueue(possible.Substring(i, 1));
}
}

public bool TestPalindrome() {
// See if we've already tested
if ( queue != null ) {
while(queue.Count > 0 && palindrome) {
string leftPart = queue.Dequeue() as string;
string rightPart = stack.Pop() as string;

palindrome = (leftPart == rightPart);
}

queue = null;
stack = null;
}

return palindrome;
}

private static void Main(string[] args) {
if ( args.Length > 0 ) {
Console.WriteLine("Testing: " + args[0]);
Palindrome p = new Palindrome(args[0]);
Console.WriteLine("Results: " + p.TestPalindrome());
} else {
Console.WriteLine("Invalid input");
}
}
}


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


unified said:
Ok, I'm working on a program that is supposed to compare each letter
of a string that is put into a stack and a queue. It is supposed to
tell whether or not a word is a palindrome or not. (a palindrome is a
word that spells the same thing backwards/forewards). I have gotten
this much so far:
using System;
using System.Collections;

namespace StackTest
{
public class Tester
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Enter in a string");
string s1=Console.ReadLine();
Console.WriteLine(" ");
Stack st=new Stack();
Queue q1=new Queue();

for(int i=0; i<s1.Length; i++)
{
q1.Enqueue(s1.Substring(i,1));
if(i==s1.Length-1)
{
for(i=0; i<s1.Length; i++)
{
char y=Convert.ToChar(q1.Dequeue());
Console.WriteLine(y);
}
Console.WriteLine(" ");
}
}
for(int i=0; i<s1.Length; i++)
{
st.Push(s1.Substring(i,1));
if(i==s1.Length-1)
{
for(i=0; i<s1.Length; i++)
{
Char x=Convert.ToChar(st.Pop());
Console.WriteLine(x);
}
}
}
string qu=Convert.ToString(q1);
string sta=Convert.ToString(st);
if(qu==sta)
{
Console.WriteLine("PALINDROME");
}
if(qu!=sta)
{
Console.WriteLine("NOT A PALINDROME");
}
}
}
}

Every string that i enter has the output of "NOT A PALINDROME" I have
played with this for a while now and am just getting tired of it. I
dont know what i have that i dont need, or what i need that i dont
have.. but any help would be appreciated.




http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
U

unified

Hmmm, let me start by thanking you both. I've read about stacks and
queues, and know about LIFO and FIFO. (Though I appretiate the
explanation.) It's just that i have played around with this program
so many times that I started to lose focus on what I was doing and
tried near anything just to get it to compile right.

After reading over the help given, I can see where I was having
problems. & yes, it was for a homework assignment. All the
other assignments for this unit i have completed fine, but this was
just difficult for some reason.

And I'm agreeing with you Josh, this is a pretty lame assignment for
Stacks/Queues.. as a matter of fact, our whole curriculum isn't as
wonderful as I would like.. but hey, we can't win em all, right?
 
J

Justin Rogers

I honestly felt the same way. Enough so that it prompted me to write a kind of
warning in
my blog about the current state of samples, tutorials, and cirriculum. The
problem seems
to be teaching a new student a concept, while allowing them to digest incorrect
concepts at
the same time. While you realized how ridiculous this assignment was, other
individuals might
look at it differently and carry this assignment on into their programming
careers.

Here is the blog entry. Don't take offense, since it was not meant to offend
anyone, but rather
awaken article and sample writers to the importance of writing solid code and
using proven
techniques in their articles.

http://weblogs.asp.net/justin_rogers/archive/2004/01/19/60495.aspx

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

unified said:
Hmmm, let me start by thanking you both. I've read about stacks and
queues, and know about LIFO and FIFO. (Though I appretiate the
explanation.) It's just that i have played around with this program
so many times that I started to lose focus on what I was doing and
tried near anything just to get it to compile right.

After reading over the help given, I can see where I was having
problems. & yes, it was for a homework assignment. All the
other assignments for this unit i have completed fine, but this was
just difficult for some reason.

And I'm agreeing with you Josh, this is a pretty lame assignment for
Stacks/Queues.. as a matter of fact, our whole curriculum isn't as
wonderful as I would like.. but hey, we can't win em all, right?




http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
U

unified

I find most of that to be true. The course of study I am currently
taking for C#, though is a good introductory course, should be alot
more focused and clear cut of what they expect us to learn. A good
thing is that they let us go at our own pace, and aren't expected to
move at the same pace. However, I feel that there is a lack of
guidance. Most of our material is online. So it is mostly like
"Watch the online movies and take the tests.. If you dont pass...
watch the movie more and read suggested articles" Sometimes the
instructor will elaborate on how the movies are setup, and will clear
many confusing things up.. but there are still many questions that
remain unanswered.
We have just reached the midpoint for this school year (Still in high
school), and at this last quarter change, I have lost close to half
of my C# class due to people either not getting it, or not being
taught correctly.
Anyways, I am just rambling now and will get off of my soap-box.

Thanks to all for any comments/insight/help offered.
Justin Rogerswrote:
I honestly felt the same way. Enough so that it prompted me to write
a kind of
 
R

Ryan Trudelle-Schwarz

Not sure what the teacher wanted but the easiest way to do it is to run
through length/2 of the string and push those characters into the stack. If
the length is odd, skip a char. Then run through the rest comparing the
chars to the pop of the stack.

Not sure why anyone'd use a queue for this though, so there must be
something else wanted.
 
U

unified

It's to show that a Stack is Last-In-First-Out, and a Queue is
First-In-First-Out. So the user enters a string, that is
pushed/enqueue onto a stack and a queue. Then taking the letters (or
characters) off one at a time and comparing them. If they are the
same word backwords and forwards, like the word RACECAR, then it
should writeline "PALINDROME". But while it is comparing, if it
should come to a letter on the stack that doesnt match the one on the
queue, it should kick out of the loop and write "NOT A PALINDROME"

I dont know if that cleared anything up...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top