Problem about stack and queue in C

This is a discussion on Problem about stack and queue in C within the C Programming forums, part of the General Programming Boards category; This source code can not run well, there is something wrong with function void Reversing(NodePtr pT, NodePtr pQ). Please, help ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Post Problem about stack and queue in C

    This source code can not run well, there is something wrong with function void Reversing(NodePtr pT, NodePtr pQ). Please, help me to correct it. Thank you!
    Code:
    #include <stdlib.h>
    #include <conio.h>
    #include <stdio.h>
    #include "string.h"
    #define TOTAL_SLOTS 100 
    #define TRUE 1 
    #define FALSE 0 
    typedef int BOOL; 
    char id[10];
    FILE *file;
    int x;
           
    typedef struct node *NodePtr; 
    struct node{ 
            int info; 
            NodePtr next; 
    }; 
    
    typedef struct queue Queue; 
    struct queue 
    { 
      NodePtr pFront, pRear; 
    }; 
    /*Implement linked-list-base stack */
    void InitializeT(NodePtr *pT){ 
         *pT=NULL;
    }
    int IsEmptyStack(NodePtr pT)
    {
         return (pT==NULL);
    }
    void pushStack (NodePtr *pT, int new_item){
        NodePtr p;
        p=(NodePtr)malloc(sizeof(struct node));
        p->info = new_item;
        p->next = *pT;
        *pT= p;
    }
    int popStack (NodePtr *pT){
        NodePtr p;
        int rtn_value;
        if (IsEmptyStack(*pT)){
        printf("underflow\n");
        exit(1);
        }
        rtn_value=(*pT)->info;
        p = *pT;
        *pT= p->next; 
        free(p); 
        return (rtn_value);
    }
    int stacktopStack(NodePtr *pT){
        if (IsEmptyStack(*pT))
        {
        printf("underflow\n");
        exit(1);
        }
        return (*pT)->info;
    }
    /*Implement linked-list-base queue*/
    void initializeQ(Queue *pQ) 
    {  pQ->pFront=NULL; 
      pQ->pRear=NULL; 
    } 
    BOOL IsEmpty(Queue *pQ) 
    {   
      return ((pQ->pFront==NULL) && (pQ->pRear==NULL)); 
    }
    void insertQ(Queue *pQ, int x) 
    {   
      NodePtr p; 
      p = (NodePtr)malloc(sizeof(struct node)); 
      p->info = x; 
      p->next = NULL; 
      if (IsEmpty(pQ)) 
        pQ->pFront = p; 
      else 
        pQ->pRear->next = p; 
      pQ->pRear = p; 
    } 
    int deleteQ(Queue *pQ) 
    {  NodePtr p; 
      int return_value; 
      if (IsEmpty(pQ)) 
      {  printf("queue underflow\n"); 
        exit(1); 
      } 
      p = pQ->pFront; 
      return_value = p->info; 
      pQ->pFront = pQ->pFront->next; 
      if (pQ->pFront==NULL) 
          pQ->pRear=NULL; 
      free(p); 
      return return_value; 
    }
    void getId(){
         FILE *file;
         printf("Enter your student ID from keyboard\n");     
         scanf("%s",id );
         printf("Your student ID is %s \n", id);
         file = fopen("Input.txt","w");
         fprintf(file, "%s", id);
         fclose(file);
    }
    
    void Reversing(NodePtr pT, NodePtr pQ){
    
    file = fopen("Input.txt", "w");
    int n=0;
    while((id[n])!='\0')
    n++;
    int i;
    for(i=0;i<=n;i++)
    pushStack(&pT,id[i]); 
    while(!IsEmptyStack(pT)){
        x = popStack(&pT);
    	insertQ(&pQ,x);
    }
    while (!IsEmpty(&pQ)){
        x = deleteQ(&pQ);
    	pushStack(&pT,x);
    	fprintf(file,"%c",popStack(&pT));   
    }
         
    }
    int main()     
    {  
      struct queue q;
      struct node pT;
      initializeQ(&q);
      InitializeT(&pT);
      getId(); 
      Reversing(&pT, &q);
      system("pause");
      getch();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What sort of "something wrong"? Explain what it is you expect it to do, and what you think it's actually doing.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Post the error message(s) that you are getting with the associated line numbers and then we can help you.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Thank Paul and quzah!
    Task of this source code is to input from keyboard a string of number.
    Then reverse it. For example, I enter my student ID 050104018, result of reversing should be 810401050.
    In my source code, after I input string of numbers and print it to screen. I got error and can not process to do next task, is to reverse this number. It got errors in line 117, 119, 120, 131, 133 with warnings:
    E:\dsa\stack_queue.c In function `Reversing':
    117 E:\dsa\stack_queue.c [Warning] passing arg 1 of `insertQ' from incompatible pointer type
    119 E:\dsa\stack_queue.c [Warning] passing arg 1 of `IsEmpty' from incompatible pointer type
    120 E:\dsa\stack_queue.c [Warning] passing arg 1 of `deleteQ' from incompatible pointer type
    E:\dsa\stack_queue.c In function `main':
    131 E:\dsa\stack_queue.c [Warning] passing arg 1 of `InitializeT' from incompatible pointer type
    133 E:\dsa\stack_queue.c [Warning] passing arg 2 of `Reversing' from incompatible pointer type

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by neyul View Post
    Thank Paul and quzah!
    Task of this source code is to input from keyboard a string of number.
    Then reverse it. For example, I enter my student ID 050104018, result of reversing should be 810401050.
    In my source code, after I input string of numbers and print it to screen. I got error and can not process to do next task, is to reverse this number. It got errors in line 117, 119, 120, 131, 133 with warnings:
    E:\dsa\stack_queue.c In function `Reversing':
    117 E:\dsa\stack_queue.c [Warning] passing arg 1 of `insertQ' from incompatible pointer type
    119 E:\dsa\stack_queue.c [Warning] passing arg 1 of `IsEmpty' from incompatible pointer type
    120 E:\dsa\stack_queue.c [Warning] passing arg 1 of `deleteQ' from incompatible pointer type
    E:\dsa\stack_queue.c In function `main':
    131 E:\dsa\stack_queue.c [Warning] passing arg 1 of `InitializeT' from incompatible pointer type
    133 E:\dsa\stack_queue.c [Warning] passing arg 2 of `Reversing' from incompatible pointer type


    I added line numbers to your code:



    Code:
    while(!IsEmptyStack(pT)){
        x = popStack(&pT);
    	insertQ(&pQ,x);		// 117
    }
    while (!IsEmpty(&pQ)){		// 119
        x = deleteQ(&pQ);		// 120
    	pushStack(&pT,x);
    	fprintf(file,"%c",popStack(&pT));
    }
    
    }
    int main()
    {
      struct queue q;
      struct node pT;
      initializeQ(&q);
      InitializeT(&pT);		// 131
      getId();
      Reversing(&pT, &q);	// 133
      system("pause");
      getch();
    }
    1. It looks like it doesn't like how you are passing your arguments. Should it really have & in front of those variables? Perhaps not.

    2. Are you using Linux or Windows? You should use a good debugger (Microsoft Visual C++ on Windows, or KDBG on Linux), so you can see exactly what is going on with your variables. (Or print them out manually if you can't install either program but a visual debugger will save you hours and hours of time.)

    3. So you have no errors at all? Only warnings?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Pushing a Queue onto a Stack?
    By MiroMage in forum C Programming
    Replies: 5
    Last Post: 10-14-2008, 10:23 PM
  3. Queue Stack Palindrome
    By silicon in forum C++ Programming
    Replies: 20
    Last Post: 07-15-2004, 04:38 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. queue / stack
    By smd in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2002, 02:30 PM

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