I need help to write this program in C++ programming language. Thank you. Write
ID: 3667882 • Letter: I
Question
I need help to write this program in C++ programming language. Thank you.
Write a class that implements an ordered linked list based on the following UML diagram:
The { query } tag indicates the method is an accessor.
The class contains a single attribute, head. Head is a pointer to the first node in the linked list.
The class contains several required methods and one extra credit method.
The required methods are:
Constructor - initializes head to null.
Destructor - responsible for deallocating the list from memory
Insert - Accepts a character argument, inserts a node containing the character into the linked list. Insert preserves the order of the linked list. THe linked list orderes the nodes in ascending order. For example, Head -> A -> E -> O -> U -> NULL.
remove - Accepts a charater argument, then searches the linked list for the first matching node and removes it. Does nothing if a matching node is not found.
search -- Accepts a character argument, then searches for a matching node in the linked list. Returns true if found, false otherwise.
print -- displays the contents of the linked list to the screen.
size -- returns the number of nodes with in the linked list.
write -- writes the contents of the linked list to a file named "list.dat". Returns true if the file is successfully written, false othewise. Each nodes value should be written to the file without whitespace. For example, the linked listmentioned in the insert method would appear as AEOU within the file.
read -- read the "list.dat" file, and constructs a linked list using the data. Returns true if linked list is succesfully created, false otherwise.
destroy -- removes the linked list from memory. Could be called by the destructor.
The extra credit methods are:
reverse -- accepts no arguments, merely calls the overloaded reverse method.
reverse -- accepts a node pointer as an argument. Uses recursion to display the contents of the linked list in reverse order. For example, the list mentioned in the insert description would be displayed as : Head-> U -> O -> E -> A -> NULL
The extra credit is worth 1 point.
Demonstrate your class in a menu-driven program.
The program should prompt the user for a selection from the following choices:
(C)lear
when the user selects this option, the linked list is destroyed.
(D)elete
Selecting this option prompts the user for a character to remove from the linked list, and then attempts to remove it.
(G)et
Loads the linked list from disk
(I)nsert
Prompts the user for a character to insert into the linked list, then the insertion is performed.
(L)ength
Returns a count of the number of nodes currently in the linked list
(M)enu
Displays a menu of options
(P)rint
Displays the contents of the linked list
(Q)uit
Terminates the program.
(R)everse
Extra credit option: displays the linked list in reverse order.
(S)earch
Asks the user for a value to search for. Indicates whether the value has been found or not.
(W)rite;
Writes the linked list to disk.
The program should continue prompting the user for selections and processing them until the user select Quit. Use your object methods to process each request.
Sample Run:
Explanation / Answer
Answer:
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
char data;
struct node *next;
}*start_element;
class uml_single_linkedlist
{
public:
node* create_node(int);
void insert();
/*void insert_at_position(); Remove Comments in Below Code To Execute */
void delete();
void search();
void write();
void reverse();
int size();
void destroy();
void print();
uml_single_linkedlist()
{
start_element = NULL;
}
};
//Main Menu
main()
{
int choice, nodes, element, position, i;
uml_single_linkedlist obj;
start_element = NULL;
while (1)
{
cout<<endl<<"Different Operations on linked list :"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at position"<<endl;
cout<<"3.Delete a Particular Element"<<endl;
cout<<"4.write Node Value"<<endl;
cout<<"5.Search Element in list"<<endl;
cout<<"6.print Linked List"<<endl;
cout<<"7.Reverse Linked List "<<endl;
cout<<"8.Size of the list"<<endl;
cout<<"9.Destroying the list"<<endl;
cout<<"10.Quit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
obj.insert();
cout<<endl;
break;
/* case 2: //Remove Comments For The Code Above Before Selecting The Case
cout<<"Inserting Node at a given position:"<<endl;
obj.insert_at_position();
cout<<endl;
break;*/
case 3:
cout<<"Delete a particular node: "<<endl;
obj.delete();
break;
case 4:
cout<<"write Node Value:"<<endl;
obj.write();
cout<<endl;
break;
case 5:
cout<<"Search element in Link List: "<<endl;
obj.search();
cout<<endl;
break;
case 6:
cout<<"print elements of link list"<<endl;
obj.print();
cout<<endl;
break;
case 7:
cout<<"Reverse elements of Link List"<<endl;
obj.reverse();
cout<<endl;
break;
case 8:
cout<<"Size of elements of Linked List"<<endl;
obj.size();
cout<<endl;
break;
case 9:
cout<<"After Destroy of elements in Linked List"<<endl;
obj.destroy();
cout<<endl;
break;
case 10:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice.Please Check"<<endl;
}
}
}
node *uml_single_linkedlist::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->data = value;
temp->next = NULL;
return temp;
}
}
// Inserting element in the list
void uml_single_linkedlist::insert()
{
char value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start_element == NULL)
{
start_element = temp;
start_element->next = NULL;
}
else
{
p = start_element;
start_element = temp;
start_element->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
/* Insertion of element at a given position
void uml_single_linkedlist::insert_at_position()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start_element;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start_element == NULL)
{
start_element = temp;
start_element->next = NULL;
}
else
{
ptr = start_element;
start_element = temp;
start_element->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start_element;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}*/
// Deletion of element at a given position
void uml_single_linkedlist::delete()
{
int pos, i, counter = 0;
if (start_element == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start_element;
if (pos == 1)
{
start_element = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start_element;
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 Entered Is Deleted"<<endl;
}
}
//write a given node value
void uml_single_linkedlist::write()
{
char value
int pos, i;
if (start_element == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the node postion to be writed: ";
cin>>pos;
cout<<"Enter the new value: ";
cin>>value;
struct node *s, *ptr;
s = start_element;
if (pos == 1)
{
start_element->data = value;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<<pos<<" elements";
return;
}
s = s->next;
}
s->data = value;
}
cout<<"Node written"<<endl;
}
//Searching an element in list
void uml_single_linkedlist::search()
{
char value;
int pos = 0;
bool flag = false;
if (start_element == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the value to be searched: ";
cin>>value;
struct node *s;
s = start_element;
while (s != NULL)
{
pos++;
if (s->data == value)
{
flag = true;
cout<<"Element "<<value<<" is found at position "<<pos<<endl;
}
s = s->next;
}
if (!flag)
cout<<"Element "<<value<<" not found in the list"<<endl;
}
//displaying reverse of a Linked List
void uml_single_linkedlist::reverse()
{
struct node *ptr1, *ptr2, *ptr3;
if (start_element == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (start_element->next == NULL)
{
return;
}
ptr1 = start_element;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
start_element = ptr2;
}
//size of linked list
int uml_single_linkedlist::size()
{
struct node *cur = start_element;
int size = 0;
while(cur) {
size++;
if(cur->next == 0) {
return size;
}
cur = cur->next;
}
return size;
}
//destroy of linked list
void uml_single_linkedlist::destroy()
{
struct node *cur=start_element ;
while (cur != NULL) {
start_element= start_element->next;
delete cur;
cur =start_element;
}
start_element= NULL;
}
//display elements of a link list
void uml_single_linkedlist::print()
{
struct node *temp;
if (start_element == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start_element;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->data<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.