Thread: Queue implementation

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    15

    Queue implementation

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct queue
    {
    	int n;
    	struct queue *next;
    };
    struct queue *q;
    void push();
    void pop();
    void show();
    int main()
    {
    	int op;
    	q=NULL;
    	while(1)
    	{
    		printf("MENU OF QUEUE OPTIONS\n");
    		printf("1.Push\n2.Pop\n3.Show\n4.Exit\n");
    		printf("CHOOSE AN OPTION!\n");
    		scanf("%d",&op);
    		switch(op)
    		{
    			case 1: push();
    				break;
    			case 2: pop();
    				break;
    			case 3: system("CLS");show();
    				break;
    			case 4: exit(1);
    			default: printf("NOT A VALID OPTION!\n");break;
    		}
    	}
    	getch();
    }
    void pop()
    {
    	struct queue *t;
    	t=q;
    	if(t==NULL)
    	{
    		printf("QUEUE IS EMPTY!\n");
    		return 0;
    	}
    	q=q->next;
    	free(t);
    	t=NULL;
    }
    void show()
    {
    	struct queue *t;
    	t=q;
    	if(t==NULL)
    	{
    		printf("QUEUE IS EMPTY!\n");
    		return 0;
    	}
    	while(1)
    	{
    		printf("%d\n",t->n);
    		if(t->next==NULL)
    			break;
    		t=t->next;
    	}
    }
    void push()
    {
    	int num;
    	struct queue *t;
    	t=q;
        printf("ENTER A NUMBER TO PUSH!\n");
    	scanf("%d",&num);
    	if(t==NULL)
    	{
    		t=(struct queue *)malloc(sizeof(struct queue));
    		t->n=num;
    		t->next=NULL;
    		q=t;
    	}
    	else
    	{
    		while(1)
    		{
    			if(t->next==NULL)
    			{
    				t->next=(struct queue *)malloc(sizeof(struct queue));
    				t=t->next;
    				t->n=num;
    				t->next=NULL;
    				break;
    				
    			}
    			t=t->next;
    		}
    	}
    }
    Hello all, I just finished writing this program based on theory (FIRST IN FIRST OUT). Anyway my question is that is the above program considered a queue. The program in the book is very different from what i have written. I compiled it and it runs fine, but i just wanted to know if it is considered a queue.

    Thank You !

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    As far as I can see yes, http://en.wikipedia.org/wiki/Queue_&#37;28data_structure%29

    I'd be very cautious about using while(1) ...

    Code:
    while(1)
    {
        printf("%d\n",t->n);
    
        if(t->next==NULL)
            break;
    
        t=t->next;
    }
    Can be written as
    Code:
    do
    {
        printf("%d\n", t->n);
        t = t->next;
    } while(t != NULL);
    You can probably see the latter is nicer on the eyes

    Try bumping up the warning level when you compile, you'll see a few.

    return 0; from void function (or something like that)
    You can't return values from void functions.

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, 03: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, 09:38 AM