Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a list of integers from the keyboard, creates a linke

ID: 1844393 • Letter: W

Question

Write a program that reads a list of integers from the keyboard, creates a linked list from them, and prints the result. Insert each integer into the tail. The list of the data is the following: 12, 23, -56, 69, 71, 15, -36, -99 Write a function that accepts the linked list of the (a), and returns the data in the node with the minimum key value. Write a function that traverses the linked list of the (a), and deletes any nodes whose data fields are negative. Write a program that adds and subtracts polynomials. Each polynomial is to be represented as a linked list. A doubly linked list is a list in which each node contains a pointer to the previous node in the list as well as a pointer to the next node in the list. To make accessing Nodes at the head of the list and the tail of the list easier, you will keep two pointers to the head and tail of the list Define the appropriate structs for a doubly linked list and then write a small program that implements a small doubly linked list and prints out the elements of the list. See picture below for a representation of the doubly linked list: Create Node struct for doubly linked entry. The values in the nodes are of type int. Create a DLList struct that contains two pointers: one to the head of the list and one to the tail of the list. This struct will represent the doubly linked list Create function to insert values in the doubly linked list in order. Therefore, the head is always pointing to the smallest element while tail is pointing to the largest element in the list, and every entry is followed by an entry larger or equal to it. Create a function to remove a given value if it exists in the doubly linked list For example if I try to delete the value v, then the function will search through the doubly linked list and remove that value if it exists. Note that the function will delete EVERY instance of the value in the list so if the value v exists more than once in the list, then all of them will be deleted. Create a function that prints the doubly linked list forward: starting from head ending at tail. Create a function that prints the doubly linked list backward: starting from tail ending at head. Create main to test your functions. Your test should test insertion of 20 random elements into the list and ensuring that the list is in order. Your test should then insert a specific value into the list and attempt to delete that value to test the delete function After each change to the list, you should print the list forward and backward. Test corner cases: what happens if you try to delete an element in an empty list, what happens if you insert a duplicate value into the list? How does delete handle a case where there is duplicate values?

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;

const int TABLE_SIZE = 20;

//structure to store variable name and value in hash table

struct var {
public:
char variable[20];
int value;
int nest;

struct var* next;

//structure initialization of next node which supports chaining in hash table

var() {

strcpy(variable, "");
next = NULL;
}

//checks whether the ndoe is null or not

bool isNull() {
if (strcmp(variable, "") == 0)
return true;
else
return false;
}

//set data in node : the variable name and value and the nested code value

void set(string vr, int v, int n) {
strcpy(variable, vr.c_str());
value = v;
nest = n;

}
} hashtbl[TABLE_SIZE];

//gets key for hash table

int getKey(string v) {
int k = 0;
for (int i = 0; i < v.length(); i++) {
k = k + (v[i] * (i + 1));
}

return k % TABLE_SIZE;
}

//set data in hash table

void setData(string v, int val, int n) {
int key = getKey(v);

if (hashtbl[key].isNull()) {
hashtbl[key].set(v, val, n);
} else {
struct var* p = &hashtbl[key];
while (p->next != NULL) {
p = p->next;
}

p->next = new struct var;
p = p->next;
p->set(v, val, n);
}

}

//gets data from hash table

int getData(string v) {
int key = getKey(v);

if (hashtbl[key].isNull()) {
return -1;
} else {
struct var* p = &hashtbl[key];
while (p->next != NULL) {

if (strcmp(p->variable, v.c_str()) == 0) {
return p->value;
}

p = p->next;
}
if (strcmp(p->variable, v.c_str()) == 0) {
return p->value;
}

}
return -1;
}

//update data in hash table

void changeData(string v, int val) {

int key = getKey(v);

if (hashtbl[key].isNull()) {
return;
} else {
struct var* p = &hashtbl[key];
while (p->next != NULL) {

if (strcmp(p->variable, v.c_str()) == 0) {
p->value = val;
}

p = p->next;
}
if (strcmp(p->variable, v.c_str()) == 0) {
p->value = val;
}

}
}

//when finish statement is seen, all local variables are removed

void finish(int nst) {
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashtbl[i].isNull())
continue;
else {
struct var* p = &hashtbl[i];
while (p->next != NULL) {

if (p->nest == nst) {
strcpy(p->variable, "");
}
p = p->next;
}
if (p->nest == nst) {
strcpy(p->variable, "");
}
}
}
}

