Create a program that will be using a character linked list
Create a program that will be using a character linked list. It will insert the letters into the list alphabetically. Ask the user to enter a word. Then, enter the word into the list. Then, print out what is in the list, and clear it out. example: enters: apple prints: aelpp Please do this by writing your own linked list, or using the STL list class. -- if you are using the STL list class, do not use the “sort” function. Thank you!
Solution
#include <iostream>
using namespace std;
class LL{
public:
char data;
LL *next;
LL(char ch)
{
this->data = ch;
this->next = NULL;
}
};
int main() {
LL *header=NULL, *node;
char ch;
while((ch=getchar())!=\'\ \'){
node = new LL(ch);
if(!header){
header = node;
}
else{
LL *temp = header;
LL *prev = NULL;
while(temp && node->data > temp->data)
{
prev = temp;
temp = temp->next;
}
prev->next = node;
node->next = temp;
}
}
while(header)
{
cout << header->data;
LL *temp = header;
header = header->next;
delete(temp);
}
return 0;
}
