HELP WITH DOUBLY LINKED LIST

I am to write a doubly linked list of at least 5 elements and output the string data attribute for each link. Then, write an add method and a delete method and test both routines.

This is for an online class and so, unfortunately, I have no clue how to do this.
Please help!!!
We're not going to do your homework for you.

If you google "doubly linked list", you will find all sorts of articles and videos that will help you. The wikipedia page is pretty good. I didn't watch any of the videos.

Start there for general information. If you have specific questions (after you have tried writing some code), then come back here and ask them. Unfortunately, "I have no clue how to do this" is not a question that is easily answered on this forum.
This is all I have so far. I do not know where to go from here.

#include "stdafx.h"
#include <iostream>
using namespace std;

class Node {
public:
Node*p;
int data;
void add(int input) {
data = input;
}
};



class LL {
public:

void add(int);
};


int main() {
LL l;
for (int j = 0; j < 5; j++) {
l.add((j + 1));
}
return 0;
}

void LL::add(int in) {
Node *head = NULL;
Node *cursor;
Node *n = new Node();
if (head == NULL) {
head = n;
cursor = head;
cursor->p = NULL;
}
else {
cursor->p = n;
cursor = cursor->p;
cursor->add(in);
}


}
First, please edit your post and use code tags.
http://www.cplusplus.com/articles/jEywvCM9/

You have posted a reasonable first attempt at a singly linked list. However, your Node needs another link in order to be part of a doubly linked list. Read the Wikipedia page description. When the pseudo-code mentions a reference, think pointer. When the pseudo-code mentions a record, think struct. The add function you are trying to write will be similar to the insertEnd function in the pseudo-code.

When you have a doubly linked list set up, you still have the following problems.

1. When you allocate the new node, you should not use the "()" if there are no arguments.

2. You never set the value of the data in the node. You need to call n-->add(in); at some point. Note: Consider changing Node to have a constructor taking an int argument rather than a default constructor. That will keep you from having to call add() after construction.

Topic archived. No new replies allowed.