Question

In: Computer Science

We discussed a stack which expands and contracts as data is inserted and deleted. We also...

We discussed a stack which expands and contracts as data is inserted and deleted. We also discussed a queue which was fixed in size. Describe a technique that could be used to implement a queue which expands and contracts.

Solutions

Expert Solution

Describe a technique that could be used to implement a queue which expands and contracts.

Answer :

The most essential technique is Dynamic memory allocation.

In static memory allocation the size of the queue is kept as fixed, where array is used to implement queue. So, the length of the queue cannot be expanded as much as user need. On the other hand in static array based implementation, though the elements can be deleted from queue, but as the array is already fixed sized memory block, so actual contraction of memory is not done in practical scenario.

Dynamic memory allocation technique is the most effective approach of queue expansion and contraction as per user need. Using dynamic memory allocation technique, the elements can be added to the queue as much as user needs at the time of running the program; it does not have any fixed length. So queue expands according to requirement.

On the other hand the while the elements are removed in FCFS basis, the space allocated by that elements are also freed. Means the queue is contracted most ideally.

Link list is one of the approaches of dynamic memory allocation technique, where, queue elements are expanded as individual node(with data and address part) in the memory, means after creating a particular node in the memory , the value is added to the data part of the node. In this way by adding as much as nodes in the memory user can expand the queue. At the same time, removing the nodes from the queue, the queue can be contracted in most effective way.

malloc is one of the efficient approach to dynamically allocate space in memory and expand sthe queue , where free function is used to remove the memory space occupied by queue nodes and contract the memory.

Example :

Suppose in queue , a node is required to create, the allocated memory address is p and structure name is queue, so

p=(struct queue*)malloc(sizeof(struct queue));

In this way by nodes in memory the queue can be expanded.

According to FCFS, the elements are deleted from queue, suppose the address of first node in queue is p

free(p); is used to remove first node from queue and queue is contracted.

Here below the dynamic memory allocation technique with link list, is used on queue to expand and contract it.

Here fundamental programming C programming language is used to implement the queue with dynamic memory allocation technique. Below the example is given with source code, screen shot and desired output.

PROGRAM

#include<stdio.h>
#include<malloc.h>
struct queue
{
   int data;
   struct queue *next;
}; struct queue *first=NULL,*last=NULL,*p;

/* queue expand with expand() function dynamically*/
void expand()
{
    /*dynamically allocated nodes in queue as required*/
   p=(struct queue*)malloc(sizeof(struct queue));
   /* insert value in queue*/
    printf("Enter value in queue :");
    scanf("%d",&p->data);
    p->next=NULL;
    /* if initially queue is fully contracted */
    if(first==NULL)
    {
first=p;
   last =p;    
}
/* if queue is already expanded */
else
{
   last->next=p;
   last=p;
   }
}

/* contract the queue with contract() function */
void contract()
{
/* if queue is already contacted */
   if(first==NULL)
   {
       printf("\nQueue is already empty, no further contraction possible...\n");
   }
   else
   {
       /* take the first node address in p */
       p=first;
       first=first->next;
       /* free p, which removes the first node,so now the first node address
       is not occupied by any element of queue */
       free(p);
      
   }
}

/* display the queue */
void display()
{
   struct queue *t;
   t=first;
   if(first!=NULL)
   {
   printf("\nQueue elements are :\n");
   while(t!=NULL)
   {
       printf("%d\t",t->data);
       t=t->next;
   }
   printf("\n");
   }
   else
   {
       printf("\nThe queue is fully contracted...\n");
   }
  
}
int main()
{
   int op;
   op=1;
   /* when op is 4 loop will be terminated */
   while(op!=4)
   {
       printf("\n1.Expand Queue :");
       printf("\n2.Contract Queue:");
       printf("\n3.Display:");
       printf("\n4.Exit:");
       /* console input of option */
       printf("\nEnter option : ");
       scanf("%d",&op);
       switch(op)
       {
           case 1:
               expand();
               break;
           case 2:
               contract();
               break;
           case 3:
               display();
               break;
           case 4:
               printf("Exit....");
               break;
           default:
               printf("Give correct choice ");
       }
   }
return 0;
}

