0% found this document useful (0 votes)
44 views4 pages

Linear List: Data Structure II Lab. Homework

This document summarizes 3 exercises on linear lists from a data structures lecture. The first exercise defines a template linear list class with functions for length, emptiness, finding/searching/deleting elements, output, and sorted insertion. The second exercise implements a sorted insertion function. The third exercise defines a string-based linear list class with functions for insertion, output, and calculating letter frequencies in a given string. Main program code demonstrates using the string list.

Uploaded by

xz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Linear List: Data Structure II Lab. Homework

This document summarizes 3 exercises on linear lists from a data structures lecture. The first exercise defines a template linear list class with functions for length, emptiness, finding/searching/deleting elements, output, and sorted insertion. The second exercise implements a sorted insertion function. The third exercise defines a string-based linear list class with functions for insertion, output, and calculating letter frequencies in a given string. Main program code demonstrates using the string list.

Uploaded by

xz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy