Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

stack.c #include #include #include \"stack.h\" #include \"status.h\" #include \"

ID: 3673494 • Letter: S

Question

stack.c #include #include #include "stack.h" #include "status.h" #include "linkedlist.h" struct stack { LinkedList_Ptr hLinkedList; }; typedef struct stack Stack, * Stack_Ptr; Stack_Ptr stack_init_default() { Stack_Ptr pStack = malloc(sizeof(Stack)); if(pStack == NULL) { return NULL; } pStack->hLinkedList = linkedlist_init_default(); if(pStack->hLinkedList == NULL) { free(pStack); return NULL; } return pStack; } Status stack_push(Stack_Ptr hStack, Item item) { return FAILURE; } Status stack_pop(Stack_Ptr hStack) { return FAILURE; } Item * stack_top(Stack_Ptr hStack) { return NULL; } Boolean stack_empty(Stack_Ptr hStack) { int size = linkedlist_get_size(hStack->hLinkedList); if(size == 0) { return TRUE; } return FALSE; } int stack_get_size(Stack_Ptr hStack) { return linkedlist_get_size(hStack->hLinkedList); } //Precondition: a pointer to a pointer to the stack //Postcondition: freeing the memory held by the stack //Precondition: a pointer to the stack //Postconidition: Returning True or False based on the stack void stack_destroy(Stack_Ptr * phStack) { if(*phStack != NULL) { linkedlist_destroy(&(*phStack)->hLinkedList); } free(*phStack); *phStack = NULL; } Makefile and the stack.c have been edited. . I #include #include "stack.h" int main(int argc, char* argv[]) { Stack_Ptr hStack = stack_init_default(); printf("size of the stack:%d ", stack_get_size(hStack)); stack_destroy(&hStack); return 0;}need to implement the stack_pop, stack_push, and stack_top functions. main_stack.c has to be updated. main_stack.c is as following: #include #include "stack.h" int main(int argc, char* argv[]) { Stack_Ptr hStack = stack_init_default(); printf("size of the stack:%d ", stack_get_size(hStack)); stack_destroy(&hStack); return 0; }.

Node.h file is as follows:

#ifndef NODE_H_
#define NODE_H_

#include "status.h"
#include "item.h"

struct node;
typedef struct node Node, * Node_Ptr;

struct node
{
Item data;
Node_Ptr next;
};

#endif // NODE_H_

stack.c

#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
#include "status.h"
#include "linkedlist.h"

struct stack
{
LinkedList_Ptr hLinkedList;
};

typedef struct stack Stack, * Stack_Ptr;

Stack_Ptr stack_init_default()
{
Stack_Ptr pStack = malloc(sizeof(Stack));
  
if(pStack == NULL)
{
return NULL;
}
pStack->hLinkedList = linkedlist_init_default();
if(pStack->hLinkedList == NULL)
{
free(pStack);
return NULL;
}
return pStack;
}


Status stack_push(Stack_Ptr hStack, Item item)
{
return FAILURE;
}


Status stack_pop(Stack_Ptr hStack)
{
return FAILURE;
}

Item * stack_top(Stack_Ptr hStack)
{
return NULL;
}

Boolean stack_empty(Stack_Ptr hStack)
{
int size = linkedlist_get_size(hStack->hLinkedList);
if(size == 0)
{
return TRUE;
}
return FALSE;
}

int stack_get_size(Stack_Ptr hStack)
{
return linkedlist_get_size(hStack->hLinkedList);
}

//Precondition: a pointer to a pointer to the stack
//Postcondition: freeing the memory held by the stack   
//Precondition: a pointer to the stack
//Postconidition: Returning True or False based on the stack
void stack_destroy(Stack_Ptr * phStack)
{
if(*phStack != NULL)
{
linkedlist_destroy(&(*phStack)->hLinkedList);
}
free(*phStack);
*phStack = NULL;
}

linkedlist.c file

#include <stdlib.h>
#include <stdio.h>
#include "linkedlist.h"
#include "node.h"

struct linkedlist
{
Node_Ptr front;
int size;
};

typedef struct linkedlist LinkedList, * LinkedList_Ptr;

// initializing and allocating the memory for our linkedlist
LinkedList_Ptr linkedlist_init_default()
{
// allocating the memory for my linkedlist
LinkedList_Ptr pLinkedList = malloc(sizeof(LinkedList));
  
// check if the memory was successfully allocated
if(pLinkedList != NULL)
{
// setting the front to NULL as the linkedlist is empty
pLinkedList->front = NULL;
pLinkedList->size = 0;
}

return pLinkedList;
}