int main() {

//opens file temp.txt there the BORG code is written
ifstream myfile;
myfile.open("temp.txt");
int nest = 0;
char *token1, *token2;

//reads the fiel till end
while (!myfile.eof()) {

//reads each line in "line"
string line;
getline(myfile, line);

char *cline = new char[line.length() + 1];
strcpy(cline, line.c_str());

//each line is broken into tokens by diviidng each line where space is found : strtok does that
char *token = std::strtok(cline, " ");
while (token != NULL) {

//if first token is com, then rest of the line is ignored as it is a comment
if (strcmp(token, "COM") == 0) {
break;
} else if (strcmp(token, "START") == 0) {
//when start is found, then nest takes care of the nested code level to take care of local variables in the scope
nest++;
break;
} else if (strcmp(token, "FINISH") == 0) {
//when finish is found, then all the local variables are deleted and nest reducess by one, because the scope is reduced
finish(nest);
nest--;
break;
} else if (strcmp(token, "VAR") == 0) {

//here strok further divides the line into token to initialize variable
token1 = std::strtok(NULL, " ");
token2 = std::strtok(NULL, " ");
token2 = std::strtok(NULL, " ");

int v = std::stoi(token2, nullptr, 0);
//then data is set into hashtable
setData(token1, v, nest);

} else if (strcmp(token, "PRINT") == 0) {
//here print token is found, then variable is printed
token1 = std::strtok(NULL, " ");

if(token1[0] == '"')
{
int start = line.find('"');
cout<<line.substr(start+1, line.length()-start - 2);
  
  
break;
}

  
int v = getData(token1);

token2 = std::strtok(NULL, " ");
if (token2 != NULL) {
char *token3 = std::strtok(NULL, " ");
int opd = std::stoi(token3, nullptr, 0);
int result = 0;

//check the operator and erform the same operation
if (strcmp(token2, "*") == 0) {
result = v*opd;
} else if (strcmp(token2, "+") == 0) {
result = v + opd;
} else if (strcmp(token2, "-") == 0) {
result = v - opd;
} else if (strcmp(token2, "/") == 0) {
result = v / opd;
} else if (strcmp(token2, "%") == 0) {
result = v % opd;
} else if (strcmp(token2, "^") == 0) {
result = v^opd;
}

cout << token1 << token2 << token3 << " IS " << result;
} else {
if (v == -1) {
cout << token1 << " IS UNDEFINED";
} else {
cout << token1 << " IS " << v;
}
}

cout << endl;

} else {
//else it is a varibael reinitialization , which is done here ++, -- and = is taken care of)
token2 = std::strtok(NULL, " ");

if (token2 == NULL)
break;

if (strcmp(token2, "=") == 0) {
token2 = std::strtok(NULL, " ");

int v = std::stoi(token2, nullptr, 0);

changeData(token, v);
} else if (strcmp(token2, "++") == 0) {
int v = getData(token);
v++;
changeData(token, v);

} else if (strcmp(token2, "--") == 0) {
int v = getData(token);
v--;
changeData(token, v);

}
}

break;

}

}

myfile.close();

return 0;
}

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

INCLUDE THIS TEXT FILE (temp.txt)------------------------------------------------------------------

COM HERE IS OUR FIRST BORG PROGRAM
COM WHAT A ROBUST LANGUAGE IT IS
START
VAR BORAMIR = 25
VAR LEGOLAS = 101
PRINT BORAMIR
BORAMIR ++
PRINT LEGOLAS
PRINT GANDALF
PRINT BORAMIR * 2
COM
COM NESTED BLOCK
COM
START
VAR GANDALF = 49
PRINT GANDALF
PRINT BORAMIR
FINISH
PRINT GANDALF
START
LEGOLAS = 1000
PRINT LEGOLAS
FINISH
PRINT LEGOLAS
LEGOLAS --
PRINT LEGOLAS
PRINT "EINSTEIN ROCKS"
FINISH

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

OUTPUT:------------------------------------------------------------------

BORAMIR IS 25
LEGOLAS IS 101
GANDALF IS UNDEFINED
BORAMIR*2 IS 52
GANDALF IS 49
BORAMIR IS 26
GANDALF IS UNDEFINED
LEGOLAS IS 1000
LEGOLAS IS 1000
LEGOLAS IS 9999
EINSTEIN ROCKS

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote