Hi, I have a problem, if any body can solve it, please help me please urgently.Thanks...

You are going to implement the doubly linked list (DLL). Your task is to simulate a workbench robot for wagon construction. Assume your DLL is a train with red, green and blue wagons. The commands of your menu are as follow:

IR Insert a red wagon from the head.
IB Insert a blue wagon from the head.
IG Insert a green wagon from the head.

RR Remove all red wagons from the train.
RB Remove all blue wagons from the train.
RG Remove all green wagons from the train.

MRB Get the first red wagon and move it to the next of the first blue wagon.
MRG Get the first red wagon and move it to the next of the first green wagon.
MBR Get the first blue wagon and move it to the next of the first red wagon.
MBG Get the first blue wagon and move it to the next of the first green wagon.
MGR Get the first green wagon and move it to the next of the first red wagon.
MGB Get the first green wagon and move it to the next of the first blue wagon.

DW Duplicate wagons.
E.g.: If your train is R-G-R-B-G,
after the DW comment it will become R-R-G-G-R-R-B-B-G-G

NR Print the number of red wagons.
NB Print the number of blue wagons.
NG Print the number of green wagons.

P Display the train in forward direction.

Q Quit & delete the DLL.

William Hemsworth commented: You must be joking.. +0
Salem commented: Piss off lamer -4

Recommended Answers

All 9 Replies

Are you serious?

It's bad enough people come here and ask for homework, but you actually named your post "Do my homework for me."? Read the forum rules, no one is going to do your homework for you.

Also, doubly linked lists are NOT reffered to as DLLs. A DLL is a dynamic link library (or dynamically linked library - not exactly sure).

do your own homework

>but you actually named your post "Do my homework for me."?
I named the thread when I split the post from an existing thread.

>Also, doubly linked lists are NOT reffered to as DLLs.
Actually, they are. Especially in books you'll see mention of DLL and SLL to refer to double and single linked lists.

>but you actually named your post "Do my homework for me."?
I named the thread when I split the post from an existing thread.

>Also, doubly linked lists are NOT reffered to as DLLs.
Actually, they are. Especially in books you'll see mention of DLL and SLL to refer to double and single linked lists.

Err...I see. Now I feel stupid...

Fuck you all!!!

>Fuck you all!!!
Right back at ya. Next time don't be a leech and you'll get a better response.

This isn't going to end well :P ..

[main.cpp]

#include "wagon_construction.h"
#include <iostream>
int main(int argc, char **argv) {
	wagon_construction train;
	train.insert_head(GREEN);
	train.insert_head(GREEN);
	train.insert_head(RED);
//	train.insert_head(BLUE);
//	train.remove_all(GREEN);
	train.move_first_to(GREEN,BLUE);
	std::cout << train.get_num(GREEN) << std::endl;
	train.duplicate();
	train.print();
	return 0;
}

[wagon_construction.h]

#ifndef WAGON_CONSTRUCTION_H_
#define WAGON_CONSTRUCTION_H_

#include <list>
using namespace std;

enum wagon{
	RED,
	BLUE,
	GREEN
};
class wagon_construction {
	list<wagon> train;
public:
	wagon_construction();
	void insert_head(wagon type);
	void remove_all(wagon type);
	void move_first_to(wagon from, wagon to);
	void duplicate();
	int get_num(wagon type);
	void print();
};

#endif /*WAGON_CONSTRUCTION_H_*/

[wagon_construction.cpp]

#include <algorithm>
#include <iostream>
#include "wagon_construction.h"

wagon_construction::wagon_construction() {

}

void wagon_construction::insert_head(wagon type) {
	train.push_front(type);
}

void wagon_construction::remove_all(wagon type) {
	train.remove(type);
}

void wagon_construction::move_first_to(wagon from, wagon to) {
	list<wagon>::iterator it;
	it = std::find(train.begin(), train.end(), from);
	if (it != train.end()) {
		train.erase(it);
	}
	else{
		return;
	}
	it = std::find(train.begin(), train.end(), to);
	if (it!= train.end()) {
		train.insert(it, from);
	} else {
		insert_head(from);
	}
}

void wagon_construction::duplicate() {
	list<wagon>::iterator it = train.begin();
	for(;it!=train.end();++it){
		train.insert(it, *it);
	}
}

int wagon_construction::get_num(wagon type) {
	return std::count(train.begin(), train.end(), type);;
}

