Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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;
}

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M91404742

Have any Question?


Related Questions in Computer Engineering

Scenario your algorithm will keep track of a customers

Scenario Your algorithm will keep track of a customer's purchases at the local fireworks stand. Customers will not know exactly how many items they will purchase, so using a For loop on this lab is not allowed. Let's kee ...

Question suppose that pa b c qa c rb are relations such

Question : Suppose that P(A, B, C), Q(A, C), R(B) are relations such that P contains 6 tuples, Q contains 2 tuples and R contains 3 tuples. Find the maximum possible number of tuples in the relation (P * Q) R, where '* ' ...

The economic dynamism which is the index of productive

The economic dynamism, which is the index of productive growth in dollars for countries that are designated by the World Bank as middle-income are in table #8.3.9 ("SOCR data 2008," 2013). Compute a 95% confidence interv ...

Explain why a successful information security program is

Explain why a successful information security program is the shared responsibility of an organizations three communities of interest.

Software design processquestion what are the best practices

Software Design Process Question: What are the best practices in writing program? Describe each one briefly. Noe: This question needs to be research. The book may all of the beat practice listing.

Short answer1 what is the difference between a logic error

Short Answer 1. What is the difference between a logic error and a syntax error? 2. Define and discuss the difference between unary, binary and ternary operators. Give an example of each. 3. When must you use curly brace ...

What is the importance of selling and salesmanship within a

What is the importance of selling, and salesmanship within a small business?

Question suppose a computer using set associative cache has

Question : Suppose a computer using set associative cache has 2^16 words of main memory and a cache of 128 blocks, and each cache block contains 8 words. Show steps, please type. a. If this cache is 2-way set associative ...

Suppose a record has the following fields in this order a

Suppose a record has the following fields in this order: A character string of length 15, an integer of 2 bytes, a SQL date, and a SQL time (no decimal point). How many bytes does the record take if: Fields can start at ...

Wildhorse inc has outstanding bonds that will mature in six

Wildhorse, Inc., has outstanding bonds that will mature in six years and pay an 8 percent coupon semiannually. If you paid $1,009.52 today and your required rate of return was 7.0 percent.  (Round intermediate calculatio ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As