Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a C code that gets the equations from equations.txt and stores them in a linked list stack. the code is given below the only

Create a C code that gets the equations fromequations.txtand stores them in a linked list stack. the code is given below the only thing missing is the part that gets the equations from the file.

// C++ program to Implement a stack

//using singly linked list

#include

#include

#include

// Declare linked list node

struct Node

{

char data;

struct Node* next;

};

int nodesCount;

// Utility function to add an element `x` to the stack

void push(struct Node **top) // insert at the beginning

{

FILE * myFile;

myFile = fopen(\"equations.txt\", \"r\");

if (myFile == 0) {

printf(\"file not opened \");

} else {

printf(\"file opened \");

}

// allocate a new node in a heap

struct Node* node = NULL;

node = (struct Node*)malloc(sizeof(struct Node));

// check if stack (heap) is full. Then inserting an element would

// lead to stack overflow

if (!node)

{

printf(\"Heap Overflow \");

exit(-1);

}

int i=0;

char equation;

while (!feof(myFile))

{

fgets(equation,nodesCount,myFile);

// set data in the allocated node

node->data = equation;

// set the .next pointer of the new node to point to the current

// top node of the list

node->next = *top;

// update top pointer

*top = node;

// increase stack's size by 1

nodesCount += 1;

}

}

// Utility function to check if the stack is empty or not

int isEmpty(struct Node* top) {

return top == NULL;

}

// Utility function to return the top element of the stack

int peek(struct Node *top)

{

// check for an empty stack

if (!isEmpty(top)) {

return top->data;

}

else {

printf(\"The stack is empty \");

exit(EXIT_FAILURE);

}

}

// Utility function to pop a top element from the stack

int pop(struct Node** top) // remove at the beginning

{

struct Node *node;

// check for stack underflow

if (*top == NULL)

{

printf(\"Stack Underflow \");

exit(EXIT_FAILURE);

}

// take note of the top node's data

int x = peek(*top);

printf(\"Removing %d \", x);

node = *top;

// update the top pointer to point to the next node

*top = (*top)->next;

// decrease stack's size by 1

nodesCount -= 1;

// free allocated memory

free(node);

return x;

}

// Utility function to return the nodesCount of the stack

int size() {

return nodesCount;

}

int main(void)

{

struct Node* top = NULL;

push(&top);

if (isEmpty(top)) {

printf(\"The stack is empty \");

}

else {

printf(\"The stack is not empty \");

}

return 0;

}

the parts inBoldon the code are an attempt that did not work

Equations.txt content:

a = ((c+d)*a)/(b/(d-a)) b = 4*[x + 3*(2*x + 1)] c = 5*{3 2*[1 4*(3 22)]} d = 5 + 2*{[3 + (2*x 1) + x] 2} e = 5 + 9 * 3 (5 + 9)*3 f (5 + 9)*3 g = 5 + 2*{[3 + (2*x 1) + x 2} h = 5 + 9 * 3) i = 5 + (9 * 3

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Identify the four ways property passes to heirs or legatees.

Answered: 1 week ago