Urgent Help!!! Design a linked list class to hold a series of integers. The clas
ID: 3859233 • Letter: U
Question
Urgent Help!!!
Design a linked list class to hold a series of integers. The class should have the following member functions:
append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges the nodes in the list so that their order is reversed), and search (which returns the position of a specific value in the list. Assume the first node is position 0. If the items is not found, then return a -1 which will tell the calling function that that value does not exist)
High level validation: Numerical data input from the user should be validated before sending to the mutator function. While a non-integer is entered, display error and get data.
Low level validation: Mutator functions which are told to access a node that doesn't exist should return a -1 so that the calling function can handle the problem.
Create a menu-driven program to demonstrate the linked list capabilities. Be sure to have proper constructors and destructors (a destructor must delete every node). Make your input and output professional. Break your code down into functions so as to maintain modular design. No global variables.
Demo your program as follows:
append node
append node
append node
append node
append node
print list
insert at position
print list
delete at position
print list
reverse
print list
search list
---------------
Please Submit to me: all of your .cpp and .h files.
THANKS IN ADVANCE
Explanation / Answer
Answer:
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *later;
}*begin;
class single_llist
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void sort();
void search();
void update();
void reverse();
void display();
single_llist()
{
begin = NULL;
}
};
main()
{
int choice, nodes, element, position, i;
single_llist sl;
begin = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at last"<<endl;
cout<<"3.Insert node at position"<<endl;
cout<<"4.Sort Link List"<<endl;
cout<<"5.Delete a Particular Node"<<endl;
cout<<"6.Update Node Value"<<endl;
cout<<"7.Search Element"<<endl;
cout<<"8.Display Linked List"<<endl;
cout<<"9.Reverse Linked List "<<endl;
cout<<"10.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.insert_last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Sort Link List: "<<endl;
sl.sort();
cout<<endl;
break;
case 5:
cout<<"Delete a particular node: "<<endl;
sl.delete_pos();
break;
case 6:
cout<<"Update Node Value:"<<endl;
sl.update();
cout<<endl;
break;
case 7:
cout<<"Search element in Link List: "<<endl;
sl.search();
cout<<endl;
break;
case 8:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 9:
cout<<"Reverse elements of Link List"<<endl;
sl.reverse();
cout<<endl;
break;
case 10:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
node *single_llist::create_node(int value)
{
struct node *support, *s;
support = new(struct node);
if (support == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
support->data= value;
support->later = NULL;
return support;
}
}
void single_llist::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *support, *p;
support = create_node(value);
if (begin == NULL)
{
begin = support;
begin->later = NULL;
}
else
{
p = begin;
begin = support;
begin->later = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
void single_llist::insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *support, *s;
support = create_node(value);
s = begin;
while (s->later != NULL)
{
s = s->later;
}
support->later = NULL;
s->later = support;
cout<<"Element Inserted at last"<<endl;
}
void single_llist::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *support, *s, *input;
support = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = begin;
while (s != NULL)
{
s = s->later;
counter++;
}
if (pos == 1)
{
if (begin == NULL)
{
begin = support;
begin->later = NULL;
}
else
{
input = begin;
begin = support;
begin->later = input;
}
}
else if (pos > 1 && pos <= counter)
{
s = begin;
for (i = 1; i < pos; i++)
{
input = s;
s = s->later;
}
input->later = support;
support->later = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
void single_llist::sort()
{
struct node *input, *s;
int value;
if (begin == NULL)
{
cout<<"The List is empty"<<endl;
return;
}
input = begin;
while (input != NULL)
{
for (s = input->later;s !=NULL;s = s->later)
{
if (input->data> s->info)
{
value = input->info;
input->data= s->info;
s->data= value;
}
}
input = input->later;
}
}
void single_llist::delete_pos()
{
int pos, i, counter = 0;
if (begin == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *input;
s = begin;
if (pos == 1)
{
begin = s->later;
}
else
{
while (s != NULL)
{
s = s->later;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = begin;
for (i = 1;i < pos;i++)
{
input = s;
s = s->later;
}
input->later = s->later;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
void single_llist::update()
{
int value, pos, i;
if (begin == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the node postion to be updated: ";
cin>>pos;
cout<<"Enter the new value: ";
cin>>value;
struct node *s, *input;
s = begin;
if (pos == 1)
{
begin->data= value;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<<pos<<" elements";
return;
}
s = s->later;
}
s->data= value;
}
cout<<"Node Updated"<<endl;
}
void single_llist::search()
{
int value, pos = 0;
bool flag = false;
if (begin == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the value to be searched: ";
cin>>value;
struct node *s;
s = begin;
while (s != NULL)
{
pos++;
if (s->data== value)
{
flag = true;
cout<<"Element "<<value<<" is found at position "<<pos<<endl;
}
s = s->later;
}
if (!flag)
cout<<"Element "<<value<<" not found in the list"<<endl;
}
void single_llist::reverse()
{
struct node *input1, *input2, *input3;
if (begin == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (begin->later == NULL)
{
return;
}
input1 = begin;
input2 = input1->later;
input3 = input2->later;
input1->later = NULL;
input2->later = input1;
while (input3 != NULL)
{
input1 = input2;
input2 = input3;
input3 = input3->later;
input2->later = input1;
}
begin = input2;
}
void single_llist::display()
{
struct node *support;
if (begin == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
support = begin;
cout<<"Elements of list are: "<<endl;
while (support != NULL)
{
cout<<support->info<<"->";
support = support->later;
}
cout<<"NULL"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.