CS301 Data Structures Assignment No. 01 Solution Fall 2013

Question : Marks 20
Write a C++ program to implement stack of marble plates. Your program should cover the following scenario. Suppose a waiter in a hotel is collecting plates from different tables and giving to dish washer. Dish washer is washing plates and throwing them into stack of plates. Some plates hit others very hard to break other (one below it) and/or itself.
You need to develop an application that will find how many plates are broken and plates from which tables are
broken. You will identify plates with unique numeric identity number. If plate’s identity number will fully dividable by
5, will break itself and one plate below it. While the number fully dividable by 7 will break only itself.

Program’s sample output is given below
Solution Guidelines:

1. First understand the code given in handouts about stack.
2. Get stack size from user to allocate space for stack dynamically.
3. Get identity number of plates that user want to enter into stack.
4. Get value one by one and push into stack.
5. While popping the values check what value is and show if it is broken.
6. Count all broken plates after popping all.
7. Don’t allow popping if stack is empty and pushing if stack is full.

Lectures Covered: This assignment covers Lecture # 1-6
Deadline: Your assignment must be uploaded / submitted on / before, 18 November, 2013

Solution:

[highlight]

#include <iostream>
#include <cstdlib>
using namespace std;
class Node{
	private: int object;
	Node *NextAddress,*preAddress;
	public:
		int getValue(){return object;};
		int NodeSet(int obj){object = obj;};
		void NodeNextAddr(Node *nextChain){NextAddress = nextChain;};
		Node *gNext(){return NextAddress;};
		Node *gPrev(){ return preAddress;}
		void setP(Node *pre){preAddress = pre;};			
};
class List{
	private: Node *current,*pointOfDiskTOP,*f1st; 
	public:List(){pointOfDiskTOP = new Node();pointOfDiskTOP->NodeNextAddr(NULL);pointOfDiskTOP->NodeSet(NULL);pointOfDiskTOP->setP(NULL);current = pointOfDiskTOP;	f1st = pointOfDiskTOP;};
		void mem(short int x){for(int i = 1; i <= x; i++){Node *newChain = new Node();newChain->setP(current);current->NodeNextAddr(newChain);newChain->NodeNextAddr(NULL);current = current->gNext();}};
		void thr(short int thr){f1st = f1st->gNext();f1st->NodeSet(thr);};
		int del(){int sam = current->getValue();Node *ref = current;current = current->gPrev();delete ref;return sam;};
		int emt(){if(current->gPrev() == NULL){return false;}if(current->gPrev() != NULL){return true;}};
		int ful(){if(pointOfDiskTOP->gNext() == NULL){return false;}if(pointOfDiskTOP->gNext() != NULL){pointOfDiskTOP = pointOfDiskTOP->gNext();return true;}}; 
};
int main(){
	List s;

	int size, SizeX,fault = 0;
	cout << " Set size of stack: ";
	cin >> size;s.mem(size);	
	cout << "\n How many values you want to enter into stack: ";
	cin  >> SizeX;	
	for(int i = 1; i <= SizeX; i++){
		short ID_OF_VAL;	
		cout << "\n Enter plate ID_OF_VAL number that you want to insert into stack: ";
		cin  >> ID_OF_VAL;	
		if(s.ful() == true){s.thr(ID_OF_VAL);}
		else{cout << "\n  Unable to insert " << ID_OF_VAL << ". List is full, can't insert more " << endl;break;}}	
	cout << "\n Following plates are fault" << endl;

	for (int i = 0; i < size; i++){ 

	// now use your brain for broken plates... etc

	cout << s.del() << endl;

	}

	system("pause");

}

[/highlight]

DOWNLOAD SOLUTION HERE
loading...