Faculty of Computer Science & Information Technology University Tun Hussein Onn Malaysia
Faculty of Computer Science & Information Technology University Tun Hussein Onn Malaysia
SEM II 2010/2011
OBJECT-ORIENTED PROGRAMMING
LAB 4
Lab 4/Tutorial 2
Bit 2063- object-oriented programming
BIT 2063
Object-Oriented Programming
4. Modify Code 5.2.1 so that the object is added at the end of the list.
/Filename: Hamtastd.cpp
//This program illustrate attributes of class Student
//using simple data structure
#include <iostream.h>
class Student
{
private:
struct Data
{
char Name[25];
char Course[30];
int Result;
}stdata;
public:
void SetData();
void GetData();
}; //class Student
void Student::SetData()
{
cout <<"Enter student name: ";
cin >>stdata.Name;
cout <<"Enter student course: ";
cin >>stdata.Course;
cout <<"Enter student Result:";
cin >>stdata.Result;
}; //method SetData
void Student::GetData()
{
cout <<"\nStudent name: "<<stdata.Name;
cout <<"\n";
cout <<"\nStudent course: "<<stdata.Course;
cout <<"\n";
cout <<"\nStudent result: "<<stdata.Result;
cout <<"\n";
}; //method GetData
//Filename:atikahlist.cpp
#include <iostream.h>
#include "hamtastd.cpp"
class StudentList
private:
struct ListNode
Student astudent;
ListNode *next;
};
ListNode *head;
public:
StudentList();
~StudentList();
int IsEmpty();
void Remove();
void DisplayList();
};
StudentList::StudentList()
head=NULL;
};
StudentList::~StudentList()
while(IsEmpty()!=0)
Remove();
if(IsEmpty()==0)
};
int StudentList::IsEmpty()
if(head==NULL)
return 0;
else
return 1;
};
if(newPtr==NULL)
else
newPtr->astudent=newstudent;
newPtr->next=head;
head=newPtr;
};
void StudentList::Remove()
if(IsEmpty()==0)
else
ListNode *temp=head;
head=head->next;
temp->next=NULL;
delete temp;
};
void StudentList::DisplayList()
ListNode *cur=head;
if(IsEmpty()==0)
else
while(cur!=NULL)
cur->astudent.GetData();
cur=cur->next;
cout <<"\n";
} //end if
//Filename:studentlistdr.cpp
#include <iostream.h>
#include "atikahlist.cpp"
int main()
int i;
Student newstudent;
StudentList alist;
cout <<"Inserting"<<size<<"objects\n";
for(i=0;i<size;i++)
newstudent.SetData();
alist.Add(newstudent);
alist.DisplayList();
cout <<"deleting\n";
alist.Remove();
alist.DisplayList();
return 0;
};
Output:
5. Test your program with the same data so your output is displayed as in Diagram 1.
Diagram 1
Input:
//Filename:student.cpp
#include<iostream.h>
class Student{
private:
struct Data{
char Name[25];
char Course[30];
int Result;
} stdata;
public:
void SetData();
void GetData();
};//class student
void Student::SetData(){
cin>>stdata.Name;
cin>>stdata.Course;
cin>>stdata.Result;
}; //method SetData
void Student::GetData(){
cout<<"\n";
cout<<"\n";
cout<<"\n";
}; //method GetData
//Filename:stdListDr.cpp
#include<iostream.h>
#include"Student.cpp"
class StudentList{
private:
struct ListNode{
Student astudent;
ListNode *next;
};
ListNode *head;
public:
StudentList();
~StudentList();
// studentlist operations
int IsEmpty();
void Remove();
void DisplayList();
}; //class StudentList
StudentList::StudentList(){
head = NULL;
};
StudentList::~StudentList(){
while(IsEmpty()!=0)
Remove();
if(IsEmpty()==0)
};
int StudentList::IsEmpty(){
if(head==NULL)
return 0;
else
return 1;
};
if(newPtr==NULL)
else
{ //assign successful
newPtr->astudent=newstudent;
newPtr->next=head;
head=newPtr;
};
void StudentList::Remove(){
if(IsEmpty()==0)
else
ListNode *temp=head;
head=head->next;
temp->next=NULL;
delete temp;
};
void StudentList::DisplayList(){
ListNode *cur=head;
if(IsEmpty()==0)
else
while(cur!=NULL){
cur->astudent.GetData();
cur=cur->next;
cout<<"\n";
};
//Filename:StListDr.cpp
#include<iostream.h>
#include"StdListDr.cpp"
int main(){
int i;
Student newstudent;
StudentList alist;
cout<<"Inserting"<<size<<"objects\n";
for(i=0;i<size;i++){
alist.DisplayList();
cout<<"deleting\n";
alist.Remove();
alist.DisplayList();
return 0;
};
Output:
6. Try to delete data Cristina from your linked list. Then display the linked list after deletion
of Cristina data. (Hint: You need to modify your Remove function so it is able to find the
correct data and delete the data, and link the pointers back. Careful programming is
needed to avoid dangling pointers.
input:
#include <iostream.h>
class Student{
public:
struct Data {
char Name[20];
int Result;
} stdata;
public:
void SetData();
void GetData();
};
void Student::SetData() {
cin>>stdata.Name
cin>>stdata.Course;
cin>>stdata.Result;
cout<<"\n";
};
void Student::GetData() {
cout<<"\n\nName:"<<stdata.Name;
cout<<"\nCourse:"<<stdata.Course;
cout<<"\nResult:"<<stdata.Result;
cout<<"\n";
};
#include <iostream.h>
#include <string.h>
#include "ci090094student.cpp"
class StudentList {
public:
struct ListNode{
Student astudent;
ListNode *next;
};
ListNode *head,*tail;
public:
StudentList();
~StudentList();
int IsEmpty();
void Remove();
void DisplayList();
};
StudentList::StudentList() {
head=tail=NULL; };
StudentList::~StudentList(){
while(IsEmpty()!=0)
Remove();
if(IsEmpty()==0)
};
int StudentList::IsEmpty() {
if(head == NULL)
return 0;
else
return 1;
};
if(newPtr==NULL)
else
{ newPtr->astudent = newstudent;
newPtr->next=NULL;
if (head == NULL)
{ head=newPtr;
tail=newPtr;
else
{tail->next = newPtr;
tail = newPtr;
};
void StudentList::Remove() {
if (IsEmpty()==0)
else
{ ListNode *temp=head;
head=head->next;
temp->next=NULL;
delete temp;
};
{ ListNode *cur,*prev,*temp;
cur=prev=head;
if(strcmp(Name,cur->astudent.stdata.Name)==0)
{ temp=head;
head=head->next;
temp->next=NULL;
delete temp;
else
while (cur!=NULL)
if(strcmp(Name,cur->astudent.stdata.Name)==0)
{ prev->next=cur->next;
temp = cur;
temp->next=NULL;
delete temp;
cur = NULL;
else
{ prev=cur;
cur=cur->next;
};
void StudentList::DisplayList() {
if (IsEmpty()==0)
else
while( cur!=NULL) {
cur->astudent.GetData();
cout<<"\n";
};
#include <iostream.h>
#include "liststdci090094.cpp"
int main(){
int i;
char name[20];
Student newstudent;
StudentList alist;
for(i=0;i<size;i++){
newstudent.SetData();
alist.Add(newstudent);
alist.DisplayList();
cin>>name;
alist.Remove(name);
alist.DisplayList();
return 0;
};