473,503 Members | 9,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ for data structures HELP SOLVE ASAP PLEASE

1 New Member
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(const SortedList& aSortedList);
~SortedList();
bool sortedIsEmpty() const;
int sortedGetLength() const;
void sortedInsert(int newItem, bool& success, bool& duplicate);
void sortedRemove(int 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
Oct 2 '06 #1
1 2043
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.
Oct 2 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1405
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...
11
2719
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...
28
2694
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...
10
4749
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...
1
1391
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...
3
2292
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 ...
2
2425
satishmovva
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)...
2
1032
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, ...
29
6313
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...
0
7207
marktang
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,...
0
7095
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...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
jinu1996
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...
1
7015
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...
0
5602
agi2029
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,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.