Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/forums/programming-forum/)
-   -   Queue linked list. Please help. :/ (http://www.go4expert.com/forums/queue-linked-list-help-t28083/)

meepot 25Mar2012 07:32

Queue linked list. Please help. :/
 
Please help, this is our final project in data struct.
------------------
Queue is an ordered collection of items from which items may be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (called the rear of the queue). It exhibits a first-in/first-out(FIFO behavior. Two basic operations associated with a queue are APPEND or ENQUEUE and SERVE or DEQUEUE.

You are required to write a program to APPEND and SERVE an element from the queue using a dynamic data structure. Please use the declaration below.
Code:

struct Node{
        int data;
        struct node *link;
};
typedef struct Node *QueuePointer;
QueuePointer append(QueuePointer head, QueuePointer tail, int num);
int serve(QueuePointer head, QueuePointer tail);
void display_queue(QueuePointer head, QueuePointer tail);

main()
{
      QueuePointer Head = Tail = NULL;
      .....
}

The program must meet the following requirements:
*Menu screen that allows the user to choose the selection to go to different process such as ENQUEUE, DEQUEUE, Display, Exit.

*Once the users choose to proceed to ENQUEUE, the program will prompt the user to enter a data and this data will be added to the queue. Once successfully added, it will display the entire queue.

*When the user chooses to proceed to DEQUEUE, the program will display the data that will be deleted from the queue. Once successfully deleted, it will display the entire queue. If the queue is empty then display an appropriate message.

*The program only terminates once Exit is chosen.


-------------
This is the program that i've made. But there are some errors. Im having a hard time debugging it. Hope someone can help me.
Code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct list{
  int data;
  struct node *link;
};
typedef struct Node *QueuePointer;

QueuePointer append(QueuePointer head, QueuePointer tail, int num);
int serve(QueuePointer head, QueuePointer tail);
void display_queue(QueuePointer head, QueuePointer tail);           



void main()
{

    int opt;       
    char ch;       
                QueuePointer head;   
                QueuePointer tail;   
                head=tail=NULL;
    do
                {
                clrscr();
                printf("\nEnter your option to perform in a queue\n");
                printf("\n[A] ENQUEUE\n");
                printf("\n[B] DEQUEUE\n");
                printf("\n[C] Display\n");
                scanf("%d",&opt);
                switch(opt)
        {
                case 'A':
                append(&head,&tail);
                break;
                case 'B':
                serve(&head,&tail);
                break;
                case 'C':
                display_queue(&head,&tail);
                break;
        }
        printf("\nDo you wish to continue[y/n]\n");
        ch=(char)getche();
    }while(ch=='Y' || ch=='y');
    printf("\nPress any key to exit\n");
    getch();
}

QueuePointer append(QueuePointer head,QueuePointer tail,int num)
{
                QueuePointer *newnode;   
                newnode=(struct node *)malloc(sizeof(struct Node));
                newnode->link=NULL;
                printf("\nEnter the character to push\n");
                fflush(stdin);
                scanf("%c",&(newnode->data));
                if(head==NULL && tail==NULL)
    {
                head=newnode;
                tail=newnode;
    }
    else
    {
   
              (tail)->link=newnode;
                head=newnode;
    }
}

int serve(QueuePointer head,QueuePointer tail)
{
                QueuePointer *delnode;     
                if((head)==NULL && (tail)==NULL)
                printf("\nQueue is empty to delete any element\n");
    else
    {
                delnode=head;
                (head)=(head)->link;
                free(delnode);
    }
}
void display_queue(QueuePointer head)
{
                while(head!=NULL)
    {
                printf(" %c ",head->data);
                head=head->link;
    }
}


shabbir 25Mar2012 08:14

Re: Queue linked list. Please help. :/
 
See - http://www.go4expert.com/showthread.php?t=1279


All times are GMT +5.5. The time now is 04:05.