// Takes the handle to the linkedlist and the item to add to the
// end of the linkedlist
Status linkedlist_push_back(LinkedList_Ptr hLinkedList, Item item)
{
// making sure that the linkedlist was allocated
if(hLinkedList == NULL)
{
return FAILURE;
}

// list is empty, allocate memory for the front node (first node)
if(hLinkedList->front == NULL)
{
hLinkedList->front = malloc(sizeof(Node));
// check if memory was allocated
if(hLinkedList->front == NULL)
   {
   return FAILURE;
   }
// hold the input item
hLinkedList->front->data = item;
// set the next to NULL
hLinkedList->front->next = NULL;
}
else
{
Node_Ptr current = hLinkedList->front;
while(current->next != NULL)
   {
   current = current->next;
   }
Node_Ptr temp = malloc(sizeof(Node));
if(temp == NULL)
   {
   return FAILURE;
   }
temp->data = item;
temp->next = NULL;
current->next = temp;
}
hLinkedList->size++;
return SUCCESS;
}

// removes the last node in the linkedlist
Status linkedlist_pop_back(LinkedList_Ptr hLinkedList)
{
if(hLinkedList == NULL || hLinkedList->front == NULL)
{
return FAILURE;
}
  
// removing the first node (the linkedlist only have one node)
if(hLinkedList->front->next == NULL)
{
free(hLinkedList->front);
hLinkedList->front = NULL;
}
else
{
//
Node_Ptr current = hLinkedList->front->next;
Node_Ptr previous = hLinkedList->front;
while(current->next != NULL)
   {
   previous = current;
   current = current->next;
   }

free(current);
previous->next = NULL;
}
hLinkedList->size--;

return SUCCESS;
}

Status linkedlist_push_front(LinkedList_Ptr hLinkedList, Item item)
{
if(hLinkedList == NULL)
{
return FAILURE;
}
if(hLinkedList->front == NULL)
{
hLinkedList->front = malloc(sizeof(Node));
  
if(hLinkedList->front == NULL)
   {
   return FAILURE;
   }

hLinkedList->front->data = item;
hLinkedList->front->next = NULL;
hLinkedList->size++;
}
else
{
Node_Ptr pNode = malloc(sizeof(Node));
if(pNode == NULL)
   {
   return FAILURE;
   }
pNode->data = item;
pNode->next = hLinkedList->front;
hLinkedList->front = pNode;
hLinkedList->size++;
}

return SUCCESS;
}

Status linkedlist_pop_front(LinkedList_Ptr hLinkedList)
{
if(hLinkedList == NULL || hLinkedList->front == NULL)
{
return FAILURE;
}
  
Node_Ptr pNode = hLinkedList->front;
hLinkedList->front = hLinkedList->front->next;
free(pNode);
hLinkedList->size--;

return SUCCESS;

}

void linkedlist_display(LinkedList_Ptr hLinkedList)
{
if(hLinkedList != NULL)
{
Node_Ptr current = hLinkedList->front;
while(current != NULL)
   {
   printf("%d ", current->data);
   current = current->next;
   }
}
}


int linkedlist_get_size(LinkedList_Ptr hLinkedList)
{
return hLinkedList->size;
}


Item* linkedlist_front(LinkedList_Ptr hLinkedList)
{
// return null if we have an empty linkedlist
if(hLinkedList == NULL || hLinkedList->front == NULL)
{
return NULL;
}

// otherwise, return the address of the data held
// by the front
return &hLinkedList->front->data;

}


Item* linkedlist_back(LinkedList_Ptr hLinkedList)
{
// return NULL if linkedlist was empty
if(hLinkedList == NULL || hLinkedList->front == NULL)
{
return NULL;
}

// go till the end of the linkedlist
Node_Ptr current = hLinkedList->front;
while(current->next != NULL)
{
current = current->next;
}
return &current->data;
}

Item* linkedlist_at(LinkedList_Ptr hLinkedList, int index)
{
if(hLinkedList == NULL || index < 0 || index >= hLinkedList->size)
{
return NULL;
}
  
Node_Ptr current = hLinkedList->front;
for(int i= 0 ; i < index; i++)
{
current = current->next;
  
}
return &current->data;

}


