Question: C Programming: Implement a single stack data structure using 2 instances of the queue.. *PLEASE NOTE* : I need both code AND an explanation with

C Programming: Implement a single stack data structure using 2 instances of the queue..

*PLEASE NOTE* : I need both code AND an explanation with the code provided--the answer MUST MEET the Instructions & Requirements below, must be in C (NOT C++) & in a SINGLE FILE--called "stack_from_queue.c".

If the answer is meets the requirements below, I WILL give awesome reviews!!

Instructions: Use **two instances of Queue data structures** to **implement a Stack**.

You MUST use only one C file to design the entire interface (i.e. no header files). The file must be named stack_from_queue.c.

Requirements: -- Program is written in C language; -- You MUST implement the following stack functions (prototypes below) using two queues ( code provided below):

(**TIP: Think about how the stack behaves and why you'd need a second queue. For me, it started making sense after I thought about how to get the stackTop function to return the last item added to the queue.)

1) struct linkedListStack

2) function linkedListStackInit

3) function linkedListStackFree

4) function linkedListStackPush

5) function linkedListStackTop

6) function linkedListStackPop

7) function linkedListStackIsEmpty

(**NOTE: You can't modify the queue itself-- instead you have to use the two queues inside the above functions to create the behavior of the stack.***This is very important!!***) -- Use an assertion to ensure that when a top or pop is performed the stack has at least one element. --

You MUST be able to test stack_from_queue implementation by pushing, checking the top, and popping.

Finally, the file MUST be able to run using ONLY this gcc command gcc -g -Wall -std=c99 -o stack_from_queue stack_from_queue.c

**********Stack struct/function prototypes-- implemented using 2 queues start here *********

struct linkedListStack {

//FIX ME

}

void linkedListStackInit (struct linkedListStack * s) {

//FIX ME

}

void linkedListStackFree (struct linkedListStack *s) {

//FIX ME

}

void linkedListStackPush (struct linkedListStack *s, TYPE d) {

//FIX ME

}

TYPE linkedListStackTop (struct linkedListStack *s) {

//FIX ME

}

void linkedListStackPop (struct linkedListStack *s) {

//FIX ME

}

int linkedListStackIsEmpty (struct linkedListStack *s) {

//FIX ME

}

****QUEUE data structures-- original given for linkedList implementation*****

struct link {

TYPE value; struct link *next;

};

struct listQueue {

struct link *firstLink;

struct link *lastLink;

};

*****Queue functions--use to implement the above stack functions*************

void listQueueInit (struct listQueue *q) {

struct link *lnk = (struct link *) malloc(sizeof(struct link));

assert(lnk != 0); /* lnk is the sentinel */

lnk->next = 0;

q->firstLink = q->lastLink = lnk;

}

void listQueueAddBack (struct listQueue *q, TYPE e) {

struct link *lnk = (struct link *) malloc(sizeof(struct link));

assert(lnk != 0);

/*allocate memory for new link*/

lnk->value = e;

lnk->next = 0;

q->lastLink->next = lnk;

/*make tail ptr point to new link*/

q->lastLink = lnk;

}

TYPE listQueueFront (struct listQueue *q) {

assert(! listQueueIsEmpty(q));

return q->firstLink->value;

/*get element in front of queue*/

}

void listQueueRemoveFront (struct listQueue *q) {

struct link *lnk = q->firstLink->next;

assert (! listQueueIsEmpty(q));

if (q->firstLink->next->next == 0) /*check if list is empty*/ {

q->lastLink = q->firstLink;

}

q->firstLink->next = lnk->next;

/*change ptr to next element*/

free(lnk);

}

int listQueueIsEmpty (struct listQueue *q) {

return q->firstLink == q->lastLink;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!