Your task
1) void PrinList(),
2)int search_Element(int X),3)
void Insert_Element(int X),
4)void
Insert_Element_at(int X, int pos),
5) bool Delete_Element(int X),
6) bool is_Empty(),
7)int Length(),
8) void Print_Reverse_List(),
9) void Empty_List(),
10) void Copy_List(…)
Note:
1)Perform the above tasks for linked list
2)make copy, default and parameterized constructors
3)make a menu in main and call the functions through the manu
4)No global declarations
#include <iostream>
using namespace std;
class List {
public:
int item;
int pos = 0;
List *nextNode;
};
void Print_Reverse_List(List *node) {
if (node == NULL)
return;
Print_Reverse_List(node->nextNode);
cout << node->item << " ";
}
void Insert_Element(List **head, char data) {
List *node = new List();
node->item = data;
node->nextNode = (*head);
(*head) = node;
}
void PrinList(List *node) {
List *temp = node;
while (temp != NULL) {
cout << temp->item << " ";
node->pos++;
temp = temp->nextNode;
}
cout << "\n";
}
void search_Element(List *node, int x) {
List *temp = node;
while (temp != NULL) {
int data = temp->item;
if (data == x) {
cout << "\nElement found\n";
}
temp = temp->nextNode;
}
cout << "\n";
}
int Length(List *node) {
return node->pos;
}
bool is_Empty(List *node) {
if (node->pos == 0) {
return true;
} else {
return false;
}
}
void Copy_List(List *node, List *list) {
List *temp = node;
while (temp != NULL) {
int data = temp->item;
Insert_Element(&list, data);
temp = temp->nextNode;
}
cout << "The copied list is:\n";
PrinList(list);
}
int main()
{
List *node = NULL;
Insert_Element(&node, 4);
Insert_Element(&node, 3);
Insert_Element(&node, 2);
Insert_Element(&node, 1);
PrinList(node);
Print_Reverse_List(node);
cout << "\nThe length is: " << Length(node) << endl;
search_Element(node, 3);
if (is_Empty(node) == 0) {
cout << "\nThe list is not empty\n";
} else {
cout << "\nThe list is empty\n";
}
List *list = NULL;
Copy_List(node, list);
return 0;
}
Comments
Leave a comment