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.
 
I came across a video regarding the use of AI/ML to work through complex datasets to determine complicated protein structures. It is a promising and beneficial use of AI/ML. AlphaFold - The Most Useful Thing AI Has Ever Done https://www.ebi.ac.uk/training/online/courses/alphafold/an-introductory-guide-to-its-strengths-and-limitations/what-is-alphafold/ https://en.wikipedia.org/wiki/AlphaFold https://deepmind.google/about/ Edit/update: The AlphaFold article in Nature John Jumper...
Thread 'Urgent: Physically repair - or bypass - power button on Asus laptop'
Asus Vivobook S14 flip. The power button is wrecked. Unable to turn it on AT ALL. We can get into how and why it got wrecked later, but suffice to say a kitchen knife was involved: These buttons do want to NOT come off, not like other lappies, where they can snap in and out. And they sure don't go back on. So, in the absence of a longer-term solution that might involve a replacement, is there any way I can activate the power button, like with a paperclip or wire or something? It looks...
Back
Top