void wagon_construction::print(){
	list<wagon>::iterator it = train.begin();
	for(;it!=train.end();++it){
		switch (*it) {
			case RED:
				cout << 'R';
				break;
			case BLUE:
				cout << 'B';
				break;
			case GREEN:
				cout << 'G';
			default:
				break;
		}
	}
}

This looked like fun, so here's a start. I wouldn't turn this in because your teacher will know it isn't your work.

#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::string;

// macro to calculate the number of elements in 
// an array.
#define SZARRAY(x) (sizeof(x)/sizeof(x[0]))

class wagon
{
public:
    typedef enum {unknown, red, green, blue} COLOR;
    wagon() 
    {
        m_color = unknown;
        next = prev = NULL;
    }
    wagon(COLOR color) 
    {
        m_color = color;
        next = prev = NULL;       
    }
    COLOR getColor() {return m_color;}
    wagon* next;
    wagon* prev;
private:
    COLOR m_color;
};

// menu stuff
//
// declare array item numbers
typedef enum {
    UNK,IR,IB,IG,RR,RB,RG,MRB,MRG,MBR,MBG,MGR,MGB,DW,NR,NB,NG,P,Q
} MENUITEMS;

// structure to hold menu information
struct menuitem
{
    string key;  // this is what you type
    string text; // explaination for key
    MENUITEMS item; // item number
};

// an array of menu items and associated numbers.
menuitem items[] = {
    {"IR ","Insert a red wagon from the head.\n", IR},
    {"IB ","Insert a blue wagon from the head.\n",IB},
    {"IG ","Insert a green wagon from the head.\n",IG},
    {"RR ","Remove all red wagons from the train.\n",RR},
    {"RB ","Remove all blue wagons from the train.\n",RB},
    {"RG ","Remove all green wagons from the train.\n",RG},
    {"MRB","Get the first red wagon and move it to the next of the first blue wagon.\n",MRB },
    {"MRG","Get the first red wagon and move it to the next of the first green wagon.\n", MRG},
    {"MBR","Get the first blue wagon and move it to the next of the first red wagon.\n", MBR},
    {"MBG","Get the first blue wagon and move it to the next of the first green wagon.\n", MBG},
    {"MGR","Get the first green wagon and move it to the next of the first red wagon.\n", MGR},
    {"MGB","Get the first green wagon and move it to the next of the first blue wagon.\n", MGB},
    {"DW ","Duplicate wagons.\n", DW},
    {"NR ","Print the number of red wagons. \n", NR},
    {"NB ","Print the number of blue wagons.\n", NB},
    {"NG ","Print the number of green wagons.\n", NG},
    {"P  ","Display the train in forward direction.\n", P},
    {"Q  ","Quit. \n", Q},

};


// global linked list of wagons
wagon* list = 0;

// display menu, get keyboard input, and translate to one of
// the enumeration types.
MENUITEMS menu()
{
    size_t i;
    MENUITEMS num = UNK;
    string choice;
    system("cls");
    for(i = 0; i < SZARRAY(items); ++i)
        cout << items[i].key << "--" << items[i].text;
    cout << "\nEnter choice\n";
    cin >> choice;
    // convert entry to all upper case
    std::transform(choice.begin(), choice.end(), choice.begin(),toupper);
    // force to 3 characters
    while(choice.length() < 3)
        choice += " ";
    // convert to enumeration type
    for(i = 0; i < SZARRAY(items); ++i)
    {
        if(choice == items[i].key )
        {
            num = items[i].item;
            break;
        }
    }

    return num;

}

// process the chosen menu item.  Returns false if Quit is chosen.
// Otherwise if not Quit then return true;
bool process(MENUITEMS item)
{
    bool cont = true;
    // TODO:  Add your code here to process each
    // of the coices.
    switch(item)
    {
    case IR:
        break;
    case IB:
        break;
    case IG:
        break;
    case RR:
        break;
    case RB:
        break;
    case RG:
        break;
    case MRB:
        break;
    case MRG:
        break;
    case  MBR:
        break;
    case MBG:
        break;
    case MGR:
        break;
    case MGB:
        break;
    case DW:
        break;
    case NR:
        break;
    case NG:
        break;
    case P:
        break;
    default:
        cont = false;
    }
    return cont;
}

int main(int argc, char* argv[])
{
    while(process(menu()))
        ;
	return 0;
}
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.