Question

I really need help making a linked list program in C++, the program in total should...

I really need help making a linked list program in C++, the program in total should have 3 source files:

  • IntList.h
  • IntList.cpp
  • IntListTest.cpp

The class should contain the following functions:

  • Constructor
  • Destructor (should use removeAll() function)
  • insert(int) - inserts the given int into the list (in order, duplicates are allowed)
  • remove(int) - removes the given int from the list. Returns true if successful, false otherwise.
  • print() - prints the list in-order (all on one line, comma delimited; no comma at the end, a newline instead)
  • count() - returns a count of the number of ints in the list.
  • sum() - returns the sum of the numbers in the list.
  • average() - returns the average of the numbers in the list.
  • find(int) - returns the given strings position in the list (e.g. 1 if first, 2 if 2nd, etc..). Returns 0 if the item is not in the list.
  • removeAll() - removes all ints from the list

int main() should be implemented in IntListTest.cpp, and should do the following:

  1. Insert the numbers 10, 20, 30, and 5; then print the count and items in the list
  2. Delete 5; then print the count and items in the list
  3. Delete 20 and 30; then print the count and items in the list
  4. Delete 10; then print the count and items in the list

Please refrain from using standard template libraries. Thank you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

intList.h

#ifndef _INTLIST_H
#define _INTLIST_H
#include<iostream>
using namespace std;

class Node
{
public:
Node* next;
int data;
};

class intList
{
public:
  
Node* head;

intList();
~intList();
void insert(int);
   bool remove(int);
   int count();
   int sum();
   double average();
   int find(int);
   void removeAll();
void print();
};
#endif

IntList.cpp

#include"IntList.h"

intList::intList()
{
this->head = NULL;
}
void intList::insert(int data)
{
Node* node = new Node();
node->data = data;
   node->next=NULL;
   if(head==NULL)
   {
       head=node;
   }
   else
   {
       Node* tempHead = this->head;
       while(tempHead->next!=NULL)
       {
           tempHead = tempHead->next;
       }
       tempHead->next=node;
   }
}
void intList::print()
{
Node* head = this->head;
int i = 1;
   if(head==NULL)
   {
       cout<<"List is empty";
       return ;
   }
while(head->next!=NULL){
cout<<head->data<<",";
head = head->next;
}
   cout<<head->data<<endl;
}
int intList::count()
{
   int count=0;
   Node* tempHead = this->head;
   while(tempHead)
   {
       count++;
       tempHead = tempHead->next;
   }
   return count;
}
int intList::sum()
{
   int sum=0;
   Node* tempHead = this->head;
   while(tempHead)
   {
       sum+=tempHead->data;
       tempHead = tempHead->next;
   }
   return sum;
}
double intList::average()
{
   double avg=sum()/(double)count();
   return avg;
}
int intList :: find(int item)
{
   int index=0;
   Node* tempHead = this->head;
   while(tempHead)
   {
       if(item==tempHead->data)
           return index+1;
       tempHead = tempHead->next;
       index++;
   }
   return 0;
}
bool intList::remove(int item)
{
   if(head->data==item)
   {
       head=head->next;
       return true;
   }
   Node* prevNode = this->head;
   Node* nextNode = this->head;
   while(nextNode)
   {
       if(nextNode->data==item)
           break;
       prevNode=nextNode;
       nextNode=nextNode->next;
   }
   prevNode->next=nextNode->next;
   return true;
}
void intList::removeAll()
{
   Node* nextNode = this->head;
   while(nextNode)
   {
       remove(nextNode->data);
       nextNode=nextNode->next;
   }
}

IntListTest.cpp

#include"IntList.h"

int main()
{
   intList* list = new intList();
   list->insert(10);
   list->insert(20);
   list->insert(30);
   list->insert(5);
   cout<<"Count is : "<<list->count()<<endl;
   cout<<"Elements in the list are: ";
   list->print();

   list->remove(5);
   cout<<"\n\nAfter removing 5 from the list:"<<endl;
   cout<<"Count is : "<<list->count()<<endl;
   cout<<"Elements in the list are: ";
   list->print();

   list->remove(20);
   list->remove(30);
   cout<<"\n\nAfter removing 20 and 30 from the list:"<<endl;
   cout<<"Count is : "<<list->count()<<endl;
   cout<<"Elements in the list are: ";
   list->print();

   list->remove(10);
   cout<<"\n\nAfter removing 10 from the list:"<<endl;
   cout<<"Count is : "<<list->count()<<endl;
   cout<<"Elements in the list are: ";
   list->print();

   cout<<endl;
   return 1;
}

output


If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
I really need help making a linked list program in C++, the program in total should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help and have to get this done asap: Using the following source codes provided...

    I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...

  • I need help and have to get this done asap: Using the following source codes provided below, create recursive functions...

    I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...

  • c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...

    c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType {                 ItemType item;                 NodeType* next; }; template<class ItemType> class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign operator with deep copy                 bool IsThere(ItemType item) const; // return true of false to indicate if item is...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • complete in C++ code Lists Many programming languages have some kind list data structure, which allows...

    complete in C++ code Lists Many programming languages have some kind list data structure, which allows for insertion and deletion of elements, essentially an array that changes size. C++ has vectors, Java has the ArrayList, Python has lists (which is what they're called generically anyway). But how do they really work? All of these actually use arrays, which naturally have immutable lengths. Your Task Your task is to write a function, insert, which inserts a new item into an existing...

  • This is a C++ program about link list, please help me complete the codes, thanks!(the .h...

    This is a C++ program about link list, please help me complete the codes, thanks!(the .h file has been provided) /* * List.h * * Class Description: List data collection ADT. * Class Invariant: Data collection with the following characteristics: * - Each element is unique (no duplicates). * - (What other characteristic does our List have?) * * * */ #pragma once // You can add #include statements if you wish. #include <string> #include "Patient.h" using namespace std; class...

  • Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers...

    Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers should be within a range (E.g., 1 to 7). The user should be prompted to enter the minimum number and maximum number of the range. Each number should be inserted at the end of the list. Section 7.8 of the textbook covers the random number generator. Examples of how to use the random number generator are in Fig 7.6 and 7.7. Here is a...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • I want the full code for this part and Its needs to be done in C++...

    I want the full code for this part and Its needs to be done in C++ Use a linked list to implement the following skeleton of an unsorted list typedef int ItemType: struct NodeType ItemType item; NodeType "next; class List List(); // default constructor List( const List &x); I copy constructor: deep copy is required List & operator = (const List &x); // assignment operator: deep copy. 1s required bool IsThere(ItemType x); identify if x is in the list void...

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT