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();
}