DAMAGE:after Normal blocks(#59)at 0x00030F08

Hello you guys,I'm doing a practice with queue,and when I run my program,trying to process the dequeue,it getting rudely crushed,so what's wrong with my dequeue function or I allocate the memory wrong.anyone can help?

here is the code

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

struct queueNode{//
char data;
struct queueNode *nextPtr;
};

typedef struct queueNode QueueNode;//create a nickname of struct queueNode
typedef QueueNode *QueueNodePtr;//create a nickname of QueueNode

void printQueue(QueueNodePtr);//Function used to print the items in a queue
int isEmpty(QueueNodePtr);//Function to check if the queue is empty
void EmptyTheQueue();//Function to clear the whole queue
char dequeue(QueueNodePtr *,QueueNodePtr *);//Function to remove an item from a queue
void enqueue(QueueNodePtr *,QueueNodePtr *,char);//Function to add an new item to a queue
void InitializeQueue(QueueNodePtr *,QueueNodePtr *);//Function to initialize a queue
void Menu();//menu function
void Quit();//quit function

int main(){
Menu();//program start from the menu
return 0;
}
QueueNodePtr headPtr=NULL,tailPtr=NULL;//defined a head pointer and a tail pointer which points to the first node of the queue and the end node of the queue separately
void Menu(){
int choice;
char item;
char a;
char b;
//print the menu
printf("====================Welcome to the Menu====================\n");
printf("= 1.Create a new queue =\n");
printf("= 2.Add an item to queue =\n");
printf("= 3.Remove an item from the queue =\n");
printf("= 4.Clear the exist queue =\n");
printf("= 5.Quit =\n");
printf("===================What can I do for you:)=================\n");
printf("So please enter a number to have fun with the queue:\n");
scanf("%d",&choice);

switch(choice){//start to work with user's choice
case 1:
InitializeQueue(&headPtr,&tailPtr);break;
case 2:
Quit();
do{
printf("It's good for you to enter a character:");
scanf("\n%c",&item);
enqueue(&headPtr,&tailPtr,item);//start to add an item into the queue
printf("Enter another character?(y/n)");
getchar();
scanf("%c",&b);
}while(b=='y');
printf("I guess you have the right to see what is your queue looks like now:\n");
printQueue(headPtr);//print the queue after added
//Give user a chance to decide go back to menu or just terminate.
printf("Do you think it's a good idea to back to menu?(y/n)");
getchar();
scanf("%c",&a);
if(a=='y'){
Quit();
Menu();
}
else Quit();
break;
case 3:
Quit();
if(!isEmpty(headPtr)){//remove an item from this queue while the queue is not empty
item=dequeue(&headPtr,&tailPtr);//start to remove
printf("Oh my god!!! %c has been dequeued\n",item);
printf("Sill want to have a look ? OK,here is the queue:\n");
printQueue(headPtr);//print the queue after remove.
}
else {
printf("The queue is already empty,there is no need to enqueue\n");
}
printf("Do you think it's a good idea to back to menu?(y/n)");
getchar();
scanf("%c",&a);
if(a=='y'){
Quit();
Menu();
}
else Quit();
break;
case 4:
Quit();
EmptyTheQueue();//call this function to clear all node in a queue
printf("Do you think it's a good idea to back to menu?(y/n)");
getchar();
scanf("%c",&a);
if(a=='y'){
Quit();
Menu();
}
else Quit();
break;
case 5:
Quit();
}
}

void Quit(){
system("cls");//clear the screen
}

void enqueue(QueueNodePtr *headPtr,QueueNodePtr *tailPtr,char value){
QueueNodePtr newPtr;//create a new node
newPtr = malloc(sizeof(QueueNodePtr));//assign the allocated memory address to newPtr

if(newPtr!=NULL){//if the memory is available
newPtr->data=value;//store the new value into the member data of the new node
newPtr->nextPtr=NULL;//set the next pointer of current node point to NULL,to indicate that this node is the last node of this queue
if(isEmpty(*headPtr))//if this queue is empty then let the head pointer points to the new node
*headPtr=newPtr;
else
(*tailPtr)->nextPtr=newPtr;//set the next pointer of current tail node points to new node,in this step we linked the now node into the queue.

*tailPtr=newPtr;//reset the tail pointer points to the new pointer to find the last pointer
}
else
printf("I'm sorry,%c was not inserted,no memory available.",value);
}

char dequeue(QueueNodePtr *headPtr,QueueNodePtr *tailPtr){
char value;
QueueNodePtr tempPtr;

value=(*headPtr)->data;//copy the item to a temporary variable
tempPtr=*headPtr;//
*headPtr=(*headPtr)->nextPtr;//let the head pointer points to next node

if(*headPtr==NULL)//If the last item has been removed,the set both head pointer and tail pointer to MULL.
*tailPtr=NULL;

free(tempPtr);//free the memory of temporary pointer
return value;
}

int isEmpty(QueueNodePtr headPtr){//check if the queue is empty
return headPtr==NULL;
}

void printQueue(QueueNodePtr currentPtr){//print the queue
if(currentPtr==NULL)
printf("Ah o,The queue is empty.\n\n");
else{
while(currentPtr!=NULL){//print the member of each node from head to tail until the queue is empty
printf("%c -->",currentPtr->data);
currentPtr=currentPtr->nextPtr;
}
printf("NULL\n");
}
}

void EmptyTheQueue(){
while(!isEmpty(headPtr))//remove each item from head to tail until the queue is empty
dequeue(&headPtr,&tailPtr);
printf("OK,now the queue is totally clean.as you see:\n");
printQueue(headPtr);
}

void InitializeQueue(QueueNodePtr *headPtr,QueueNodePtr *tailPtr){//initialize the queue
char a;
Quit();

*headPtr=NULL;//set both head pointer and tail pointer to NULL cause we are create an empty queue here
*tailPtr=NULL;

printf("The Queue has been initialized,you are welcome.\n");
printf("Back to menu?(y/n)");
getchar();
scanf("%c",&a);
if(a=='y'){
Quit();
Menu();
}
else Quit();
}
Last edited on
Topic archived. No new replies allowed.