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

Create a class (to be named LinkList), which will hold integers. Use dynamic mem

ID: 669113 • Letter: C

Question

Create a class (to be named LinkList), which will hold integers. Use dynamic memory allocation for handling any number of integers at run-time. The member data elements should be private with public member functions to handle the following operations:

Constructors:

1. One with no parameters to initialize the class to zero

2. One with a pointer to an integer as parameter to initialize the class to contain a copy of the integer

Add to the linklist function (Append) to allow the adding an integer at the end of the list

Print function (PrintList) to display the integers in the List on the standard output

Count function (CountList) to return the number of integers stored in the List

Sort function (SortList) to arrange the integers (use Bubble sort) in the List from Smallest to Largest

Explanation / Answer

Program:

/*
* C++ Program to Implement Singly Linked List
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;

/*
* Class Declaration
*/
class single_llist
{
public:

node* create_node(int);
void AddAppend();
void PrintList();
int CountList();
void SortList();

single_llist()
{
start = NULL;
}
};

/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i,count;
single_llist sl;
start = NULL;
  
while (1)
{
cout<<endl<<"-------------------------"<<endl;
cout<<endl<<"Operations on linked list"<<endl;
cout<<endl<<"-------------------------"<<endl;
cout<<"1.Add / Append List "<<endl;
cout<<"2.Count list"<<endl;
cout<<"3.Print List"<<endl;
cout<<"4.Sorted List"<<endl;
cout<<"5.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;

switch(choice)
{
case 1:
cout<<"Add or Append an integer: "<<endl; // at end
sl.AddAppend();   
break;
  
case 2:
count=sl.CountList();
cout<<endl<<"No.ofElements in List:"<<count<<endl;   
break;
case 3:
sl.PrintList();   
break;
       case 4:
           cout<<"Sorted List: "<<endl;
sl.SortList();   
break;  
case 5:
cout<<"Quitting..."<<endl;
exit(0);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}


/*
* Creating Node
*/
node *single_llist::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;   
return temp;
}
}

/*
* Appending a Node at last
*/
void single_llist::AddAppend()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;   
       PrintList();   
}
else
{
s = start;
while (s->next != NULL)
{   
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<<endl;
PrintList();
   }
}

/*
*No.of elements in the Linked List
*/
int single_llist::CountList()
{
struct node *ptr, *s;
int count=0;
if (start == NULL)
{
cout<<"The List is empty"<<endl;
return 0;
}
ptr = start->next;
count=1;
while (ptr != NULL)
{
count++;
ptr=ptr->next;
}
return count;

}

/*
* Display Elements of a link list
*/
void single_llist::PrintList()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}

void single_llist::SortList()
{

struct node *current = start, *current2 = current->next;
int temp;

while(current->next->next != NULL)
{
if(current->info > current->next->info)
{
temp = current->info;
current->info = current2->info;
current2->info = temp;
}
current = current->next;
}
PrintList();
}

Output:


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

Operations on linked list

-------------------------
1.Add / Append List
2.Count list
3.Print List
4.Sorted List
5.Quit
Enter your choice : 1
Add or Append an integer:
Enter the value to be inserted: 34
Elements of list are:
34->NULL

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

Operations on linked list

-------------------------
1.Add / Append List
2.Count list
3.Print List
4.Sorted List
5.Quit
Enter your choice : 1
Add or Append an integer:
Enter the value to be inserted: 23
Element Inserted at last
Elements of list are:
34->23->NULL

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

Operations on linked list

-------------------------
1.Add / Append List
2.Count list
3.Print List
4.Sorted List
5.Quit
Enter your choice : 1
Add or Append an integer:
Enter the value to be inserted: 1
Element Inserted at last
Elements of list are:
34->23->1->NULL

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

Operations on linked list

-------------------------
1.Add / Append List
2.Count list
3.Print List
4.Sorted List
5.Quit
Enter your choice : 2

No.ofElements in List:3

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

Operations on linked list

-------------------------
1.Add / Append List
2.Count list
3.Print List
4.Sorted List
5.Quit
Enter your choice : 3
Elements of list are:
34->23->1->NULL

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

Operations on linked list

-------------------------
1.Add / Append List
2.Count list
3.Print List
4.Sorted List
5.Quit
Enter your choice : 4
Sorted List:
Elements of list are:
1->23->34-->NULL

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

Operations on linked list

-------------------------
1.Add / Append List
2.Count list
3.Print List
4.Sorted List
5.Quit
Enter your choice :5

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