Question: Please help fix my code. Program Description: In C programming, write the functions required to implement a stack using two queues. Problem Description: This is

Please help fix my code. Program Description: In C programming, write the functions required to implement a stack using two queues. Problem Description: This is what I have so far, but when I try to run the program it goes straight into segmentation fault. It doesn't even go into the first call in main. #include  #include  #include  #ifndef TYPE #define TYPE int #endif /************************************************** All of the initial Queue functions ***************************************************/ struct link { TYPE value; struct link * next; }; struct listQueue { struct link *firstLink; struct link *lastLink; int size; }; /* * This function takes a queue and allocates the memory. It then * creates a sentinel and assigns the first link and last link * to the sentinel. */ void listQueueInit(struct listQueue *q) { struct link *lnk = (struct link *)malloc(sizeof(struct link)); assert(lnk != 0); lnk->next = 0; q->firstLink = q->lastLink = lnk; } /* * This function creates a new queue. Parts of the create include allocating * the memory, initializing all of the values and returning a pointer to the newly * created queue. */ struct listQueue * listQueueCreate() { struct listQueue *newNode = (struct listQueue *)malloc(sizeof(struct listQueue)); newNode->firstLink = newNode->lastLink = NULL; return newNode; } /* * This function returns a 1 or 0 based on the statement looking at * the first link. If the next value is null it is empty, and will return 1 */ int listQueueIsEmpty(struct listQueue *q) { return q->firstLink == q->lastLink; } /* * This function adds a new link and value to the back of the queue. It takes * a list and a type variable, allocates the memory and adjusts the proper links * to make the connection. No return value. */ void listQueueAddBack(struct listQueue *q, TYPE e) { struct link *lnk = (struct link *)malloc(sizeof(struct link)); assert(lnk != 0); lnk->value = e; lnk->next = 0; q->lastLink->next = lnk; q->lastLink = lnk; } /* * This function takes a list argument and removes the link at the front. */ void listQueueRemoveFront(struct listQueue *q) { struct link *lnk = q->firstLink->next; assert(!listQueueIsEmpty(q)); if (q->firstLink->next->next == 0) { q->lastLink = q->firstLink; } q->firstLink->next = lnk->next; free(lnk); } /* * Function returns the value at the front of the list. */ TYPE listQueueFront(struct listQueue *q) { assert(!listQueueIsEmpty(q)); return q->firstLink->value; } /* * This function is a tester function that iterates through the list * and prints out the values at each link. */ void printList(struct listQueue* l) { assert(l != 0); struct link * printMe = l->firstLink->next; while (printMe != NULL) { printf("Value: %d ", printMe->value); printMe = printMe->next; } } /************************************************** Stack Implementation ***************************************************/ struct linkedListStack { struct listQueue *Q1; struct listQueue *Q2; int structSize; }; /* * This function initializes the values of the created Stack. Initializes both * queues and the structSize. */ void linkedListStackInit(struct linkedListStack * s) { s->structSize = 0; listQueueInit(s->Q1); listQueueInit(s->Q2); } /* * This function creates the linked list stack. It allocates the memory and calls the * initialization function to initialize all of the values. It then returns the stack. */ struct linkedListStack * linkedListStackCreate() { struct linkedListStack* newNode = malloc(sizeof(struct linkedListStack)); linkedListStackInit(newNode); return newNode; } /* * This function returns 1 if the linked list stack is empty and otherwise returns a 0. */ int linkedListStackIsEmpty(struct linkedListStack *s) { if (s->structSize == 0) { return 1; } return 0; } /* * This is the linked list acting as a stack push function. It takes * a linked list stack argument and a value and pushes it onto the stack. The * funciton then also increases the size of the stack by 1. */ void linkedListStackPush(struct linkedListStack *s, TYPE d) { s->structSize++; listQueueAddBack(s->Q2, d); while (!listQueueIsEmpty(s->Q1)) { listQueueAddBack(s->Q2, listQueueFront(s->Q1)); listQueueRemoveFront(s->Q1); } struct listQueue* q = s->Q1; s->Q1 = s->Q2; s->Q2 = q; } /* * This funciton pops a value off of the stack. It does this by moving all values * that are currently on the stack to the other queue. The stack top is maintained * at the back of the queue list. */ void linkedListStackPop(struct linkedListStack *s) { assert(!listQueueIsEmpty(s->Q1)); listQueueRemoveFront(s->Q1); s->structSize--; } /* * This function returns the value that is at the back of the queue that is * maintaing the values of the stack. */ TYPE linkedListStackTop(struct linkedListStack *s) { assert(!listQueueIsEmpty(s->Q1)); return listQueueFront(s->Q1); } /* * This function gores through the stack and removes each link in the queue. * It then frees the struct itself. */ void linkedListStackFree(struct linkedListStack *s) { free(s->Q1); free(s->Q2); } /* * Main is used to test the stack ADT. */ int main(int argc, char* argv[]) { struct linkedListStack *stack = linkedListStackCreate(); //Test Stack //Push 4 values onto the stack printf("Pushing the value: 1 "); linkedListStackPush(stack, 1); printf("Pushed. "); printf("Pushing the value: 2 "); linkedListStackPush(stack, 2); printf("Pushed. "); printf("Pushing the value: 3 "); linkedListStackPush(stack, 3); printf("Pushed. "); printf("Pushing the value: 4 "); linkedListStackPush(stack, 4); printf("Pushed. "); //Print value at the top and then remove it printf("Value at the top of stack %d now being popped. ", linkedListStackTop(stack)); linkedListStackPop(stack); printf("Value popped. "); printf("Value at the top of stack: %d now being popped. ", linkedListStackTop(stack)); linkedListStackPop(stack); printf("Value popped. "); printf("Value at the top of stack: %d now being popped. ", linkedListStackTop(stack)); linkedListStackPop(stack); printf("Value popped. "); printf("Value at the top of stack: %d now being popped. ", linkedListStackTop(stack)); linkedListStackPop(stack); printf("Value popped. "); //Try to pop when the stack is empty prints error: printf("Trying to pop empty stack: "); linkedListStackPop(stack); //Push and Pop alternating printf("Pushing the value: 10 "); linkedListStackPush(stack, 10); printf("Pushed. "); printf("Pushing the value: 11 "); linkedListStackPush(stack, 11); printf("Pushed. "); printf("One more pop: "); linkedListStackPop(stack); printf("Value at the top of stack: %d ", linkedListStackTop(stack)); linkedListStackFree(stack); return 0; }

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!