void linkedlist_destroy(LinkedList_Ptr* phLinkedList)
{
if(*phLinkedList != NULL)
{
if((*phLinkedList)->front != NULL)
   {
   Node_Ptr current = (*phLinkedList)->front;
   Node_Ptr next = (*phLinkedList)->front->next;
   while(next != NULL)
   {
   free(current);
   current = next;
   next = next->next;
   }
   free(current);
   }
free(*phLinkedList);
*phLinkedList = NULL;
}
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"

int main(int argc, char* argv[])
{
LinkedList_Ptr hLinkedList = linkedlist_init_default();
linkedlist_push_back(hLinkedList, 10);
linkedlist_push_back(hLinkedList, 20);
linkedlist_push_back(hLinkedList, 30);
linkedlist_push_back(hLinkedList, 40);
linkedlist_push_back(hLinkedList, 50);

linkedlist_push_front(hLinkedList,5);

printf("Current linkedlist: ");
linkedlist_display(hLinkedList);

printf("linkedlist front: %d ", *linkedlist_front(hLinkedList));
printf("linkedlist back: %d ", *linkedlist_back(hLinkedList));
printf("linkedlist at index 3: %d ", *linkedlist_at(hLinkedList, 3));
printf("linkedlist size: %d ", linkedlist_get_size(hLinkedList));

linkedlist_pop_back(hLinkedList);
linkedlist_pop_front(hLinkedList);
printf("Current linkedlist: ");
linkedlist_display(hLinkedList);
printf("linkedlist size: %d ", linkedlist_get_size(hLinkedList));

linkedlist_destroy(&hLinkedList);
return 0;
}

Explanation / Answer

#ifndef NODE_H_
#define NODE_H_

#include "status.h"
#include "item.h"

struct node;
typedef struct node Node, * Node_Ptr;

struct node
{
Item data;
Node_Ptr next;
};

#endif // NODE_H_

stack.c

#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
#include "status.h"
#include "linkedlist.h"

struct stack
{
LinkedList_Ptr hLinkedList;
};

typedef struct stack Stack, * Stack_Ptr;

Stack_Ptr stack_init_default()
{
Stack_Ptr pStack = malloc(sizeof(Stack));
  
if(pStack == NULL)
{
return NULL;
}
pStack->hLinkedList = linkedlist_init_default();
if(pStack->hLinkedList == NULL)
{
free(pStack);
return NULL;
}
return pStack;
}


Status stack_push(Stack_Ptr hStack, Item item)
{
return FAILURE;
}


Status stack_pop(Stack_Ptr hStack)
{
return FAILURE;
}

Item * stack_top(Stack_Ptr hStack)
{
return NULL;
}

Boolean stack_empty(Stack_Ptr hStack)
{
int size = linkedlist_get_size(hStack->hLinkedList);
if(size == 0)
{
return TRUE;
}
return FALSE;
}

int stack_get_size(Stack_Ptr hStack)
{
return linkedlist_get_size(hStack->hLinkedList);
}

//Precondition: a pointer to a pointer to the stack
//Postcondition: freeing the memory held by the stack   
//Precondition: a pointer to the stack
//Postconidition: Returning True or False based on the stack
void stack_destroy(Stack_Ptr * phStack)
{
if(*phStack != NULL)
{
linkedlist_destroy(&(*phStack)->hLinkedList);
}
free(*phStack);
*phStack = NULL;
}

linkedlist.c file

#include <stdlib.h>
#include <stdio.h>
#include "linkedlist.h"
#include "node.h"

struct linkedlist
{
Node_Ptr front;
int size;
};

typedef struct linkedlist LinkedList, * LinkedList_Ptr;

// initializing and allocating the memory for our linkedlist
LinkedList_Ptr linkedlist_init_default()
{
// allocating the memory for my linkedlist
LinkedList_Ptr pLinkedList = malloc(sizeof(LinkedList));
  
// check if the memory was successfully allocated
if(pLinkedList != NULL)
{
// setting the front to NULL as the linkedlist is empty
pLinkedList->front = NULL;
pLinkedList->size = 0;
}

return pLinkedList;
}

// Takes the handle to the linkedlist and the item to add to the
// end of the linkedlist
Status linkedlist_push_back(LinkedList_Ptr hLinkedList, Item item)
{
// making sure that the linkedlist was allocated
if(hLinkedList == NULL)
{
return FAILURE;
}

// list is empty, allocate memory for the front node (first node)
if(hLinkedList->front == NULL)
{
hLinkedList->front = malloc(sizeof(Node));
// check if memory was allocated
if(hLinkedList->front == NULL)
   {
   return FAILURE;
   }
// hold the input item
hLinkedList->front->data = item;
// set the next to NULL
hLinkedList->front->next = NULL;
}
else
{
Node_Ptr current = hLinkedList->front;
while(current->next != NULL)
   {
   current = current->next;
   }
Node_Ptr temp = malloc(sizeof(Node));
if(temp == NULL)
   {
   return FAILURE;
   }
temp->data = item;
temp->next = NULL;
current->next = temp;
}
hLinkedList->size++;
return SUCCESS;
}

// removes the last node in the linkedlist
Status linkedlist_pop_back(LinkedList_Ptr hLinkedList)
{
if(hLinkedList == NULL || hLinkedList->front == NULL)
{
return FAILURE;
}
  
// removing the first node (the linkedlist only have one node)
if(hLinkedList->front->next == NULL)
{
free(hLinkedList->front);
hLinkedList->front = NULL;
}
else
{
//
Node_Ptr current = hLinkedList->front->next;
Node_Ptr previous = hLinkedList->front;
while(current->next != NULL)
   {
   previous = current;
   current = current->next;
   }

free(current);
previous->next = NULL;
}
hLinkedList->size--;

return SUCCESS;
}

Status linkedlist_push_front(LinkedList_Ptr hLinkedList, Item item)
{
if(hLinkedList == NULL)
{
return FAILURE;
}
if(hLinkedList->front == NULL)
{
hLinkedList->front = malloc(sizeof(Node));
  
if(hLinkedList->front == NULL)
   {
   return FAILURE;
   }

hLinkedList->front->data = item;
hLinkedList->front->next = NULL;
hLinkedList->size++;
}
else
{
Node_Ptr pNode = malloc(sizeof(Node));
if(pNode == NULL)
   {
   return FAILURE;
   }
pNode->data = item;
pNode->next = hLinkedList->front;
hLinkedList->front = pNode;
hLinkedList->size++;
}

return SUCCESS;
}

Status linkedlist_pop_front(LinkedList_Ptr hLinkedList)
{
if(hLinkedList == NULL || hLinkedList->front == NULL)
{
return FAILURE;
}
  
Node_Ptr pNode = hLinkedList->front;
hLinkedList->front = hLinkedList->front->next;
free(pNode);
hLinkedList->size--;

return SUCCESS;

}

void linkedlist_display(LinkedList_Ptr hLinkedList)
{
if(hLinkedList != NULL)
{
Node_Ptr current = hLinkedList->front;
while(current != NULL)
   {
   printf("%d ", current->data);
   current = current->next;
   }
}
}


int linkedlist_get_size(LinkedList_Ptr hLinkedList)
{
return hLinkedList->size;
}


Item* linkedlist_front(LinkedList_Ptr hLinkedList)
{
// return null if we have an empty linkedlist
if(hLinkedList == NULL || hLinkedList->front == NULL)
{
return NULL;
}

// otherwise, return the address of the data held
// by the front
return &hLinkedList->front->data;

}


Item* linkedlist_back(LinkedList_Ptr hLinkedList)
{
// return NULL if linkedlist was empty
if(hLinkedList == NULL || hLinkedList->front == NULL)
{
return NULL;
}

// go till the end of the linkedlist
Node_Ptr current = hLinkedList->front;
while(current->next != NULL)
{
current = current->next;
}
return &current->data;
}

Item* linkedlist_at(LinkedList_Ptr hLinkedList, int index)
{
if(hLinkedList == NULL || index < 0 || index >= hLinkedList->size)
{
return NULL;
}
  
Node_Ptr current = hLinkedList->front;
for(int i= 0 ; i < index; i++)
{
current = current->next;
  
}
return &current->data;

}


void linkedlist_destroy(LinkedList_Ptr* phLinkedList)
{
if(*phLinkedList != NULL)
{
if((*phLinkedList)->front != NULL)
   {
   Node_Ptr current = (*phLinkedList)->front;
   Node_Ptr next = (*phLinkedList)->front->next;
   while(next != NULL)
   {
   free(current);
   current = next;
   next = next->next;
   }
   free(current);
   }
free(*phLinkedList);
*phLinkedList = NULL;
}
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"

int main(int argc, char* argv[])
{
LinkedList_Ptr hLinkedList = linkedlist_init_default();
linkedlist_push_back(hLinkedList, 10);
linkedlist_push_back(hLinkedList, 20);
linkedlist_push_back(hLinkedList, 30);
linkedlist_push_back(hLinkedList, 40);
linkedlist_push_back(hLinkedList, 50);

linkedlist_push_front(hLinkedList,5);

printf("Current linkedlist: ");
linkedlist_display(hLinkedList);

printf("linkedlist front: %d ", *linkedlist_front(hLinkedList));
printf("linkedlist back: %d ", *linkedlist_back(hLinkedList));
printf("linkedlist at index 3: %d ", *linkedlist_at(hLinkedList, 3));
printf("linkedlist size: %d ", linkedlist_get_size(hLinkedList));

linkedlist_pop_back(hLinkedList);
linkedlist_pop_front(hLinkedList);
printf("Current linkedlist: ");
linkedlist_display(hLinkedList);
printf("linkedlist size: %d ", linkedlist_get_size(hLinkedList));

linkedlist_destroy(&hLinkedList);
return 0;
}