Assignment Implement PushThird function Test PushThird function Test InsertNode
ID: 3675009 • Letter: A
Question
Assignment
Implement PushThird function
Test PushThird function
Test InsertNode function
Submit
Document file (.docx/.doc) with source code and output
Header file
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int value;
struct node* next;
}NodeType;
typedef struct queue
{
NodeType* qFront;
NodeType* qRear;
}QueType;
QueType* CreateQueue(void)
{
QueType* q = (QueType *)malloc(sizeof(QueType));
q->qFront = NULL;
q->qRear = NULL;
return q;
}
void Enqueue(QueType* q, int newItem)
{
NodeType* newNode = (NodeType*)malloc(sizeof(NodeType));
newNode->value = newItem;
newNode->next = NULL;
if (q->qRear == NULL)
q->qFront = newNode;
else
q->qRear->next = newNode;
q->qRear = newNode;
}
void Dequeue(QueType* q, int* item)
{
NodeType* tempPtr;
tempPtr = q->qFront;
*item = q->qFront->value;
q->qFront = q->qFront->next;
if (q->qFront == NULL)
q->qRear = NULL;
free(tempPtr);
}
void DestroyQueue(QueType* q)
{
NodeType* tempPtr;
while (q->qFront != NULL)
{
tempPtr = q->qFront;
q->qFront = q->qFront->next;
free(tempPtr);
}
free(q);
}
int IsQueueEmpty(QueType* q)
{
return q->qFront == NULL;
}
void PrintQue(QueType* q)
{
NodeType* node = q->qFront;
while (node != NULL)
{
printf("%d, ", node->value);
node = node->next;
}
}
typedef struct stack
{
NodeType* top;
}StackType;
StackType* CreateStack()
{
StackType* s = (StackType*)malloc(sizeof(StackType));
s->top = NULL;
return s;
}
void Push(StackType* stack, char c)
{
NodeType*node = (NodeType*)malloc(sizeof(NodeType));
node->value = c;
node->next = stack->top;
stack->top = node;
}
void Pop(StackType* stack, int* c)
{
NodeType* node = stack->top;
if (node != NULL)
{
*c = node->value;
stack->top = node->next;
free(node);
}
}
void Destroystack(StackType*stack)
{
int temp;
while (stack->top != NULL)
Pop(stack, &temp);
free(stack);
}
void PrintStack(StackType* s)
{
NodeType* node = s->top;
while (node != NULL)
{
printf("%d, ", node->value);
node = node->next;
}
}
void PushThird(StackType* s, int value)
{
}
typedef struct list
{
int count;
NodeType* head;
}LIST;
void InsertNode(LIST *alist, int value)
{
NodeType* newnode;
newnode = (NodeType*)malloc(sizeof(NodeType*));
alist->head = newnode;
newnode->value = value;
newnode->next = alist->head;
alist->count++;
}
void PrintList(LIST *alist)
{
NodeType* newnode;
newnode = alist->head;
while (newnode != NULL)
{
printf("%d ", newnode->value);
newnode = newnode->next;
}
}
************plz
Implement PushThird function
Test PushThird function
Test InsertNode function
Explanation / Answer
Hi, I have corrected all code and tested. I have attached output at bottow.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int value;
struct node* next;
}NodeType;
typedef struct queue
{
NodeType* qFront;
NodeType* qRear;
}QueType;
QueType* CreateQueue(void)
{
QueType* q = (QueType *)malloc(sizeof(QueType));
q->qFront = NULL;
q->qRear = NULL;
return q;
}
void Enqueue(QueType* q, int newItem)
{
NodeType* newNode = (NodeType*)malloc(sizeof(NodeType));
newNode->value = newItem;
newNode->next = NULL;
if (q->qRear == NULL)
q->qFront = newNode;
else
q->qRear->next = newNode;
q->qRear = newNode;
}
void Dequeue(QueType* q, int* item)
{
NodeType* tempPtr;
tempPtr = q->qFront;
*item = q->qFront->value;
q->qFront = q->qFront->next;
if (q->qFront == NULL)
q->qRear = NULL;
free(tempPtr);
}
void DestroyQueue(QueType* q)
{
NodeType* tempPtr;
while (q->qFront != NULL)
{
tempPtr = q->qFront;
q->qFront = q->qFront->next;
free(tempPtr);
}
free(q);
}
int IsQueueEmpty(QueType* q)
{
return q->qFront == NULL;
}
void PrintQue(QueType* q)
{
NodeType* node = q->qFront;
while (node != NULL)
{
printf("%d, ", node->value);
node = node->next;
}
}
typedef struct stack
{
NodeType* top;
}StackType;
StackType* CreateStack()
{
StackType* s = (StackType*)malloc(sizeof(StackType));
s->top = NULL;
return s;
}
void Push(StackType* stack, int c)
{
NodeType*node = (NodeType*)malloc(sizeof(NodeType));
node->value = c;
node->next = stack->top;
stack->top = node;
}
void Pop(StackType* stack, int* c)
{
NodeType* node = stack->top;
if (node != NULL)
{
*c = node->value;
stack->top = node->next;
free(node);
}
}
void Destroystack(StackType*stack)
{
int temp;
while (stack->top != NULL)
Pop(stack, &temp);
free(stack);
}
void PrintStack(StackType* s)
{
NodeType* node = s->top;
while (node != NULL)
{
printf("%d, ", node->value);
node = node->next;
}
}
void PushThird(StackType* s, int value)
{
// checking wether at least two nodes are in stack
if(s->top ==NULL || s->top->next== NULL)
return;
NodeType*node = (NodeType*)malloc(sizeof(NodeType));
node->value = value;
//getting third pointer to third node from stack
NodeType* temp = s->top->next->next;
// new node pointing to previously third element
node->next = temp;
s->top->next->next = node;
}
typedef struct list
{
int count;
NodeType* head;
}LIST;
void InsertNode(LIST *alist, int value)
{
NodeType* newnode;
newnode = (NodeType*)malloc(sizeof(NodeType*));
newnode->value = value;
if(alist->head == NULL){
alist->head = newnode;
alist->count = 1;
return;
}
newnode->next = alist->head;
alist->head = newnode;
alist->count++;
}
void PrintList(LIST *alist)
{
NodeType* newnode;
newnode = alist->head;
while (newnode != NULL)
{
printf("%d ", newnode->value);
newnode = newnode->next;
}
}
int main(){
// testing list
printf("#########List Testing####### ");
LIST *alist = (LIST*)malloc(sizeof(LIST));
InsertNode(alist,10);
InsertNode(alist,5);
InsertNode(alist,20);
InsertNode(alist,1);
PrintList(alist);
printf(" ");
// testing stack
printf("######Stack Testing####### ");
StackType* s = CreateStack();
Push(s,9);
Push(s,12);
Push(s,13);
Push(s,11);
PushThird(s,14);
PrintStack(s);
return 0;
}
/*
output:
#########List Testing#######
1 20 5 10
######Stack Testing#######
11, 13, 14, 12, 9,
------------------
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.