The problem
Implement a SortedList class which stores a list of int data in ascending order.
Program requirements
The public interface of your class is as follows:
class SortedList{
public:
SortedList();
SortedList(cons t SortedList& aSortedList);
~SortedList();
bool sortedIsEmpty() const;
int sortedGetLength () const;
void sortedInsert(in t newItem, bool& success, bool& duplicate);
void sortedRemove(in t index, bool& success);
void sortedRetrieve( int index, int& dataItem, bool& success) const;
int locatePosition( int dataItem, bool& success) const;
void printList() const;
/* private and protected members here ... */
};
You may include private and protected class members as you see fit.
The function sortedIsEmpty should test whether a given list is empty, and sortedLength should return the length of a given list.
The member function sortedRetrieve should return the kth value in the list for a positive integer k. If k is out of the range of indices of the list, the flag success should be false.
The member function sortedInsert should insert a given int into the list in the correct place. DUPLICATE ENTRIES ARE NOT ALLOWED. The flag duplicate should be true when there is a duplicate entry. The flag success should be false if the item to be inserted is a duplicate or (inclusive or) the insertion is unsuccessful because there is no more room to insert an item into the list.
(Note this last case will happen in an array-based implementation if there is no more space in the array, and will happen in a pointer-based implementation if the new operator is unable to allocate space for a new node.)
printList should print the entries of the list to the screen in the correct order.
locatePosition should locate the position of a given item in the linked list. Your code should use the flag success to handle the case when the item is not found in the list.
The class declaration and implementation must all be included in one file, named sortedlist.h
Your solution must work correctly with the driver program sortedlist.cpp. You may not change the file sortedlist.cpp in order to make your solution work. Your solution must also work with other driver programs.
How to do it
Much of your code can be taken or modified from the List class. At least in the pointer-based approach, you must take some care when inserting a new node at the beginning of the list, as opposed to in the middle or at the end of the list. Be sure your code handles both cases correctly.
The output of my solution (using the driver file sortedlist.cpp) :
The list is empty.
The item 6 has been successfully inserted into the list.
The list now contains 6
The item 5 has been successfully inserted into the list.
The list now contains 5 6
The item -9 has been successfully inserted into the list.
The list now contains -9 5 6
-9 is already in the list.
Insertion unsuccessful.
The list now contains -9 5 6
6 is already in the list.
Insertion unsuccessful.
The list now contains -9 5 6
The item -15 is not in the list.
The item 30 is not in the list.
The item 6 is in the 3-th position in the list.
Index 6 out of range. No item has been removed.
The list now contains -9 5 6
The item 16 has been successfully inserted into the list.
The list now contains -9 5 6 16
The item 1 has been successfully inserted into the list.
The list now contains -9 1 5 6 16
The item 4 has been successfully inserted into the list.
The list now contains -9 1 4 5 6 16
The item 9 has been successfully inserted into the list.
The list now contains -9 1 4 5 6 9 16
16 is already in the list.
Insertion unsuccessful.
The list now contains -9 1 4 5 6 9 16
The item 25 has been successfully inserted into the list.
The list now contains -9 1 4 5 6 9 16 25
The 5-th item on the list has been removed.
The list now contains -9 1 4 5 9 16 25
The item -9 is in the 1-th position in the list.
The 1-th item on the list has been removed.
The list now contains 1 4 5 9 16 25
The item 20 has been successfully inserted into the list.
The list now contains 1 4 5 9 16 20 25
The item 14 has been successfully inserted into the list.
The list now contains -9 5 6 14 16
1 2049 Banfa 9,065
Recognized Expert Moderator Expert
Clearly the last 2 lines of your output are in error, however since you have only shown us the definition of your class and not the implementation it's hard to say what the problem is.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: MetalOne |
last post by:
I am fairly new to Python.
Today, I wanted a priority queue.
I notice that Python does not have any sorted lists.
I know that I can call list.sort(), but that seems rather inefficient
to call every time an element is added.
I am wondering why there is not a module or modules containing many
common data structures. Has such a thing been decided against? Is it
that just nobody has done it? Has nobody else suggested the need for
it? ...
|
by: Innocence |
last post by:
Hi
I've been considering how to optimize map data structures for a tile
based Python game. However, since I'm a Python newbie I lack
experience with Pythons 'exotic' data types like lists and tuples, and
thus I'm unsure whether such types could solve my problem.
My basic thought is this: If my world map consists of 80% water and
20% land, then why reserve full data-structures for all the water
tiles?
|
by: John McCabe |
last post by:
Hi
I'm looking for something equivalent to the Data Structures and
Algorithms in Ada 95 books by Biedler and Feldman etc, but based
towards efficient C++ implementations.
Does anyone know of such a thing and could recommend one?
I'm particularly interested in coverage of binary search trees,
especially Red-Black, Splay and AVL, as well as hashing tables and
|
by: Bart Goeman |
last post by:
Hi,
I have a question about how to put redundant information in data
structures, initialized at compile time. This is often necessary
for performance reasons and can't be done at run time (data
structures are read only)
Ideally one should be able to put the redundant
information there automatically so no mistakes are possible, but in a lot
of case I see no way how to do it.
|
by: Ripal |
last post by:
Hi
I am facing problem descripbed as follows:
I have 4 enums, 10 structures and methods exposed via web method
It was working fine till the 7 structures after adding more 3
structures I am not able to create the instance of the 2 structures out
of 10.
| |
by: osp |
last post by:
hi to every one....
i just started out with c++ and i think i am
doing well.i use Robert Laffore to study. which book should i use for
data structures ? please help.
thank you
with regards
osp
|
by: satishmovva |
last post by:
Hi EveryOne....
Is there any one to pull out from the following problem. I have a small problem in my project, i want to store my clients images along with their data in my database (oracle 9i) through frontend (VB6.0). After saving the image and data of my clients again i want to get report from Crystal Reports which contains the client information along with his/her image. Please guide me to solve this problem ASAP.
Bye
Satish Kumar. M
|
by: ashok1 |
last post by:
Hi friend can anyone help me,
i need a code in .net(C#,ASP.Net) which is send data from one folder to another folder
i have bit idea it will solve by file handling but i m not able to solve it,
please if u have time or idea about this then send me ASAP
|
by: Mik0b0 |
last post by:
Hallo to everyone.
This fall I am going to start data structures as a part of C language
course. The problem is I could not find any satisfying tutorial about
structures in C. There are plenty of books about data structures in C+
+ etc., could anyone please recommend me such a C -specific book ? And
another question: are data structures (like stack, structure etc.)
used in C++ identical to those in C and is it possible to use C++
books to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |