Having Trouble with 'D' Option in Stack and Queue ADT Program?

  • Thread starter Thread starter sunilkamadolli
  • Start date Start date
  • Tags Tags
    Queue
Click For Summary
SUMMARY

The forum discussion centers on a user encountering issues with the 'D' option in a Stack and Queue Abstract Data Type (ADT) program written in C. The user reports that the program fails to print the name of the deleted item when the 'D' command is executed. The problem is traced back to improper memory allocation for the string variable used to store names. The user resolves the issue by allocating sufficient memory using the expression sizeof(char) * SIZE, where SIZE is a defined constant.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with Stack and Queue data structures
  • Knowledge of dynamic memory allocation in C
  • Experience with debugging C code
NEXT STEPS
  • Review C dynamic memory allocation techniques
  • Learn about Stack and Queue implementations in C
  • Explore error handling and debugging strategies in C
  • Investigate memory management best practices in C programming
USEFUL FOR

Programmers, particularly those working with C, who are developing applications using Stack and Queue data structures and need to troubleshoot memory allocation issues.

sunilkamadolli
Messages
41
Reaction score
0
Hello everyone, I have been trying to figure this problem since hrs, please help me out. It is most likey very easy for you people. I think the problem is mostly syntax but here it is. I am writing this program which uses Stack and Queue ADT . If you look at the code the program is self-explanatory. Please help me out as soon as possible. The more replies i get the more helpful it is. Thanks a lot.


- When the user enters “S” or “s”, prompt the user for a name and save it.
- When the user enters “D” or “d”, delete the oldest name that was saved. If the user deletes more than the number of names in the saved buffer, print a warning. If the user deletes before doing any save, print a warning.
- When the user enters “U” or “u”, undo the most recent delete by putting the name back as newest saved data. The user can undo until all the deleted names are put back. If the user selects undo without any previous delete, or if the user selects undo more than the number of deletes, print a warning.
- When the user enters “R” or “r”, remove the oldest saved name. A remove means that the name is completely gone. After a remove, the user cannot undo and get the name back.
- When the user enters “Q”, end the program.

CODE
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
#include "queue.h"


void getString(char str[]);
char menu();

int main()
{
STACK *s;
QUEUE *q;
void *temp;
void *temp2;
char *temp3;
char *temp4;
void *temp5, *temp6;
void *dataPtr;
char str[10];
char c;
void *strPointer;
s=createStack();
q=createQueue();

c='p';

strPointer=(char *)malloc(sizeof(char));

while(c!='q' || c!='Q')
{
c=menu();

if(c=='s' || c=='S')
{
getString(strPointer);
enqueue(q,(void*)strPointer);
queueRear(q,&temp3);
printf("%s was saved \n",temp3);
}
temp4='/0';
if(c=='d' || c=='D')
{
dequeue(q,(void *)&temp4);
printf("%s was deleted \n",temp4);
pushStack(s,temp4);
}
if(c=='U' || c=='u')
{
if(emptyQueue(q))
printf("No deleted names left");
temp2=popStack(s);
enqueue(q,&temp2);
}
if(c=='R' || c=='r')
{
dequeue(q,&temp);
}

}
}


char menu()
{
char c;
printf("You have the following choices: \n");
printf(" S - Save a name \n");
printf(" D - Delete oldest saved name \n");
printf(" U - Undo most recent delete \n");
printf(" R - Remove oldest saved name \n");
printf(" Q - Quit \n");

scanf("%s",&c);
return c;
}

void getString(char str[])
{
printf("please enter a name to be saved \n");
scanf("%s",str);
}

I am having problem with the 'd' every time i hit 'd' it does not print what was deleted. This is what i mean. If there are any other problems please let me know.
***********OUTPUT***********
You have the following choices:
S - Save a name
D - Delete oldest saved name
U - Undo most recent delete
R - Remove oldest saved name
Q - Quit
s
please enter a name to be saved
tech
tech was saved
You have the following choices:
S - Save a name
D - Delete oldest saved name
U - Undo most recent delete
R - Remove oldest saved name
Q - Quit
s
please enter a name to be saved
forum
forum was saved
You have the following choices:
S - Save a name
D - Delete oldest saved name
U - Undo most recent delete
R - Remove oldest saved name
Q - Quit
d
forum was deleted
You have the following choices:
S - Save a name
D - Delete oldest saved name
U - Undo most recent delete
R - Remove oldest saved name
Q - Quit
d
forum was deleted
You have the following choices:
S - Save a name
D - Delete oldest saved name
U - Undo most recent delete
R - Remove oldest saved name
Q - Quit


If you want to look at the adt of the dequeue here it is
/* Deletes a node from the queue */
/* Pre queue has been created */
/* Post data pointer to queue front returned */
/* to user, and front node deleted and */
/* recycled */
/* Return 1 if successful */
/* 0 if underflow */
int dequeue (QUEUE *queue, void **itemPtr)
 
Computer science news on Phys.org
Itz ok. I found out that the error has to do with memory allocation. When i allocate memory for the string i should have allocated enough space >> sizeof(char) * SIZE . where SIZE is a defined constant. There are few other errors i made that caused the 'd' error. The algorithm works. Thanks anyways.
 

Similar threads

Replies
7
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K