472,985 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 software developers and data experts.

Need help implementing an ordered-list:storage structure is linked-list using 2arrays

I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one to use as an index to the freearray cells).
Here is the exact problem specifications:
Create an ordered list template class named OLType to implement an ordered list with operations of insert, remove, print, empty, full, size. The storage structure is to be a linked-list emulation using dynamic arrays. It will require a minimum of two arrays, one for the data and one for the 'next' links. The 'free' list can be incorporated into the 'next' links array if done correctly. The 'public' portion of your class file must be as follows:
public:
OLType(size_t n=100);
~OLType();
void insert(const T& item);
void remove(const T& item);
void print() const;
size_t size() const;
bool empty() const;
bool full() const;

Here is the code I have so far:

#ifndef OLType_H
#define OLType_H

#include <iostream>

using namespace std;

template <class T>
class OLType {
public:
OLType(size_t n=100);
~OLType();
void insert(const T& item);
void remove(const T& item);
void print() const;
size_t size() const;
bool empty() const;
bool full() const;
private:
size_t MAX_LENGTH;
size_t n; // max number that can be stored in the list
int count; // number of elements in the list
T *data; // pointer to the array that holds the list elements
};

template <class T>
OLType<T>::OLType(size_t n) {
MAX_LENGTH = n;
count = 0;
data = new T[MAX_LENGTH];
}

template <class T>
OLType<T>::~OLType() {
delete [] data;
}

template <class T>
void OLType<T>::insert(const T& item) {
data[count] = item;
count++;
}

template <class T>
void OLType<T>::remove(const T& item) {
int i = 0;
while (i<count && item != data[i])
i++;
if (i<count) {
data[i] = data[count-1];
count--;
}
}

template <class T>
void OLType<T>::print() const {
int i;
for(i=0; i<count; ++i)
cout << data[i] << " ";
}

template <class T>
size_t OLType<T>::size() const {
return count;
}

template <class T>
bool OLType<T>::empty() const {
return (count == 0);
}

template <class T>
bool OLType<T>::full() const {
return (count == MAX_LENGTH);
}

#endif

Any help with solving this problem would be greatly appreciated. I am not that strong of a programmer and just cannot seem to find the correct way to do this problem. Thank you.
Sep 12 '06 #1
1 3280
Banfa
9,065 Expert Mod 8TB
Well I personally would not normally implement a linked list as 2 arrays, I would use a single array of structures.

However it is possible.

You next step is to set up the array of "next" pointers. However rather than pointers I think these will need to be an integer type. This array will need the same number of entries as data. Assuming that this array of ints is called nextIndex then for any given current index IX

data[IX] = the value at this index
nextIndex[IX] = the next index in the list

You will need 3 other things

1. A special value (#defined or static const) indicating the end of the list

2. A data member recording the first index in the list

3. A data member recording the first index in the free list

Since any given index can only be used (exclusive) or not used (i.e. can't be used and not used then the same array of indexes can be used to hold the list of free entries. As I have stated in 3 above all you need is a new starting point.
Sep 12 '06 #2

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

Similar topics

6
by: Kevin Altis | last post by:
Does anyone have experience running Python from a USB storage device? Whether it is the need for doing a demo of your application, doing a bit of consulting, or just showing off Python, it would be...
1
by: adeleinandjeremy | last post by:
I am taking a second programming course in Java and am currently attempting to apply what I have learned using Python instead. One thing that is puzzling me is how to use an iterator. I am...
2
by: JTrigger | last post by:
I am rather new to the XML and XSD world and was wondering what the code would look like to write the following XSD items as classes in C# with all the proper XML attributes to make them...
2
by: fumanchu | last post by:
I need to insert records into a Sybase database via a linked table. I have a valid, tested DSN ODBC connection to the Sybase database named 'SBA9link'. I also have created a link to a table in...
10
by: Duncan M Gunn | last post by:
Hi, I need to store the following matrix of values: T U V A 2 1 1 B 2 - - C - - 2 Where (-) is an empty cell.
3
by: Dongsheng Ruan | last post by:
Not quite related with Python. But my Data Structure course is experiemented on python and there is no data structure group, So I have to post here: Write a procedure (in pseudocode!) to increase...
1
by: hmoundekar | last post by:
actually i want to generate a tree structure on client side, the application is simple web based, and need to display the data extracted from the database to client in tree structure. but the problem...
1
by: a Wellner | last post by:
I have a Database stored on the server, and a replica on a laptop, used for data collection in the field. The laptop is only connected to the network during synchronization. I am linking to tables...
3
by: dkultasev | last post by:
Hello, I have database, which contains the list of laptop companies and some information about them. Some laptop models, have series, sub series, family's and so on. for example. Acer Aspire...
2
by: e2point | last post by:
i need to create a storage module which can store any thing the user wants. I want it to handle almost any storage requirement, so that clients can define their own key classes derived from the...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.