ADT IteratedList - represented using a DLL

Hey! I need help with my assigment and i don't know where to start with the implementation part, can somebody give me any resources or advice?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
IteratedList.h
#pragma once
//DO NOT INCLUDE LISTITERATOR//DO NOT CHANGE THIS PART
class ListIterator;
#define NULL_TELEM -11111;
typedef int TElem;class IteratedList {
private:
//TODO - Representation
//DO NOT CHANGE THIS PART
friend class ListIterator;
public: // constructor
IteratedList (); // returns the number of elements from the list
int size() const; // verifies if the list is empty
bool isEmpty() const; // returns the first position from the list
ListIterator first() const; // returns the element from the given position
//throws an exception if the position is not valid
TElem getElement(ListIterator pos) const; // changes the element from the current position to the given one.
//returns the old value from the position
//throws exception if the position is not valid
TElem setElement(ListIterator pos, TElem e); // adds a new element to the end of the list
void addToEnd(TElem e);
//adds a new element to the beginning of the list
void addToBeginning(TElem e); // // adds a new element after the current element from the iterator
//after addition, pos points to the newly added element
//throws an exception if pos is not valid
void addToPosition(ListIterator& pos, TElem e); // removes the element from position pos
//returns the removed element
//after removal pos is positioned on the next element (the one after the removed one) or it is invalid if the last element was removed
//throws an exception if pos is not valid
TElem remove(ListIterator& pos); // searches for the first occurrance of an element
//returns an iterator that points to the element, if it appear in the list, or an invalid iterator if the element is not in the list
ListIterator search(TElem e) const; //destructor ~IteratedList();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
IteratedList.cpp
#include <exception>
#include "ListIterator.h"
#include "IteratedList.h"IteratedList::IteratedList() {
//TODO - Implementation
}int IteratedList::size() const {
//TODO - Implementation
return 0;
}bool IteratedList::isEmpty() const {
//TODO - Implementation
return false;
}ListIterator IteratedList::first() const {
return ListIterator(*this);
}TElem IteratedList::getElement(ListIterator pos) const {
//TODO - Implementation
return NULL_TELEM;
}TElem IteratedList::remove(ListIterator& pos) {
//TODO - Implementation
return NULL_TELEM;
}ListIterator IteratedList::search(TElem e) const{
//TODO - Implementation
return ListIterator(*this);
}TElem IteratedList::setElement(ListIterator pos, TElem e) {
//TODO - Implementation
return NULL_TELEM;
}void IteratedList::addToPosition(ListIterator& pos, TElem e) {
//TODO - Implementation
}void IteratedList::addToEnd(TElem e) {
//TODO - Implementation
}IteratedList::~IteratedList() {
//TODO - Implementation
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ListIterator.h
#pragma once
#include "IteratedList.h"//DO NOT CHANGE THIS PART
class IteratedList;
typedef int TElem;class ListIterator{
friend class IteratedList;
private:
//TODO - Representation //DO NOT CHANGE THIS PART
const IteratedList& list;
ListIterator(const IteratedList& lista);
public:
void first();
void next();
bool valid() const;
TElem getCurrent() const;};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ListIterator.cpp
#include "ListIterator.h"
#include "IteratedList.h"
#include <exception>ListIterator::ListIterator(const IteratedList& list) : list(list) {
//TODO - Implementation
}void ListIterator::first() {
//TODO - Implementation
}void ListIterator::next() {
//TODO - Implementation
}bool ListIterator::valid() const {
//TODO - Implementation
return false;
}TElem ListIterator::getCurrent() const {
//TODO - Implementation
return NULL_TELEM;
}


Last edited on
break it down.
the first thing you probably want to do with any new class is write its constructor(s) so you can create an object of that type and give it a quick 'duh' test that it compiles and works.
then fill in the most basic stuff first: a list is no good if you can't add an item to it, and adding an item to is is no good if you can't get to that and print it back out...

so try starting with those 3 things, a constructor, an add of some sort (top or bottom, doesn't matter which one you do first, top is easiest and most efficient if its a linked list base) and a getter of some flavor that can retrieve what you put in.

then maybe work on getting its current size, ... just pick something, add it, test it, repeat one by one. If you pay attention, some of the methods can use each other, eg is empty and size are redundant: if size is 0, its empty... right? so is empty is literally one line: return !size() since 0 size is empty and not zero is true...

assuming this is a homework classic linked list, make sure you understand the pointer requirement.
a typical linked list is
struct llist
{
llist*next; // this pointer to its own type is what makes it work.
///other stuff
};

this is a hefty assignment. That means you should have a fair bit of background to do all the things it asks for. If you have been able to do your work up to now, give it a shot and ask specific questions if you get stuck. If you have not been able to do your work up to this point, this may be the one that finally highlights that you need to back up and learn the previous material.
Last edited on
Thank youu! This is actually helpful and I will try to put it to use.

The code you posted looks like it's been word-wrapped. Is your copy correct? I'd hate to try to implement this based on what's posted here. For example:
1
2
3
public: // constructor
IteratedList (); // returns the number of elements from the list
int size() const; // verifies if the list is empty 

probably should be
1
2
3
4
5
6
public:
// constructor
IteratedList ();

// returns the number of elements from the list
int size() const;


In IteratedList.cpp, you have a TODO implementation for IteratedList's destructor, but no destructor has been declared. You may need to ask the prof about this since the the destructor would fall into the public section labelled "DO NOT CHANGE"
Topic archived. No new replies allowed.