So all this program does is sort...at least some of the time.
If you look at Version 1, I DO NOT use a pointer in my structure as char data1. This version works just fine. Enter [q w e r t] and you get back [e q r t w]...it works as planned.
With Version 2 I have changed char data1 to char* data1. With this version if you enter [q w e r t] it spits back [q w e r t]. I cannot tell if it is my function prototype or something else that is doing this.
Any thoughts...I compile with Visual C++ 6.0?
VERSION 1: (this one sorts)
Code:
#include<iostream.h>
struct ds_node
{
char data1;
ds_node* next;
};
class ds_class
{
private:
ds_node* head;
public:
ds_class();
~ds_class();
void DisplayAll(ds_node*& head);
ds_node* SortedInsert(ds_node*& head, char data1);
};
//----------------------------------------------Constructor
ds_class::ds_class()
{
cout << "Constructor Called" << endl;
head = NULL;
}
//----------------------------------------------Destructor
ds_class::~ds_class()
{
cout << "Destructor Called" << endl;
delete head;
head = NULL;
}
//----------------------------------------------Display All Data
void ds_class:: DisplayAll(ds_node*& head_ptr)
{
while(head_ptr != NULL)
{
cout << head_ptr->data1 << endl;
cout << "----------------------------------------------" << endl;
head_ptr = head_ptr->next;
}
cout << endl;
}
//----------------------------------------------Sort / Insert Data
ds_node* ds_class::SortedInsert(ds_node*& head, char data1)
{
ds_node* newNode;
// populate the new node that we're adding.
newNode = new ds_node;
newNode->data1 = data1;
newNode->next = NULL;
// figure out where to add that node in
if((head == NULL) || (head->data1 >= newNode->data1))
{
// this element should go at the head of the list
newNode->next = head;
head = newNode;
}
else // this is not the first element in the list
{
// create a pointer that will traverse the list to find
// where to insert the new element
ds_node* current;
current = head;
// move current down the list till we find where our new node is supposed to go
while((current->next != NULL) && (current->next->data1 < newNode->data1))
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
return head;
}
//----------------------------------------------Client Program
void main()
{
ds_class ds;
ds_node* head = NULL;
char temp;
char response;
do
{
cout << "Enter first letter: ";
cin >> temp;
cin.ignore(100, '\n');
head = ds.SortedInsert(head, temp);
cout << "Do Again...(y) or (n): ";
cin >> response;
cin.ignore(100, '\n');
}while(response == 'y');
cout << "You entered these letters:" << endl;
ds.DisplayAll(head);
}
VERSION 2: (this one will not sort)
Code:
#include<iostream.h>
#include<string.h>
struct ds_node
{
char* data1;
ds_node* next;
};
class ds_class
{
private:
ds_node* head;
public:
ds_class();
~ds_class();
void DisplayAll(ds_node*& head);
ds_node* SortedInsert(ds_node*& head, char* data1);
};
//----------------------------------------------Constructor
ds_class::ds_class()
{
cout << "Constructor Called" << endl;
head = NULL;
}
//----------------------------------------------Destructor
ds_class::~ds_class()
{
cout << "Destructor Called" << endl;
delete head;
head = NULL;
}
//----------------------------------------------Display All Data
void ds_class:: DisplayAll(ds_node*& head_ptr)
{
while(head_ptr != NULL)
{
cout << head_ptr->data1 << endl;
cout << "----------------------------------------------" << endl;
head_ptr = head_ptr->next;
}
cout << endl;
}
//----------------------------------------------Sort / Insert Data
ds_node* ds_class::SortedInsert(ds_node*& head, char* data1)
{
ds_node* newNode;
// populate the new node that we're adding.
newNode = new ds_node;
newNode->data1 = data1;
newNode->next = NULL;
// figure out where to add that node in
if((head == NULL) || (head->data1 >= newNode->data1))
{
// this element should go at the head of the list
newNode->next = head;
head = newNode;
}
else // this is not the first element in the list
{
// create a pointer that will traverse the list to find
// where to insert the new element
ds_node* current;
current = head;
// move current down the list till we find where our new node is supposed to go
while((current->next != NULL) && (current->next->data1 < newNode->data1))
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
return head;
}
//----------------------------------------------Client Program
void main()
{
ds_class ds;
ds_node* head = NULL;
char* one;
char temp[20];
char response;
do
{
cout << "Enter first letter: ";
cin.get(temp, 20, '\n');
cin.ignore(100, '\n');
one = new char[strlen(temp) + 1];
strcpy(one, temp);
strcpy(temp, " ");
head = ds.SortedInsert(head, one);
cout << "Do Again...(y) or (n): ";
cin >> response;
cin.ignore(100, '\n');
}while(response == 'y');
cout << "You entered these letters:" << endl;
ds.DisplayAll(head);
}