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

  • Thread starter Thread starter sunilkamadolli
  • Start date Start date
  • Tags Tags
    Queue
AI Thread Summary
The discussion revolves around a program that implements Stack and Queue abstract data types (ADTs) to manage names through various user commands. Key functionalities include saving names, deleting the oldest name, undoing deletions, and permanently removing names. The user interface provides options for these actions, and the program is designed to handle user input and display appropriate messages for each command.The main issue encountered was related to the deletion functionality, where the program failed to print the name that was deleted. This was traced back to memory allocation errors, specifically insufficient space allocated for the string that stores names. The resolution involved adjusting the memory allocation to ensure enough space for the names being saved. Overall, the program's logic was confirmed to be sound, with the identified memory issues being the primary source of the problem.
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.
 
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...
Back
Top