Queue

This is a discussion on Queue within the C++ Programming forums, part of the General Programming Boards category; Ok I ve rewritten the code. I got the stacks to work beautifully but got to the queue and hit ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    17

    Queue

    Ok I ve rewritten the code. I got the stacks to work beautifully but got to the queue and hit a snag. I m getting an error that says unresolved external 'charqueue::front2()' refenenced from s2p1.obj.

    [code]
    #include<iostream>
    const int maxsize = 50;

    class stackchar
    {
    public: stackchar();
    void push(char newval);
    char top2();
    void pop();
    bool isfull();
    bool isempty();
    private:
    char s2[maxsize];
    int top;
    char newval;
    };

    class charqueue
    {
    public: charqueue();
    bool isempty2();
    bool isfull2();
    void enqueue(int val);
    char front2();
    void dequeue();
    private:
    char s1[maxsize];
    int front;
    int rear;
    };
    ----------------------------------------------------------------------------
    #include "s2p1.h"
    #include<iostream>

    const int vecsize = maxsize;

    charqueue::charqueue()
    {
    rear = front = 0;
    }
    bool charqueue::isempty2()
    {
    return(rear == front);
    }
    bool charqueue::isfull2()
    {
    return((rear+1)%vecsize == front);
    }
    void charqueue::enqueue(int val)
    {
    rear =(val)%vecsize;
    }
    void charqueue::dequeue()
    {
    front = (front+1)%vecsize;
    }
    char charqueue::front2()
    {
    return q[(front+1)%vecsize];
    }
    stackchar::stackchar()
    {
    top=-1;
    }
    bool stackchar::isfull()
    {
    return(top == maxsize-1);
    }
    bool stackchar::isempty()
    {
    return(top == -1);
    }
    void stackchar:ush(char newval)
    {
    s2[++top] = newval;
    }
    char stackchar::top2()
    {
    return s2[top];
    }
    void stackchar:op()
    {
    top--;
    }

    ---------------------------------------------------------------------------
    #include"s2p1.cpp"
    #include<iostream>
    #include<fstream.h>
    #include <string>
    #include <istream>
    int main()
    {
    stackchar s1;
    stackchar s2;
    stackchar s3;
    charqueue q;
    char ch[50];
    int i;
    bool test1(stackchar s1, stackchar s2);
    bool test2(stackchar s1, charqueue q);

    cout<<"Please enter a sentence: ";
    cin.getline(ch, 49);
    for(i = 0; i<strlen(ch); ++i)
    {
    s1.push(ch[i]);
    s2.push(ch[i]);
    q.enqueue(ch[i]);
    }
    while(!s2.isempty())
    {
    cout<<s2.top2();
    s3.push(s2.top2());
    s2.pop();
    }
    bool b1 = test1(s1, s3);
    if (b1 == false)
    {
    cout<<"this sentence is not a palindrome if applying stacks.";
    }
    else
    {
    cout<<"This sentence is a palindrome if applying stacks.";
    }
    bool b2 = test2(s1, q);
    if (b2 == false)
    {
    cout<<"This sentence is not a palindrome if applying stack and queue. ";
    }
    else
    {
    cout<<"This sentence is a palindrome if applying stack and queue.";
    }
    return 0;
    }
    //------------------------------------------------------------------------------------------------------------------
    bool test1(stackchar s1, stackchar s3)
    {
    while(!s1.isempty())
    {
    if(s1.top2() == s3.top2())
    {
    s1.pop();
    s3.pop();
    }
    else return false;
    }
    return true;
    }

    bool test2(stackchar s1, charqueue q)
    {
    while(!s1.isempty())
    {
    if (s1.top2() == q.front2())
    {
    s1.pop();
    q.dequeue();
    }
    else return false;
    }
    return true;
    }

    Any help is appreciated. Thank you.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    try changing
    Code:
    char charqueue::front2()
    {
    	return q[(front+1)%vecsize];
    }
    to
    Code:
    char charqueue::front2()
    {
    	return s1[(front+1)%vecsize];
    }
    Last edited by XSquared; 09-30-2002 at 06:22 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    Thanks for the try but that gave me about 2 pages worth of similar errors to all my functions.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    i edited my post, try my new idea
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    Main problem with that is S1 is the array for my stack I need to compair S1 to q to get my results.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    Noone have other suggestions? I still am getting knowhere though have fixed a few other problems I saw coming up.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    ok got past that problem and found I am doing something wrong in building or checking the queue. The stacks work fine the que simply wont come out right here is the revised code. Pls help if you can.

    Code:
    #include<iostream>
    const int maxsize = 50;
    
    class stackchar
    {
    public: stackchar();
    	void push(char newval);
    	char top2();
    	void pop();
    	bool isfull();
    	bool isempty();
    private:
    	char s2[maxsize];
    	char s1[maxsize];
    	char s3[maxsize];
        int top;
    };
    
    class charqueue
    {
    public: charqueue();
    	bool isempty2();
    	bool isfull2();
    	char front2();
        void enqueue(int val);
    	void dequeue();
    private:
    	char q[maxsize];
    	int rear;
    	int front;
    
    };
    	
    	
    		
    	
    -----------------------------------------------------------------------
    #include "s2p1.h"
    #include<iostream>
    
    const int vecsize = maxsize;
    
    charqueue::charqueue()
    {
    	rear = front = 0;
    }
    bool charqueue::isempty2()
    {
    	return(rear == front);
    }
    bool charqueue::isfull2()
    {
    	return((rear+1)%vecsize == front);
    }
    void charqueue::enqueue(int val)
    {	
    	rear =(val)%vecsize;
    
    }
    void charqueue::dequeue()
    {
    	front = (front+1)%vecsize;
    	
    }
    char charqueue::front2()
    {
        return q[front+1%vecsize];
    }
    stackchar::stackchar()
    {	
    	top=-1;
    }
    bool stackchar::isfull()
    {
    	return(top == maxsize-1);
    }
    bool stackchar::isempty()
    {
    	return(top == -1);
    }
    void stackchar::push(char newval)
    {
    	s2[++top] = newval;
    }
    char stackchar::top2()
    {
    	return s2[top];
    }
    void stackchar::pop()
    {
    	top--;
    }
    ---------------------------------------------------------------------
    #include"s2p1.cpp"
    #include<iostream>
    #include<fstream.h>
    #include <string>
    #include <istream>
    int main()
    {
    	stackchar s1;
    	stackchar s2;
    	stackchar s3;
    	charqueue q;
    	char ch[50];
    	int i;
    	bool test1(stackchar s1,  stackchar s3, stackchar s2);
    	bool test2(stackchar s1, charqueue q);
    
    	cout<<"Please enter a sentence: ";
    	cin.getline(ch, 49);
    	for(i = 0; i<strlen(ch); ++i)
    	{
    		s1.push(ch[i]);
    		s2.push(ch[i]);
    		q.enqueue(ch[i]);
    	}
    	while(!s2.isempty())
    	{
    		cout<<s2.top2();
    		s3.push(s2.top2());
    		s2.pop();
    	}
    	cout<<'\n';
    	bool b1 =  test1(s1,  s3, s2);
    	if (b1 == false)
    	{
    		cout<<"this sentence is not a palindrome if applying stacks."<<'\n';	
    	}
    	else
    	{
    		cout<<"This sentence is a palindrome if applying stacks."<<'\n';
    	}
    	
    	while(!s2.isempty())
    	{
    		s1.push(s2.top2());
    		s2.pop();
    	}
    	bool b2 = test2( s1,  q);
    	
    	if (b2 == false)
    	{
    		cout<<"This sentence is not a palindrome if applying stack and queue. "<<'\n';
    	}
    	else
    	{
    		cout<<"This sentence is a palindrome if applying stack and queue.";
    	}
    return 0;
    }
    //------------------------------------------------------------------------------------------------------------------
    bool test1(stackchar s1, stackchar s3, stackchar s2)
    {
    	while(!s1.isempty())
    	{
    		
    		if(s1.top2() == s3.top2())
    		{
    			s2.push(s1.top2());
    			s1.pop();
    			s3.pop();
    		}
    		else return false;
    	}
    	return true;
    }
    
    bool test2(stackchar s1, charqueue q)
    {
    	while(!s1.isempty())
    	{
    		if (s1.top2() == q.front2() )
    		{
    			s1.pop();
    			q.dequeue();
    		}
    		else return false;
    	}
    	return true;
    }
    My only problem now is it gives the wrong response for que. It always said false.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    so you dont have to wade through all the code here are the spots I think the problem lies in.
    Code:
    for(i = 0; i<strlen(ch); ++i)
    	{
    		s1.push(ch[i]);
    		s2.push(ch[i]);
    		q.enqueue(ch[i]);
    	}
    void charqueue::enqueue(int val)
    {	
    	rear =(val)%vecsize;
    
    }
    void charqueue::dequeue()
    {
    	front = (front+1)%vecsize;
    	
    }
    char charqueue::front2()
    {
        return q[front+1%vecsize];
    }

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    How do you expect it to enqueue anything if you don't
    modifiy q. Something like this but you might have to
    change it.

    q[front] = val;
    front++;
    front %= vecsize;

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    I knew that was the problem but I really didn't know how to fix it. tks I will try that.

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    no such luck

  12. #12
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You have to define what you mean by front and rear.
    Is q[front] the top element of the queue or the first
    empty position above the top element?
    If you define front and rear so that front is the index to
    the top element and rear is the index to the back index
    you would enqueue into the front like this
    front++;
    front %= vectsize;
    q[front] = val;

    Adding some checks for full arrays.

    Dequeueing could be done like this
    back++
    back %= vectsize;

    You would start out with
    back = 1
    front = 0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 08:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21