|
-
May 3rd, 2012, 07:22 PM
#8
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: %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
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
|