Professional Documents
Culture Documents
Linear List: Data Structure II Lab. Homework
Linear List: Data Structure II Lab. Homework
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; }
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();
};
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]++;
3
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string>
#include "Linear_List.h"
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();
}