2. Fazl-e-BasitFazl-e-Basit
ProgramProgram InstitutionInstitution
MS (Software Engineering )MS (Software Engineering )
(2 years)(2 years)
NUST (National UniversityNUST (National University
of Science &of Science &
Technology), RawalpindiTechnology), Rawalpindi
Computer SoftwareComputer Software
EngineeringEngineering
(4 years)(4 years)
Foundation UniversityFoundation University
Islamabad (FUI)Islamabad (FUI)
B. Sc (Computer Science)B. Sc (Computer Science) Peshawar UniversityPeshawar University
3. Working ExperienceWorking Experience
CurrentlyCurrently Assistant ProfessorAssistant Professor herehere –– CSCS
DepartmentDepartment
Worked as “Worked as “Project DirectorProject Director” LEADS Software” LEADS Software
House Peshawar (Registered with PSEB).House Peshawar (Registered with PSEB).
Worked as “Worked as “Lecturer/CoordinatorLecturer/Coordinator” Greenwich” Greenwich
University Peshawar.University Peshawar.
Worked as “Worked as “Software DeveloperSoftware Developer” Pi-Sigma (Prosol)” Pi-Sigma (Prosol)
Technologies, Software Technology Park,Technologies, Software Technology Park,
Islamabad.Islamabad.
4. Objective of the CourseObjective of the Course
Covering well-known data structures suchCovering well-known data structures such
as dynamic arrays, linked lists, stacks,as dynamic arrays, linked lists, stacks,
queues, trees and graphs etc.queues, trees and graphs etc.
To Prepare students for (and is aTo Prepare students for (and is a
prerequisite for) the more advancedprerequisite for) the more advanced
material students will encounter in latermaterial students will encounter in later
courses.courses.
Implementing data structures and classicalImplementing data structures and classical
computer science problems in C++.computer science problems in C++.
6. Material / ResourcesMaterial / Resources
Text BookText Book
Fundamentals of Data Structures in C++ byFundamentals of Data Structures in C++ by
horowitz, sahni and mehta.horowitz, sahni and mehta.
wwwwww
Any other good book on Data StructureAny other good book on Data Structure
8. Keys for success in the course…….Keys for success in the course…….
100 %100 % AttendanceAttendance
Solving AssignmentsSolving Assignments YourselvesYourselves
AttentiveAttentive in class sessionsin class sessions
AskingAsking questionsquestions && questionsquestions unless youunless you
are clear about your problem.are clear about your problem.
10. Every byte in the computer’s memory has anEvery byte in the computer’s memory has an
address.address. Addresses are numbers just as everyAddresses are numbers just as every
house in a locality has got an address.house in a locality has got an address.
Your program, when it is loaded into memory,Your program, when it is loaded into memory,
occupies certain range of these addresses. Thisoccupies certain range of these addresses. This
means that every variable and every function inmeans that every variable and every function in
your program starts with a particular addressyour program starts with a particular address
NOTE :NOTE : Pointer can only contain thePointer can only contain the ADDRESSADDRESS
of certain memory location.of certain memory location.
11. Normally a variable directly contains a specificNormally a variable directly contains a specific
value. Pointer, on the other hand, contains anvalue. Pointer, on the other hand, contains an
address of the variable that contains the specificaddress of the variable that contains the specific
value.value.
Pointers, like other variables must be declaredPointers, like other variables must be declared
before they are used. For e.g.before they are used. For e.g.
int *pttrr, temp;int *pttrr, temp;
Pointers should be initialized either when they arePointers should be initialized either when they are
declared or in an assignment statement. A pointerdeclared or in an assignment statement. A pointer
may be initialized tomay be initialized to 00,, NULLNULL or anor an addressaddress. A. A
pointer with the valuepointer with the value 00 oror NULLNULL points to nothing.points to nothing.
12. NULLNULL is a symbolic constant defined in theis a symbolic constant defined in the
header fileheader file <iostream.h>.<iostream.h>. Initializing a pointer toInitializing a pointer to
NULLNULL is equivalent to initializing a pointer tois equivalent to initializing a pointer to 00..
Initializing a pointer is necessary to prevent itInitializing a pointer is necessary to prevent it
pointing to unknown or un-initialized area ofpointing to unknown or un-initialized area of
memory.memory.
There are two pointer operators;There are two pointer operators;
thethe address ofaddress of operatoroperator &&
andand
indirection operatorindirection operator oror dereferencing operatordereferencing operator
i.e.i.e. **..
13. && is a unary operator that returns theis a unary operator that returns the address ofaddress of itsits
operand. For e.g.operand. For e.g.
int x = 5;int x = 5;
int *ptrr = NULL;int *ptrr = NULL;
ptrr = &x;ptrr = &x;
In aboveIn above ** showsshows ptrrptrr is pointer variable whose typeis pointer variable whose type
isis intint i.e. it can only contain thei.e. it can only contain the address ofaddress of anyany
integer variablesinteger variables in memory.in memory.
SimilarlySimilarly
14. cout<< *ptrr <<“n”;cout<< *ptrr <<“n”;
cout<< x <<“n”;cout<< x <<“n”;
Will produce the same resultWill produce the same result 55. Here. Here ** meansmeans
value ofvalue of the variable to whichthe variable to which ptrrptrr points to, whichpoints to, which
isis xx..
Note that the dereferenced pointer may also beNote that the dereferenced pointer may also be
used on the left side of an assignment statementused on the left side of an assignment statement
as follows.as follows.
*pttr = 9;*pttr = 9; oror
cin>>*ptrr;cin>>*ptrr;
Following program explains the concept ofFollowing program explains the concept of
pointerspointers
15. #include<iostream.h>#include<iostream.h>
void main( )void main( )
{{
int x = 5;int x = 5;
int *ptrr = NULL;int *ptrr = NULL;
ptrr = &x;ptrr = &x;
cout<< *ptrr <<“n”;cout<< *ptrr <<“n”;
cout<< x <<“n”;cout<< x <<“n”;
*pttr = 9;*pttr = 9;
cout<< &x <<“n”;cout<< &x <<“n”;
cout<< *&ptrr <<“n”;cout<< *&ptrr <<“n”;
// cancel effect of each other// cancel effect of each other ( &( & andand *)*)
cout<< &*ptrr <<“n”;cout<< &*ptrr <<“n”;
cout<< ptrr <<“n”;cout<< ptrr <<“n”;
cout<< *ptrr <<“n”;cout<< *ptrr <<“n”;
cout<< x <<“n”;cout<< x <<“n”;
}}
Output of ProgramOutput of Program
55
55
0x0064FDF40x0064FDF4
0x0064FDF40x0064FDF4
0x0064FDF40x0064FDF4
0x0064FDF40x0064FDF4
99
99
16. newnew andand deletedelete operatorsoperators
Variables created during execution ( run time ) ofVariables created during execution ( run time ) of
a program by means of special operation isa program by means of special operation is
known asknown as Dynamic DataDynamic Data. In C++ operation is. In C++ operation is
new.new.
The new operation has two formsThe new operation has two forms
newnew DataTypeDataType
newnew DataType [intExpression]DataType [intExpression]
First form is used for creating a single varaible ofFirst form is used for creating a single varaible of
type DataType ( e.g. int,…) attype DataType ( e.g. int,…) at run timerun time.. SecondSecond
form creates an array whose elements are ofform creates an array whose elements are of
type DataType ( e.g. char,…) attype DataType ( e.g. char,…) at run time.run time.
17. ExampleExample
intint **intptr;intptr;
charchar **namestr;namestr;
intptr =intptr = newnew int;int;
namestr =namestr = newnew char[8];char[8];
Variables created byVariables created by newnew are said to be on theare said to be on the
free storefree store oror heapheap, a region of memory set aside, a region of memory set aside
forfor Dynamic VariablesDynamic Variables..
The new operator obtains a chunk (portion) ofThe new operator obtains a chunk (portion) of
memory from thememory from the free storefree store..
18. AA Dynamic VariableDynamic Variable isis unnamedunnamed and cannotand cannot
bebe directly addresseddirectly addressed. It must be. It must be indirectlyindirectly
addressedaddressed through the pointer returned by thethrough the pointer returned by the
newnew operator.operator.
intint **intptr =intptr = newnew int;int;
charchar **namestr =namestr = newnew char[8];char[8];
*intptr = 357;*intptr = 357;
strcpy( namestr, “datastructure” );strcpy( namestr, “datastructure” );
19. Dynamic dataDynamic data can becan be destroyeddestroyed at any timeat any time
during theduring the executionexecution of a program when it is noof a program when it is no
longer needed. The built-in operatorlonger needed. The built-in operator deletedelete doesdoes
that and has two forms,that and has two forms, oneone for deleting singlefor deleting single
variable, thevariable, the otherother for deleting an array.for deleting an array.
deletedelete pointer;pointer;
deletedelete [ ] pointer;[ ] pointer;
21. Inaccessible object :Inaccessible object :
A dynamic variable on the free store withoutA dynamic variable on the free store without
any pointer pointing to it.any pointer pointing to it.
Dangling pointer :Dangling pointer :
A pointer that points to a variable that hasA pointer that points to a variable that has
been deallocated.been deallocated.
NOTE :NOTE : Leaving inaccessible objects on the freeLeaving inaccessible objects on the free
store should be considered a logic error.store should be considered a logic error.
25. void trial :: startin()void trial :: startin()
{{
cout<<"n Making use of "new" and "delete" is as follows.n";cout<<"n Making use of "new" and "delete" is as follows.n";
cout<<"n ---------------------------------------------------nn";cout<<"n ---------------------------------------------------nn";
temp1 = new node;temp1 = new node;
temp2 = new node;temp2 = new node;
cout<<"n Enter information about temp1.n";cout<<"n Enter information about temp1.n";
cin>>temp1->info;cin>>temp1->info;
cout<<"n Enter information about temp2.n";cout<<"n Enter information about temp2.n";
cin>>temp2->info;cin>>temp2->info;
temp3 = &obj1;temp3 = &obj1;
cout<<"n Enter information about temp3.n";cout<<"n Enter information about temp3.n";
cin>>temp3->info;cin>>temp3->info;
cout<<"n Showing information of temp1.n";cout<<"n Showing information of temp1.n";
cout<<temp1->info;cout<<temp1->info;
cout<<"n Showing information of temp2.n";cout<<"n Showing information of temp2.n";
cout<<temp2->info;cout<<temp2->info;
cout<<"n Showing information of temp3.n";cout<<"n Showing information of temp3.n";
cout<<temp3->info;cout<<temp3->info;
cout<<"n ---------------------------------------------------nn";cout<<"n ---------------------------------------------------nn";
26. cout<<" Now enter the length of character array.n";cout<<" Now enter the length of character array.n";
cin>>length;cin>>length;
p = new char[length]; // allocate 10 charsp = new char[length]; // allocate 10 chars
q = p;q = p;
cout<<" Now enter "<<length<<" characters to fill an array.n";cout<<" Now enter "<<length<<" characters to fill an array.n";
for( i=0;i<length; i++ )for( i=0;i<length; i++ )
{{
cin>>*p;cin>>*p;
p = p + 1;p = p + 1;
}}
p = q;p = q;
27. cout<<" nElements of array are as follows.n";cout<<" nElements of array are as follows.n";
for( i=0;i<length; i++ )for( i=0;i<length; i++ )
{{
cout<<*p<<", ";cout<<*p<<", ";
p = p + 1;p = p + 1;
}}
p = q;p = q;
}//--- END of startin( )---------------------------------------------------------------}//--- END of startin( )---------------------------------------------------------------