Question: home / study / engineering / computer science / computer science questions and answers / This Is The Stack.c File Given /* * File: Stack.c

home / study / engineering / computer science /

home / study / engineering / computer science / computer science questions and answers / This Is The Stack.c File Given /* * File: Stack.c * ------------- * This File Implements The ...

Question: This is the Stack.c file given /* * File: stack.c * ------------- * This file implements the stac...

home / study / engineering / computer science /

This is the Stack.c file given

/* * File: stack.c * ------------- * This file implements the stack.h interface. Note that the * implementation is independent of the type of value stored * in the stack. That type is defined by the interface as * the type name stackElementT. */

#include #include "genlib.h" #include "stack.h"

/* * Constant: InitialStackSize * -------------------------- * This constant defines the initial size of the stack array. * Any positive value will work correctly, although changing * this parameter can affect performance. Making this value * larger postpones the first reallocation but causes stacks * to consume more memory. */

#define InitialStackSize 100

/* * Type: stackCDT * -------------- * The type stackCDT is the concrete representation of the type * stackADT defined by the interface. In this implementation, * the elements are stored in a dynamic array that doubles in * size if the old stack becomes full. */

struct stackCDT { stackElementT *elements; int count; int size; };

/* Private function prototypes */

static void ExpandStack(stackADT stack);

/* Exported entries */

stackADT NewStack(void) { stackADT stack;

stack = New(stackADT); stack->elements = NewArray(InitialStackSize, stackElementT); stack->count = 0; stack->size = InitialStackSize; return (stack); }

void FreeStack(stackADT stack) { FreeBlock(stack->elements); FreeBlock(stack); }

void Push(stackADT stack, stackElementT element) { if (stack->count == stack->size) ExpandStack(stack); stack->elements[stack->count++] = element; }

stackElementT Pop(stackADT stack) { if (StackIsEmpty(stack)) Error("Pop of an empty stack"); return (stack->elements[--stack->count]); }

bool StackIsEmpty(stackADT stack) { return (stack->count == 0); }

bool StackIsFull(stackADT stack) { return (FALSE); }

int StackDepth(stackADT stack) { return (stack->count); }

stackElementT GetStackElement(stackADT stack, int index) { if (index = stack->count) { Error("Non-existent stack element"); } return (stack->elements[stack->count - index - 1]); }

/* Private functions */

/* Function: ExpandStack * Usage: ExpandStack(stack); * -------------------------- * This function expands a full stack by doubling the size of its * dynamic array, copying the old elements to the new array, and * then freeing the old array storage. */

static void ExpandStack(stackADT stack) { stackElementT *array; int i, newSize;

newSize = stack->size * 2; array = NewArray(newSize, stackElementT); for (i = 0; i size; i++) { array[i] = stack->elements[i]; } FreeBlock(stack->elements); stack->elements = array; stack->size = newSize; }

This is the stck.h file given

/* * File: stack.h * ------------- * This interface defines an abstraction for stacks. In any * single application that uses this interface, the values in * the stack are constrained to a single type, although it * is easy to change that type by changing the definition of * stackElementT in this interface. */

#ifndef _stack_h #define _stack_h

#include "genlib.h"

/* * Type: stackElementT * ------------------- * The type stackElementT is used in this interface to indicate * the type of values that can be stored in the stack. Here the * stack is used to store values of type double, but that can * be changed by editing this definition line. */

typedef double stackElementT;

/* * Type: stackADT * -------------- * The type stackADT represents the abstract type used to store * the elements that have been pushed. Because stackADT is * defined only as a pointer to a concrete structure that is not * itself defined in the interface, clients have no access to * the underlying fields. */

typedef struct stackCDT *stackADT;

/* * Function: NewStack * Usage: stack = NewStack(); * -------------------------- * This function allocates and returns a new stack, which is * initially empty. */

stackADT NewStack(void);

/* * Function: FreeStack * Usage: FreeStack(stack); * ------------------------ * This function frees the storage associated with the stack. */

void FreeStack(stackADT stack);

/* * Function: Push * Usage: Push(stack, element); * ---------------------------- * This function pushes the specified element onto the stack. */

void Push(stackADT stack, stackElementT element);

/* * Function: Pop * Usage: element = Pop(stack); * ---------------------------- * This function pops the top element from the stack and returns * that value. The first value popped is always the last one * that was pushed. If the stack is empty when Pop is called, * the function calls Error with an appropriate message. */

stackElementT Pop(stackADT stack);

/* * Functions: StackIsEmpty, StackIsFull * Usage: if (StackIsEmpty(stack)) . . . * if (StackIsFull(stack)) . . . * ------------------------------------- * This functions test whether the stack is empty or full. */

bool StackIsEmpty(stackADT stack); bool StackIsFull(stackADT stack);

/* * Function: StackDepth * Usage: depth = StackDepth(stack); * --------------------------------- * This function returns the number of elements currently pushed * on the stack. */

int StackDepth(stackADT stack);

/* * Function: GetStackElement * Usage: element = GetStackElement(stack, index); * ----------------------------------------------- * This function returns the element at the specified index in * the stack, where the top of the stack is defined as index 0. * For example, calling GetStackElement(stack, 0) returns the top * element on the stack without removing it. If the caller tries * to select an out-of-range element, GetStackElement calls Error. * Note: This function is not a fundamental stack operation and * is instead provided principally to facilitate debugging. */

stackElementT GetStackElement(stackADT stack, int index);

#endif

Code in C please use stack.c for the program.

home / study / engineering / computer science /

CS 2123-001 Data Structures Instructor Dr. Turgay Korkmaz Homework 3 Due date: check BB Learn !!!NO LATE HOMEWORK WILL BE ACCEPTED!! (Command-line Arguments, Files, Abstract Data Type - Library) You are asked to write a program to check if the HTML tags in a given file are nested correctly or not. This problem is similar to the example of proper matching of { { [ ] } , but now you will deal with HTML tags and read them from a file. So you need to use stack ADT as we discussed in class [ First get booklib.zip and 08-Abstract-Data-Types.zip from the class web page. For this, click the link "programs from the textbook" under online materials. Then click 00-zip-files. Get README.txt and read it. Accordingly, get other files as described in it ] The implementation in 08-Abstract... has the stack library implementation and a driver program (rpncal.c) for RPN calculator. During the recitation you will modify it. But for this homework, you need to write a new application program (say htmlchecker.c), which will use the stack lib as rpncal.c does. So you will use the existing implementation of stack library, but in your new application since you will push/pop html tags (strings), you need to make sure stack.h has typedef void *stackElementT; I/ or typedef char *stackElementT Background HTML files consist of regular text and tags enclosed in angle brackets, the symbols Tags are used to identify the structure of a document. Most tags come in pairs: a beginning tag and a closing tag. For example, the tags and are the beginning and closing tags. There are several such tags including etc. Tags may have several attributes and such attributes will be in the beginning tag for example link HTML allows two-sided tags to be nested without overlapping, as in the example of proper matching of ]) . Some tags are single-sided (eg..,
,
) and they appear alonec. They will not affect the nesting but you still need to process them since they might be in the file. You can simply ignore the tags start with Also the file may contain HTML comments such as Your program should ignore everything between and -->

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!