Implement the following sequence of operations one by one
1. Make a linked list for 26 (a to z) English alphabets where each node consists a single alphabet, an integer data for frequency count (initially 0) and next pointer. This list should be sorted in ascending order according to the ASCII value of the alphabets.
2. Now you have given a string consists of English alphabets. Your task is to update the frequency count values of above list for each occurrence of alphabets inside the given string.
For example, if the string is “babaaedd” then the updated list should be like this:
3. Finally traverse the whole list and print each Alphbets with their frequency one by one from a to z. Please follow the sample input-output format. Input string length is not more than 100.
Sample Input:
Mistcsedepartment
Sample Output:
a : 1
b : 0
c : 1
d : 1
e : 3
f : 0
g : 0
h : 0
i : 1
j : 0
k : 0
l : 0
m : 2
n : 1
o : 0
p : 1
q : 0
r : 1
s : 2
t : 3
u : 0
v : 0
w : 0
x : 0
y : 0
z : 0
#include <iostream>
using namespace std;
struct Node {
char data;
int pos;
Node* next;
};
Node* add(char data)
{
Node* node1 = new Node;
node1 ->data = data;
node1 ->pos = 0;
node1->next = NULL;
return node1;
}
Node* stringToLinkedList(string text, Node* head1)
{
head1 = add(text[0]);
Node* currNode = head1;
for (int i = 1; i < text.size(); i++) {
currNode ->next = add(text[i]);
currNode = currNode ->next;
}
return head1;
}
void updateAlphabetCount(string text, Node* head1){
Node* currNode = head1;
for(int i=0; i<text.size();i++){
char item = currNode->data;
if(item==text[i]){
currNode->pos++;
}
currNode = currNode->next;
}
while (currNode != NULL) {
cout << currNode->data <<":"<<currNode->pos<<endl;
currNode = currNode->next;
}
}
void print(Node* head)
{
Node* currNode = head;
while (currNode != NULL) {
cout << currNode->data <<":"<<currNode->pos<<endl;
currNode = currNode->next;
}
}
int main()
{
string text = "abcdefghijklmnopqrstuvwxyz";
Node* headNode = NULL;
headNode = stringToLinkedList(text, headNode);
print(headNode);
updateAlphabetCount("mistcsedepartment",headNode);
return 0;
}
Comments
Leave a comment