Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program C explanantion 1 is just explaination that how my previous task was but after reading this, read the second explanation 2 and implement a

Program C

explanantion 1 is just explaination that how my previous task was but after reading this, read the second explanation 2 and implement a code for that ONLY:

Explanation 1:

A stack is a data structure that stores a sequence of data, and that provides (at least) two functions: push , which puts an element first (on top) of the stack, and pop , which removes the first element. (In other words, it is a LIFO: "last in first out".) You can use a stack in many different contexts, but one example is to make a calculator: a program that can calculate various compound expressions and functions. In this task, you must implement a stack containing floating-point numbers of type double , using a linked list. Create two files, a double_stack.h and a double_stack_list.c . The H file should contain the specification for the stack data type, but nothing about the implementation itself. Implementation should instead be in the C file. In order to encapsulate (hide) the implementation, you must declare the following in the H file:

typedef struct double_stack_struct double_stack;

So we say that the type double_stack should be the same as a type called struct double_stack_struct . However, this is an incomplete data type, because we have only declared it and not said what the struct actually contains! But as long as we only handle pointers to double_stack, it doesn't matter. However, we will not be able to dereference such a pointer, except in the code located in double_stack_list.c .

You get to write down the actual implementation of the struct double_stack_structi C file:

struct double_stack_struct{ /***/ };

To be able to encapsulate the actual implementation, you need to implement a function create_stack that creates an empty stack. This is so that the user does not have to know which data structure the stack uses internally. Because outside of the file double_stack_list.c you cannot use the incomplete type, but only pointers to it, we must in this way give the user a pointer that he can use. This function thus constitutes a kind of "stack factory" (or constructor), which outputs a pointer to a new empty stack, which the person who wants to use the stack can work with. Let the function have the following prototype:

double_stack*create_stack();

This function should therefore create an empty list, and return a pointer to it. For it to work, the list must be created dynamically (on the heap), i.e. with malloc . Otherwise, the list created in the function would automatically disappear after the function exits, and the pointer we get back would not be valid anymore. In other words, create a double_stack * stack = malloc( sizeof( double_stack ) ); and return that pointer.

Test that you can build a small test program that uses your code, and just includes double_stack.h and creates a new double_stack* using create_stack.

#include "double.stack.h" int main(int argc, char *argv[]){ double_stack*stack; stack = creat_stack(); return 0; }

This program should be able to compile, even if it produces no visible output when run.

Implement the push function to put new elements on the stack. int push(double_stack*stack, double d);

This function thus takes a pointer to a (possibly empty) stack and a number as input. It should return 1 if everything went well, and 0 otherwise.

Now your test program might look like this. #include "double_stack.h" int main(int argc, char *argv[]){ double_stack*stack; stack = creat_stack(); //empty stack push(stack,1); //now the stack contains 1 push(stack,2); //now the stack contains 2.1 return 0; }

also implement the function pop int pop(double_stack*stack, double*d);

which removes the top element from the stack and puts it where d points to. The function should also return 1 if everything went well, and 0 if it failed to pop (if the stack was empty).

Now in your double_stack.h you have a complete interface for a stack containing floating-point numbers, with all the necessary functions. Finally, create a test program that puts some numbers (taking them as arguments via argv as before) into a stack, and then pops them one by one and prints them, each on a new line.

./task 1 2 3 4 5 5.0000 4.0000 3.0000 2.0000 1.0000

Since there is no function that can check if the stack is empty or not, you have to make sure yourself that you pop as many elements as you have pushed. The program should work regardless of how many numbers come in via argv, not only for five as in the example above. It is not an acceptable solution to, for example, build a list and print it. You should pop one element at a time, and print the result of pop.

Now: I will show the C and H file fo this task after that implement a code BY reading EXPLANASION 2 using my H and C file

---------------------------'

double_stack.h

#ifndef DOUBLE_STACK_H #define DOUBLE_STACK_H 
typedef struct double_stack_struct double_stack; double_stack* create_stack(); int empty(double_stack* stack); int push(double_stack* stack,double d); int pop(double_stack* stack,double*d); double get_value_at_head(double_stack* stack); #endif

----------------------------------------

task_4.c

#include  #include  #include "double_stack.h" int main(int argc, char*argv[]){ double_stack*stack; stack = create_stack(); for(int i = 1; i < argc; i++){ double data = atof(argv[i]); push(stack,data); } double d; while(pop(stack,&d)){ printf("%.6f ",d); } return 0; }

-------------------------------------------

Explanation2:

Redo the implementation of your stack. Thanks to it being an encapsulated abstract data type, where the implementation is separate, you can change without breaking existing code that uses your stack implementation! What you need to do is create a new file double_stack_array.c which, like double_stack_list.c, includes double_stack .h , but implements those three functions like push, pop, using fields instead of a linked list. The rest of the code can be exactly the same! In this case, your struct double_stack_struct will need to contain a double field with "just enough" elements instead of a pointer to a list. In addition, the struct needs to contain a counter that shows which index in the field is the top of the stack at the moment. If you want, instead of using a static field in your struct (eg exdouble data[10] ), you can use a field that is dynamically allocated (using malloc ) instead. If you do it that way, you can also use the realloc function to make the field bigger if needed.

You should not make a new file task_5.c here, but reuse your task_4.c without changes! In CMakeLists.txt , make sure that the program task_5 is built by task_4.c and double_stack_array.c.

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

Step: 3

blur-text-image

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

Progress Monitoring Data Tracking Organizer

Authors: Teacher'S Aid Publications

1st Edition

B0B7QCNRJ1

More Books

Students also viewed these Databases questions

Question

2 The goals and tools of monetary policy.

Answered: 1 week ago

Question

6. Are my sources reliable?

Answered: 1 week ago

Question

5. Are my sources compelling?

Answered: 1 week ago