Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Urgent help needed.

  1. Oct 1, 2004 #1
    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)
     
  2. jcsd
  3. Oct 1, 2004 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Urgent help needed.
  1. Urgent help needed ! (Replies: 3)

Loading...