Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write in C language. Must answer using template code given below: main.c #include #include #include stack.h int main(int argc, char* argv[]) { STACK hStack; int

image text in transcribed

Write in C language. Must answer using template code given below:

main.c

#include

#include

#include "stack.h"

int main(int argc, char* argv[])

{

STACK hStack;

int i;

hStack = stack_init_default();

for (i = 0; i

{

stack_push(hStack, i);

}

while (!stack_empty(hStack))

{

printf("top of the stack %d ", stack_top(hStack, NULL));

stack_pop(hStack);

}

stack_destroy(&hStack);

return 0;

}

linked_list.c

#include

#include

#include "stack.h"

struct node;

typedef struct node Node;

struct node

{

int data;

Node* next;

};

//known type

struct stack

{

Node* head;

};

typedef struct stack Stack;

//init

STACK stack_init_default(void)

{

Stack* pStack = NULL;

pStack = (Stack*)malloc(sizeof(Stack));

if (pStack != NULL)

{

pStack->head = NULL;

}

return (STACK)pStack;

}

//push

Status stack_push(STACK hStack, int item)

{

Stack* pStack = (Stack*)hStack;

Node* temp;

temp = (Node*)malloc(sizeof(Node));

if (temp == NULL)

{

return FAILURE;

}

temp->data = item;

temp->next = pStack->head;

pStack->head = temp;

return SUCCESS;

}

//pop

Status stack_pop(STACK hStack)

{

Stack* pStack = (Stack*)hStack;

Node* temp;

if (pStack->head == NULL)//stack is empty

{

return FAILURE;

}

temp = pStack->head;

pStack->head = pStack->head->next;

free(temp);

return SUCCESS;

}

//top

//Status stack_top(STACK hStack, int* pValue_of_top);

int stack_top(STACK hStack, Status* pStatus)

{

Stack* pStack = (Stack*)hStack;

if (pStack->head == NULL)//empty

{

if (pStatus != NULL)

{

*pStatus = FAILURE;

}

return -1337;

}

if (pStatus != NULL)

{

*pStatus = SUCCESS;

}

return pStack->head->data;

}

//empty

Boolean stack_empty(STACK hStack)

{

Stack* pStack = (Stack*)hStack;

return (Boolean)(pStack->head == NULL);

}

//destroy

void stack_destroy(STACK* phStack)

{

Stack* pStack = (Stack*)*phStack;

Node* temp;

temp = pStack->head;

while (temp != NULL)

{

pStack->head = pStack->head->next;

free(temp);

temp = pStack->head;

}

free(pStack);

*phStack = NULL;

return;

}

stack.h

#include "status.h" typedef void* STACK;

//init STACK stack_init_default(void);

//push Status stack_push(STACK hStack, int item);

//pop Status stack_pop(STACK hStack);

//top //Status stack_top(STACK hStack, int* pValue_of_top); int stack_top(STACK hStack, Status* pStatus);

//empty Boolean stack_empty(STACK hStack);

//destroy void stack_destroy(STACK* phStack);

status.h

enum status { FAILURE, SUCCESS }; typedef enum status Status;

enum boolean { FALSE, TRUE }; typedef enum boolean Boolean;

You are given a string consisting of parenthesis style characters ) [] and {). A string of this type is said to be correct if: (a) It is an empty string (b) If string A is correct and string B is correct then string AB is correct. (c) If A is correct then (A), [A] and (A are all correct. You are to write a program that takes as input from the keyboard a series of strings of this type that can be of any length. The first line of input will contain a single integer n telling you how many strings you will be testing. Following that integer will be n lines (each ending in a new line character) that represent the strings you are supposed to test with one string per line Your program should output Yes if a given string is correct and No otherwise For example if the user types the following as input to ) Then your program should give the following output: Yes No Yes You will note that if you do your program correctly there is no prompt for data and the yes and no answers will be interspersed with your input data when you enter it manually. The following would be a more a the input into your program. ccurate representatio n of what you see on the screen when you cut and paste Yes No to O) Yes You may find it useful to use a text file for testing your code so that you do not have to keep retyping the tests. You can do this from the command line by typing the name of the executable file followed by a less than sign and the name of y named day7 then I could type: our file. For example, if my executable is day7 input.txt This would redirect standard input so that the input instead comes from the file named input.txt You should note that the input.txt file needs to be in the same directory as your executable file for the above command to work. Alternatively you can give the complete path information for the input file You may not assume anything about the size of the test strings coming in. In fact, some of the test strings may be hundreds of thousands of characters long You are given a string consisting of parenthesis style characters ) [] and {). A string of this type is said to be correct if: (a) It is an empty string (b) If string A is correct and string B is correct then string AB is correct. (c) If A is correct then (A), [A] and (A are all correct. You are to write a program that takes as input from the keyboard a series of strings of this type that can be of any length. The first line of input will contain a single integer n telling you how many strings you will be testing. Following that integer will be n lines (each ending in a new line character) that represent the strings you are supposed to test with one string per line Your program should output Yes if a given string is correct and No otherwise For example if the user types the following as input to ) Then your program should give the following output: Yes No Yes You will note that if you do your program correctly there is no prompt for data and the yes and no answers will be interspersed with your input data when you enter it manually. The following would be a more a the input into your program. ccurate representatio n of what you see on the screen when you cut and paste Yes No to O) Yes You may find it useful to use a text file for testing your code so that you do not have to keep retyping the tests. You can do this from the command line by typing the name of the executable file followed by a less than sign and the name of y named day7 then I could type: our file. For example, if my executable is day7 input.txt This would redirect standard input so that the input instead comes from the file named input.txt You should note that the input.txt file needs to be in the same directory as your executable file for the above command to work. Alternatively you can give the complete path information for the input file You may not assume anything about the size of the test strings coming in. In fact, some of the test strings may be hundreds of thousands of characters long

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

7. What decisions would you make as the city manager?

Answered: 1 week ago

Question

8. How would you explain your decisions to the city council?

Answered: 1 week ago