Assignment 4
Assignment 4
Tauseef Iftikhar
1
void addElement (T ) ;
/∗ ∗ Add e l e m e n t a t t h e s p e c i f i c p o s i t i o n i n l i s t
∗ Precondition : List i s created
∗ P o s t c o n d i t i o n : Add e l e m e n t i n l i s t a t t h e g i v e n p o s i t i o n as i n p u t .
∗/
void addElementAt ( int , T ) ;
/∗ ∗ D e l e t e e l e m e n t
∗ P r e c o n d i t i o n : o b j e c t p a s s e d as i n p u t s h o u l d e x i s t i n l i s t .
∗ P o s t c o n d i t i o n : e l e m e n t found a t f i r s t l o c a t i o n s h o u l d be
∗ removed from l i s t .
∗/
T& removeElement (T& )
/∗ ∗ D e l e t e e l e m e n t a t s p e c i f i c l o c a t i o n
∗ P r e c o n d i t i o n : i n d e x p a s s e d as i n p u t s h o u l d be v a l i d
∗ (1<= i n d e x <= l a s t +1)
∗ P o s t c o n d i t i o n : e l e m e n t found a t s p e c i f i e d i n d e x s h o u l d be removed
∗ from l i s t w i t h o u t c r e a t i n g h o l e i n t h e l i s t .
∗/
T& removeElementAt ( int ) ;
/∗ ∗ Find e l e m e n t
∗ Precondition : List should e x i s t
∗ P o s t c o n d i t i o n : I f found t h e n r e t u r n TRUE e l s e r e t u r n FALSE
∗/
bool f i n d E l e m e n t (T ) ;
/∗ ∗ Find e l e m e n t a t s p e c i f i c l o c a t i o n
∗ P r e c o n d i t i o n : i n d e x p a s s e d as i n p u t s h o u l d be v a l i d
∗ (1<= i n d e x <= l a s t +1)
∗ P o s t c o n d i t i o n : I f found a t s a i d i n d e x t h e n r e t u r n TRUE e l s e
∗ r e t u r n FALSE
∗/
bool findElementAt ( int ) ;
/∗ ∗ Update e l e m e n t a t s p e c i f i c l o c a t i o n
∗ P r e c o n d i t i o n : L i s t s h o u l d not be empty , i n d e x s h o u l d be v a l i d
∗ (1<= i n d e x <= l a s t +1).
∗ P o s t c o n d i t i o n : d a t a a t s a i d l o c a t i o n s h o u l d be u p d a t e d w i t h o u t
∗ changing anything e l s e .
∗/
void updateElementAt ( int , T&);
/∗ ∗ Make empty
∗ P r e c o n d i t i o n : L i s t i s non empty .
∗ P o s t c o n d i t i o n : L i s t s h o u l d be empty .
∗/
void makeEmpty ( ) ;
private :
bool i s F u l l ( ) const { return l a s t+1==l e n g t h ; }
bool isEmpty ( ) const { return l a s t == −1;}
int l a s t ; //!< Member v a r i a b l e ” l a s t ” i n d i c a t e t h e l a s t e l e m e n t i n l i s t
int l e n g t h ; //!< Member v a r i a b l e ” l e n g t h ”
T∗ e l e m e n t ; //!< Member v a r i a b l e ” a r r a y ”
};
#endif // LIST H
Page 2
2 List: Linked-list implementation
1. Implement a generic list which can hold node of any type. It should be a linked-list implemen-
tation.
#i f n d e f LIST H
#define LIST H
#include ” node . h”
template<c l a s s T>
class List {
template<c l a s s U>
friend ostream& operator<<(ostream &,const L i s t <U> ∗ ) ;
public :
/∗ ∗ d e f a u l t c o n s t r u c t o r
∗ i n i t i a l i z e c o u n t e r w i t h 0 and head t o NULL.
∗/
List ( ) ;
/∗ ∗ d e s t r u c t o r
∗ r e l e a s e a l l memory o c c u p i e d by t h e nodes i n t h e l i s t .
∗/
˜ List ( ) ;
/∗ ∗ By d e f a u l t add node a t t h e end
∗ Precondition : List i s created .
∗ P o s t c o n d i t i o n : New node i s added a t t h e end o f t h e l i s t .
∗ c o u n t e r i s i n c r e m e n t e d by one .
∗/
void addNode (T∗ ) ;
/∗ ∗ i n s e r t node a t p o s i t i o n . p o s i t i o n s h o u l d be a v a l i d
∗ l o c a t i o n 1−# o f n o d e s + 1 .
∗ Precondition : List i s created
∗ P o s t c o n d i t i o n : I f p o s i t i o n i s v a l i d t h e n node s h o u l d be added a t t h e
∗ s a i d p o s i t i o n o t h e r w i s e p r o p e r e r r o r message s h o u l d be d i s p l a y e d
∗/
void addNodeAt ( int , T∗ ) ;
/∗ ∗ i n s e r t node a f t e r p o s i t i o n . p o s i t i o n s h o u l d be a v a l i d l o c a t i o n
∗ b e t w e e n 1−# o f n o d e s + 1 .
∗ Precondition : List i s created
∗ P o s t c o n d i t i o n : I f p o s i t i o n i s v a l i d t h e n node s h o u l d be added a f t e r
∗ t h e s a i d p o s i t i o n o t h e r w i s e p r o p e r e r r o r message s h o u l d be d i s p l a y e d
∗/
void addNodeAfter ( int , T∗ ) ;
/∗ ∗ i n s e r t node b e f o r e p o s i t i o n . p o s i t i o n s h o u l d be a v a l i d l o c a t i o n
∗ 1−# o f n o d e s + 1 . Node c o u l d not be added b e f o r e 1 p o s i t i o n .
∗ Precondition : List i s created
∗ P o s t c o n d i t i o n : I f p o s i t i o n i s v a l i d t h e n node s h o u l d be added a f t e r
∗ t h e s a i d p o s i t i o n o t h e r w i s e p r o p e r e r r o r message s h o u l d be d i s p l a y e d .
∗/
void addNodeBefore ( int , T∗ ) ;
/∗ ∗ remove node from t h e i n d e x p a s s e d as i n p u t . I n d e x s h o u l d be v a l i d
∗ l o c a t i o n b e t w e e n 1−# o f n o d e s .
∗ P r e c o n d i t i o n : L i s t s h o u l d not be empty
∗ P o s t c o n d i t i o n : L i s t s h o u l d not c o n t a i n t h e s a i d node . and f u n c t i o n
∗ s h o u l d r e t u r n t h e p o i n t e r t o removed node .
Page 3
∗/
Node<T>∗ removeNodeAt ( int ) ;
/∗ ∗ remove node h a v i n g t h e same d a t a item as p a s s e d i n i n p u t . i f d a t a
∗ item d o e s ’ t e x i s t t h e n d i s p l a y p r o p e r e r r o r message and r e t u r n NULL
∗ P r e c o n d i t i o n : L i s t i s not empty
∗ P o s t c o n d i t i o n : i f node i s found t h e n remove node from l i s t and r e t u r n t h e
∗ a d d r e s s o f removed node o t h e r w i s e r e t u r n NULL w i t h p r o p e r e r r o r message .
∗/
Node<T>∗ removeNode (T ) ;
/∗ ∗ s e a r c h node w i t h g i v e n d a t a item .
∗ P r e c o n d i t i o n : L i s t s h o u l d not be empty
∗ P o s t c o n d i t i o n : r e t u r n TRUE i f node i s found o t h e r w i s e r e t u r n FALSE
∗/
bool findNode (T) const ;
/∗ ∗ show t h e d a t a o f t h e node i f i n d e x i s v a l i d
∗ P r e c o n d i t i o n : L i s t i s not empty
∗ P o s t c o n d i t i o n : d i s p l a y t h e d a t a o f t h e node i f i n d e x r e c e i v e d as i n p u t
∗ i s v a l i d , o t h e r w i s e d i s p l a y p r o p e r e r r o r message .
∗/
void showNodeAT ( int ) const ;
/∗ ∗ Update e l e m e n t a t s p e c i f i c l o c a t i o n
∗ P r e c o n d i t i o n : L i s t s h o u l d not be empty , i n d e x s h o u l d be v a l i d
∗ (1<= i n d e x <= l a s t +1).
∗ P o s t c o n d i t i o n : d a t a a t s a i d l o c a t i o n s h o u l d be u p d a t e d w i t h o u t c h a n g i n g
∗ anything e l s e .
∗/
void updateElementAt ( int , T&);
/∗ ∗ Make empty
∗ P r e c o n d i t i o n : L i s t i s non empty .
∗ P o s t c o n d i t i o n : L i s t s h o u l d be empty .
∗/
void makeEmpty ( ) ;
private :
int c o u n t e r ; // d a t a member t o c ou n t t h e number o f nodes i n l i s t .
};
#endif // LIST H
Page 4
Node (T∗ ) ;
˜Node ( ) ;
void s e t D a t a (T& ) ;
void s e t N e x t P t r ( Node<T> ∗p ) ;
T getData ( ) const ;
Node<T> ∗ g et Ne xt Pt r ( ) const ;
private :
T∗ data ;
Node<T> ∗ n e x t P t r ;
};
#endif // NODE H
3 Person Implementation
1. Implement a generic node which can hold any type of objects.
#i f n d e f PERSON H
#define PERSON H
#include<s t r i n g >
#include<i o s t r e a m >
using namespace s t d ;
c l a s s Person
{
friend ostream &operator<<(ostream& , const Person& ) ;
public :
/∗ ∗ D e f a u l t c o n s t r u c t o r ∗/
Person ( int , s t r i n g , int ) ;
/∗ ∗ D e f a u l t d e s t r u c t o r ∗/
˜ Person ( ) ;
/∗ ∗ Access name
∗ \ r e t u r n The c u r r e n t v a l u e o f name
∗/
s t r i n g Getname ( ) ;
/∗ ∗ S e t name
∗ \param v a l New v a l u e t o s e t
∗/
void Setname ( s t r i n g v a l ) ;
/∗ ∗ Access i d
∗ \ r e t u r n The c u r r e n t v a l u e o f i d
∗/
int Getid ( ) ;
/∗ ∗ S e t i d
∗ \param v a l New v a l u e t o s e t
∗/
void S e t i d ( int v a l ) ;
/∗ ∗ Access age
∗ \ r e t u r n The c u r r e n t v a l u e o f age
∗/
Page 5
int Getage ( ) ;
/∗ ∗ S e t age
∗ \param v a l New v a l u e t o s e t
∗/
void S e t a g e ( int v a l ) ;
/∗ ∗ Equal t o
∗ \param p compare w i t h t h i s o b j e c t and r e t u r n TRUE i f b o t h
∗ a r e same o t h e r w i s e r e t u r n FALSE .
∗/
bool operator==(const Person & p ) const ;
private :
s t r i n g name ; //!< Member v a r i a b l e ”name”
int i d ; //!< Member v a r i a b l e ” i d ”
int age ; //!< Member v a r i a b l e ” age ”
};
#endif // PERSON H
4 Submission Guidelines
• Write your code in C++ files (arraylist.cpp) , (linklist.cpp), (node.cpp) and (person.cpp)
• Put your files in a folder. Name the folder according to your roll number and zip it.
Page 6