Do not use classes in this assignment. Do not use any of the STL (including the
ID: 3587133 • Letter: D
Question
Do not use classes in this assignment. Do not use any of the STL (including the vector type) in this assignment. 1) 2) Create an ordered linked list to store integer values. Define routines to: Traverse the linked list a. b. C. Insert an element into the linked list Delete an element from the linked list 3) Test the routines using a random number generator a. Construct a linked list of at least size 50 b. Traverse this linked list, storing values in an output file called "P2OutputA.txt" c. Delete the smallest element of the linked list d. Delete the largest element of the linked list e. Delete some value in the middle of the linked list f. Traverse the linked list, storing values in an output file called "P2OutputB.txt"Explanation / Answer
Here is the program as per your requirements.
#include<iostream> // must include
#include<cstdio>
#include<cstdlib>
#include<fstream>
using namespace std;
struct node // defining a structure
{
int info;
struct node *next;
}*start;
class single_llist
{
public:
node* create_node(int value); // creating a node
void insert_last(int n); // inserting a node
void delete_pos(int pos); // deleting a node
void sort();
single_llist()
{
start = NULL;
}
};
int main()
{
single_llist sl;
int c, i;
struct node *temp;
ofstream outfile1;
ofstream outfile2;
outfile1.open("P2OutputA.txt");
outfile2.open("P2OutputB.txt");
start = NULL;
for( i = 1; i <=50; i++)
{
c = 1 + (rand() % 6); // random number genereation
sl.insert_last(c); // inserting the random number into the linked list
}
temp = start;
outfile1 <<"Elements of list are: "<<endl;
while (temp != NULL) // traversing the linked list
{
outfile1 <<temp->info<<"->";
temp = temp->next;
}
outfile1 <<"NULL"<<endl;
outfile1.close();
sl.sort();
sl.delete_pos(1);
sl.delete_pos(49);
sl.delete_pos(33);
temp = start;
outfile2 <<"Elements of list are: "<<endl;
while (temp != NULL)
{
outfile2 <<temp->info<<"->";
temp = temp->next;
}
outfile2 <<"NULL"<<endl;
outfile2.close();
return 0;
}
node *single_llist::create_node(int value)
{
struct node *temp ;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
void single_llist::insert_last(int n)
{
struct node *temp, *s;
temp = create_node(n);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
}
void single_llist::sort()
{
struct node *ptr, *s;
int value;
if (start == NULL)
{
cout<<"The List is empty"<<endl;
return;
}
ptr = start;
while (ptr != NULL)
{
for (s = ptr->next;s !=NULL;s = s->next)
{
if (ptr->info > s->info)
{
value = ptr->info;
ptr->info = s->info;
s->info = value;
}
}
ptr = ptr->next;
}
}
void single_llist::delete_pos(int pos)
{
int i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
Output
Two files.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.