1) Modify the C language codes below on the implementation of queues using array
ID: 3723688 • Letter: 1
Question
1) Modify the C language codes below on the implementation of queues using arrays with string data rather than int data. You may use char * or char [].
2) Modify the C language codes below of queues using arrays to deal with structure called House that has 2 elements : int hseNo and string type of hse, a rather than int data or a string data.
#include
#define MAX 8
void enqueue(int x);
int dequeue();
void display();
//Global variable - qArray[MAX], front = -1, back = -1
int qArray[MAX], front = -1, back = -1;
int main()
{
display();
enqueue(34);
enqueue(90);
enqueue(55);
display();
printf(" New front value : %d ",dequeue());
printf(" New front value : %d ", dequeue());
display();
}
void enqueue(int x)
{
if (back == MAX - 1)
{
printf("Queue is full ");
}
else
{
qArray[back++] = x; //if you ++back (0 will be in the queue)
}
}
int dequeue()
{
printf(" Removing %d", qArray[front]);
return qArray[++front]; //difference
}
void display()
{
printf(" In the queue : ");
for (int i = front; i < back; i++)
{
printf(" %d", qArray[i]);
}
printf(" ");
}
Explanation / Answer
//PART 1)
#include <stdio.h>
#define MAX 8
void enqueue(char* x);
char* dequeue();
void display();
//Global variable - qArray[MAX], front = -1, back = -1
char* qArray[MAX];
int front = -1, back = -1;
int main()
{
display();
enqueue("santosh");
enqueue("kumar");
enqueue("NIT");
display();
printf(" New front value : %s ",dequeue());
printf(" New front value : %s ", dequeue());
display();
}
void enqueue(char* x)
{
if (back == MAX - 1)
{
printf("Queue is full ");
}
else
{
qArray[back++] = x; //if you ++back (0 will be in the queue)
}
}
char* dequeue()
{
printf(" Removing %s", qArray[front]);
return qArray[++front]; //difference
}
void display()
{
printf(" In the queue : ");
for (int i = front; i < back; i++)
{
printf(" %s ", qArray[i]);
}
printf(" ");
}
//PART 2)
#include <stdio.h>
#define MAX 8
struct House
{
int hseNo;
char *type_of_hse;
};
void enqueue(struct House x);
struct House dequeue();
void display();
//Global variable - qArray[MAX], front = -1, back = -1
struct House qArray[MAX];
int front = -1, back = -1;
int main()
{
struct House hs;
display();
hs.hseNo=34;
hs.type_of_hse="Condominiums";//TAKEN EXAMPLE
enqueue(hs);
hs.hseNo=90;
hs.type_of_hse="Townhouse";
enqueue(hs);
hs.hseNo=55;
hs.type_of_hse="Duplex/Triplexs";
enqueue(hs);
display();
struct House dq=dequeue();
printf(" New front value : (%d,%s) ",dq.hseNo,dq.type_of_hse);
dq=dequeue();
printf(" New front value : (%d,%s) ", dq.hseNo,dq.type_of_hse);
display();
}
void enqueue(struct House x)
{
if (back == MAX - 1)
{
printf("Queue is full ");
}
else
{
qArray[back++] = x; //if you ++back (0 will be in the queue)
}
}
struct House dequeue()
{
printf(" Removing (%d,%s)", qArray[front].hseNo,qArray[front].type_of_hse);
return qArray[++front]; //difference
}
void display()
{
printf(" In the queue : ");
for (int i = front; i < back; i++)
{
printf(" (%d,%s) ", qArray[i].hseNo,qArray[i].type_of_hse);
}
printf(" ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.