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

Data Structure II Lab.

Homework
Lecture 1
Linear List
By: Mustafa Hasan Ali

Exercises: 1 and 2)
 Class Header File: Linear_List.h
#pragma once
template<class T>
class Linear_List
{
int length;
int maxsize;
T *element;
public:
Linear_List(int);
~Linear_List() { delete [ ]element; }
int Lengthvalue() { return length; }
bool Isempty() { return (length==0); }
bool Find(int k, T &x);
int Search(T &x);
void Delete(int k, T &x);
void Output();
void Modify(int k, T x) { element[k-1] = x; }
void Sorted_Insert(T x);
};

1
 Class Source File: Linear_List.cpp
template<class T> void Linear_List<T>::SortedInsert(T x)
{
if(length == maxsize) { cout<<"List is full"; return; }

//Case 1: When list is empty


if(Isempty())
{element[length] = x; length++; return; }

//Case 2: When x should inserted between first and last


element
element[length] = x;
for(int i=length-1; i>=0; i--)
if(element[i+1] < element[i] )
{ T tmp = element[i];
element[i] = element[i+1];
element[i+1] = tmp;
}
else break;

length++;
}

Exercise: 3)
 Class Header File: Linear_List.h
#pragma once
#include<string>
using namespace std;
class Linear_List
{
int length;
string element;
public:
Linear_List() { length = 0; }
~Linear_List() { }
void Insert(string x);

2
void Output();
void Calculate();
};

 Class Source File: Linear_List.cpp


#include "stdafx.h"
#include<iostream>
#include "Linear_List.h"
#include<string>
using namespace std;

void Linear_List::Insert(string x)
{
element = x;
length = element.length(); //String class function
}

void Linear_List::Output()
{
for(int i=0; i<length; i++)
cout<<element[i];
}

void Linear_List::Calculate()
{
int count[26] = {0};
int ch = 97;//because first letter "a" in ASCII is 97
for (int i = 0; i < 26; i++)
for(int j=0;j<length;j++)
if( tolower(element[j]) == (char)(ch+i) )
//tolower() function convert char to lowercase
count[i]++;

for (int i = 0; i < 26; i++)


cout<< (char) (97+i) <<": "<<count[i]<<endl;
}
 Main Program File: Main_program.cpp

3
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string>
#include "Linear_List.h"

using namespace std;

void main()
{
Linear_List temp;
string s;
cout<<"Enter Your Text: "<<endl;
getline(cin,s); //getline() read user input text line
temp.Insert(s);
temp.Output();
cout<<endl;
temp.Calculate ();

_getch();
}

You might also like