Thread: Help me about queue

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Ha Noi,Viet Nam
    Posts
    3

    Help me about queue

    Exercise: Queue using Lists
    We assume that you qrite a mobile phone's address book.
    Declare a structure "Address" that can hold at least "name","telephone number",and "email address".
    Write a program that copies data of an address book from the file to other file using a queue.First,read data of the address book from the file and add them to the queue.Then retrieve data from the queue and write them to the file in order of retrieved.In other words ,data read in first should be read out first and data read in last should be read out last.
    I'm studing C programming about "Data Structure and Algorithms" but I learned very poor and I'm not good at English for very.Because I hope to receive assistance from you.Thank you all

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You will get plenty of help but you need to give me a start with your code. I have read that book before and know that the one I read only had PASCAL code in it. So... given that, do you wish for me to write code in PASCAL for you?

    I won't be too sarcastic, just show us what you are understanding or we will assume you do not understand enough at this point to even fathom a question.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Ha Noi,Viet Nam
    Posts
    3
    Thanks.This is my code but It's fautl:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAX_LEN 100
    typedef char Elementtype;  /*Store the elements*/
    typedef struct queue{
       Elementtype element[MAX_LEN];
       int front,rear;
    }queue;
    void makenull_queue(queue *q) /*Initialize and check the status*/
    {
       q->front=-1;
       q->rear=-1;
    }
    int Empty_queue(queue *q) 
    {
       return q->front==-1;
    }
    int Full_queue(queue *q) 
    {
       return (q->rear-q->front+1)==MAX_LEN;
    }
    void Enqueue(Elementtype x,queue *q) 
    {
      if(!Full_queue(q))
        {
           if(Empty_queue(q))
             q->front=0;
           q->rear=q->rear+1;
           q->element[q->rear]=x;
        }
      else
        {
          printf("Queue is Full\n");
        } 
    }
    void Dequeue(queue *q) 
    {
      if(!Empty_queue(q))
        {
           q->front=q->front+1;
           if(q->front>q->rear)
             makenull_queue(q);   /*Queue become empty*/
        }
       else
        {
          Printf("Queue is Empty\n");
        }
    }
    struct address{
     char name[30];
     char tel[12];
     char mail[30];
    }address;
    main()
    {
       FILE *fptr1,*fptr2;
       int i;
       char *s;
       queue *q;
       struct address hs[3];
       for(i=0;i<3;i++)
         {
            printf("Name:");scanf("&#37;s",hs[i].name);
            printf("Phone:");scanf("%s",hs[i].tel);
            printf("Email:");scanf("%s",hs[i].mail);
         }
        if((fptr1=fopen("Add1.txt","w+"))==NULL)
          {
            printf("Can not file Add1.txt\n");
          }
    /*Write data into file*/
        for(i=0;i<3;i++)
          {
             fprintf(fptr1,"%s\n",hs[i].name);
             fprintf(fptr1,"%s\n",hs[i].tel);
             fprintf(fptr1,"%s\n",hs[i].mail);
          }
      fclose(fptr1);
      fptr1=fopen("Add1.txt","r");
      fptr2=fopen("Add2.txt","w+");
         makenull_queue(q); 
      i=0;
      while((fgets(s,200,fptr1))!=NULL) 
         {
            Enqueue(s,q);            
            fprintf(fptr2,"%s\n",q->element[i]); /* Write each element into file*/
            i++;
         }   
    
    }
    Last edited by tqn_hnvietnam; 09-29-2008 at 07:15 PM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I prefer pointers, though the author of the book in question does use some valid rationale for doing a deque this way....

    Code:
    int Empty_queue(queue *q) 
    {
       return q->front==-1;
    }
    Is this right? It may be, I am in sort of a rush and multitasking.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Ha Noi,Viet Nam
    Posts
    3
    Can you edit my code for It true?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Actually, since you go to the trouble of calling makenull when you dequeue the last element, that should work for checking whether a queue is empty.

    What faults have you seen in your code?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Can I? Sort of... I am multitasking and this is my low-priority way of keeping sane. Will I? Not until you meet me halfway. When someone makes it interesting for me, I tend to just rewrite a lot of their code. When its not interesting, I tend to nit-pick a few lines of code at a time and critique your ability to speak articulately.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    I think the error here :
    Code:
    typedef char Elementtype;
    Elementtype is char
    and
    Code:
    while((fgets(s,200,fptr1))!=NULL) 
         {
            Enqueue(s,q);            
            fprintf(fptr2,"%s\n",q->element[i]); /* Write each element into file*/
            i++;
         }
    s is a string.

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