Professional Documents
Culture Documents
Terms
Type – a collection of values
Data Type – a type together with a collection of
operations to manipulate the type
Abstract Data Type (ADT) – defines a data
type solely in terms of a type and a set of operations
on that type
Data Structure – the physical implementation
of the ADT
Data Structures and OOP
Principles
Data Structures are Objects
Abstraction (ability to distill a software system into its most
basic parts) and Encapsulation (implementing the
abstraction without revealing how is implemented) applies to
Data Structures
Inheritance and Polymorphism
Data Structures are often “containers” and share
similar operations
Defining an ADT
Stages/Phases of defining an ADT
1. Specifications
-structure; operations/functions; definition of the
ADT
2. Representation or variable/constant declaration
-choosing the appropriate data structure fitted for
the given specs used in no. 1.
3. Implementation
-choosing the appropriate commands/programming
statements given a chosen programming language
to implement the necessary operations of each of
the defined functions.
A. Definition: Letter String
- A letter string is a sequence of n characters.The length is
referred to as length.
Operations:
1. Append- to add a character at the tail of the sequence.
2. Remove- to delete a character at any position in the given
sequence.
3. Print- display the elements of the letter string.
4. Makenull- make the letter string empty.
5. Empty- returns true if the letter string is empty otherwise it
returns false.
6. Full- returns true if the letter string is full otherwise it returns
false.
1. APPEND
Pre: LS is not full
Post: a new char is added at the tail end of the LS & likewise the gap
is closed
2. REMOVE
Pre: not empty
Post: one character should have been removed from the LS
3. PRINT
Pre: LS is not empty/none
Post: LS is printed
4. MAKENULL
Pre: 0 ≤ length ≤ ls. Length/none
Post: All elements were deleted
5. EMPTY
Pre: 0≤length≤ ls /none
Post: returned TRUE if LS is empty, otherwise returned false
6. FULL
Pre: LS must be full/none
Post: returned true if LS is full, otherwise returned false
Representation:
int n=100;
class LS
{char letter_string [n];
int length;
void append(char c);
void remove(int n);
void print(int i); void makenull()
int empty(); int full(); };
Implementation
void LS :: append (char c)
{ if (LS.length != n-1)
{ LS.length = LS.length + 1;
LS.letter_string [LS.length ] = c;}
else if (LS.length == n-1)
cout<<“the letterstring is full”;
}
void LS :: remove (int n)
{if (LS.length<0)
cout<<”string is empty”;
else if (LS.length<n)
cout<<”position is beyond the length”;
else {
for (int i=n;i<LS.length; i++)
LS.letter_string[i] = LS.letter_string [i+1];
}
LS.length = LS.length-1
}
void LS :: print (int i)
{ if (LS.length<= -1) cout<<”empty”;
else
for( i=0; i<=LS.length; i++)
cout<<”LS.letter_string[i]”;}
void LS::makenull ()
{ if (LS.length> -1)
LS.length= -1;
else
cout<<”theres no character in LS”;
}
int LS ::empty ()
{ if (LS.length<0)
return 1;
else return 0; }
int LS:: full ()
{ if (LS.length>99)
return 1;
else return 0;
}