Thread: Queue program

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    Queue program

    i am trying to make a queue program that takes an input of numbers and return the first number first. i am pasting my code. during compilation it gives an error on the last line. it says 'end of input' expecting }
    can anyone help me out at this...

    Code:
    #include<stdio.h>
    #define SIZE 5
    
    struct queue
    {
    	int term[SIZE];
    	int rear;
    	int front;
    };
    
    void initqueue( struct queue*);
    void join(int, struct queue*);
    int leave( struct queue*);
    
    int main()
    {
    	struct queue q;
    	int a, var;
    	initqueue(&q);
     
    	while(1)
    	{
    		printf("press 1 to join value press 2 to leave value, 3 to exit \n");
    		scanf("%d", &a);
    
    		switch(a)
    		{
    			case 1 :
    			scanf("%d", &var);
    			join(var, &q);
    			break;
    
    			case 2: 
    			printf("leave value: %d \n", leave(&q));
    			break;
    
    			case 3:
    			return 0;
    
    		}
    
    	}
    
    }
    
    void join(int a, struct queue*q)
    {
    	if(q->rear == SIZE)
    	{
    		q->term[q->rear]=a;
    		q->rear--;
    
    	if(q->rear == 0)
    	{
    		printf ( "queue is full");
    	}
    }
    
    int leave(struct queue*q);
    {
    	if(q->front == q->rear )
    	{
    	printf("queue is empty");
    	}
    	else{
    		q->front--;
    		printf("%d", q->term[q->front]);
    	}
    }
    
    void initqueue(struct queue*q);
    {
    	q->front = SIZE;
    	q->rear = SIZE;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    #include<stdio.h>
    #define SIZE 5
    
    struct queue
    {
            int term[SIZE];
            int rear;
            int front;
    };
    
    void initqueue( struct queue*);
    void join(int, struct queue*);
    int leave( struct queue*);
    
    int main()
    {
            struct queue q;
            int a, var;
            initqueue(&q);
     
            while(1)
            { // add \n's in your print statements so the output is more readable
                    printf("press 1 to join value\npress 2 to leave value\n3 to exit \n");
                    scanf("&#37;d", &a);
    
                    switch(a)
                    {
                            case 1 :
                            scanf("%d", &var);
                            join(var, &q);
                            break;
    
                            case 2: 
                            printf("leave value: %d\n", leave(&q)); // your saying to print the value returned by leave, but your leave function doesnt return anything
                            break;
    
                            case 3:
                            return 0;
    
                    }
    
            }
    
    }
    
    void join(int a, struct queue*q)
    {
            if(q->rear == SIZE)
            {
                    q->term[q->rear]=a;
                    q->rear--;
            } // MISSED THIS CLOSING BRACKET
            if(q->rear == 0)
            {
                    printf ( "queue is full\n");
            }
    }
    
    int leave(struct queue*q) // HAD SEMICOLON ON THIS LINE
    {
            if(q->front == q->rear )
            {
            printf("queue is empty\n");
            }
            else{
                    q->front--;
                    printf("%d\n", q->term[q->front]);
            }
      // return something here
    }
    
    void initqueue(struct queue*q)//HAD SEMICOLON ON THIS LINE
    {
            q->front = SIZE;
            q->rear = SIZE;
    }
    Last edited by nadroj; 04-06-2007 at 11:25 AM.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    the program compiles... i enter the values but when i give the leave command it gives some garbage value.. plz help..

    Code:
    #include<stdio.h>
    #define SIZE 2
    
    struct queue
    {
    	int term[SIZE];
    	int rear;
    	int front;
    };
    
    void initqueue( struct queue*);
    void join(int, struct queue*);
    int leave( struct queue*);
    
    int main()
    {
    	struct queue q;
    	int a, var;
    	initqueue(&q);
     
    	while(1)
    	{
    		printf("press 1 to join value press 2 to leave value, 3 to exit \n");
    		scanf("&#37;d", &a);
    
    		switch(a)
    		{
    			case 1 :
    			scanf("%d", &var);
    			join(var, &q);
    			break;
    
    			case 2: 
    			printf("leave value: %d \n", leave(&q));
    			break;
    
    			case 3:
    			return 0;
    
    		}
    
    	}
    
    }
    
    void join(int a, struct queue*q)
    {
    	if(q->rear == SIZE)
    	{
    		q->term[q->rear]=a;
    		q->rear--;
    	}
    	if(q->rear == 0)
    	{
    		printf ( "queue is full");
    	}
    }
    
    int leave(struct queue*q)
    {
    	if(q->front != q->rear )
    	{
    		q->front--;
    		return q->term[q->front];
    	
    	}
    	if(q->front == q->rear)
    	{
    		printf("queue is empty");
    	}
    }
    
    void initqueue(struct queue*q)
    {
    	q->front = SIZE;
    	q->rear = SIZE;
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    read the comments in the code from my previous post.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    yes i read it. the program compiles. but the desired results are not given. instead it gives garbage value

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by esi
    yes i read it. the program compiles. but the desired results are not given. instead it gives garbage value
    if you read it youd know why its printing garbage values. if your not going to try then im not going to help.
    Quote Originally Posted by esi
    the program compiles... i enter the values but when i give the leave command it gives some garbage value.. plz help..
    Quote Originally Posted by nadroj
    read the comments in the code from my previous post.
    Code:
    printf("leave value: &#37;d\n", leave(&q)); // your saying to print the value returned by leave, but your leave function doesnt return anything
    Code:
    int leave(struct queue*q) // HAD SEMICOLON ON THIS LINE
    {
      // ...
      // return something here
    }
    Last edited by nadroj; 04-06-2007 at 12:07 PM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    How is this "queue" supposed to work exactly?

    Linked list? Nope, definitely no linked list here.
    Circular buffer? Nope, can't be as I see no places that wrap front and rear back around between the start and end of the buffer.
    An (inefficient) array where you move items over when an item is removed? Nope, no such loop or memcpy is present.

    To be able to be called a queue, you really have to be able to endlessly add and remove items, so long as there are never more than a certain number in it at once, and you never remove an item when it is empty.
    In your code, once it works, you cannot fill it up, then remove one item, and add another.

    Therefore, this is not the code for a queue. It is at best a temporary buffering mechanism.
    Last edited by iMalc; 04-06-2007 at 03:43 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void join(int a, struct queue*q)
    {
    	if(q->rear == SIZE)
    	{
    		q->term[q->rear]=a;
    		q->rear--;
    	}
    	if(q->rear == 0)
    	{
    		printf ( "queue is full");
    	}
    }
    Your first entry into the queue writes data to "one past" the actual array. This is bad.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    i tried everything but still cant get it working. what i'm trying to do is make an array and enter values in it. when all the values have been inserted i want it to return the values in the order that the first value should come out first. i was able to make a code for a stack process in which the last value comes out first and then the next. i'm pasting both my codes but i dunt know where i'm making a mistake in the queue code. pls help me out.

    queue code:
    Code:
    #include<stdio.h>
    #define SIZE 2
    
    struct queue
    {
    	int term[SIZE];
    	int rear;
    	int front;
    };
    
    void initqueue( struct queue*);
    void join(int, struct queue*);
    int leave( struct queue*);
    
    int main()
    {
    	struct queue q;
    	int a, var;
    	initqueue(&q);
     
    	while(1)
    	{
    		printf("press 1 to join value press 2 to leave value, 3 to exit \n");
    		scanf("&#37;d", &a);
    
    		switch(a)
    		{
    			case 1 :
    			scanf("%d", &var);
    			join(var, &q);
    			
    			break;
    
    			case 2: 
    			printf("leave value: %d \n", leave(&q));
    			break;
    
    			case 3:
    			return 0;
    
    		}
    
    	}
    
    }
    
    void join(int a, struct queue *q)
    {
    	if(q->rear == SIZE)
    	{
    		q->term[q->rear]=a;
    		q->rear--;
    	}
    	if(q->rear == 0)
    	{
    		printf ( "queue is full");
    	}
    }
    
    int leave(struct queue*q)
    {
    	if(q->front == q->rear )
    	{
    		printf("queue is empty");
    	
    	}
    	else
    	{
    		q->front--;
    		return q->term[q->front];
    	}
    }
    
    void initqueue(struct queue*q)
    {
    	q->front = SIZE;
    	q->rear = SIZE;
    }
    stack code:
    Code:
    #include<stdio.h>
    #define SIZE 5
    
    struct stack
    {
    	int term[SIZE];
    	int top;
    };
    
    void initstack( struct stack*);
    void push(int, struct stack*);
    int pop( struct stack*);
    
    int main()
    {
    	struct stack st;
    	int a, var;
    	initstack(&st );
     
    	while(1)
    	{
    		printf("press 1 to push value press 2 to pop value, 3 to exit \n");
    		scanf("%d", &a);
    
    		switch(a)
    		{
    			case 1 :
    			scanf("%d", &var);
    			push(var, &st);
    			break;
    
    			case 2: 
    			printf("pop value: %d \n", pop(&st));
    			break;
    
    			case 3:
    			return 0;
    
    		}
    
    	}
    
    }
    
    void push(int a, struct stack*st)
    {
    	if(st->top == SIZE)
    		printf ( "Stack is full");
    	else{
    		st->term[st->top]=a;
    		st->top++;
    	}
    }
    
    int pop(struct stack*st)
    {
    	if(st->top == 0 )
    		printf("Stack is empty");
    	else{
    		st->top--;
    		return st->term[st->top];
    	}
    }
    
    void initstack(struct stack*st)
    {
    	st->top = 0;
    }

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your leave function (queue) only returns a value in the 'else' clause, add a return statement in the 'if' as well. similar situation in (stack) pop function.

    after fixing this, what are your problems your still having? ie what are the conditions that it happens, and what IS the problem.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    same thing... but my stack program works perfectly fine... the problem is that the queue pprogram should return me the values in the order in wich i inserted the values but it is returning some garbage value.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    post an updated version of the code, with the problems mentioned fixed so we can have a look.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heap management
    By Frost Drake in forum C Programming
    Replies: 12
    Last Post: 10-15-2006, 10:06 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM