Buscar

HW3P3_oop_final

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes
Você viu 3, do total de 11 páginas

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes
Você viu 6, do total de 11 páginas

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes
Você viu 9, do total de 11 páginas

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você também pode ser Premium ajudando estudantes

Prévia do material em texto

CS311 Yoshii HW3 Part 3 - Linked List Extended (based on Week 7)
===================================================================
DUE: Week 9 Tuesday at the beginning of the class
TOTAL: 30 pts Your score is:
*Your Name:Lucas Henrique Silva
*Date Turned In: Oct/21/2014
*Did you do the extra credit?[5pts] <Yes>
---------------------------------------------------------------
Purpose: To be able to implement and use an overloaded operator and a copy constructor to complete the linked list class.
---------------------------------------------------------------
PROGRAMMING: Finalizing Linked List [30pts] 
===================================
Finally, add the following to your Searchable List (slist) class from HW3P2.
Pseudo code was already provided for you.
overload operator = 		(see Week7 Notes-7A notes)
create a copy constructor 		 (see Week7 Notes-7A notes)
Client:
Your client file will have two functions: Main() and CopyTest
Main will
Create a 5 element list with 1,2,3,4,5 (L1)
Pass the list to a client function called CopyTest to 
test your copy constructor.
Copytest will receive the list passed by value from main() and
Simply 1) add a node to its rear with 6 in it (should not affect the original)
 2) display it (6 elements 1,2,3,4,5,6)
Display L1 	(this should still be a 5 element list)
Do L1 = L1;
Display L1 	(this should still be 1 2 3 4 5)
Create a 4 element list L2 with 7,8,9,10.
Display L2
Do L2 = L1; 	(L2 becomes 5 elements 1,2,3,4,5)
Display L2.
Remove a rear node from L1. (This should not affect L2).
Display L1. 	(L1 is 1,2,3,4)
Display L1 again. (4 elements)
Display L2 again. (still 5 elements 1,2,3,4,5)
It is very important to complete this linked list class and make sure the = and copy constructor work perfectly. You will use this to implement HW6 and HW7 graphs.
Q) State of the program [2pts] <answer here>
Does your program compile without errors?
<No ERRORS>
List any bugs you are aware of, or state “No bugs”:
<NO BUGS>
Submit:	(stapled in this order!! )
this assignment sheet with your answers
slist.h
slist.C
the client file
test results (one run)
Whether working or not, test result must include the lines for compiling your files or we will not grade our program i.e. 0 points for the program.
Did you check your comments and style against CS311 How To Comment.doc??
EXTRA credit [5pts] – highly recommended to do
Overload operator == by adding it to the above slist.h and slist.C
Must work perfectly to receive any points. No partial credit. So, if it does not work perfectly, do not submit it.
Create a separate client file to test ==.
Test cases – right before displaying the result of each comparison, must display the following description of the case:
L1 is empty and L2 is empty
L1 is empty and L2 has 2 elements
L1 has 2 elements and L2 is empty
L1 has 1,2,3 and L2 has 1,2,3
L1 has 1,2,3 and L2 has 1,2
L1 has 1,2,3 and L2 has 1,2,3,4
L1 has 1,2,3 and L2 has 1,2,4
Attach at the end of your submission:
Client for extra credit (you can have the == in the original header and implementation)
Test results for extra credit.
// =========================================================
//HW3P3 list
//Your name: Lucas Henrique Silva
//Complier: GCC
//File type: header file slist.h
//================================================================
#include "list.h"
class slist : public LinkList{
 public:
 //constructor of the class slist
 slist();
 slist(const slist& Original);
 //will search one element and return the position
 int search(el_t elem);
 //will locate the element and change it to a new one
 void swap(el_t elem, int I);
 //overload of the operator '='
 slist& operator=(const slist& OtherOne);
 //Function to test the constructor
 void CopyTest(const slist OtherOne);
 //overload of the operator '=='
 bool operator==(const slist& L1);
};
// =========================================================
//HW3P3 list
//Your name: Lucas Henrique Silva
//Complier: GCC
//File type: implementation file file slist.cpp
//================================================================
#include "slist.h"
#include <iostream>
using namespace std;
 //constructor
 slist :: slist(){
 Front = NULL;
 Rear = NULL;
 count = 0;
 }
 /**This function will search one element in the linklist
 will receive the element which you want to search and
 return the position if the element is in the list,
 otherwise will return 0 which means that the element
 is not in the list
 */
 int slist :: search(el_t elem){
 int found=0;
 Node *P;
 P=Front;
 //go though the list looking for the element
 for(int k=1; k<=count;k++){
 if(P->elem==elem){
 //if found the value of found will change
 found =k;
 return found;
 }
 P=P->Next;
 }
 return found;
 }
 /**This function will swap an element in to another
 will receive the position that you want to change to
 the new element
 */
 void slist :: swap(el_t elem, int I){
 if((I<1) || (I>count)){//out of limits
 cout << "This position is out of limits\n";
 }else{
 Node *P;
 P=Front;
 //look for the element and change it
 for(int k=1; k<I;k++){
 P=P->Next;
 }
 P->elem=elem;
 }
 }
 bool slist::operator==(const slist& L1){
 int sizeL1 = L1.count;//size receive the size of the list passed
 //by reference
 int sizeL2 = this->count;//size receive the size of the list
 //if the both list are empty they are equal
 if((this->Front== NULL) && (L1.Front==NULL)){
 return true;
 }
 //if the size is different so they are no equal
 if(sizeL1!=sizeL2){
 return false;
 }else{//other cases
 Node *P;
 Node *Q;
 P=L1.Front;
 Q=this->Front;
 //if at any time the elements are different so the list is different
 for(int i=0;i<sizeL1;i++){//go through the list checking
 if(P->elem != Q->elem){
 return false;
 }
 P=P->Next;
 Q=Q->Next;
 }
 }
 //if all the elements are equal, so, the both lists are equal
 return true;
 }
 /**Overloading the operator '='
 with this function the = this way it will also can be
 used with objects ( in specific lists).
 */
 slist& slist::operator=(const slist& OtherOne){
 if(this!=&OtherOne){//check to see if the list is not the same
 while(!this->isEmpty()){//if the list is no empty delete all
 //elements
 el_t elem;
 this->deleteFront(elem);
 }
 Node *P;
 P=OtherOne.Front;
 while(P!=NULL){//while still elements in the list it will copy
 this->addRear(P->elem);
 P=P->Next;
 }
 }
 return *this;//returns the new list to the list that will receive
 }
 /**
 Another constructor
 This constructor will receive an object and will
 initialize the new list withall elements in the
 list passed as a parameter
 */
 slist::slist(const slist& Original){
 slist();//utilize the old constructor to eliminate unnecessary code
 int size = Original.count;//size receive the size of the list passed
 //by reference
 Node *P;
 P=Original.Front;
 for(int i=0;i<size;i++){//copy every element of the list passed to the new one
 this->addRear(P->elem);
 P=P->Next;
 }
 }
