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

  • Thread starter sunilkamadolli
  • Start date
  • Tags
    Queue
In summary, the user is trying to write a program using Stack and Queue ADT to save and manipulate data. The user has implemented a menu system where the user can save, delete, undo, or remove names. However, the user is experiencing an issue with deleting names and has also encountered some errors in their implementation. The user has identified one error related to memory allocation and believes there may be other errors causing problems. Overall, the user is seeking assistance in finding and fixing these errors in their code.
  • #1
sunilkamadolli
41
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
  • #2
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.
 
  • #3

{
QUEUE_NODE *deleteLoc;

if (!queue->count)
return 0;

*itemPtr = queue->front->dataPtr;
deleteLoc = queue->front;
if (queue->count == 1)
queue->rear = queue->front = NULL;
else
queue->front = queue->front->next;

(queue->count)--;
free (deleteLoc);

return 1;
}


Hi there, it seems like you are having trouble with the 'd' option in your menu. From looking at your code, it seems like you are dequeueing the queue before printing the deleted item, so the item is no longer in the queue when you try to print it. You should dequeue the item after printing it, so the item is still in the queue when you try to print it. Also, make sure to check if the queue is empty before trying to dequeue, as an empty queue will cause an underflow.
Another thing to note is that in your code, you are not actually deleting the item from the queue when the user selects the 'd' option. Instead, you are just moving the item from the front of the queue to the top of the stack. To fully delete the item, you will need to use the dequeue function from your ADT, which you have included at the end of your code.
I hope this helps and good luck with your program!
 

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

What is a stack data structure and how does it work?

A stack is a data structure that follows the Last In, First Out (LIFO) principle. This means that the last item added to the stack is the first item to be removed. Stacks have two main operations: push, which adds an item to the top of the stack, and pop, which removes the top item from the stack. This data structure can be implemented using either an array or linked list.

What is a queue data structure and how does it work?

A queue is a data structure that follows the First In, First Out (FIFO) principle. This means that the first item added to the queue is the first item to be removed. Queues have two main operations: enqueue, which adds an item to the back of the queue, and dequeue, which removes an item from the front of the queue. This data structure can also be implemented using either an array or linked list.

What are the main differences between a stack and a queue?

The main difference between a stack and a queue is the order in which items are added and removed. In a stack, the last item added is the first item removed, while in a queue, the first item added is the first item removed. Stacks also have an additional operation called peek, which allows you to view the top item without removing it. Queues do not have this operation.

What are some common applications of stacks and queues?

Stacks and queues have many practical applications in computer science and everyday life. Stacks are used in programming languages for function calls and recursion, as well as in undo/redo functionality in applications. Queues are commonly used in operating systems for managing processes and in computer networks for managing data packets. They can also be used in real-life scenarios, such as waiting in line at a store or bank.

What are some potential drawbacks to using stacks and queues?

One potential drawback of using stacks and queues is that they have a fixed size, which means they cannot hold an unlimited number of items. Another drawback is that accessing or removing items in the middle of a stack or queue is not efficient, as all items above the accessed item must be removed first. Additionally, using a linked list implementation can result in memory fragmentation and slow performance compared to using an array implementation.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top