-
April 25th, 2012, 10:30 PM
#1
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|