Buscar

Exercicios listas

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 8 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 8 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

Prévia do material em texto

CS311 Yoshii HW3 Part 2 - Inherited Linked List (based on week 6)
=======================================================
DUE: Tuesday of week 8 at the beginning of class
TOTAL: 30 points Your score is:
Your NAME: Lucas Henrique Silva
Date Turned in:
------------------------------------------------------------
Purpose: To be able to extend the linked list class to allow additional functionality through inheritance
------------------------------------------------------------
============================
PRE-PROGRAMMING TASKS [6pts] 					Your score is:
============================
Review Questions: Must answer these first before you do the program.
Provide your answers to Inter1 – Inter6 found in Notes-6Bdoc. [1pt per answer]
Type here (in this file) both questions and answers.
*Inter1:* In publicly accessed Mom, data member M is private. Will it ever become
 accessible to 
me?<No>
derived classes? < No >
their clients through inheritance?? < No >
*Inter2:* In privately accessed Dad, data member D is public. Will it ever become
 accessible to 
me? <Yes>
derived classes? < No >
their clients through inheritance? < No >
*Inter3:* Clients at some level can access private a? < No >
 Clients at some level can access protected b? < No >
 Clients at some level can access protected y? < No >
 Whose clients can access public c? <C d1,d2> 
and public z? <Z d2>
*Inter4:* Out of b, c, y and z, which ones can be accessible to d2's clients?<C,Z>
 Which ones can be passed down?<all of them>
*Inter5:* What should be #included where?
<Dad.h>
*Inter6:* Which files should we compile and why? e.g. g++ ?????
<all .cpp, because the .h files are not needed because they are already included in the .cpp file>
==========================================================
PROGRAMMING: LinkedList class through Inheritance [24pts] 		Your score:
==========================================================
We will use the Linked List from HW3P1 to expand it into a Searchable List class by adding more functions.
(Make sure you make a copy of each file from HW3P1 before you begin)
HW3P2 will create slist.h and slist.C
Updating the Header Files (llist.h and slist.h)
Inherit all member functions from the Linked List class.
Note that the client of Searchable List will need to be able to call these functions. Pick the access type accordingly.
Searchable List class must have access to the data members of the base class.
But the clients should not access these data members.
Thus, data members should be in which part of the Linked List header??
Now add these 3 functions to slist.h and slist.C:
slist(); // constructor which is the same as the llist one
int search(el_t Key)
 search through the list to see if any node contains Key.
 If so, return its position (>=1). Otherwise, return 0.
void replace(el_t Elem, int I)
	go to the Ith node (I is between 1 and Count) and replace the element there with Elem. 
 If I was an illegal value, throw an exception.
The client program should:
add to front once (element 4)
add to rear 3 times (elements 6,7,8)
displayAll 				- 4 6 7 8
search for 6, report the result 	- found in pos 2
replace the 6 with 0 using the search result
search for 8 and report the result 	– found in pos 4
replace the 8 with 2 using the search result
displayAll 		- 4 0 7 2
search for 5 and report the result - not found
===================================================================
Q1) We did not create the slist desctructor. Did the llist destructor get called? [2pts]<answer> 
Q2) State of the Program[2pts] <answer here>
Does your program compile without errors?
List any bugs you are aware of, or state “No bugs”:
Submit:
this assignment sheet with your answers.
- llist.h updated
- slist.h commented well
- slist.C commented well
- the client file commented well
test result (one run)
Stapled in this order!! 
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??
End.
// =========================================================
//HW3P2 list
//Your name: Lucas Henrique Silva
//Complier: GCC
//File type: header file list.h
//================================================================
#include <iostream>
#include <cmath>
typedef int el_t;
struct Node{
 el_t elem;//element
 Node *Next;//next pointer
};
class LinkList{
 protected:
 Node *Front;
 Node *Rear;
 int count;
 public:
 LinkList();//constructor
 ~LinkList();//destructor
 bool isEmpty();//check to see if is empty
 void displayAll();//display all elements
 void addRear(el_t newNum);//add the newNum int the rear
 void deleteFront(el_t &oldNum);//delete the first element and put it in old num
 void addFront(el_t newNum);//add in the front of the list
 void deleteIth(int I,el_t &oldNum);//delete an element in the
 //desired position and put it in oldNum
 void addBeforeIth(int I, el_t &newNum);//add the newNum in the desired
 //position
};
// =========================================================
//HW3P2 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();
 //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);
};
// =========================================================
//HW3P2 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;
 for(int k=1; k<=count;k++){
 if(P->elem==elem){
 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)){
 cout << "This position is out of limits\n";
 }else{
 Node *P;
 P=Front;
 for(int k=1; k<I;k++){
 P=P->Next;
 }
 P->elem=elem;
 }
 }
// =========================================================
//HW3P2 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;
int main(){
 slist linkList;
 el_t elem;
 int pos;
 linkList.displayAll();
 linkList.addFront(4);
 linkList.addRear(6);
 linkList.addRear(7);
 linkList.addRear(8);
 linkList.displayAll();
 cout << "Looking for number 6...\n";
 pos=linkList.search(6);
 cout << "6 is in position: "<< pos << endl;
 linkList.swap(0,pos);
 linkList.displayAll();
 cout << "Looking for number 8...\n";
 pos=linkList.search(8);
 cout << "8 is in position: "<< pos << endl;
 linkList.swap(2,pos);
 linkList.displayAll();
 cout << "Looking for number 5...\n";
 pos=linkList.search(5);
 if(pos==0){
 cout<<pos << " NOT FOUND\n";
 }else{
 cout << "5 is in position: "<< pos << endl;
 }
}
Script started on Mon 13 Oct 2014 10:07:45 PM PDT
�]0;henri011@empress:~/CS311/HW_6/List1.1
[henri011@empress List1.1]$ g++ list.cpp cliente.cpp������������������������./a.out�[K�������script result.txt�����������������ls�[K��g++ list.cpp slist.cpp cliente.cpp 
�]0;henri011@empress:~/CS311/HW_6/List1.1
[henri011@empress List1.1]$ g++ list.cpp slist.cpp cliente.cpp �����������������������[11Pcliente.cpp������������������������./a.out�[K
The Link List is EMPTY
 Elements: 4 6 7 8 
Looking for number 6...
6 is in position: 2
 Elements: 4 0 7 8 
Looking for number 8...
8 is in position: 4
 Elements: 4 0 7 2 
Looking for number 5...
0 NOT FOUND
�]0;henri011@empress:~/CS311/HW_6/List1.1
[henri011@empress List1.1]$ exit
exit
Script done on Mon 13 Oct 2014 10:08:02 PM PDT

Outros materiais