Stacks and queues.

This is a discussion on Stacks and queues. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #define limit 100000 /*Queue*/ typedef char queueElements; typedef struct { queueElements *contents; int front; int ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    3

    Stacks and queues.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define limit 100000
    
    
         
    /*Queue*/
    
    
    typedef char queueElements;
    typedef
         struct 
         {
             queueElements *contents;
             int front;
             int rear;
             int size;
             int n;
         } queueT;
         
    int queueInit(queueT *queueP, int n);
    void enqueue (queueT *Q, int element);
    int queueIsfull (queueT *Q);
    int dequeue (queueT *Q);
    int queueIsempty (queueT *Q);
    void queuemove (queueT *Q);
    void queueprint (queueT *Q);
    void queueDestroy (queueT *Q);
        
    int queue(queueT *a);
    
    
    int main ()
    {
        
        queueT *a2;
        int sel,res,n;
        do {
             printf("CHOOSE! \n");
             printf("1:Stacks\n");
             printf("2:Queue\n");
             printf("3:Exit\n");
             printf("Choose your destiny.\n\n");
             scanf("%d",&sel);
             switch (sel)
             {
                  case 1:
                       break;
                  case 2:
                       do {
                            printf("***********************************************************************\n");
                            printf("How many elements? (Please give a positive integer smaller than 100000)\n");
                            printf("***********************************************************************\n");
                            scanf("%d",&n);
                            } while (n>limit || n<=0);
                       res=queueInit(a2,n);
                       if (res)
                       {
                            printf("Error in creating stack!\n");
                            exit(0);
                       }
                        printf("!!!!\n");
                       queue(a2);
                       break;
                  case 3:
                       break;     
             } 
             if (sel==3)
                  break;
        }  while (1);
        system("pause");  
        return 0;
    }              
    
    
    
    
    
    
    
    
    
    
    
    
    int queue(queueT *a)
    {
        int sel;
        queueElements element;
        while (1) {
             printf("What to do? \n");
             printf("1:Enqueue\n");
             printf("2:Dequeue\n");
             printf("3:Move elements to the beginning\n");
             printf("4:Print\n");
             printf("5:Destroy and exit.\n");
             printf("Choose your destiny.\n\n");
             scanf("%d",&sel);
             printf("\n");
             switch (sel)
             {
                  case 1:
                       if ( queueIsfull(a) )
                            printf("Error: Queue is full! Dequeue an element in order to enqueue one. \n\n");
                       else
                       {
                            printf("Enter the value to insert.\n\n");
                            scanf("%d",&element);
                            enqueue(a,element);
                       }
                       break;
                  case 2:
                       if ( queueIsempty(a) )
                            printf("Error: Queue is empty! Enqueue an element in order to dequeue one.\n\n");
                       else
                       {
                            element = dequeue(a);
                            printf("Element removed from queue is %d.\n\n", element);
                       }
                       break;
                  case 3:
                       if ( queueIsfull(a) )
                            printf("Error: Queue is full! Dequeue an element in order to move the elements of the queue.\n\n"); 
                       else if ( queueIsempty(a))
                            printf("Error: Queue is empty!\n\n");
                       else
                            queuemove (a);
                       break;
                  case 4:
                       if ( queueIsempty(a) )
                            printf("Error: Queue is empty! Enqueue an element in order to show the queue.\n\n");
                       else
                            queueprint(a);
                       break;
                  case 5:
                       queueDestroy(a);
                       return 0;
             }
                  }
    }
    
    
    int queueInit(queueT *queueP, int n)
    {
        queueElements *newcontents;
        newcontents=(queueElements*)malloc(sizeof(queueElements)*n);
        if (newcontents==NULL)                                                                                                                   //
             return 1;   
        system("pause");     
        printf("!!!!\n");
        queueP->contents=newcontents;                                           ..
        printf("!!!!\n");
        system("pause");
        queueP->front=0;                                                         
        queueP->rear=-1;
        queueP->size=0;
        queueP->n=n;
        return 0;                                                                  //Function crashes here...
    }
    
    
    
    
    
    
    void enqueue (queueT *Q, int element)
    {
         Q->rear++;
         Q->contents[Q->rear]=element;
         Q->size++;
    }
    
    
    int queueIsfull (queueT *Q)
    {
        if ( Q->size == Q->n)
             return 1;
        return 0;
    }
    
    
    int dequeue (queueT *Q)
    {
        int element;
        element = Q->contents[Q->front];
        Q->size--;
        Q->contents[Q->front]=0;
        Q->front++;
        return element;     
    }
    
    
    int queueIsempty (queueT *Q)
    {
         if ( Q->front > Q->rear )
              return 1;
         else
              return 0;
    }
    
    
    void queuemove (queueT *Q)
    {
         int i;
         if (Q->front>=1)
              {
              for (i=0;i<=Q->size;i++)
              {
                   Q->contents[i]=Q->contents[Q->front];
                   Q->front++;
              }
              Q->front=0;
              Q->rear=Q->size-1;
              }
         else
              printf("The elements of the queue are already placed in the beginning.\n\n");
    }
         
    
    
    void queueprint (queueT *Q)
    {
         int i;
         for (i=Q->front;i<=Q->rear;i++)
             printf("%d ",Q->contents[i]);
         printf("\n");
    }
    
    
    void queueDestroy (queueT *Q)
    {
         free(Q->contents);    
    }

    Howdy! I have a problem. I can't use the queue option because my program crashes after I input a number, well the function "int stackInit" won't end, which indicates the number of elements of the queue. The stacks option however is fine (I removed it because I don't think it's related to the problem). I use DEV C++ to compile this source code.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int main ()
    {
        queueT *a2;
    ...
        res=queueInit(a2,n);
    ...
    }
    
    int queueInit(queueT *queueP, int n)
    {
    ...
        queueP->contents=newcontents;                                           
        printf("!!!!\n");
        system("pause");
        queueP->front=0;                                                         
        queueP->rear=-1;
        queueP->size=0;
        queueP->n=n;
        return 0;                                                                  //Function crashes here...
    }
    Where does "a2" point to?
    Inside queueInit() you try to dereference a pointer which points to garbage.

    Bye, Andreas

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    queueElements is a single char.
    But you read an int
    Code:
    scanf("%d",&element);
    Kurt

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    3
    Quote Originally Posted by ZuK View Post
    queueElements is a single char.
    But you read an int
    Code:
    scanf("%d",&element);
    Kurt
    Hmmm didn't notice... so I changed that
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define limit 100000
    
    
         
    /*Queue*/
    
    
    typedef char queueElements;
    typedef
         struct 
         {
             queueElements *contents;
             int front;
             int rear;
             int size;
             int n;
         } queueT;
         
    int queueInit(queueT *queueP, int n);
    void enqueue (queueT *Q, queueElements element);
    int queueIsfull (queueT *Q);
    int dequeue (queueT *Q);
    int queueIsempty (queueT *Q);
    void queuemove (queueT *Q);
    void queueprint (queueT *Q);
    void queueDestroy (queueT *Q);
        
    int queue(queueT *a);
    
    
    int main ()
    {
        
        queueT *a2;
        int sel,res,n;
        do {
             printf("CHOOSE! \n");
             printf("1:Stacks\n");
             printf("2:Queue\n");
             printf("3:Exit\n");
             printf("Choose your destiny.\n\n");
             scanf("%d",&sel);
             switch (sel)
             {
                  case 1:
                       break;
                  case 2:
                       do {
                            printf("***********************************************************************\n");
                            printf("How many elements? (Please give a positive integer smaller than 100000)\n");
                            printf("***********************************************************************\n");
                            scanf("%d",&n);
                            } while (n>limit || n<=0);
                       res=queueInit(a2,n);
                       if (res)
                       {
                            printf("Error in creating stack!\n");
                            exit(0);
                       }
                        printf("!!!!\n");
                       queue(a2);
                       break;
                  case 3:
                       break;     
             } 
             if (sel==3)
                  break;
        }  while (1);
        system("pause");  
        return 0;
    }              
    
    
    
    
    
    
    
    
    
    
    
    
    int queue(queueT *a)
    {
        int sel;
        queueElements element;
        while (1) {
             printf("What to do? \n");
             printf("1:Enqueue\n");
             printf("2:Dequeue\n");
             printf("3:Move elements to the beginning\n");
             printf("4:Print\n");
             printf("5:Destroy and exit.\n");
             printf("Choose your destiny.\n\n");
             scanf("%d",&sel);
             printf("\n");
             switch (sel)
             {
                  case 1:
                       if ( queueIsfull(a) )
                            printf("Error: Queue is full! Dequeue an element in order to enqueue one. \n\n");
                       else
                       {
                            printf("Enter the value to insert.\n\n");
                            scanf("%c",&element);
                            enqueue(a,element);
                       }
                       break;
                  case 2:
                       if ( queueIsempty(a) )
                            printf("Error: Queue is empty! Enqueue an element in order to dequeue one.\n\n");
                       else
                       {
                            element = dequeue(a);
                            printf("Element removed from queue is %d.\n\n", element);
                       }
                       break;
                  case 3:
                       if ( queueIsfull(a) )
                            printf("Error: Queue is full! Dequeue an element in order to move the elements of the queue.\n\n"); 
                       else if ( queueIsempty(a))
                            printf("Error: Queue is empty!\n\n");
                       else
                            queuemove (a);
                       break;
                  case 4:
                       if ( queueIsempty(a) )
                            printf("Error: Queue is empty! Enqueue an element in order to show the queue.\n\n");
                       else
                            queueprint(a);
                       break;
                  case 5:
                       queueDestroy(a);
                       return 0;
             }
                  }
    }
    
    
    int queueInit(queueT *queueP, int n)
    {
        queueElements *newcontents;
        newcontents=(queueElements*)malloc(sizeof(queueElements)*n);
        if (newcontents==NULL)                                                                                                                   //
             return 1;   
        system("pause");     
        printf("!!!!\n");
        queueP->contents=newcontents;                                          
        printf("!!!!\n");
        system("pause");
        queueP->front=0;                                                         
        queueP->rear=-1;
        queueP->size=0;
        queueP->n=n;
        return 0;                                                                  //Function crashes here...
    }
    
    
    
    
    
    
    void enqueue (queueT *Q, queueElements element)
    {
         Q->rear++;
         Q->contents[Q->rear]=element;
         Q->size++;
    }
    
    
    int queueIsfull (queueT *Q)
    {
        if ( Q->size == Q->n)
             return 1;
        return 0;
    }
    
    
    int dequeue (queueT *Q)
    {
        int element;
        element = Q->contents[Q->front];
        Q->size--;
        Q->contents[Q->front]=0;
        Q->front++;
        return element;     
    }
    
    
    int queueIsempty (queueT *Q)
    {
         if ( Q->front > Q->rear )
              return 1;
         else
              return 0;
    }
    
    
    void queuemove (queueT *Q)
    {
         int i;
         if (Q->front>=1)
              {
              for (i=0;i<=Q->size;i++)
              {
                   Q->contents[i]=Q->contents[Q->front];
                   Q->front++;
              }
              Q->front=0;
              Q->rear=Q->size-1;
              }
         else
              printf("The elements of the queue are already placed in the beginning.\n\n");
    }
         
    
    
    void queueprint (queueT *Q)
    {
         int i;
         for (i=Q->front;i<=Q->rear;i++)
             printf("%d ",Q->contents[i]);
         printf("\n");
    }
    
    
    void queueDestroy (queueT *Q)
    {
         free(Q->contents);    
    }
    Quote Originally Posted by AndiPersti View Post
    Code:
    int main ()
    {
        queueT *a2;
    ...
        res=queueInit(a2,n);
    ...
    }
    
    int queueInit(queueT *queueP, int n)
    {
    ...
        queueP->contents=newcontents;                                           
        printf("!!!!\n");
        system("pause");
        queueP->front=0;                                                         
        queueP->rear=-1;
        queueP->size=0;
        queueP->n=n;
        return 0;                                                                  //Function crashes here...
    }
    Where does "a2" point to?
    Inside queueInit() you try to dereference a pointer which points to garbage.

    Bye, Andreas
    I really don't get what you mean. Are you suggesting to remove the queueT *a2 from the main and queueT *queuePfrom int queueInit and make it a global pointer?

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Andreas said that your queueT * a2 points nowhere

    you could

    Code:
    queueT a2;
    .....
    res=queueInit(&a2,n);
    Kurt

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    3
    Quote Originally Posted by ZuK View Post
    Andreas said that your queueT * a2 points nowhere

    you could

    Code:
    queueT a2;
    .....
    res=queueInit(&a2,n);
    Kurt
    Holy macaroni! It worked! Was trying to figure out the problem for hours now... Thanks a ton!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Queues and Stacks
    By rpmischris in forum C# Programming
    Replies: 1
    Last Post: 11-29-2012, 01:41 AM
  2. Stacks and Queues
    By antros48 in forum C Programming
    Replies: 2
    Last Post: 11-07-2011, 11:02 PM
  3. linked lists, stacks and queues
    By aniramg69 in forum C Programming
    Replies: 10
    Last Post: 11-29-2008, 11:58 AM
  4. Stacks and queues info
    By Emeighty in forum C++ Programming
    Replies: 4
    Last Post: 11-02-2008, 01:41 AM
  5. stacks and queues
    By j0hnb in forum C Programming
    Replies: 4
    Last Post: 04-16-2003, 10:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21