newNode = head;
for ( int i = 1; i < numberOfNodes; i++ )
{
for ( int index = 0; index < numberOfNodes - i; index++ )
{
// if last name in first node is greater than last name in second node
// swap the positions of the nodes
if ( strcmp ( newNode -> lName, newNode -> next -> lName ) == 1 )
{
temp1 = newNode;
newNode = newNode -> next;
newNode -> next = temp1;
}// end if
}// end for
}// end for
// preprocessor directives
# include <iostream>
# include <string>
usingnamespace std;
// class nodeType
class nodeType
{
// class members
public:
char fName[ 15 ];
char lName[ 15 ];
nodeType *next;
};
// main
int main()
{
// declare pointers of type nodeType
nodeType *head, *first, *newNode, *last, *temp1, *temp2;
// set pointers to 0
head = NULL;
first = NULL;
newNode = NULL;
last = NULL;
temp1 = NULL;
temp2 = NULL;
char fn[ 15 ];
int numberOfNodes = 0;
//============Input Names===========================
cout << "Enter first name: ";
cin >> fn;
// loop for 3 times
for ( int a = 0; a < 3; a++ )
{
// create new node
newNode = new nodeType;
// store first name in node
for ( int i = 0; i < 15; i++ )
newNode -> fName[ i ] = fn[ i ];
// count number of nodes in list
numberOfNodes++;
// input last name
cout << "Enter last name: ";
cin >> newNode -> lName;
// set end of node to 0
newNode -> next = NULL;
// if node is empty set pointers to 0
if ( first == NULL )
{
head = newNode;
first = newNode;
last = newNode;
}
else
{
last -> next = newNode;
last = newNode;
}
cout << "Enter first name: ";
cin >> fn;
} // end while
//=================Print unsorted list========================
cout << endl << endl << "Unsorted" << endl << endl;
newNode = head;
while ( newNode != NULL )
{
cout << newNode -> fName << " " << newNode -> lName << endl;
newNode = newNode -> next;
}
//==================Print sorted list============================
cout << endl << endl << "Sorted" << endl << endl;
newNode = head;
for ( int i = 1; i < numberOfNodes; i++ )
{
for ( int index = 0; index < numberOfNodes - i; index++ )
{
if ( strcmp ( newNode -> lName, newNode -> next -> lName ) == 1 )
{
temp1 = newNode;
newNode = newNode -> next;
newNode -> next = temp1;
}// end if
}// end for
}// end for
newNode = head;
while ( newNode != NULL )
{
cout << newNode -> fName << " " << newNode -> lName << endl;
newNode = newNode -> next;
}// end while
system ("pause");
}
I am not sure to use the bubblesort in your assignmet, but I would not use any sort function when I create a linked list, if it needs to be listed in a sorted order.
Simple, when you store a node, keep it at right place, thats all.
In your code, you enter all first names first in a loop and enter the last names last, (no idea, if you are thinking, as their name say, to enter first names first and last names last, lol :))
For your example, I would do (if not told to use a sort function)
if (head == NULL) // list is empty
{
head = first = newNode;
}
else if (strcmp(newNode->lname, ln) < 0) // then insert before first one
{
newNode->next = head;
head = newNode;
}
else {
temp1 = head;
//start a search for right place, by checking the next one's last name
while ( strcmp(temp1->next->lname, ln) < 0) && temp10>next != NULL )
{
temp1 = temp1->next;
}
// bubbleSort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// preprocessor directives
# include <iostream>
# include <string>
usingnamespace std;
// class nodeType
class nodeType
{
// class members
public:
char fName[ 15 ];
char lName[ 15 ];
nodeType *next;
};
nodeType *bubblesort(nodeType *list);
// main
int main()
{
// declare pointers of type nodeType
nodeType *head = NULL;
nodeType *first = NULL;
nodeType *newNode = NULL;
nodeType *last = NULL;
nodeType *temp1 = NULL;
nodeType *temp2 = NULL;
char fn[15];
int numberOfNodes = 0;
//============Input Names===========================
// loop for 3 times
for ( int a = 0; a < 5; a++ )
{
cout << "Enter first name: ";
cin >> fn;
// create new node
newNode = new nodeType;
// store first name in node
for ( int i = 0; i < 15; i++ )
newNode -> fName[ i ] = fn[ i ];
// count number of nodes in list
numberOfNodes++;
// input last name
cout << "Enter last name: ";
cin >> newNode -> lName;
// set end of node to 0
newNode -> next = NULL;
// if node is empty set pointers to 0
if ( first == NULL )
{
head = newNode;
first = newNode;
last = newNode;
}
else
{
last -> next = newNode;
last = newNode;
}
} // end while
//=================Print unsorted list========================
cout << endl << endl << "Unsorted" << endl << endl;
newNode = head;
while ( newNode != NULL )
{
cout << newNode -> fName << " " << newNode -> lName << endl;
newNode = newNode -> next;
}
//==================Print sorted list============================
cout << endl << endl << "Sorted" << endl << endl;
head = bubblesort(head);
newNode = head;
while ( newNode != NULL )
{
cout << newNode -> fName << " " << newNode -> lName << endl;
newNode = newNode -> next;
}// end while
system ("pause");
}
////////////////////////////////////////////////////////////////////////////
nodeType *bubblesort(nodeType *list)
{
nodeType *lst, *tmp = list, *prev, *potentialprev = list;
int idx, idx2, n = 0;
//determine total number of nodes
for (;tmp->next; tmp=tmp->next)
{
n++;
}
for (idx=0; idx<n-1; idx++)
{
for (idx2=0,lst=list; lst && lst->next && (idx2<=n-1-idx); idx2++)
{
if (!idx2)
{
//we are at beginning, so treat start
//node as prev node
prev = lst;
}
//compare the two neighbors
if ( strcmp ( lst->lName, lst->next->lName ) == 1 )
{
//swap the nodes
tmp = (lst->next?lst->next->next:0);
if (!idx2 && (prev == list))
{
//we do not have any special sentinal nodes
//so change beginning of the list to point
//to the smallest swapped node
list = lst->next;
}
potentialprev = lst->next;
prev->next = lst->next;
lst->next->next = lst;
lst->next = tmp;
prev = potentialprev;
}
else
{
lst = lst->next;
if(idx2)
{
//just keep track of previous node,
//for swapping nodes this is required
prev = prev->next;
}
}
}
}
return list;
}
Hello satm2008
Thank you very much for your reply! I am totally new to linked lists and your reply was a great help.
I tried my best to incorporate your solution into the program and it works (sorts last names alphabetically). However, something I wrote (or left out) causes the program to stall sometimes. Still trying to figure it out.
Here is the new code and thanks again:
// preprocessor directives
# include <iostream>
# include <string>
usingnamespace std;
// class nodeType
class nodeType
{
// class members
public:
char fName[ 15 ];
char lName[ 15 ];
nodeType *next;
};
int main()
{
// all your declaration goes here
nodeType *head, *first, *next, *newNode, *temp1, *temp2;
char fn[ 15 ];
char ln[ 15 ];
char anyMore;
head = first = next = newNode = temp1 = temp2 = NULL;
do {
system ( "cls" );
// prompt and input first name and last name
cout << "\nEnter first name: ";
cin >> fn;
cout << "Enter last name: ";
cin >> ln;
newNode = new nodeType;
strcpy( newNode -> fName, fn );
strcpy( newNode -> lName, ln );
newNode -> next = NULL;
if ( head == NULL ) // list is empty
{
head = first = newNode;
}
elseif ( strcmp( newNode -> lName, head -> lName ) < 0 ) // then insert before first one
{
newNode -> next = head;
head = newNode;
}
else
{
temp1 = head;
//start a search for right place, by checking the next one's last name
while (( strcmp( temp1 -> next -> lName, ln ) < 0 ) && ( temp1 -> next != NULL ))
{
temp1 = temp1 -> next;
}
newNode -> next = temp1 -> next;
temp1 -> next = newNode;
}
// now list the items
temp1 = head;
while ( temp1 != NULL )
{
cout << temp1 -> fName << " " << temp1 -> lName << endl;
temp1 = temp1 -> next;
}
// here prompt for an input to enter more (ie, anyMore Y/N)
cout << "\nEnter Y to input, or N to exit: ";
cin >> anyMore;
} while ( anyMore == 'Y' || anyMore == 'y' ); // end dowhile
system ( "pause" );
// all done;
}
Hello Grey Wolf,
Thanks very much for your solution. It works!
I commented out the preprocessor directive: #include "stdafx.h" because I got the following error while using Dev C++: "stdafx.h: No such file or directory". Thanks again.