SCREEN SHOT

OUTPUT


Related Solutions

Which of the following statements is true about sets? A) The data items are inserted in...
Which of the following statements is true about sets? A) The data items are inserted in the same order as they are traversed by an iterator. B) The data items are inserted in the first in last out manner. C) The data items are traversed by an iterator in the same order as they were inserted. D) The data items are traversed by an iterator in a sorted order that can be different from the order in which they were...
We also discussed that the difference between a spot price and a futures price (i.e. the...
We also discussed that the difference between a spot price and a futures price (i.e. the basis) for storable commodities should be given primarily by cost of carry and transportation cost. Let us say that the spot price for corn in Lincoln is $3.45/bu, while the futures price for May delivery is $3.82/bu. Now let us assume that the cost of carry is $0.03/bu/month, while the transportation cost between Lincoln and the delivery area of the futures market is $0.20/bu....
1. We discussed phage in the context of sausage fermentation; it is also a serious concern...
1. We discussed phage in the context of sausage fermentation; it is also a serious concern in many modern, high-throughput cheese fermentations. However, phage problems never occur during traditional sauerkraut or kefir fermentations. Why would this be the case? 2. Is phage likely to be a problem in beer fermentations? Why or why not?
We also discussed the use of the Extended Euclidian algorithm to calculate modular inverses. Use this...
We also discussed the use of the Extended Euclidian algorithm to calculate modular inverses. Use this algorithm to compute the following values. Show all of the steps involved. 9570-1(mod 12935) 550-1 (mod 1769)
In addition to the metrics discussed in class, we can also use t-test to evaluate the...
In addition to the metrics discussed in class, we can also use t-test to evaluate the difference between two models, i.e., to determine if two sets of performance results are significantly different from each other. In general, the t-test is a statistical hypothesis test in which the test statistic follows a Student's t-distribution under the null hypothesis. Now suppose that we would like to select between two prediction models, M1 and M2. We have performed 10 rounds of 10-fold cross...
Early theories of motivation are discussed in your textbook. We also are given instructions in Scripture...
Early theories of motivation are discussed in your textbook. We also are given instructions in Scripture for how we work. What instructions does God give us regarding how we carry out our jobs?
At the onset of this course, we discussed converting data into information, or knowledge. In your...
At the onset of this course, we discussed converting data into information, or knowledge. In your own words, describe that process. Why is it important?
Q- We have discussed the topic “Rationale of Regulations “ in which we talked about the...
Q- We have discussed the topic “Rationale of Regulations “ in which we talked about the reasons for having regulations in a given country. Rationale of Regulations including issues of monopoly, windfall profits, externalities, social policy, information inadequacies, continuity and availability of services, anti-competitive behavior and predatory pricing, public goods and moral hazard, unequal bargaining power, scarcity and distribution of wealth, rationalization and coordination, and planning. Examine only one of the above Rationale of Regulations by providing a definition, an...
Real estate Finance 1. When we discussed the Space Market, also known as the Rental Market,...
Real estate Finance 1. When we discussed the Space Market, also known as the Rental Market, we said rent is determined by the supply and demand dynamics of the space. What are the demand and supply drivers in the rental market? How does segmentation affect rental prices for physically similar space? 2.When the quantity of space demanded equals the quantity supplied, the market is said to have reached ­­­­­­­­­­­­­­______________________________ (fill in the blank)
Description: In week 1 we discussed which amino acids were termed “essential” but we lacked the...
Description: In week 1 we discussed which amino acids were termed “essential” but we lacked the knowledge to really understand why these would be difficult to make. Now that we have examined the process of making amino acids, let's take a second look at why the body doesn’t make certain amino acids. Instructions: Write a response to the following prompt and then review your peers response: Prompt: Propose a reason for why so many of the essential amino acids belong...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT