473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Stack/Queue Compare

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 =---
Nov 15 '05 #1
7 12345
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" <la*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
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

=---
Nov 15 '05 #2
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[i]);
stack.Push(inString[i]);
}

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" <la*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
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

=---
Nov 15 '05 #3
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?

----== 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 =---
Nov 15 '05 #4
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.../19/60495.aspx

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

"unified" <la*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
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?

----== 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

=---
Nov 15 '05 #5
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 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.../19/60495.aspx

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

"unified" <la*******@hotmail-dot-com.no-spam.invalid> wrote in message news:40**********@127.0.0.1...


----== 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 =---
Nov 15 '05 #6
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.
Nov 15 '05 #7
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...

----== 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 =---
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
4
by: deanfamily | last post by:
I have a rather pecurliar C++ assignment. I need to create a program (using a stack or queue) to verify if the grouping symbols in an arithmetic expression match. For example: {25 + (3 - 6) * 8}...
4
by: raghu | last post by:
// Program to implement both stack and queue on an array int main(void) { int a,i; for(i=0;i<5;i++) scanf("%d",&a); // here i'm filling (pushing) the elements for(i=4;i>=0;i--)...
4
by: SP | last post by:
Hi, I've a Stack. Now I would use this to mantain a LIFO objects chain. My problem is that I want to limit the Stack dimension. If the Stack is full and I want to add a object, I eant to remove...
7
by: DevNull | last post by:
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways...
2
by: waceys | last post by:
hello everybody i need some body help me to make code for a linked list and arrays build stack and queue such that when i push an element in the stack i can dequeue it first from the queue and when...
3
by: mark4asp | last post by:
Stack of limited size containing unique items? Hi guys, How would I implement a stack of limited size containing unique items? For example. Suppose my stack has . I add 2 to it and it is now...
8
by: t | last post by:
The stack container adaptor uses deque as its default underlying sequential container. I don't understand why since we only add elements and remove elements from one side of a stack. Why isn't...
0
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.