Thread: C Programming (Uni Work)

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    2

    C Programming (Uni Work)

    Hi Guys

    Just wondering if someone on here can help a uni student out.

    Using the lecture example below write two new que functions:

    emptyQ(que) returns 1 if queue que is empty and 0 otherwise

    fullQ(que) returns 1 if queue que is full and 0 otherwise

    Code:
    #include <stdio.h>
    #define MAX 5
    
    typedef struct
    {
    	int list[MAX];	/*Queue of integers*/
    	int size;  		/*Size of queue in use*/
    	int front; 		/*Index representing the front of the queue*/
    	int rear;  		/*Index representing the rear of the queue*/
    }queue;
    
    /* Prototypes */
    int menu_display();
    void initQ(queue *);
    int insertQ(queue *, int);
    int deleteQ(queue *);
    
    int empty(queue);
    int full(queue);
    void printQ(queue);
    
    int main(void)
    {
    	int choice, item, ok;
    	queue myqueue;
    	initQ(&myqueue);
    
    	choice = menu_display();
    
    	while (choice != 0)/*0 - quit*/
    	{
    		switch(choice)
    		{
    			case 1:/*Insert*/
    				printf("\nEnter a value to insert:");
    				scanf("%d", &item);
    				ok = insertQ(&myqueue, item);
    				if (ok)
    					printf("\n%d inserted\n\n", item);
    				else
    					puts("\nQUEUE FULL\n");
    				break;
    			case 2:/*Remove*/
    				item = deleteQ(&myqueue);
    				if (item >= 0)
    					printf("\n%d removed\n\n", item);
    				else
    					puts("\nQUEUE EMPTY\n");
    				break;
                case 3:/*  empty? */
                    ok = empty(myqueue);
                    if (ok)
                        puts("QUEUE IS EMPTY\n");
                    else
                        puts("QUEUE NOT EMPTY\n");
                    break;
                case 4:/*  full? */
                    ok = full(myqueue);
                    if (ok)
                        puts("QUEUE IS FULL\n");
                    else
                        puts("QUEUE NOT FULL\n");
                    break;
                case 5:
                    printQ(myqueue);
                    break;
    			default:/*Invalid*/
    				puts("\nINVALID choice – re-enter");
    		}/*end switch*/
    		choice = menu_display();
    	}/* end while */
    
    	return 0;
    }/*end main*/
    
    int menu_display()
    {
    	int choice;
        puts("\nMenu selection\n");
        puts("Enter 1 insert");
        puts("Enter 2 remove");
        puts("Enter 3 empty?");
       	puts("Enter 4 full?");
    	puts("Enter 5 print");
    	puts("Enter 0 for EXIT\n");
    	scanf("%d",&choice);
    	return choice;
    }/*end menu_display*/
    
    /*Initialise the queue*/
    void initQ(queue *qu)
    {
    	qu->size  = MAX;  /*  max size of queue qu */
    	qu->front = 0;	  /*  queue qu is initially empty */
    	qu->rear  = -1;   /*  set rear to -1 (will be incremented by 1 before use)*/
    }/*end initQ*/
    
    int insertQ(queue *qu, int item)
    {
    	/*Remember the size of an array is always 1 more than its upper index
    	*so if the size of the array is 3 then the elements are 0 to 2.
    	*The rear index represents the LAST USED index position for an item in the queue
    	*/
    	if (qu->rear < qu->size - 1)
    	{
    		/*Increment rear by 1 so we don't overwrite the last position used*/
    		qu->rear++;
    		/*then add item value at index rear*/
    		qu->list[qu->rear] = item;
    		/*  return 1 (true) to indicate insertion was successful*/
    		return 1;
    	}/*end if*/
    	else
    	{
    		return 0;/* return 0 (false) to indicate insertion failed - queue was full*/
    	}/*end else*/
    }/*end insertQ*/
    
    int deleteQ(queue *qu)
    {
    	/*Check if the index of the item at the front isn't greater than the rear*/
    	if (qu->front <= qu->rear)
    	{
    		/*Return the item at index front. The item has now left the queue so
    		*increase front by 1 (after returning the value) to point to the next item
    		*/
    		return qu->list[qu->front++];
    	}/*end if*/
    	else/*Queue is empty*/
    	{
    		return -1;
    	}/*end else*/
    }/*end deleteQ*/
    
    int empty(queue qu)
    {
            if(queue.top > 0);
            {
                return 0;
            }
    		return 0;
    }
    
    int full(queue qu)
    {
    
        return 0;
    }
    
    void printQ(queue qu)
    {
    	puts("not yet implemented");
    }
    Attached Files Attached Files
    Last edited by Salem; 04-04-2017 at 11:13 AM. Reason: Inlined code

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    lol, no. If you're asking for someone to hand you the solution, you need to pay the appropriate people.

    If on the other hand you try to program it yourself and return with genuine questions, we may then help you.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programming , program does't work
    By AmirHossein H-m in forum C Programming
    Replies: 2
    Last Post: 04-19-2013, 12:38 PM
  2. Need help with C++ programming work
    By moey187 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2011, 08:39 AM
  3. How to co-work in programming?
    By ovid in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2010, 12:28 PM
  4. New to programming: home work
    By chaos_defined in forum C Programming
    Replies: 4
    Last Post: 09-07-2006, 12:43 PM
  5. device driver programming....how does it work
    By the bassinvader in forum C Programming
    Replies: 4
    Last Post: 07-09-2006, 03:31 AM

Tags for this Thread