Programming and Problem Solving with C++ Chapter 14 programming problem 1 The co
ID: 3674948 • Letter: P
Question
Programming and Problem Solving with C++ Chapter 14 programming problem 1
The code should not be one file but many like client.cpp and List.cpp, and have no virtual or template. Also uses pointer or else it wrong!.
Imagine you have been asked by a first grade teacher to create a hangman game for her students to help them spelling and word recognition. In this game of hangman you are first presented with a list of blanks representing unknown letters of a hidden word. you must then guess a letter to see if that letter exists in the word. if it does, the blank that corresponds to the guessed letter is then displayed along with blanks for the letters that have not been guessed yet. If you guess a previously guessed letter you get to try again. If you guess wrong you incrementally move toward being hung. You have eight bad guesses until you lose the game. To represent the different states of "being hang" we will use the values PLATFORM, HEAD, BODY, LEFT_ARM, RIGHT_ARIM,, LEFT_LEG, RIGHT_LEG and HUNG. To implement the game, you should use the Linked List ADT to represent the word being guessed. Each element in the list should be an object of type Slot that scores the letter of the word and a boolean that indicates if the letter is to be displayed or if a blank should be displayed. The Slot class should provide the appropriate observer and mutator methods for accessing and manipulating a slot. You should use a second linked list to represent the letters that have already been guessed and an enumeration to represent the states of the game. Your program should keep asking the user for another guess until either the entire word has been guessed or the state of game is HUNG. After each guess, the program should display the current state of the word to be guessed, the list of characters already guessed, and the state of the game.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class list
{
struct node
{
int data;
node *link;
}*p;
public:
void inslast(int);
void insbeg(int);
void insnext(int,int);
void delelement(int);
void delbeg();
void dellast();
void disp();
int seek(int);
list(){p=NULL;}
~list();
};
void list::inslast(int x)
{
node *q,*t;
if(p==NULL)
{
p=new node;
p->data=x;
p->link=NULL;
}
else
{
q=p;
while(q->link!=NULL)
q=q->link;
t=new node;
t->data=x;
t->link=NULL;
q->link=t;
}
cout<<"
Inserted successfully at the end..
";
disp();
}
void list:: insbeg(int x)
{
node *q;
q=p;
p=new node;
p->data=x;
p->link=q;
cout<<"
Inserted successfully at the begining..
";
disp();
}
void list::delelement(int x)
{
node *q,*r;
q=p;
if(q->data==x)
{
p=q->link;
delete q;
return;
}
r=q;
while(q!=NULL)
{
if(q->data==x)
{
r->link=q->link;
delete q;
return;
}
r=q;
q=q->link;
}
cout<<"
Element u entered "<<x<<" is not found..
";
}
void list:: delbeg()
{
cout<<"
The list before deletion:
";
disp();
node *q;
q=p;
if(q==NULL)
{
cout<<"
No data is present..
";
return;
}
p=q->link;
delete q;
return;
}
void list:: dellast()
{
cout<<"
The list before deletion:
";
disp();
node *q,*t;
q=p;
if(q==NULL)
{
cout<<"
There is no data in the list..
";
return;
}
if(q->link==NULL)
{
p=q->link;
delete q;
return;
}
while(q->link->link!=NULL)
q=q->link;
q->link=NULL;
return;
}
list::~list()
{
node *q;
if(p==NULL) return;
while(p!=NULL)
{
q=p->link;
delete p;
p=q;
}
}
void list::disp()
{
node *q;
q=p;
if(q==NULL)
{
cout<<"
No data is in the list..
";
return;
}
cout<<"
The items present in the list are :
";
while(q!=NULL)
{
cout<<" "<<q->data;
q=q->link;
}
}
void list :: insnext(int value,int position)
{
node *temp,*temp1;
temp=p;
if(temp1==NULL)
{
temp1= new node;
temp1->data=value;
temp1->link=NULL;
p=temp1;
return;
}
for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++)
{
if(i==(position-1))
{
temp1= new node;
temp1->data= value;
temp1->link=temp->link;
temp->link=temp1;
}
temp=temp->link;
}
//cout<<"
Inserted successfully at the position.."<<position;
disp();
}
int list::seek(int value)
{
node *temp;
temp=p;
int position=0;
while(temp!=NULL)
{
if(temp->data==value)
return position+1;
else
{
temp=temp->link;
position=position+1;
}
}
cout<<"
Element "<<value<<" not found";
return 0;
}
void main()
{
list l;
int ch,v,p,ps;
do
{
clrscr();
cout<<"
Operations on List..
";
cout<<"
1.Insertion
2.Deletion
3.Display
4.Seek
5.Exit";
cout<<"
Enter ur choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"
1.Insertion at begining
2.Insertion at the end
";
cout<<"3.Insertion after the mentioned position
";
cout<<"
Enter ur choice:";
cin>>ps;
cout<<"
Enter the value to insert:";
cin>>v;
switch(ps)
{
case 1:
l.insbeg(v);
break;
case 2:
l.inslast(v);
break;
case 3:
cout<<"
Enter the position to insert the value:";
cin>>p;
l.insnext(v,p);
break;
default:
cout<<"
The choice is invalid
";
return;
}
break;
case 2:
cout<<"
1.Delete the first element
2.Delete the last element";
cout<<"
3.Enter the element to delete from the list";
cout<<"
Enter ur choice:";
cin>>ps;
switch(ps)
{
case 1:
l.delbeg();
cout<<"
The list after deletion:
";l.disp();
break;
case 2:
l.dellast();
cout<<"
The list after deletion:
";l.disp();
break;
case 3:
l.disp();
cout<<"
Enter the element to delete : ";
cin>>v;
l.delelement(v);
cout<<"
The list after deletion:
";l.disp();
break;
default:
cout<<"
The option is invalid...
";
break;
}
break;
case 3:
l.disp();
break;
case 4:
l.disp();
cout<<"
Enter the element to search:";
cin>>v;
cout<<"
The position of the element "<< v<<" is "<<l.seek(v);
getch();
break;
case 5:
exit(1);
default:
cout<<"
The option is invalid...
";
return;
}
getch();
}while(ch!=5);
getch();
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.