Reference no: EM13834906
Suppose you have a deque D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty queue Q. Give a pseudo-code description of a function that uses only D and Q (and no other variables or objects) and results in D storing the elements (1,2,3,5,4,6,7,8), in this order.
Here is my C++ code so far, I am getting errors unitialized variable d, and newPtr.
#include "stdafx.h"
# include
# include
using namespace std;
/*----------------------------------------------*/
/* TYPES */
typedef int itemtype;
struct node {
struct node * previous;
itemtype item;
struct node * next;
};
typedef struct node node;
struct deque {
node * front; /* pointer to a dummy node. */
node * end; /* pointer to a dummy node. */
int *size;
};
typedef struct deque deque;
/*----------------------------------------------*/
/* PROTOTYPES */
deque newEmptyDeque(void);
int isEmpty(deque d);
void printFrontToEnd(deque d);
void insertAfterInDLL(itemtype i, node * nodePtr); /* auxiliary */
void addToFront(itemtype i, deque d);
void addToEnd(itemtype i, deque d);
itemtype getFront(deque d);
itemtype getEnd(deque d);
itemtype rmFront(deque d);
/*----------------------------------------------*/
int main(void) {
deque d;
d = newEmptyDeque();
printf("%d\n", isEmpty(d));
printf("Here is the deque, front to end: ");
printFrontToEnd(d);
printf(". Listing completed.\n");
addToFront(1, d);
addToFront(2, d);
addToFront(3, d);
addToEnd(4, d);
addToEnd(5, d);
addToEnd(6, d);
printf("Here is the deque, front to end: ");
printFrontToEnd(d);
printf(". Listing completed.\n");
printf("%d\n", isEmpty(d));
printf("%d\n", rmFront(d));
printFrontToEnd(d);
return 0;
}
/*----------------------------------------------*/
deque newEmptyDeque(void)
{
deque d;
d.front = (d.front);malloc(sizeof(node));
d.end = (d.end);malloc(sizeof(node));
d.size = (d.size);malloc(sizeof(int));
*(d.size) = 0;
(d.front)->next = d.end;
(d.front)->previous = NULL;
(d.end)->previous = d.front;
(d.end)->next = NULL;
return d;
}
/*----------------------------------------------*/
int isEmpty(deque d) {
return (d.front)->next == d.end;
}
/*----------------------------------------------*/
void printFrontToEnd(deque d) {
node * aux;
aux = (d.front)->next;
while ((aux->next) != NULL) {
printf("%d ", aux->item);
aux = aux->next;
}
}
/*----------------------------------------------*/
/* Auxiliary function to insert an item after a given node
in a doubly linked list.
*/
void insertAfterInDLL(itemtype i, node * nodePtr) {
node * nextPtr; /* next node after *nodePtr. */
node * newPtr; /* new node. */
nextPtr = nodePtr->next;
newPtr = (newPtr); malloc(sizeof(node));
newPtr->item = i;
nodePtr->next = newPtr;
newPtr->previous = nodePtr;
newPtr->next = nextPtr;
nextPtr->previous = newPtr;
}
/*----------------------------------------------*/
void addToFront(itemtype i, deque d) {
insertAfterInDLL(i, d.front);
*(d.size) = *(d.size) + 1;
}
/*----------------------------------------------*/
void addToEnd(itemtype i, deque d) {
insertAfterInDLL(i, (d.end)->previous);
}
/*----------------------------------------------*/
itemtype getFront(deque d) {
return (d.front)->next->item;
}
itemtype getEnd(deque d) {
return (d.end)->previous->item;
}
itemtype rmFront(deque d) {
itemtype x = (d.front)->next->item;
node * aux;
aux = (d.front)->next->next;
node * z;
z = (d.front)->next;
free(z);
return x;
}
Analyze an operations process in a local organisation
: To briefly describe and analyse an operations process in a local organisation in your country or region, and to explore this process by applying at least one of the core concepts you have learned in this subject.
|
Discuss set of metrics to use by feets
: Using SCOR Level 1, discuss and recommend a set of metrics to use by Feets, the class scenario company. You will want to discuss the effect the metrics you choose will have on the company and the order in which you would implement the metrics
|
What is the minimum size of sequence number in bits
: Suppose that a window size in Selective and Repeat is 4, what is the minimum size of sequence number in bits required to have no problems at receiver.
|
Improve the security and confidentiality of patient health
: he benefits of using information technology are plentiful. For example, the promise of fully realized electronic medical records is having a single record that includes all of a patient's health information: a record that is up to date, complete, ..
|
Pseudocode description of a function that uses only d and q
: Suppose you have a deque D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty queue Q. Give a pseudo-code description of a function that uses only D and Q (and no other variables or objects) and ..
|
How different empires developed in two world culture areas
: analyze how two different empires developed in two world culture areas in the premodern world (between the Bronze Age to 1600 CE).
|
Holly manufacturing case
: Holly Manufacturing Company produces two cello models. One is a standard acoustic cello that sells for $600 and is constructed from medium-grade materials. The other model is a custom-made amplified cello with pearl inlays and a body constr..
|
How would you determine ph of vinegar using cabbage juice
: How would you determine the PH of vinegar using cabbage juice as an indicator
|
Joint stock companies motivate nations to engage exploration
: What motivated each group to explore and colonize the New World? How were their motivations similar, how were they different? How did Mercantilism and Joint Stock companies motivate these nations to engage in exploration
|