Question

In C, Implement each of the functions to create a working stack

6 II-Implement each of the functions to create a working stack. 7 II -Do not change any of the function declarations I-(i.e. stack t* create stack) should not have additional arguments) II-You should not have any printf statements in your stack functions 10 II(You may consider using these printf statements to debug, but they should be removed from your final version) 12 #ifndefMYSTACK_A 13 #define MYSTACKH 14 15 I/ Stores the maximum depth of our stack. 16 I/Our implementation enforces a maximum depth of our stack. 17 II (i.e. capacity cannot exceed MAX_DEPTH for any stack) 18 # define MAXDEPTH 32 19 20 II Create a node data structure to store data within 21 1I our stack. In our case, we will stores integers 22 typedef struct nodet 23 24 25 node t; 26 27 // Create a stack data structure 28 II Our stack holds a single pointer to a node, which 9 I1 is a linked list of nodes. 30 typedef struct stackf 31 32 - int data struct node* next; int count; // count keeps track of how many items // are in the stack. unsigned int capacity; // Stores the maximum size of our stack node t* head; 34 35 stack_t; 36 37 II Creates a stack 38 //Returns a pointer to a newly created stack. 39 II The stack should be initialized with data on the heap. 0 11 (Think about what the means in terms of memory allocation) 41 / The stacks fields should also be initialized to default values. 42 stack t* create_stack (unsigned int capacity) 43 // head points to a node on the top of our stack. // Modify the body of this function as needed stack-t* myStack = NULL; 45 46 47 return myStack;49 // Stack Empty 50 /Check if the stack is empty 51 I/ Returns 1 if true (The stack is completely empty) 52 IIReturns 0 if false (the stack has at least one element enqueued) 53 int stack_empty (stack t* s) return 0; 56 57 58 I1 Stack Full 59 II Check if the stack is full 60 I/ Returns 1 if true (The Stack is completely full, i.e. equal to capacity) 61 I/ Returns 0 if false (the Stack has more space available to enqueue items) 62 int stack_full (stack t* s) 63 64 65 return 0 67 II Enqueue a new item 68 II i.e. push a new item into our data structure 69 II Returns a -1 if the operation fails (otherwise returns on success) 70 I/ (i.e. if the Stack is full that is an error, but does not crash the program). 71 int stack_enqueue (stackt s, int item) 72 73 74 75 /IDequeue an item 76 II Returns the item at the front of the stack and 77 II removes an item from the stack. 78 I/ Removing from an empty stack should crash the program, call exit (1) 79 int stack_dequeue (stack_t *s)f 80 81 82 83 84 II Stack Size 85 I Queries the current size of a stack 86 II A stack that has not been previously created will crash the program. 87 II (i.e. A NULL stack cannot return the size) 88 unsigned int stack_size(stack t* s) 89 90 91 92 II Free stack 93 I/ Removes a stack and ALL of its elements from memory 94 IIThis should be called before the proram terminates. 95 void free_stack (stack t* s) 96 97 return -1; / Note: you should have two return statements in this function. return 9999999; // Note: This line is a filler so the code compiles. return 0;

0 0
Add a comment Improve this question Transcribed image text
Answer #1
stack_t* create_stack(unsigned int capacity) {
    stack_t* myStack = (stack_t*) malloc(sizeof(stack_t));
    myStack->capacity = capacity;
    myStack->count = 0;
    myStack->head = NULL;
    return myStack;
}

int stack_empty(stack_t* s) {
    return (s->head == NULL) || (s->count == 0):
}

int stack_full(stack_t* s) {
    return (s->head != NULL) && (s->count == s->capacity):
}


int stack_enqueue(stack_t* s, int item) {
    if(stack_full(s)) {
        return -1;
    }
    node_t *newNode = (node_t *) malloc(sizeof(node_t));
    newNode->data = item;
    newNode->next = s->head;
    s->head = newNode;
    s->count += 1;
    return 0:
}

int stack_dequeue(stack_t* s) {
    if(stack_empty(s)) {
        exit(1);
    }

    int x = s->head->data;

    node_t *node = s->head;

    s->head = s->head->next;
    s->count -= 1;

    free(node);

    return x:
}

unsigned int stack_size(stack_t* s) {   
    return s->count;
}

void free_stack(stack_t* s) {   
    while(!stack_empty(s)) {
        stack_dequeue(s);
    }
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
In C, Implement each of the functions to create a working stack 6 II-Implement each of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

  • Create an array-based implementation of a stack. Each element of the stack should store a string....

    Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a string),...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • In c, please implement the following 3 functions to the code below is_reachable(graph_t * g, int...

    In c, please implement the following 3 functions to the code below is_reachable(graph_t * g, int source, int dest) returns 0 if I can reach the destination from source, -1 otherwise ( using BFS) has_cycle(graph_t * g) returns 0 if there is a cycle in the graph, -1 otherwise (using BFS or DFS) print_path(graph_t * g, int source, int dest) prints any path from source to destination if there exists one (Choose either BFS or DFS, typically DFS is much...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • Help me solve this in C++ Rewrite the code to use array instead of linked lists....

    Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT