Help with Deqcue program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Help with Deqcue program

  1. #1
    Join Date
    Apr 2012
    Posts
    3

    Help with Deqcue program

    I've been racking my brain on this thing for the past week+ and can't move forward in it. I'm making a decque without using the decque lib. I can't figure out my Push. It just seems to be making two identical lists. And my prev and next don't mean squat.

    The program is supposed to read a file and runs commands based off what it reads in.

    I'm just going to shut up now and post code. Please help:
    Code:
    // INCLUDE/IMPORT/REQUIRE/USING ++++++++++++++++++++++++++++++++++
    #include <ctype.h>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    using namespace std;
    
    // CONSTANTS +++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    #define USAGE "\n\tUSAGE: No txt file loaded.\n\n"
    #define EMPTY_DECQUE_ERR "\tOPERATION ON AN EMPTY DECQUE: %s\n"
    #define INVALID_DATA_ERR "\tNO DATA AFTER COMMAND PROMPT: %s\n"
    
    // DEFINES/TYPE/TYPEDEF ++++++++++++++++++++++++++++++++++++++++++
    
    /* ===============================================================
     * NAME: to_upper()
     * PURPOSE: macro defined using toupper() function to convert
     *          an entire string to UPPER-CASE characters.
     * NOTES: looks for null terminator at the end of the string.
     */
    #define to_upper(x) {
       for(cntr=0;inBfr[cntr];cntr++) {
          inBfr[cntr] = toupper(inBfr[cntr]);
       }
    }
    
    #define MAX_OPS 7
    #define MAX_NXT 5
    
    typedef enum {
       PUSH   = 0,
       POP    = 1,
       POPALL = 2,
       PEEK   = 3,
       POKE   = 4,
       POLL   = 5,
       SENTRY = MAX_OPS - 1
    } ENUM_OPS;
    
    char *OPS_LIST[MAX_OPS] = {
       "PUSH",
       "POP",
       "POPALL",
       "PEEK",
       "POKE",
       "POLL",
       "\tUNKNOWN OPERATION"
    };
    
    typedef enum {
       HEAD   = 0,
       TAIL   = 1,
       B2F    = 2,
       F2B    = 3,
       FLAG = MAX_NXT - 1
    } ENUM_ENDS;
    
    char *NXT_LIST[MAX_NXT] = {
        "HEAD",
        "TAIL",
        "B2F",
        "F2B",
        "\tUNKNOWN OPERATION"
    };
    
    // STRUCTS/CLASSES +++++++++++++++++++++++++++++++++++++++++++++++
    
    /* Formerly NODE.H +++++++++++++++++++++++++++++++++++++++++++++++
     * NAME: NODE (S_NODE)
     * PURPOSE: implements the NODE data structure
     * NOTES: this is a C data structure incorporated into C++
     *        code simply because it is only a container structure
     *        and doesn't require the additional overhead of a class.
     */
    typedef struct NODE {
       int data;
       struct NODE *prev;
       struct NODE *next;
    } S_NODE;
    
    class CDECQUE {
    
    public:
    	S_NODE *Tail;
    	S_NODE *Head;
    
       CDECQUE(int stackName) {
    		Tail = (S_NODE *) malloc(sizeof(S_NODE));
    		Head = (S_NODE *) malloc(sizeof(S_NODE));
    		Tail->data = stackName;
    		Head->data = stackName;
    		// Tail->prev = Head->next;
    		Tail->next = NULL;
    		// Head->next = Tail->prev;
    		Head->prev = NULL;
    		Tail = NULL;
    		Head = NULL;
       };
    
    
    
        int Push(int thisVal){
    		S_NODE *newNode = (S_NODE *) malloc(sizeof(S_NODE));
    
    		if (Tail == Head){
    		    newNode->next = Head;
    		    newNode->prev = Tail;
    		    newNode->data = thisVal;
    		    Tail = newNode;
    		    Head = newNode;
            }
    		else{
    		    cout << "P8\n";
    		    newNode->prev = NULL;
    		    newNode->next = NULL;
    		}
    		cout << "PUSH  : " << newNode->data << "\n";
    	}
    
    
    
       int Pop(int popCheck, int thisVal){
    		if(Head != NULL){
                S_NODE *cursor = (S_NODE *) malloc(sizeof(S_NODE));
                cursor = Head;
                int val = cursor->data;
                if(popCheck == 1){                 // Pop first instance
                    while (val != thisVal || val != NULL){
                        if (val == NULL){
                            cout << "POP   : " << thisVal << " not found\n";
                            break;
                        }
                        else if(val == thisVal){
                            cout << "POP   : " << val << " popped\n";
                            break;
                        }
                        cursor = cursor->next;
                        val = cursor->data;
                    }
                }
                else if(popCheck == 2){                 // Pop first instance
                    int i = 0;
                    while (val != thisVal || val != NULL){
                        if (val == NULL && i != 0){
                            cout << "POPALL: " << thisVal << " popped " << i << " times\n";
                            break;
                        }
                        else if (val == NULL){
                            cout << "POPALL: " << thisVal << " not found\n";
                            break;
                        }
                        else if(val == thisVal){
                            i = i++;
    
                        }
                        cursor = cursor->next;
                        val = cursor->data;
                    }
                }
                else{
                    printf(EMPTY_DECQUE_ERR,"POP");
                }
            }
       }
    
    
    
       int Peek(int peekCheck){
    		if(Head != NULL) {
                if(peekCheck == 1){                 // Peek at Tail
                    cout << "PEEK  : TAIL - " << Tail->data << "\n";
                    }
                else if(peekCheck == 2){            // Peek at Head
                    cout << "PEEK  : HEAD - " << Head->data << "\n";
                    }
                }
            else{
                printf(EMPTY_DECQUE_ERR,"PEEK");
                }
    	}
    
    
    
       int Poke(int pokeCheck, int thisVal){
    		if(Head != NULL) {
                if(pokeCheck == 1){                 // Poke at Tail
                    Tail->data = thisVal;
                    cout << "POKE  : TAIL - "<< Tail->data << "\n";
                    }
                else if(pokeCheck == 2){            // Poke at Head
                    Head->data = thisVal;
                    cout << "POKE  : HEAD - "<< Head->data << "\n";
                    }
                }
            else{
    			printf(EMPTY_DECQUE_ERR,"POKE");
                }
        }
    
    
    
       int Poll(int pollCheck){
    		if(Head != NULL) {
                S_NODE *cursor = (S_NODE *) malloc(sizeof(S_NODE));
                cursor = Head;
                printf("POLL  : ");
    
                if(pollCheck == 1){
                    printf("FtoB - ");
                    while(cursor != NULL){
                        cout << "<" << cursor->data <<"> ";
                        cursor = cursor->next;
                        }
                    printf("END\n");
                    }
                else if(pollCheck == 2){
                    printf("BtoF - ");
                    while(cursor != NULL){
                        cout << "<" << cursor->data <<"> ";
                        cursor = cursor->prev;
                        }
                    printf("END\n");
                    }
    		}
            else{
    			printf(EMPTY_DECQUE_ERR,"POLL");
    			}
        }
    };
    
    // FUNCTIONS/METHODS +++++++++++++++++++++++++++++++++++++++++++++
    
    int parseOp(char *thisOp) {
       int op;
    
       for(op=PUSH;op<SENTRY;op++) {
          if(!strcmp(thisOp,OPS_LIST[op])) break;
       }
       return((int) op);
    }
    
    int parseNxt(char *htCheck) {
       int op;
    
       for(op=PUSH;op<FLAG;op++) {
          if(!strcmp(htCheck,NXT_LIST[op])) break;
       }
       return((int) op);
    }
    
    // MAINLINE ++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    int main(int argc, char **argv, char **envp) {
       int cntr = 0;
       FILE *inFile;
       char inBfr[64];
       char *ops;
       char *data;
       char *nxtSt;
       int thisOp = 0;
       int htCheck = 0;
       CDECQUE thisDecque(666);
    
       if(argc == 2) {
          inFile = fopen(argv[1],"r");
          cntr = 0;
          while (! feof(inFile)) {
             fgets(inBfr,sizeof(inBfr),inFile);
             to_upper(inBfr[cntr]);             // Upper case data
             ops = strtok(inBfr," \n");         // set string token
             thisOp = parseOp(ops);             // passes ops to be parsed to determine
             switch(thisOp) {                   //  the appropriate Switch case.
                case PUSH:{                      // Push a value to the tail
                    nxtSt = strtok(NULL,"\n");
                    if (nxtSt != NULL){
                        int val = atoi(nxtSt);
                        thisDecque.Push(val);
                    }
                    else{
                        printf(INVALID_DATA_ERR, "PUSH");
                    }
                break;}
                case POP:{                      // Push a value to the tail
                    nxtSt = strtok(NULL,"\n");
                    if (nxtSt != NULL){
                        int val = atoi(nxtSt);
                        thisDecque.Pop(1, val);
                    }
                    else{
                        printf(INVALID_DATA_ERR, "POP");
                    }
                break;}
                case POPALL:{                      // Pop the value from the head
                    nxtSt = strtok(NULL,"\n");
                    if (nxtSt != NULL){
                        int val = atoi(nxtSt);
                        thisDecque.Pop(2, val);
                    }
                    else{
                        printf(INVALID_DATA_ERR, "POPALL");
                    }
                break;}
                case PEEK:{                     // Peek at the head/tail value
                    nxtSt = strtok(NULL," \n");
                    if (nxtSt != NULL){
                        htCheck = parseNxt(nxtSt);    // passes ends to be check for
                       switch(htCheck)              //  head or tail
                       {
                            case TAIL:              // Tail
                                thisDecque.Peek(1);
                            break;
                            case HEAD:              // Head
                                thisDecque.Peek(2);
                            break;
                            default:
                                printf("%s: %s\n",NXT_LIST[htCheck],ops);
                       }
                    }
                    else{
                        printf(INVALID_DATA_ERR, "PEEK");
                    }
                break;}
                case POKE:{                     // Poke at the head/tail value
                    nxtSt = strtok(NULL," \n");
                    if (nxtSt != NULL){
                        htCheck = parseNxt(nxtSt);    // passes ends to be check for
                        switch(htCheck)              //  head or tail
                        {
                            case TAIL:{              // Tail
                                int val = atoi(data = strtok(NULL,"\n"));
                                thisDecque.Poke(1, val);
                            break;}
                            case HEAD:{              // Head
                                int val = atoi(data = strtok(NULL,"\n"));
                                thisDecque.Poke(2, val);
                            break;}
                            default:
                                printf("%s: %s\n",NXT_LIST[htCheck],ops);
                        }
                    }
                    else{
                        printf(INVALID_DATA_ERR, "POKE");
                    }
                break;}
                case POLL:{                         // Poll the Queue
                    nxtSt = strtok(NULL," \n");      // creates a new stringtoken
                    if (nxtSt != NULL){
                        htCheck = parseNxt(nxtSt);       // passes ends to be check for
                            switch(htCheck) {               //  head or tail
                                case F2B:                   // Poll f2b
                                    thisDecque.Poll(1);
                                break;
                                case B2F:                   // Poll b2f
                                    thisDecque.Poll(2);
                                break;
                                default:
                                    printf("%s: %s\n",NXT_LIST[htCheck],ops);
                                }
                    }
                    else{
                        printf(INVALID_DATA_ERR, "POLL");
                    }
                break;}
                default:
                   printf("%s: %s\n",OPS_LIST[thisOp],ops);
                break;
             }
          }
          fclose(inFile);
       } else {
          printf("%s",USAGE);
       }
       system("pause");
       //   exit(0);
    }
    Use this to create a txt file and drag it onto what you compile.
    (the first few line are error checking):
    Code:
    peek tail
    peek head
    pop 2
    poke head 1369
    poll f2b
    poll b2f
    push
    pop
    popall
    poke
    peek
    poll
    push 0
    push 1
    push 2
    push 3
    push 4
    push 5
    push 6
    push 7
    push 8
    push 9
    poll f2b
    poll b2f
    peek tail
    peek head
    poke tail 23
    poll f2b
    poll b2f
    poke head 45
    pop 2
    poll f2b
    poll b2f
    popall 6
    poll f2b
    poll b2f
    pop 32
    poll f2b
    poll b2f

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Help with Deqcue program

    Quote Originally Posted by dfoles View Post
    I've been racking my brain on this thing for the past week+ and can't move forward in it. I'm making a decque without using the decque lib. I can't figure out my Push. It just seems to be making two identical lists. And my prev and next don't mean squat.
    I'm just going to shut up now and post code.
    Have you debugged your code? Have you used your compiler's debugger to single step through your program to see where the program doesn't do what you expected? If so, then where does it go awry?

    In general, instead of just posting code, debug your code. Debugging your own code is mandatory. This means learning how to use your compiler's debugger. That is exactly what we would use to solve your problem, so you need to learn what we use to answer many questions here.

    Once you've identified where the problem is, then you fix the problem, or you post back here explaining exactly what line, function, variable, etc. is not correct (by that time, you should have already figured out a solution anyway).

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Help with Deqcue program

    Secondly:
    * NOTES: this is a C data structure incorporated into C++
    * code simply because it is only a container structure
    * and doesn't require the additional overhead of a class.
    */
    To be blunt, this is mostly nonsense. By the time you try to get this working, you have saved very little if any overhead if you just used a std::deque, and instead, you would have saved a whole lot of time getting your program to work. Once you get the program to work with known, working components, then you see if it's worthwhile optimizing anything in terms of speed or size.

    In addition, your program is leaking memory all over the place. You allocate memory using malloc(), and as far as I can tell, there isn't any call to free() to deallocate the memory.

    Another issue:
    Code:
    int val = atoi(data = strtok(NULL,"\n"));
    You do this in several places. If strtok() returns NULL, the atoi() call goes into the world of undefined behaviour. You cannot pass NULL to atoi(), as there is no guarantee how it will work when passed NULL values. Don't be surprised if the code crashes.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 26th, 2012 at 12:40 AM.

  4. #4
    Join Date
    Apr 2012
    Posts
    3

    Re: Help with Deqcue program

    I've debugged my code. I wouldn't have registered on this forum if I hadn't already tried myself.

    The code runs. But I'm just not sure on the initialization of the decque as well as the push.

    All of the
    Code:
            Tail->data = decqueName;
            Head->data = decqueName;
            Tail->next = Head;
            Tail->prev = NULL;
            Head->prev = Tail;
            Head->next = NULL;
            Tail = NULL;
            Head = NULL;
    and
    Code:
                newNode->data = thisVal;
                newNode->next = Head;
                newNode->prev = Tail;
                //Head->next->prev = newNode;
                //Head->next = newNode;
                //Tail->prev->next = newNode;
                //Tail->prev = newNode;
                Head = newNode;
                Tail = newNode;
    makes my head spin.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Help with Deqcue program

    Quote Originally Posted by dfoles View Post
    I've debugged my code. I wouldn't have registered on this forum if I hadn't already tried myself.
    Did you write the code? If you did, then you must have had a plan worked out on paper as to what you wanted to do. When you wrote the program, you are basically executing this plan. So, when you use the debugger, single-stepping into the program, where does the program break down?

    The bottom line is that if you wrote the code, you must be able to debug the code you wrote. Otherwise, the implication is that you didn't know what you were doing when you initially wrote the code. At the code writing stage, you shouldn't be unsure of anything -- everything must be planned out. If the final program has a logical bug, then it becomes very easy to find what it is by simply stepping into the program and debugging using the debugger to see where the running program goes against what you had planned.

    But I'm just not sure on the initialization of the decque as well as the push.
    Did you work this out on paper? This is my point -- you never write code you're not sure of. On paper, how would you initialize your structure? If you drew the deque on paper, how would the head, tail, and whatever other pointers be manipulated to add an item when you call push?

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Help with Deqcue program

    This code:
    Code:
    int Push(int thisVal){
    		S_NODE *newNode = (S_NODE *) malloc(sizeof(S_NODE));
    
    		if (Tail == Head){
    		    newNode->next = Head;
    		    newNode->prev = Tail;
    		    newNode->data = thisVal;
    		    Tail = newNode;
    		    Head = newNode;
            }
    So the code in red -- if you say you debugged this, isn't it strange that you are making Tail and Head the same value all the time when you Push? Now on paper, is that how you envisioned adding an item to work?

    Again, this should have been drawn out on paper, using simple boxes and arrows mimicking the nodes and pointers, as to what happens when adding items.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,276

    Re: Help with Deqcue program

    I'm worried about: "I'm making a decque without using the decque lib", because what you are implementing is actually a list.

    Anywhoo, I see you have expanded a lot of resources on writing an entire test case, when you haven't taken the time to validate the most basic steps: Constructions, and push_back.

    Your code should not have gotten past this:
    Code:
    // INCLUDE/IMPORT/REQUIRE/USING ++++++++++++++++++++++++++++++++++
    #include <ctype.h>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    using namespace std;
    
    /* Formerly NODE.H +++++++++++++++++++++++++++++++++++++++++++++++
     * NAME: NODE (S_NODE)
     * PURPOSE: implements the NODE data structure
     * NOTES: this is a C data structure incorporated into C++
     *        code simply because it is only a container structure
     *        and doesn't require the additional overhead of a class.
     */
    typedef struct NODE {
       int data;
       struct NODE *prev;
       struct NODE *next;
    } S_NODE;
    
    class CDECQUE {
    
    public:
    	S_NODE *Tail;
    	S_NODE *Head;
    
       CDECQUE(int stackName) {
    		Tail = (S_NODE *) malloc(sizeof(S_NODE));
    		Head = (S_NODE *) malloc(sizeof(S_NODE));
    		Tail->data = stackName;
    		Head->data = stackName;
    		// Tail->prev = Head->next;
    		Tail->next = NULL;
    		// Head->next = Tail->prev;
    		Head->prev = NULL;
    		Tail = NULL;
    		Head = NULL;
       };
    
        int Push(int thisVal){
    		S_NODE *newNode = (S_NODE *) malloc(sizeof(S_NODE));
    
    		if (Tail == Head){
    		    newNode->next = Head;
    		    newNode->prev = Tail;
    		    newNode->data = thisVal;
    		    Tail = newNode;
    		    Head = newNode;
            }
    		else{
    		    cout << "P8\n";
    		    newNode->prev = NULL;
    		    newNode->next = NULL;
    		}
    		cout << "PUSH  : " << newNode->data << "\n";
    	}
    };
    
    int main()
    {
      CDECQUE a(1);
      a.Push(2);
      a.Push(3);
    }
    Your constructor essentially does nothing (but leak), since by the end, you've set HEAD and TAIL to 0 anyways.

    As for your push back... well its so wrong I don't even know where to start. What exactly is "Tail == Head" supposed to test? Because this is true when the list is empty, OR has a single element. If there is a single element, then the content of the if will actually have the new node bump out the old one.

    You'll never reach the else clause (since you list doesn't seem to get bigger than 1). That said, given the content of the else clause, I'm not sure what it is supposed to achieve.

    I'll have to agree with Paul: Paper before code.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    Apr 2012
    Posts
    3

    Re: Help with Deqcue program

    [ Yves M : Moderation note - image deleted. ]


    Code:
    // Decques.cpp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //
    // PURPOSE: implementation of DECQUE in C++.
    //
    
    // INCLUDE/IMPORT/REQUIRE/USING ++++++++++++++++++++++++++++++++++
    
    #include <ctype.h>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    using namespace std;
    
    // CONSTANTS +++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    #define USAGE "\n\tUSAGE: No txt file loaded.\n\n"
    #define EMPTY_DECQUE_ERR "\tOPERATION ON AN EMPTY DECQUE: &#37;s\n"
    #define INVALID_DATA_ERR "\tNO DATA AFTER COMMAND PROMPT: %s\n"
    
    // DEFINES/TYPE/TYPEDEF ++++++++++++++++++++++++++++++++++++++++++
    
    #define to_upper(x) {                                            \
       for(cntr=0;inBfr[cntr];cntr++) {                              \
          inBfr[cntr] = toupper(inBfr[cntr]);                        \
       }                                                             \
    }
    
    #define MAX_OPS 7
    #define MAX_NXT 5
    
    typedef enum {
        PUSH   = 0,
        POP    = 1,
        POPALL = 2,
        PEEK   = 3,
        POKE   = 4,
        POLL   = 5,
        SENTRY = MAX_OPS - 1
    } ENUM_OPS;
    
    char *OPS_LIST[MAX_OPS] = {
        "PUSH",
        "POP",
        "POPALL",
        "PEEK",
        "POKE",
        "POLL",
        "\tUNKNOWN OPERATION"
    };
    
    typedef enum {
        HEAD   = 0,
        TAIL   = 1,
        B2F    = 2,
        F2B    = 3,
        FLAG = MAX_NXT - 1
    } ENUM_ENDS;
    
    char *NXT_LIST[MAX_NXT] = {
        "HEAD",
        "TAIL",
        "B2F",
        "F2B",
        "\tUNKNOWN OPERATION"
    };
    
    // STRUCTS/CLASSES +++++++++++++++++++++++++++++++++++++++++++++++
    
    typedef struct NODE {
        int data;
        struct NODE *prev;
        struct NODE *next;
    } S_NODE;
    
    class CDECQUE {
    
    public:
    	S_NODE *Tail;
    	S_NODE *Head;
    
        CDECQUE(int decqueName) {
            Tail = NULL;
            Head = NULL;
        };
    
       /* ------------------------------------------------------------
       * NAME: Push(char *)
       * PURPOSE: compares thisVal with head and tail to create an
       * ordered Decque list.
       */
        int Push(int thisVal){
    		S_NODE *newNode = (S_NODE *) malloc(sizeof(S_NODE));
    		newNode->data = thisVal;
    		if (!Tail && !Head){
    		    newNode->next = NULL;
                newNode->prev = NULL;
                Head = newNode;
                Tail = newNode;
            }
            else if(newNode->data <= Tail->data){
                newNode->next = Tail;
                newNode->prev = Tail->prev;
                Tail->prev = newNode;
                Tail = newNode;
            }
            else if(newNode->data >= Head->data){
                newNode->next = Head->next;
                newNode->prev = Head;
                Head->next = newNode;
                Head = newNode;
            }
            else{
                S_NODE *cursor;
                cursor = Tail;
                while (cursor && (newNode->data > cursor->data)){
                    cursor = cursor->next;
                }
                newNode->next = cursor;
                newNode->prev = cursor->prev;
                cursor->prev->next = newNode;
                cursor->prev = newNode;
    
            }
    		cout << "PUSH  : " << newNode->data << "\n";
    	}
    
       /* ------------------------------------------------------------
       * NAME: Pop(void)
       * PURPOSE: checks thisVal againt the DECQUE and removes the value
       *          from the decque.
       */
       int Pop(int popCheck, int thisVal){
    		if(Head != NULL){
                S_NODE *cursor;
                cursor = Head;
                int i = 0;
                if(popCheck == 1){                          // Pop first instance
                    cout << "POP   : " << thisVal;
                    while (cursor != NULL){
                        if(cursor->data == thisVal){
                            i = 1;
                            if (cursor->prev == NULL){
                                Tail = cursor->next;
                                cursor->next->prev = cursor->prev;
                            }
                            else if(cursor->next == NULL){
                                cursor->prev->next = cursor->next;
                                Head = cursor->prev;
                            }
                            else{
                                cursor->prev->next = cursor->next;
                                cursor->next->prev = cursor->prev;
                            }
                            break;
                        }
                        else{
                            cursor = cursor->prev;
                        }
                    }
                    if (i == 1){
                        cout << " popped\n";
                    }
                    else{
                        cout << " not found\n";
                    }
                }
                else if(popCheck == 2){                     // Pop all instances
                    cout << "POPALL: " << thisVal;
                    while (cursor != NULL){
                        if(cursor->data == thisVal){
                            i = i++;
                            if (cursor->prev == NULL){
                                Tail = cursor->next;
                                cursor->next->prev = cursor->prev;
                            }
                            else if(cursor->next == NULL){
                                cursor->prev->next = cursor->next;
                                Head = cursor->prev;
                            }
                            else{
                                cursor->prev->next = cursor->next;
                                cursor->next->prev = cursor->prev;
                            }
                            if (cursor->prev != NULL){
                                cursor = cursor->prev;
                            }
                            else{
                                break;
                            }
                        }
                        else{
                            if (cursor->prev == NULL){
                                break;
                            }
                            else{
                                cursor = cursor->prev;
                            }
                        }
                    }
                    if (i != 0){
                        cout << " popped " << i << " times\n";
                    }
                    else{
                        cout << " not found\n";
                    }
                }
                else{
                    printf(EMPTY_DECQUE_ERR,"POP");
                }
            }
       }
    
       /* ------------------------------------------------------------
       * NAME: Peek()
       * PURPOSE: implements the PEEK method of the QUEUE class
       * NOTES: added functionality to Peek either the head or tail
       */
       int Peek(int peekCheck){
    		if(Head != NULL) {
                if(peekCheck == 1){                 // Peek at Tail
                    cout << "PEEK  : TAIL - " << Tail->data << "\n";
                    }
                else if(peekCheck == 2){            // Peek at Head
                    cout << "PEEK  : HEAD - " << Head->data << "\n";
                    }
                }
            else{
                printf(EMPTY_DECQUE_ERR,"PEEK");
                }
    	}
    
       /* ------------------------------------------------------------
       * NAME: Poke(char *)
       * PURPOSE: implements the POKE method of the QUEUE class
       * NOTES: added functionality to Poke either the head or tail
       */
       int Poke(int pokeCheck, int thisVal){
    		if(Head != NULL) {
                if(pokeCheck == 1){                 // Poke at Tail
                    Tail->data = thisVal;
                    cout << "POKE  : TAIL - "<< Tail->data << "\n";
                    }
                else if(pokeCheck == 2){            // Poke at Head
                    Head->data = thisVal;
                    cout << "POKE  : HEAD - "<< Head->data << "\n";
                    }
                }
            else{
    			printf(EMPTY_DECQUE_ERR,"POKE");
                }
        }
    
       /* ------------------------------------------------------------
       * NAME: Poll()
       * PURPOSE: implements the POLL method of the Queue class
       */
       int Poll(int pollCheck){
    		if(Head != NULL) {
    		    cout << "POLL  : ";
                S_NODE *cursor;
    
                if(pollCheck == 1){
                    cursor = Head;
                    printf("HtoT - ");
                    while(cursor != NULL){
                        cout << "<" << cursor->data <<"> ";
                        cursor = cursor->prev;
                        }
                    }
                else if(pollCheck == 2){
                    cursor = Tail;
                    printf("TtoH - ");
                    while(cursor != NULL){
                        cout << "<" << cursor->data <<"> ";
                        cursor = cursor->next;
                        }
                    }
                    printf("END\n");
    		}
            else{
    			printf(EMPTY_DECQUE_ERR,"POLL");
    			}
        }
    };
    
    // FUNCTIONS/METHODS +++++++++++++++++++++++++++++++++++++++++++++
    
    /* ===============================================================
    * NAME: parseOp(char *)()
    * PURPOSE: parses the arguments received from the test set
    *          so that the appropriate method corresponding to
    *          the argument can be invoked.
    */
    int parseOp(char *thisOp) {
       int op;
    
       for(op=PUSH;op<SENTRY;op++) {
          if(!strcmp(thisOp,OPS_LIST[op])) break;
       }
       return((int) op);
    }
    /* ===============================================================
    * NAME: parseNxt(char *)()
    * PURPOSE: parses the arguments received from the test set
    *          in the event of a Poke or Peek to determine if
    *          the method affects the Head or Tail of the Queue.
    */
    int parseNxt(char *htCheck) {
       int op;
    
       for(op=PUSH;op<FLAG;op++) {
          if(!strcmp(htCheck,NXT_LIST[op])) break;
       }
       return((int) op);
    }
    
    // MAINLINE ++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    int main(int argc, char **argv, char **envp) {
        int cntr = 0;
        FILE *inFile;
        char inBfr[64];
        char *ops;
        char *data;
        char *nxtSt;
        int thisOp = 0;
        int htCheck = 0;
        CDECQUE thisDecque(666);
    
        if(argc == 2) {
          inFile = fopen(argv[1],"r");
          cntr = 0;
          while (! feof(inFile)) {
             fgets(inBfr,sizeof(inBfr),inFile);
             to_upper(inBfr[cntr]);
             ops = strtok(inBfr," \n");
             thisOp = parseOp(ops);
             switch(thisOp) {
                case PUSH:{
                    nxtSt = strtok(NULL,"\n");
                    if (nxtSt != NULL){
                        int val = atoi(nxtSt);
                        thisDecque.Push(val);
                    }
                    else{
                        printf(INVALID_DATA_ERR, "PUSH");
                    }
                break;}
                case POP:{
                    nxtSt = strtok(NULL,"\n");
                    if (nxtSt != NULL){
                        int val = atoi(nxtSt);
                        thisDecque.Pop(1, val);
                    }
                    else{
                        printf(INVALID_DATA_ERR, "POP");
                    }
                break;}
                case POPALL:{
                    nxtSt = strtok(NULL,"\n");
                    if (nxtSt != NULL){
                        int val = atoi(nxtSt);
                        thisDecque.Pop(2, val);
                    }
                    else{
                        printf(INVALID_DATA_ERR, "POPALL");
                    }
                break;}
                case PEEK:{
                    nxtSt = strtok(NULL," \n");
                    if (nxtSt != NULL){
                        htCheck = parseNxt(nxtSt);
                        switch(htCheck)
                        {
                            case TAIL:
                                thisDecque.Peek(1);
                            break;
                            case HEAD:
                                thisDecque.Peek(2);
                            break;
                            default:
                                printf("%s: %s\n",NXT_LIST[htCheck],ops);
                        }
                    }
                    else{
                        printf(INVALID_DATA_ERR, "PEEK");
                    }
                break;}
                case POKE:{
                    nxtSt = strtok(NULL," \n");
                    if (nxtSt != NULL){
                        htCheck = parseNxt(nxtSt);
                        switch(htCheck)
                        {
                            case TAIL:{
                                int val = atoi(data = strtok(NULL,"\n"));
                                thisDecque.Poke(1, val);
                            break;}
                            case HEAD:{
                                int val = atoi(data = strtok(NULL,"\n"));
                                thisDecque.Poke(2, val);
                            break;}
                            default:
                                printf("%s: %s\n",NXT_LIST[htCheck],ops);
                        }
                    }
                    else{
                        printf(INVALID_DATA_ERR, "POKE");
                    }
                break;}
                case POLL:{
                    nxtSt = strtok(NULL," \n");
                    if (nxtSt != NULL){
                        htCheck = parseNxt(nxtSt);
                            switch(htCheck) {
                                case F2B:
                                    thisDecque.Poll(1);
                                break;
                                case B2F:
                                    thisDecque.Poll(2);
                                break;
                                default:
                                    printf("%s: %s\n",NXT_LIST[htCheck],ops);
                            }
                    }
                    else{
                        printf(INVALID_DATA_ERR, "POLL");
                    }
                break;}
                default:
                   printf("%s: %s\n",OPS_LIST[thisOp],ops);
                break;
            }
        }
        fclose(inFile);
        }
        else {
          printf("%s",USAGE);
        }
       system("pause");
       //   exit(0);
    }
    Last edited by Yves M; May 4th, 2012 at 03:51 AM. Reason: Abusive image

  9. #9
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Help with Deqcue program

    1) So what is the purpose of your silly, immature picture you posted?

    2) Your "solution" still leaks memory. Too bad you now won't get anyone here to help you fix this problem.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 3rd, 2012 at 07:40 PM.

  10. #10
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,694

    Re: Help with Deqcue program

    "Thanks For Nothing"

    Are you kidding?

    People here (professional programmers) have gone out of their way here to give you practical and useful advice on how to approach your problem and point out some fundamental errors in your current solution.

    If you can't stand constructive criticism then that's your loss.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center