I'll start any posts I make the same way, just so people know that I'm NOT trying to get homework done...

Before I begin, let me start by saying that this is NOT homework. I am also not enrolled in a C++ class. I am simply trying to learn C++ as I have time. I am using random assigments/tutorials/whatever I can find on the net and shopping amazon for books. I'm not trying to become an awesome programmer, just trying to get a grasp on basic concepts (I'm a network admin and work with a bunch of programmers and just want to be able to understand things better)

A co worker of mine is a recent grad and took C++ a couple years ago and still had some of his old assignments I could look at but no longer had the code. I've worked several of the other problems he gave me the other day, but don't really even understand how to begin with this one..

Could someone take a look at this and either show me how this code would work or show me the code with a couple of things left out that I would need to fill in to make the code work?

Here's what he gave me:

Your are to implement a 'List' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. The list has no order, except for the order you insert or delete. The methods you are to implement are as given:

Constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'List listA(listB)' )
empty returns true or false if list is empty or not.
first positions you at the beginning of the list
last positions you at the end of a list.
prev places you at the next element in the list
next places you at the previous element in the list
getPos returns where you are in the list
setPos(int) places you in a certian position in the list
<< output method to print out the list.
insertBefore insert a new element before the current position
insertAfter insert a new element after the current position
getElement returns the one element that pos is pointing to

You are to implement the List class using an array. For now the array can be 20 in size.
You will need to use the 'ElementType' for typing your data.
You will need to use CAPACITY constant for the size of the array.

Thanks in advance. I'm going to try not to post here very frequently, but my last post and this post have been things that I have had trouble with. I'm really trying to get this all on my own, but it seems like I'll need some guidance with some of this. I'm sorry to bother people with what are probably really simple programs, but I'd rather not bother my coworkers with things like this as the only time I'm around any of them is at work.

