Question

In this project, you will be writing an object that implements an ordered linked list with operator overload support for insertions and deletions. The specification for the list object will be provided upfront and you must design an implementation that supports the provided specification. In addition, you will be given a list of tasks that should be performed.

CIS 221 Programming II C++ Programming Project ope

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

/*
   Following c++ code performs all operations mentioned in problem statement.
   Description of each statement or function given in a comment except the given specifications.
*/
#include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

const int OK=1;
const int NOT_FOUND=2;
const int ERROR_MEMORY=-1;

struct OOLNode
{
   int payload;
   OOLNode* next;
};
class OOList
{
private:
   OOLNode* start;
public:
   OOList()
   {
       start=NULL;
   }
   int getListSize()
   {
       int count=0;
       OOLNode* temp=start;
       while(temp!=NULL)
       {
           count++;
           temp=temp->next;
       }
       return count;
   }
   int getListSizeInBytes()
   {
       return (getListSize()*sizeof(OOLNode));
   }
   int getListElements(int* populateMeWithElements)
   {
       int count=0;
       OOLNode* temp=start;
       while(temp!=NULL)
       {
           populateMeWithElements[count]=temp->payload;
           count++;
           temp=temp->next;
       }
       return count;
   }
   int addElement(int addMe)
   {
       //Algorithm for inserting an element in list in increasing order.
       OOLNode* t=new OOLNode;
       t->next=NULL;
       t->payload=addMe;
       if(start==NULL)
       {  
           start=t;
           return OK;
       }
       else if(start->payload>addMe)
       {
           t->next=start;
           start=t;
           return OK;
       }
       else
       {
           OOLNode* temp=start->next;
           OOLNode* prev=start;
           while(temp!=NULL)
           {
               if(temp->payload>addMe)
               {
                   t->next=temp;
                   prev->next=t;
                   break;
               }
               prev=temp;
               temp=temp->next;
           }
           if(temp==NULL)
               prev->next=t;
           return OK;
       }
       return ERROR_MEMORY;
   }
   int operator+(int addMe)
   {
       return addElement(addMe);
   }
   int deleteElement(int deleteMe)
   {
       //Algorithm for deleting an element in a list in increasing order.
       OOLNode* temp=start;
       if(start->payload==deleteMe)
       {
           start=start->next;
           delete temp;
           return OK;
       }
       OOLNode* prev=start;
       temp=temp->next;
       while(temp!=NULL)
       {
           if(temp->payload==deleteMe)
           {
               OOLNode* t=temp;
               prev->next=temp->next;
               delete t;
               return OK;
           }
           prev=temp;
           temp=temp->next;
       }
       return NOT_FOUND;
   }
   int operator-(int deleteMe)
   {
       return deleteElement(deleteMe);
   }
};

int main()
{
   OOList obj;
   int n=100;       //Suggest take small values
   int min=100;      
   int max=100000;      
   int res=0;
   while(n) //It will take long time since we iterating it 100 times.
   {
       int len=obj.getListSize();
       int* arr=new int[len];
      
       len=obj.getListElements(arr);
       for(int i=0;i<len;i++)
           cout<<arr[i]<<" ";
       cout<<"\n";
      
       delete []arr;

       int r=min+(rand()%(int)(max-min+1)); //Generating random number between 100 and 100000
       //int r=2;
       while(r)
       {
           int addMe=1+(rand()%(int)(100)); //Generating random number between 1 and 100
           obj.addElement(addMe);
           r--;
       }
      
       r=min + (rand() % (int)(max - min + 1));
       while(r)
       {
           int addMe=1+(rand()%(int)(100));
  
           res=obj.operator+(addMe);
           r--;
       }
   //Deleting elements with payload 1 to 50
       for(int i=1;i<=50;i++)
           res=obj.deleteElement(i);
       //Deleting elements with payload 51 to 100
       for(int i=51;i<=100;i++)
           res=obj.operator-(i);//res=-obj(i);
       n--;
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
In this project, you will be writing an object that implements an ordered linked list with...
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
  • 8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code...

    8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked...

    C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to create a queue of user-specified capacity (c) enqueue (d) dequeue (e) is_full (f) is_empty display (h) destructor that deallocates all nodes (i) copy constructor (j) overloaded assignment operator Demonstrate using a main function.

  • Using Python. 3) Create a single-linked list (StudentList) where the data in the link is an object of type pointer to Student as defined below. Note that you need to store and pass a pointer of type...

    Using Python. 3) Create a single-linked list (StudentList) where the data in the link is an object of type pointer to Student as defined below. Note that you need to store and pass a pointer of type Student so that you do not create duplicate objects. Test your list with the provided program. You will find example code for a single-linked list of integers in Moodle that you can base your solution on. For your find methods, you can either...

  • In c++ language Design and implement a Queue data structure using linked list. Support the following...

    In c++ language Design and implement a Queue data structure using linked list. Support the following usual operations: default constructor parameterized constructor to create a queue of user-specified capacity enqueue dequeue is_full is_empty display destructor that deallocates all the nodes copy constructor overloaded assignment operator Demonstrate using a main function.

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a...

    Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a class Employee (with member variables: char * name, int id, and double age). 2. Create a constructor that should be able to take arguments and initialize the member variables. 3. Create a copy constructor for employee class to ensure deep copy. 4. Create a destructor and de allocate the dynamic memory. 5. Overload the assignment operator to prevent shallow copy and ensure deep copy....

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Linked Lists implementation on Lists Executive Summary: Linked list implementation on lists includes two parts: Data...

    Linked Lists implementation on Lists Executive Summary: Linked list implementation on lists includes two parts: Data part - stores an element of the list Next part- stores link/pointer to next element You are asked to implement Lists through Linked Lists to perform basic ADT functions. Project Objective: in completing this project, you will Enhance your ability to understand Lists Familiar with the idea of linked list Enable yourself to perform Linked Lists programming skills Due Dates and Honor: This project...

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
ADVERTISEMENT