In C, Implement each of the functions to create a working stack
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!
In C, Implement each of the functions to create a working stack 6 II-Implement each of...
// =================== 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 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 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 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 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 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 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 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 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 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...