Ok so I need these following exercises completed in C++. The code that goes with
ID: 3882390 • Letter: O
Question
Ok so I need these following exercises completed in C++. The code that goes with them will follow after the exercises.
The code is after two lines. In all needs to be completed in C++. Also each individual program will be separated by Asteriks(************)
Exercise 2: Implementing a Doubly Linked List
Modify the class Linked List in Exercise 1 to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards.
void addEnd(int x): create this method to add x to the end of the list.
void displayInReverse(): create this method to display the list elements from the last item to the first one.
Create a main() function to test your DoublyLinkedList class.
Exercise 3: Implementing a Bag Class With a Linked List
Create a class Bag that uses a linked list to store the bag items. The item type must be char. The class should have the methods listed below. Create a main() that will store in a bag object a fixed number of characters entered by the program user. After the input is completed, the program should modify the bag content so that it does not contain any duplicate characters, if duplicates were entered. For example, if the user entered 'M' 'I' 'S' 'S' 'I' 'S' 'S' 'I' 'P' 'P' 'I', the characters remaining in the bag after the removal of duplicates would be 'M' 'I' 'S' 'P'.
Bag(): default constructor
~Bag(): class destructor
bool isEmpty(): determines whether the bag is empty
void print(): prints the bag elements
int getSize(): returns the number of items in the bag
void clear(): removes all of the items from the bag
void add(char item): adds an item to the bag
void remove(char item): removes an item from the bag; only one occurrence of the item should be removed.
int count(char item): counts the number of occurrences of an item in the bag.
(Note that you can reuse the code in Exercise 1 for the LinkedList class to create your Bag class. It will help you to save development time.)
Exercise 4: Using the C++ STL List
Write a program that fills a STL list object with 10 random integers, each in the interval [0, 20], and prints how many times each integer in the interval [0, 20] appears in the list.
____________________________________________________________________________________________________________________________________________________________________________________________
main.cpp for list container
/*******************************
* Week 2 lesson: *
* using the list container *
*******************************/
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> numbers;
for (int i=0; i<10; i++)
numbers.push_back(rand()%100);
while(!numbers.empty())
{
int x = numbers.front();
cout << x << " ";
numbers.pop_front();
}
cout << endl;
return 0;
}
*******************************************************************************
Main.cpp(linkedlists)
/*******************************
* Week 2 lesson: *
* a simple LinkedList class *
*******************************/
#include <iostream>
#include "LinkedList.h"
using namespace std;
int main()
{
LinkedList myList;
int x;
//Add 5 random numbers to list
for (int i=0; i < 5; i++)
myList.add(rand()%20);
cout << "1 - Display the list elements" << endl;
cout << "2 - Is it empty?" << endl;
cout << "3 - Add element" << endl;
cout << "4 - Delete element" << endl;
cout << "5 - Exit" << endl;
int option;
//Loop to test the LinkedList class methods
do
{
cout << endl << "Enter your choice: ";
cin >> option;
switch(option)
{
case 1:
cout << "List elements: ";
myList.display();
break;
case 2:
if (myList.isEmpty()) cout << "List is empty"<< endl;
else cout << "List is not empty" << endl;
break;
case 3:
cout << "Enter an element to add at the beginning of the list: ";
cin >> x;
myList.add(x);
break;
case 4:
cout << "Enter an element to delete from the list: ";
cin >> x;
myList.remove(x);
break;
case 5:
cout << "All done!" << endl;
break;
default: cout << "Invalid choice!" << endl;
}
} while (option != 5);
return 0;
}
Linkedlist .h file
/*******************************
* Week 2 lesson: *
* a simple LinkedList class *
*******************************/
/*
* Linked list node.
*/
struct Node
{
int info; //element stored in this node
Node *next; //link to next node
};
/*
* Class implementing a linked list.
*/
class LinkedList
{
public:
LinkedList();
~LinkedList();
bool isEmpty();
void display();
void add(int);
void remove(int);
private:
Node *first; //pointer to header (dummy) node
};
Linkedlist.cpp
/*******************************
* Week 2 lesson: *
* a simple LinkedList class *
*******************************/
#include <iostream>
#include "LinkedList.h"
using namespace std;
/*
* Initializes the list to empty creating a dummy header node.
*/
LinkedList::LinkedList()
{
first = new Node;
first->next = NULL;
}
/*
* Destructor. Deallocates all the nodes of the linked list,
* including the header node.
*/
LinkedList::~LinkedList()
{
Node *temp;
while (first != NULL)
{
temp=first;
first=first->next;
delete temp;
}
}
/*
* Determines whether the list is empty.
*
* Returns true if the list is empty, false otherwise.
*/
bool LinkedList::isEmpty()
{
return first->next == NULL;
}
/*
* Prints the list elements.
*/
void LinkedList::display()
{
Node * current = first->next;
while(current != NULL)
{
cout << current->info << " ";
current = current->next;
}
cout << endl;
}
/*
* Adds the element x to the beginning of the list.
*
* x: element to be added to the list.
*/
void LinkedList::add(int x)
{
Node *p = new Node;
p->info = x;
p->next = first->next;
first->next = p;
}
/*
* Removes the first occurrence of x from the list. If x is not found,
* the list remains unchanged.
*
* x: element to be removed from the list.
*/
void LinkedList::remove(int x)
{
Node * old = first->next,
* p = first;
//Finding the address of the node before the one to be deleted
bool found = false;
while (old != NULL && !found)
{
if (old->info == x) found = true;
else
{
p = old;
old = p->next;
}
}
//if x is in the list, remove it.
if (found)
{
p->next = old->next;
delete old;
}
}
**********************************************************************************************
main.cpp for iterators
/*******************************
* Week 2 lesson: *
* using iterators *
*******************************/
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> numbers;
for (int i=0; i<10; i++)
numbers.push_back(rand()%100);
list<int>::iterator it;
for (it = numbers.begin(); it!=numbers.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
return 0;
}
Explanation / Answer
2.
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*start;
//class declaration
class double_llist
{
public:
void addEnd(int value);
void display_dlist();
void count();
void displayInReverse();
double_llist()
{
start = NULL;
}
};
//main function with driven menu
int main()
{
int choice, element;
double_llist dl;
while (1)
{
cout<<"Operations on Doubly linked list"<<endl;
cout << "1.Insert at end 2. Display 3. Reverse 4.exit Enter your choice ";
cin>>choice;
switch ( choice )
{
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.addEnd(element);
cout<<endl;
break;
case 2:
dl.display_dlist();
cout<<endl;
break;
case 3:
if (start == NULL)
{
cout<<"List is empty , there is no element to reverse"<<endl;
break;
}
dl.displayInReverse();
cout<<endl;
break;
case 4:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}
//creating list
void double_llist::addEnd(int value)
{
struct node *s, *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
if (start == NULL)
{
temp->prev = NULL;
start = temp;
}
else
{
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s;
}
}
//method to display the current status of the linked list
void double_llist::display_dlist()
{
struct node *q;
if (start == NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
}
q = start;
cout<<"The Doubly Link List is :"<<endl;
while (q != NULL)
{
cout<<q->info<<" <-> ";
q = q->next;
}
cout<<"NULL"<<endl;
}
//method to reverse the doubly linked list
void double_llist::displayInReverse()
{
struct node *p1, *p2;
p1 = start;
p2 = p1->next;
p1->next = NULL;
p1->prev = p2;
while (p2 != NULL)
{
p2->prev = p2->next;
p2->next = p1;
p1 = p2;
p2 = p2->prev;
}
start = p1;
cout<<"List Reversed , Enter choice 2 to see the status of reversed doubly linked list "<<endl;
}
Output -
aashish@aashish ~/Desktop/My_D/chegg $ gedit d_list.cpp
aashish@aashish ~/Desktop/My_D/chegg $ g++ d_list.cpp
aashish@aashish ~/Desktop/My_D/chegg $ ./a.out
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
1
Enter the element: 3
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
1
Enter the element: 6
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
1
Enter the element: 7
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
1
Enter the element: -4
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
1
Enter the element: 0
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
1
Enter the element: 4
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
2
The Doubly Link List is :
3 <-> 6 <-> 7 <-> -4 <-> 0 <-> 4 <-> NULL
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
3
List Reversed , Enter choice 2 to see the status of reversed doubly linked list
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
2
The Doubly Link List is :
4 <-> 0 <-> -4 <-> 7 <-> 6 <-> 3 <-> NULL
Operations on Doubly linked list
1.Insert at end 2. Display 3. Reverse 4.exit
Enter your choice
4
aashish@aashish ~/Desktop/My_D/chegg $
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.