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...

  • 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...

  • 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...

  • 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...

  • 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...

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int heig...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

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
ADVERTISEMENT