Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Lab.

Sheet One

Linear List

Computer engineering department


Second class
Linear List: is an implementation of List, backed by an array.

#include<iostream>
using namespace std;
template <class T>
class LinearList{
int length;
int maxsize;
T *element;
public:
LinearList(int maxlistsize=10);
~LinearList() { delete [ ]element; }
int Length() { return length; }
bool Isempty() { return (length==0); }
bool Find(int, T&);
int Search(T);
LinearList<T>& Delete(int, T&);
void Sort_Insert(T);
void Insert(int, T);

//modify insertion function to creat


Insert_Without_Indexing so it can add values without set index
you may choose to add at the beginning or the end of the List

void output( ); };

template<class T> LinearList<T> :: LinearList(int max)


{
maxsize = max;
element = new T[maxsize];
length = 0;
}

template<class T> bool LinearList<T> ::Find(int k, T& x)


{
if(k<1 || k>Length())
return false;
x = element[k-1];
return true;
}

template<class T> int LinearList<T> ::Search(T x)


{
for (int i=0; i<Length(); i++)
if(element[i]==x)
return ++i;
return 0; }
template<class T> LinearList<T>& LinearList<T> ::Delete(int k,
T& x)
{
if(Find(k,x)){
for(int i=k; i<Length() -1; i++)
element[i] = element[i+1];
length--;
return *this; }
else throw("Out of bounds");
}

template<class T> void LinearList<T> ::Insert(int k, T x)


{
if(k<0 || k>Length()) throw("Out of bounds");
if(Length() == maxsize) throw("List is full");
for(int i=Length()-1; i>=k; i--)
element[i+1] = element[i];
element[k]= x;
length++;
}
//template<class T> void LinearList<T>::Insert_Without_Indexing (T x){
set your code hear }

template<class T> void LinearList<T> ::output()


{
for(int i=0; i<Length(); i++)
cout<<element[i]<<" ";
}

void main( )
{
// write here code that will test the LinearList class
functions
}
Lab Exercise:

When a message is transmitted in secret code over a transmission channel,


Due to noise in the transmission channel, the transmitted message may become
corrupted. That is, the message received at the destination is not the same as the
message transmitted; some of the values may have been changed. There are several
techniques to check the validity of the transmitted message at the destination. One
technique is to transmit the same message twice. At the destination, both copies of
the message are compared value by value. If the corresponding values are the same,
the message received is error-free.
Let’s write a program that use “LinearList class functions” to check whether
the message received at the destination is error-free. For simplicity, assume that the
secret code representing the message is a sequence of digits (0 to 9) and the
maximum length of the message is 250 digits. Also, the first number in the message
is the length of the message. For example, if the secret code is:
79278356
then the actual message is 7 digits long, and it is transmitted twice. The above
message is transmitted as:
7927835679278356
Input:- A a message containing the secret code and its copy
Output:- The secret message, its copy, if the received code is error-free then the
output would be in the following form:

Note: add any other functions that your program may need.
Exercises:

1. Add to (LinearList class) function modify that will modify the value for
specific element.
2. Add to (LinearList class) function Sorted_inseart that will insert elements to
the class with sorted order.
3. (Line and letter count) using “LinearList class functions” write a program
that reads a given text, outputs the text as it is, and also prints the number of
times each letter appears in the text. An uppercase letter and a lowercase letter
are treated as being the same. Because there are 26 letters, we use an array of
26 components to perform the letter count.

You might also like