// =========================================================
//HW3P3 list
//Your name: Lucas Henrique Silva
//Complier: GCC
//File type: client file client.cpp
//================================================================
#include <iostream>
#include <string>
#include <cstdlib>
#include "slist.h"
using namespace std;
/** Function created to test the constructor
The Function will receive an object and will be used
by the constructor to create a list with the same
elements in the list passed by parameter
*/
void CopyTest(slist OtherOne){
 slist List(OtherOne);//List being initialized
 List.addRear(6);//add 6
 List.displayAll();// show all elements plus 6
}
int main(){
 slist L1,Copy;
 el_t elem;
 int pos;
 //adding elements to L1
 L1.addFront(1);
 L1.addRear(2);
 L1.addRear(3);
 L1.addRear(4);
 L1.addRear(5);
 CopyTest(L1);//testing the function
 L1.displayAll();
 L1=L1;//testint operator '='
 L1.displayAll();
 slist L2;
 //adding elements in L2
 L2.addFront(7);
 L2.addRear(8);
 L2.addRear(9);
 L2.addRear(10);
 L2.displayAll();
 L2=L1;//L2 receives L2
 L2.displayAll();//L2 will have the same elements in L1
 L1.deleteFront(elem);//Wont affect L2
 L1.displayAll();
 L1.displayAll();//item 12 no point of display L1 again
 L2.displayAll();//prove that L2 still intact
}
EXTRA CREDIT CLIENT
// =========================================================
//HW3P3 list
//Your name: Lucas Henrique Silva
//Complier: GCC
//File type: client file client.cpp ( EXTRA CREDIT)
//================================================================
#include <iostream>
#include <string>
#include <cstdlib>
#include "slist.h"
using namespace std;
int main(){
 slist L1,L2;
 el_t elem;
 int pos;
 if(L2==L1){
 cout << "True\n";
 }else{
 cout << "False\n";
 }
 //adding elements to L1
 L2.addFront(1);
 L2.addRear(2);
 if(L2==L1){
 cout << "True\n";
 }else{
 cout << "False\n";
 }
 L1.addRear(1);
 L1.addRear(2);
 L2.deleteFront(elem);
 L2.deleteFront(elem);
 if(L2==L1){
 cout << "True\n";
 }else{
 cout << "False\n";
 }
 L1.addRear(3);
 L2.addRear(1);
 L2.addRear(2);
 L2.addRear(3);
 if(L2==L1){
 cout << "True\n";
 }else{
 cout << "False\n";
 }
 L2.deleteIth(3,elem);
 if(L2==L1){
 cout << "True\n";
 }else{
 cout << "False\n";
 }
 L2.addRear(3);
 L2.addRear(4);
 if(L2==L1){
 cout << "True\n";
 }else{
 cout << "False\n";
 }
 L2.deleteIth(3,elem);
 if(L2==L1){
 cout << "True\n";
 }else{
 cout << "False\n";
 }
}
Script started on Mon 20 Oct 2014 10:17:29 PM PDT
�]0;henri011@empress:~/CS311/HW_7/List
[henri011@empress List]$ ./a.out
 Elements: 1 2 3 4 5 6 
 Elements: 1 2 3 4 5 
 Elements: 1 2 3 4 5 
 Elements: 7 8 9 10 
 Elements: 1 2 3 4 5 
 Elements: 2 3 4 5 
 Elements: 2 3 4 5 
 Elements: 1 2 3 4 5 
�]0;henri011@empress:~/CS311/HW_7/List
[henri011@empress List]$ exit
exit
Script done on Mon 20 Oct 2014 10:17:43 PM PDT
EXTRA CREDIT 
Script started on Mon 20 Oct 2014 10:30:12 PM PDT
�]0;henri011@empress:~/CS311/HW_7/ List_Extra_Credit 
[henri011@empress List_Extra_Credit]$ ./a.out
True
False
False
True
False
False
False
�]0;henri011@empress:~/CS311/HW_7/ List_Extra_Credit 
[henri011@empress List_Extra_Credit]$ exit
exit
Script done on Mon 20 Oct 2014 10:30:43 PM PDT

Outros materiais