I have one book by savitch (I can't recall the name of the book). It's been pretty helpful so far. I just want to see how this program would work but I can't seem to find much help in the book I have. If anybody can throw something together for this I would appreciate it.

Here is my advice:

Correct me if i am wrong.

class MYLIST
{
private:
int *front;
int *end;
int *current;
int size;
int elements_value;
int top;

public:
MYLIST()
{
 //constructor is here, initialize your private elements.
}
~MYLIST()
{}
void insert_front();
void insert_after();
bool empty();
void display();// this one used to print out the elements;
int getvalue();
void setvalue();
void setpos();
int getpos();
}

I know this is not the all but you could think about the class like that.
My situation is like your. I also is the beginner in c++ which did some webdeveloper job now(also a junior web developer). So i was thinking writing code is the best way to learn c++. You could read others code first and then try to change it. When you have some questions, just search online or read books or just post your questions in DANIWEB.
Good luck!

Actually, using "template" is a better way to do the LIST

Here is my advice:

Correct me if i am wrong.

class MYLIST
{
private:
int *front;
int *end;
int *current;
int size;
int elements_value;
int top;

public:
MYLIST()
{
 //constructor is here, initialize your private elements.
}
~MYLIST()
{}
void insert_front();
void insert_after();
bool empty();
void display();// this one used to print out the elements;
int getvalue();
void setvalue();
void setpos();
int getpos();
}

I know this is not the all but you could think about the class like that.
My situation is like your. I also is the beginner in c++ which did some webdeveloper job now(also a junior web developer). So i was thinking writing code is the best way to learn c++. You could read others code first and then try to change it. When you have some questions, just search online or read books or just post your questions in DANIWEB.
Good luck!

Sorry, but this makes absolutely no sense to me.

I just don't understand how this code is supposed to work...

Ok. I've got the "int main" part of the code, but I'm still drawing a blank on how I should set up this class to work...

Here's what I've got to use. Could someone show me what the class should look like to go with this??

int main()
{  
    List listA, listB;
    int const Max=20;
    int val;
   
    cout << "    Pos" << listA.getPos() << endl;
    cout << "    Empty? " << listA.empty() << endl;
    
    cout << "To Start: listA Empty " << listA << endl;
    
    for ( int k = 1; k <Max; k++ )
    {   
        listA.insertAfter(k);
        listA.next();
    }
    cout << "listA " << listA << endl;
    
    for ( int k = 1; k <Max; k++ )
    {   
        listB.insertBefore(k);
    }
    cout << "listB " << listB << endl;
    
    listB.clear();
    for ( int k = 0; k <Max/2; k++ )
    {   
        listB.last();
        listB.insertAfter(k);
        listB.first();
        listB.insertBefore(k);
    }    
    cout << "listB " << listB << endl;
   
    listB.clear();
    ElementType one = 1;
    listB.insertBefore( one );
    for ( int k = 1; k <Max; k++ )
    {   
        val = listB.getElement() * 2;
        listB.insertAfter(val);
        listB.next();
    }    
    cout << "listB " << listB << endl; 
    
    List listE;
    ElementType two = 2;
    listE.insertAfter(one);
    listE.next();
    listE.insertAfter(two);
    for ( int k = 2; k <Max; k++ )
    {    
        val = listE.getElement();
        listE.next();
        val = val + listE.getElement();
        listE.insertAfter(val);
    }    
    cout << "listE " << listE << endl; 
    
    List listC(listA), listD;
    listD = listB;
    
    cout << " listC " << listC << endl;
    
    cout << " listD " << listD << endl;
    listD.clear();  
    cout << " Cleared listD " << listD << endl;
    system("PAUSE");
    return 0;
}

This is looking more and more like homework. Where did you get the main from?

This is looking more and more like homework. Where did you get the main from?

If it were homework, I would simply ask the teacher for help instead of wasting my own time beating my head against a wall with this program.

I came up with the majority of that and had a little help from a co-worker. I'm trying not to take up too much of his time so I just tried to come up with something kind of basic for int main then he took what I wrote and added a few things and told me a couple of things. He said if he had time he would show me the class, but I don't want to bug him as he is always pretty busy. I try to do a little as I get a chance and see what I can come up with.

Please don't think I'm trying to get homework answers. The original problem was at one time a couple of years ago my co workers homework, but it is most definitely not mine. I'm leaning towards enrolling in a couple of programming classes sometime in the next couple of years, and would like to go in with a pretty decent understanding.

Sorry to bug you guys. I'm just trying to see what this should look like.

so you just want to make the LIST class working for the int main() driver?? and the member function??
is that true??
You want the LIST.h??

The original problem was at one time a couple of years ago my co workers homework, but it is most definitely not mine. I'm just trying to see what this should look like.

That explains why it looks like homework! Here's an example list.h for you:

// list.h

#ifndef _LIST_H_
#define _LIST_H_

class List {
    int pos;
    int size;
    ElementType elem [CAPACITY];
public:
    List();
    List (List &list);
    bool empty();
    void first();
    void last();
    void prev();
    void next();
    int getPos();
    void setPos (int pos_in);
    ElementType getElement();
    void insertBefore (ElementType e);
    void insertAfter (ElementType e);
    void clear();
    friend ostream& operator<< (ostream &os, List &list);
};

#endif

That explains why it looks like homework! Here's an example list.h for you:

// list.h

#ifndef _LIST_H_
#define _LIST_H_

class List {
    int pos;
    int size;
    ElementType elem [CAPACITY];
public:
    List();
    List (List &list);
    bool empty();
    void first();
    void last();
    void prev();
    void next();
    int getPos();
    void setPos (int pos_in);
    ElementType getElement();
    void insertBefore (ElementType e);
    void insertAfter (ElementType e);
    void clear();
    friend ostream& operator<< (ostream &os, List &list);
};

#endif

So I would just put the code in like this (where the green comments are)??:

// list.h

#ifndef _LIST_H_
#define _LIST_H_

class List {
    int pos;
    int size;
    ElementType elem [CAPACITY];
public:
    List();
    List (List &list);
    bool empty();
    void first();
       //code for first
    void last();
       //code for last
    void prev();
       //code for previous  
    void next();
    int getPos();
    void setPos (int pos_in);
    ElementType getElement();
    void insertBefore (ElementType e);
    void insertAfter (ElementType e);
    void clear();
    friend ostream& operator<< (ostream &os, List &list);
};

#endif

That's one possibility. Better yet, you can put the code in a separate file, list.cpp, which includes list.h. Also your main program file, main.cpp, would include list.h.

So part of list.cpp might look like this:

// list.cpp

#include "list.h"

bool List::empty() {
    return (size == 0);
}

void List::first() {
    if (size > 0) pos = 0;
}

void List::last() {
    if (size > 0) pos = size - 1;
}

And if you want to use templates (to do a list of int, of string, of any type), replace:

class List

by

template typename<TYPE> class List

In an another file (.cpp), implement your functions like this:

template typename <TYPE> void List::first()
{
}

And I hope for you that it's not an homework, because it's not by asking people for already written code that you will learn.

And if you want to use templates (to do a list of int, of string, of any type), replace:

class List

by

template typename<TYPE> class List

In an another file (.cpp), implement your functions like this:

template typename <TYPE> void List::first()
{
}

And I hope for you that it's not an homework, because it's not by asking people for already written code that you will learn.

IT'S NOT HOMEWORK.

I think some of this is a little over my head. Couldnt I just put the class in the same program without having to reference an .h file?

I'm crazy lost.

I think you can, but it's a bad practice. Declare your class in a .h file, implement it in a .cpp file, and use an another .cpp file for your main.

With templates, you can also put the declaration and the implementation into one .h file. One of my teachers, an experienced one, said this to me.

So I could technically run the program like this after the code is complete (which I have spent the last 30 minutes trying to do, but apparently I haven't learned enough because I can't for the life of me figure out how to put any of the code in to get anything to work right).

(I know it's bad practice, but I just wanna see this frickin thing run and work)

#include <iostream>
using namespace std;

class List
{
    int pos;
    int size;
    ElementType elem [CAPACITY];
public:
    List();
    List (List &list);
    bool empty();
		//code for empty
    void first();
		//code for first
    void last();
		//code for last
    void prev();
		//code for prev
    void next();
		//code for next
    int getPos();
		//code for getPos
    void setPos (int pos_in);
		//code for setPos
    ElementType getElement();
    void insertBefore (ElementType e);
		//code for insertBefore
    void insertAfter (ElementType e);
		//code for insertAfter
    void clear();
		//code for clear
    friend ostream& operator<< (ostream &os, List &list);
};

int main()
{  
    List listA, listB;
    int const Max=20;
    int val;
   
    cout << "    Pos" << listA.getPos() << endl;
    cout << "    Empty? " << listA.empty() << endl;
    
    cout << "To Start: listA Empty " << listA << endl;
    
    for ( int k = 1; k <Max; k++ )
    {   
        listA.insertAfter(k);
        listA.next();
    }
    cout << "listA " << listA << endl;
    
    for ( int k = 1; k <Max; k++ )
    {   
        listB.insertBefore(k);
    }
    cout << "listB " << listB << endl;
    
    listB.clear();
    for ( int k = 0; k <Max/2; k++ )
    {   
        listB.last();
        listB.insertAfter(k);
        listB.first();
        listB.insertBefore(k);
    }    
    cout << "listB " << listB << endl;
   
    listB.clear();
    ElementType one = 1;
    listB.insertBefore( one );
    for ( int k = 1; k <Max; k++ )
    {   
        val = listB.getElement() * 2;
        listB.insertAfter(val);
        listB.next();
    }    
    cout << "listB " << listB << endl; 
    
    List listE;
    ElementType two = 2;
    listE.insertAfter(one);
    listE.next();
    listE.insertAfter(two);
    for ( int k = 2; k <Max; k++ )
    {    
        val = listE.getElement();
        listE.next();
        val = val + listE.getElement();
        listE.insertAfter(val);
    }    
    cout << "listE " << listE << endl; 
    
    List listC(listA), listD;
    listD = listB;
    
    cout << " listC " << listC << endl;
    
    cout << " listD " << listD << endl;
    listD.clear();  
    cout << " Cleared listD " << listD << endl;
    system("PAUSE");
    return 0;
}

I'm not going to check back till later tonight or tomorrow and I'm gonna work on this, but if in the meantime someone could please show me how this should actually compile and run, I promise not to come back with programs like this. It's absolutely driving me crazy.

I think I'll go revisit some of the beginning things I have after I see how this works so I can work on my logic and code some simple projects. This may be a little advanced for me. Maybe if I see how it works, and then go back and review the basics and work my way back up to this then maybe it will make more sense.

One more tip, if you have no idea of how to implement the functions, just think of a picture of the list. Imagine how a list is represented in your head (a group of cells, an array). If you need it, just take a pen and a paper and draw the list state before, during and after an operation (one of the methods). This can help.

One way to implement a data structure is to put aside all thoughts of coding and draw the data structure when the operations are done on it. When you master COMPLETELY the structure, start coding. By doing this, you forgot all the complex things of a programming langage (C++ is good in that...) and you focus on the understanding of what you want to do.

It's good for you that some code has been provided. The code suggests that the list is represented by a primitive C++ array. But if, and only it, you thought of your list as a linked list, or anything else, change the representation, but consider this: will the methods be more efficient and more easy to code with that representation. For example, with an array, if I add an element and the array is full, what do I do? If I want to delete an element in the middle of the list, what are the consequences? (Move all elements at the right of the array by 1 position left...) Your representation must be choiced with the knowledge of the operations you will do on the data structure.

I hope that this post can help your understanding of data structures.

I spent several hours last night trying to understand this. I've come to the conclusion that I definitely need to go back to the basics. Could someone please show me how this is supposed to work. Not knowing how this is supposed to look and run is frustrating me beyond belief. After I see this, I'm going to put learning C++ on hold for a week or so and revisit the basics then.

I, for one, believe you that it's not homework. So here is a simple implementation using a C array (which I believe was your original requirement). It may not be entirely correct since I wrote it almost as fast as I can type.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.