Professional Documents
Culture Documents
Terms
Type a collection of values Data Type a type together with a collection of
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
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.
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.
Representation:
int n=100; class LS {char letter_string [n]; int length; void append(char c); void remove(int n);
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: 0length 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
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; }
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 }
{ 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; }
else return 0;
}