C++ Given the definition and implementation of the singly linked list in figure
ID: 3862216 • Letter: C
Question
C++ Given the definition and implementation of the singly linked list in figure 3.2 (below), add the following member functions to the linked list (assuming list1 is an object of the class IntSLList)
Member function
Description
1. list1.deleteAll()
This member function deletes all the nodes of list1.
If the list is empty, print an error message.
2. list1.appendList(list2)
This member function appends list2 to list1 and then removes all the nodes of list2.
If both list1 and list2 are empty, print an error message.
3. list1.multiply(value)
where value can be any integer
This member function takes any integer value and multiplies all the nodes (info) by the value of that integer.
4. list1.frequency(value)
where value can be any integer
This member function returns the frequency of a given integer in the linked list.
If the list is empty, print an error message.
5. list1.listcount()
This member function returns the number of nodes in the list.
6. List1.removeNodes(n,k)
This member function removes all nodes from the nth node through the kth node. For example, list1.removeNodes(5,7) would remove the 5th, 6th and 7th nodes. The member function should check that the values of n and k are less than or equal to the number of nodes in the list.
7. List1.display()
This Member function displays the contents of all the nodes.
-------------------------------------------------------------Figure 3.2----------------------------------------------------------------
//************************ intSLList.h **************************
// singly-linked list class to store integers
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST
class IntSLLNode {
public:
IntSLLNode() {
next = 0;
}
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
int info;
IntSLLNode *next;
};
class IntSLList {
public:
IntSLList() {
head = tail = 0;
}
~IntSLList();
int isEmpty() {
return head == 0;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead(); // delete the head and return its info;
int deleteFromTail(); // delete the tail and return its info;
void deleteNode(int);
bool isInList(int) const;
void printAll() const;
private:
IntSLLNode *head, *tail;
};
#endif
//************************ intSLList.cpp **************************
#include <iostream.h>
#include "intSLList.h"
IntSLList::~IntSLList() {
for (IntSLLNode *p; !isEmpty(); ) {
p = head->next;
delete head;
head = p;
}
}
void IntSLList::addToHead(int el) {
head = new IntSLLNode(el,head);
if (tail == 0)
tail = head;
}
void IntSLList::addToTail(int el) {
if (tail != 0) { // if list not empty;
tail->next = new IntSLLNode(el);
tail = tail->next;
}
else head = tail = new IntSLLNode(el);
}
int IntSLList::deleteFromHead() {
int el = head->info;
IntSLLNode *tmp = head;
if (head == tail) // if only one node on the list;
head = tail = 0;
else head = head->next;
delete tmp;
return el;
}
int IntSLList::deleteFromTail() {
int el = tail->info;
if (head == tail) { // if only one node on the list;
delete head;
head = tail = 0;
}
else { // if more than one node in the list,
IntSLLNode *tmp; // find the predecessor of tail;
for (tmp = head; tmp->next != tail; tmp = tmp->next);
delete tail;
tail = tmp; // the predecessor of tail becomes tail;
tail->next = 0;
}
return el;
}
void IntSLList::deleteNode(int el) {
if (head != 0) // if non-empty list;
if (head == tail && el == head->info) { // if only one
delete head; // node on the list;
head = tail = 0;
}
else if (el == head->info) { // if more than one node on the list
IntSLLNode *tmp = head;
head = head->next;
delete tmp; // and old head is deleted;
}
else { // if more than one node in the list
IntSLLNode *pred, *tmp;
for (pred = head, tmp = head->next; // and a non-head node
tmp != 0 && !(tmp->info == el);// is deleted;
pred = pred->next, tmp = tmp->next);
if (tmp != 0) {
pred->next = tmp->next;
if (tmp == tail)
tail = pred;
delete tmp;
}
}
}
bool IntSLList::isInList(int el) const {
IntSLLNode *tmp;
for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
return tmp != 0;
}
void IntSLList::printAll() const {
for (IntSLLNode *tmp = head; tmp != 0; tmp = tmp->next)
cout << tmp->info << " ";
cout << endl;
}
C++ Given the definition and implementation of the singly linked list in figure 3.2 (below), add the following member functions to the linked list (assuming list1 is an object of the class IntSLList)
Member function
Description
1. list1.deleteAll()
This member function deletes all the nodes of list1.
If the list is empty, print an error message.
2. list1.appendList(list2)
This member function appends list2 to list1 and then removes all the nodes of list2.
If both list1 and list2 are empty, print an error message.
3. list1.multiply(value)
where value can be any integer
This member function takes any integer value and multiplies all the nodes (info) by the value of that integer.
4. list1.frequency(value)
where value can be any integer
This member function returns the frequency of a given integer in the linked list.
If the list is empty, print an error message.
5. list1.listcount()
This member function returns the number of nodes in the list.
6. List1.removeNodes(n,k)
This member function removes all nodes from the nth node through the kth node. For example, list1.removeNodes(5,7) would remove the 5th, 6th and 7th nodes. The member function should check that the values of n and k are less than or equal to the number of nodes in the list.
7. List1.display()
This Member function displays the contents of all the nodes.
Explanation / Answer
Here is the code for the first 5 functions:
//************************ intSLList.h **************************
// singly-linked list class to store integers
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST
class IntSLLNode {
public:
IntSLLNode() {
next = 0;
}
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
int info;
IntSLLNode *next;
};
class IntSLList {
public:
IntSLList() {
head = tail = 0;
}
~IntSLList();
int isEmpty() {
return head == 0;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead(); // delete the head and return its info;
int deleteFromTail(); // delete the tail and return its info;
void deleteNode(int);
bool isInList(int) const;
void printAll() const;
void deleteAll(); //deletes all the nodes of list1.
void appendList(IntSLList&); //appends list2 to list1 and then removes all the nodes of list2.
void multiply(int); //multiplies all the nodes (info) by the value of that integer.
int frequency(int); //returns the frequency of a given integer in the linked list.
int listcount(); //returns the number of nodes in the list.
private:
IntSLLNode *head, *tail;
};
#endif
And the implementation file is:
//************************ intSLList.cpp **************************
#include <iostream>
#include "intSLList.h"
using namespace std;
IntSLList::~IntSLList() {
for (IntSLLNode *p; !isEmpty(); ) {
p = head->next;
delete head;
head = p;
}
}
void IntSLList::addToHead(int el) {
head = new IntSLLNode(el,head);
if (tail == 0)
tail = head;
}
void IntSLList::addToTail(int el) {
if (tail != 0) { // if list not empty;
tail->next = new IntSLLNode(el);
tail = tail->next;
}
else head = tail = new IntSLLNode(el);
}
int IntSLList::deleteFromHead() {
int el = head->info;
IntSLLNode *tmp = head;
if (head == tail) // if only one node on the list;
head = tail = 0;
else head = head->next;
delete tmp;
return el;
}
int IntSLList::deleteFromTail() {
int el = tail->info;
if (head == tail) { // if only one node on the list;
delete head;
head = tail = 0;
}
else { // if more than one node in the list,
IntSLLNode *tmp; // find the predecessor of tail;
for (tmp = head; tmp->next != tail; tmp = tmp->next);
delete tail;
tail = tmp; // the predecessor of tail becomes tail;
tail->next = 0;
}
return el;
}
void IntSLList::deleteNode(int el) {
if (head != 0) // if non-empty list;
if (head == tail && el == head->info) { // if only one
delete head; // node on the list;
head = tail = 0;
}
else if (el == head->info) { // if more than one node on the list
IntSLLNode *tmp = head;
head = head->next;
delete tmp; // and old head is deleted;
}
else { // if more than one node in the list
IntSLLNode *pred, *tmp;
for (pred = head, tmp = head->next; // and a non-head node
tmp != 0 && !(tmp->info == el);// is deleted;
pred = pred->next, tmp = tmp->next);
if (tmp != 0) {
pred->next = tmp->next;
if (tmp == tail)
tail = pred;
delete tmp;
}
}
}
bool IntSLList::isInList(int el) const {
IntSLLNode *tmp;
for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
return tmp != 0;
}
void IntSLList::printAll() const {
for (IntSLLNode *tmp = head; tmp != 0; tmp = tmp->next)
cout << tmp->info << " ";
cout << endl;
}
//his member function deletes all the nodes of list1.
//If the list is empty, print an error message.
void IntSLList::deleteAll() {
if(head == 0)
cout << "List is readily empty, and there is nothing to delete."<<endl;
else
while(head != 0) //if non-empty list;
deleteFromTail();
}
int IntSLList::listcount()
{
int count = 0;
IntSLLNode *temp = head;
while(temp != 0)
{
count++;
temp = temp->next;
}
return count;
}
//This member function appends list2 to list1 and then removes all the nodes of list2.
//If both list1 and list2 are empty, print an error message.
void IntSLList::appendList(IntSLList &list2)
{
if(head == 0 && list2.isEmpty())
cout << "Both lists are empty, and nothing to append." << endl;
else
{
while(list2.head != 0)
addToTail(list2.deleteFromHead());
}
}
// This member function takes any integer value and multiplies all the nodes (info) by the
// value of that integer.
void IntSLList::multiply(int value)
{
IntSLLNode *temp = head;
while(temp != 0)
{
temp->info = temp->info * value;
temp = temp->next;
}
}
//This member function returns the frequency of a given integer in the linked list.
//If the list is empty, print an error message.
int IntSLList::frequency(int value)
{
if(head == 0)
cout << "No elements in list."<<endl;
IntSLLNode *temp = head;
int count = 0;
while(temp != 0)
{
if(temp->info == value)
count++;
temp = temp->next;
}
return count;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.