using c++ , implement a linked list ADT using actual pointers. The purpose of th
ID: 3723658 • Letter: U
Question
using c++ ,
implement a linked list ADT using actual pointers. The purpose of this program is to :
A) Build 2 list (list_1 and List_2 ). Each list consist of 15 random integer numbers that are between 0 and 20 exclusive Display List_1 and List_2.
B) Creat new list ( List_3 ) that contain all elements of list_1 followed by all elements of list_2. Display list_3
C) Display the number of occurrences of the First element in List_3.
D) Creat a new list ( list_4) by removing all duplicate numbers that are in list_3. Display list_4.
E) Display number of elements in list_4.
F) Display list_4 backwards.
G) Move the last element in list_4 and places it in the 3rd element in the same list. Display list_4.
H) Sort list_4 in increasing order. Display list 4
repeat the procces A-H untill user terminates the program
Explanation / Answer
Hi... I have written cpp program for the above a,b,c,d and e questions. As per chegg rules we need to answer 4 parts of the question. Please check below.
Main.cpp
#include <iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
void display()
{
node *tmp;
tmp = head;
while (tmp != NULL)
{
cout << tmp->data << " ";
tmp = tmp->next;
}
}
int getVal(int key){
node *tmp;
tmp = head;
int i=0;
int val=0;
while (tmp != NULL)
{
if(i==key){
val = tmp->data;
return val;
}
i++;
//cout << tmp->data << endl;
tmp = tmp->next;
}
}
};
int main()
{
linked_list List_1;
int num;
srand (time(NULL));
for(int i=0;i<15;i++){
num = rand() % 20;
List_1.add_node(num);
}
linked_list List_2;
for(int i=0;i<15;i++){
num = rand() % 20;
List_2.add_node(num);
}
linked_list List_3;
int *backup = new int[30];
int b=0;
for(int i=0;i<15;i++){
int ab = List_1.getVal(i);
List_3.add_node(ab);
backup[b]=ab;
b++;
}
for(int i=0;i<15;i++){
int ab = List_2.getVal(i);
List_3.add_node(ab);
backup[b]=ab;
b++;
}
cout << "List 3 Values: ";
List_3.display();
cout<<" ";
int firstNum = List_3.getVal(0);
int repeat = 0;
for(int i=1;i<30;i++){
if(firstNum==List_3.getVal(i)){
repeat++;
}
}
cout << "number of occurrences of the First element "<<firstNum<<" is: "<<repeat<<" ";
int n=30;
for(int i=0;i<n;++i)
for(int j=i+1;j<n;)
{
if(backup[i]==backup[j])
{
for(int k=j;k<n-1;++k)
backup[k]=backup[k+1];
--n;
}
else
++j;
}
cout<<" ";
linked_list List_4;
for(int i=0;i<n;++i){
List_4.add_node(backup[i]);
}
cout << "List 4 Values: ";
List_4.display();
return 0;
}
Output:
List 3 Values:
12 4 14 14 17 19 19 13 16 9 6 14 2 17 1 14 19 13 4 16 14 0 10 0 10 12
15 5 0 3
number of occurrences of the First element 12 is: 1
List 4 Values:
12 4 14 17 19 13 16 9 6 2 1 0 10 15 5 3
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.