0

Hi, I have to write a code which should allow to append and item to a list. That is FIFO Logic, First In First Out, but I cannot think of anything of that kind actually.

I mean, I should run the list until the last pointer, and then make it point to the new object. The only thing that comes in my mind is to write a LIFO list and then reverse it, but I bet there could be something better. University Exam tomorrow: sorry for all these questions!
Any Idea?

1
Contributor
2
Replies
5
Views
6 Years
Discussion Span
Last Post by HeartBalloon
0

Just for the noobs who will think like me:

struct Bingo{
	int iValue;
	Bingo *next;
	Bingo *prev;
};

void addValue(Bingo*&, int);
void printValue(Bingo*);

int main(){
	Bingo *bingo=NULL;
	addValue(bingo,2);
	printValue(bingo);
	addValue(bingo,6);
	printValue(bingo);
	addValue(bingo,54);
	printValue(bingo);
	addValue(bingo,90);
	printValue(bingo);
	addValue(bingo,12);
	printValue(bingo);
	addValue(bingo,23);
	printValue(bingo);
	addValue(bingo,78);
	printValue(bingo);
	std::cin.get();
	return 0;
}

void addValue(Bingo*& bingo, int iVal){
	Bingo *New,*temp;
	New = new Bingo;
	New->iValue = iVal;
	New->next=NULL;
	New->prev=NULL;
	if(bingo!=0){
		for(temp=bingo;temp->next!=NULL;temp=temp->next){}
		temp->next=New;
	}
	else{
		New->next = bingo;
		bingo = New;
	}
}

void printValue(Bingo *bingo){
	using namespace std;
	Bingo *temp;
	for(temp=bingo;temp!=NULL;temp=temp->next){
		cout << "Yo! Number: " << temp->iValue << endl;
	}
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.