Hello
I am trying to figure out how to create a number generator to generate a number between 65-90 (ascii capital letters) and 97-122 (ascii lowercase letters). I need to convert the number to a char and then put the char in the linked list in order while ignoring duplicates. So far, I am having trouble getting my random generator to work with the range of numbers that I want.. I am currently working in the createList function of my program.
For example:
65 converts to A
66 converts to B
67 converts to C
97 converts to a
98 converts to b
99 converts to c
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*& last);
void alphasort (nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
int main()
{
nodeType *first, *last;
int num;
createList(first,last);
alphasort(first,last);
printList(first);
system ("PAUSE");
return 0;
}
void createList(nodeType*& first, nodeType*& last)
{
int number = rand() % 100 + 1;
nodeType *newNode;
first = NULL;
last = NULL;
while ((number >= 65)&&(number <= 90)||(number >= 97)&&(number <= 122))
{
newNode = new nodeType;
newNode->info = char(number);
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
}
}
void nodeSwap(struct nodeType *p, struct nodeType *q) // swaps node
{
struct nodeType *t1, *t2, *t3;
if (p != q)
{
t1 = p->link; t2 = q->link; t3 = q->link->link;
p->link = q->link;
q->link = t1;
t2->link = t1->link;
t1->link = t3;
}
}
void alphasort (nodeType*& first, nodeType*& last) // sorts alphabetically by using Selection Sort
{
struct nodeType *i,*j,*min;
for (i = first; i->link != NULL; i = i->link)
{
min = i;
for (j = i->link; j->link != NULL; j = j->link)
{
if (j->link->info < min->link->info) min = j;
}
nodeSwap(i,min);
}
}
void printList(nodeType*& first)
{
nodeType *current;
current = new nodeType;
current = first;
while (current != NULL)
{
cout << char(current->info)<<endl;
current = current->link;
}
}