I need help implementing a linked list, making the sort function work, and makin
ID: 3732034 • Letter: I
Question
I need help implementing a linked list, making the sort function work, and making the linear search funtion work in the existing code. The program is supposed to read from the input file and convert the arabic number or roman numeral into its counterpart. I have also provide an sample for the input file. Any and all help would be appreciated.
In C++ and all in one file. Thank you.
Objectives:
• Implement a linked list
• Use structures to create nodes for a linked list
• Use pointers to manipulate a linked list
-Fix sort function
-Fix linear search function
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include<cstring>
#include <list>
using namespace std;
// function prototypes
int convertRomanToArabic(string);
string convertArabicToRoman(int);
void sort();
void Linear_search(int );
// Number struct
struct Number
{
char roman[16];
char arabic[5];
};
struct node
{
int data;
struct node *next;
}*head;
// main function
int main()
{
// open the file to read
fstream file;
file.open("numbers.txt", ios::in);
// exit from the program if the input file does not exist
if (file.fail())
{
cout << "The input file could not be opened!" << endl;
exit(1);
}
// declare the required variables
list<Number> lst;
Number num;
string str;
int si; // space index
int count = 0;
int choice;
// read data from the file
file.seekg(0); // go to the first line
file.read((char *)&num, sizeof(Number));
while (!file.eof())
{
num.roman[15] = '';
num.arabic[4] = '';
if (num.roman[0] == ' ')
{
str = num.arabic;
si = str.find(' ');
str = str.substr(0, si);
string rom = convertArabicToRoman(atoi(str.c_str()));
strcpy(num.roman, rom.c_str());
}
else
{
str = num.roman;
si = str.find(' ');
str = str.substr(0, si);
int arab = convertRomanToArabic(str);
stringstream ss;
ss << arab;
string s = ss.str();
strcpy(num.arabic, s.c_str());
}
lst.push_back(num);
count++;
file.read((char *)&num, sizeof(Number));
}
// close the file
file.close();
// open the file to write
file.open("numbers.txt", ios::out);
// write data to the file
list<Number>::iterator itr = lst.begin();
while (itr != lst.end())
{
file << left << setw(15) << (*itr).roman << " " << setw(4) << (*itr).arabic;
itr++;
if (itr != lst.end())
file << endl;
}
// close the file
file.close();
cout<<"1. Search"<<endl;
cout<<"2. Sort"<<endl;
cout<<"3. Exit"<<endl;
cin >> choice;
if (choice == 1)
{
// Linear_search();
}
else if (choice == 2)
{
sort();
}
else if (choice == 3)
{
return 0;
}
else
{
cout << "Invalid option" << endl;
}
return 0;
} // end of main function
// convertRomanToArabic function implementation
int convertRomanToArabic(string str)
{
if (str.length() == 0)
return 0;
int arabic = 0;
char ch; // current character
char nch; // next character
for (unsigned int i = 0; i < str.length() - 1; i++)
{
ch = str[i];
nch = str[i + 1];
if (ch == 'M')
arabic += 1000;
else if (ch == 'D')
arabic += 500;
else if (ch == 'C' && (nch == 'D' || nch == 'M'))
arabic -= 100;
else if (ch == 'C')
arabic += 100;
else if (ch == 'L')
arabic += 50;
else if (ch == 'X' && (nch == 'L' || nch == 'C'))
arabic -= 10;
else if (ch == 'X')
arabic += 10;
else if (ch == 'V')
arabic += 5;
else if (ch == 'I' && (nch == 'V' || nch == 'X'))
arabic -= 1;
else if (ch == 'I')
arabic += 1;
else
{
cout << "Invalid roman number! " << ch << endl;
exit(1);
}
}
ch = str[str.length() - 1];
if (ch == 'M')
arabic += 1000;
else if (ch == 'D')
arabic += 500;
else if (ch == 'C')
arabic += 100;
else if (ch == 'L')
arabic += 50;
else if (ch == 'X')
arabic += 10;
else if (ch == 'V')
arabic += 5;
else if (ch == 'I')
arabic += 1;
else
{
cout << "Invalid roman number! " << ch << endl;
exit(1);
}
return arabic;
} // end of convertRomanToArabic function
// convertArabicToRoman function implementation
string convertArabicToRoman(int arabic)
{
string roman;
int curr;
if (arabic >= 1000)
{
curr = arabic / 1000;
for (int i = 0; i < curr; i++)
{
roman += 'M';
}
arabic = arabic % 1000;
}
if (arabic >= 100)
{
curr = arabic / 100;
if (curr == 9)
{
roman += "CM";
}
else if (curr >= 5)
{
roman += 'D';
for (int i = 0; i < curr - 5; i++)
{
roman += 'C';
}
}
else if (curr == 4)
{
roman += "CD";
}
else if (curr >= 1)
{
for (int i = 0; i < curr; i++)
{
roman += 'C';
}
}
arabic = arabic % 100;
}
if (arabic >= 10)
{
curr = arabic / 10;
if (curr == 9)
{
roman += "XC";
}
else if (curr >= 5)
{
roman += 'L';
for (int i = 0; i < curr - 5; i++)
{
roman += 'X';
}
}
else if (curr == 4)
{
roman += "XL";
}
else if (curr >= 1)
{
for (int i = 0; i < curr; i++)
{
roman += 'X';
}
}
arabic = arabic % 10;
}
if (arabic >= 1)
{
curr = arabic;
if (curr == 9)
{
roman += "IX";
}
else if (curr >= 5)
{
roman += 'V';
for (int i = 0; i < curr - 5; i++)
{
roman += 'I';
}
}
else if (curr == 4)
{
roman += "IV";
}
else if (curr >= 1)
{
for (int i = 0; i < curr; i++)
{
roman += 'I';
}
}
}
return roman;
} // end of convertArabicToRoman function
void sort()
{
struct node *ptr, *s;
int value;
if (head == NULL)
{
cout << "The List is empty" << endl;
return;
}
ptr = head;
while (ptr != NULL)
{
for (s = ptr->next;s !=NULL;s = s->next)
{
if (ptr->data < s->data)
{
value = ptr->data;
ptr->data = s->data;
s->data = value;
}
}
ptr = ptr->next;
}
}
void Linear_search(int )
{
int value, pos = 0;
bool flag = false;
cout<<"Enter the value to be searched: ";
cin>>value;
struct node *s;
s = head;
while (s != NULL)
{
pos++;
if (s->data == value)
{
flag = true;
cout<<"Element "<<value<<" is found at position "<<pos<<endl;
}
s = s->next;
}
}
Sample Input:
Explanation / Answer
Your sort function was correct, but it sorts the value of data not the link. Here is a sorting algorithm for your linked list which sorts based on links. Your search function was also correct, i have added few lines there. Your program does not have insert node function. Add an insert node function to insert nodes in the list, otherwise search and sort function cannot be tested.
/*******************/
void sort()
{
struct node *ptr, *s, *prev;
if (head == NULL)
{
cout << "The List is empty" << endl;
return;
}
ptr = head;
while ( ptr && ptr->next ) // check preset and next
{
if ( ptr->data > ptr->next->data ) // check next element is greater than or not
{
if ( prev == NULL ) // for first condition, when head needs to change
{
s = ptr->next; // save next
ptr->next = ptr->next->next; // connect next to next next
s->next = ptr; // connect previous
head = s; // change the head
ptr = head; // reset the ptr
}
else if ( prev && ptr->next->next ) // when swap is not for first element
{
s = ptr->next;
ptr->next = ptr->next->next;
s->next = ptr;
prev->next = s;
ptr = head;
prev = NULL; // reset prev
}
else if ( prev && ptr->next ) // condition for last elements
{
s = ptr->next;
ptr->next = NULL; // last element connect to null
s->next = ptr;
prev->next = s;
ptr = head;
prev = NULL;
}
}
else // executes first elements need not to be swap
{
prev = ptr;
ptr = ptr->next;
}
}
}
void Linear_search( )
{
int value, pos = 0;
cout<<"Enter the value to be searched: ";
cin>>value;
struct node *s;
s = head;
while (s != NULL)
{
pos++;
if (s->data == value)
{
cout<<"Element "<<value<<" is found at position "<<pos<<endl;
return;
}
s = s->next;
}
cout<<"Element "<<value<<" not found"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.