0

i am trying to create a program that shows details of 40 different hills. he dteails must include hieght and distance from home. i am trying to show tge details in order of height from smallest to highest but i cant get them in order here is all of my code

#include <iostream>
using namespace std;

struct Node{		//a structure that can be 
	int data;		//thought of as a simple class
	Node *link;		//All fields are public
};

class LinkedList{



public:
	Node *head;		//pointer to the first node
	Node *last;		//pointer to the last node
	int count;		//number of nodes
	int index;

	LinkedList();	//default constructor 
					//first = NULL
					//last = NULL
					//count = 0
	
	
	
	void insertFirstNode(int newinfo); // inserts a node
							//at the start of the list
	void insertLastNode(int newinfo);  // inserts a node
							//at the end of the list
	int getNodeCount();  //returns the number of nodes
	Node* getFirst();	 //returns the pointer to the 
						 //the first node - the value of "head"
	void displayData();
};







#include "coursework.h"
#include <iostream>
#include <cstdlib> 
#include <ctime> 
using namespace std;



void insertionSort(LinkedList *ll){
	Node *current = ll->head->link;  //2 variables used for searching 
	Node *trailCurrent = ll->head;	 //through the linked list
			//now use them to find how much of the list is currently
			//in order, by starting at the head and continuing so long 
			//as the data in one node is greater than that in the previous node 
	while(current->data > trailCurrent->data){
		trailCurrent = current;
		current = current->link;
	}
	Node *lastInOrder = trailCurrent;	//place the information obtained
	Node *firstOutOfOrder = current;	//above into these variables

	
	//now start at the end of the ordered part of the list and deal one
	//at a time with the unordered nodes  
	while(lastInOrder->link != NULL){
		//first special case - the next node must go to the start of the list
		if(lastInOrder->data < firstOutOfOrder->data){
			lastInOrder = firstOutOfOrder;
			firstOutOfOrder = firstOutOfOrder->link; //see below - repeat
		}											 //but no harm done
		else{
			//second special case - the first out of order node is greater
			//than any in the ordered list and stays where it is, but the pointers
			//"lastInOrder" & "firstOutOfOrder" must now be made to point to
			//the nodes along one 
			if(ll->head->data > firstOutOfOrder->data){
				lastInOrder->link = firstOutOfOrder->link;
				firstOutOfOrder->link = ll->head;
				ll->head = firstOutOfOrder;
			}
			//otherwise go through the linked list to find where the 
			//first out of order node goes and rearrange the links
			//accordingly
			else{
				current = ll->head->link;
				trailCurrent = ll->head;
				while(current->data < firstOutOfOrder->data){
					trailCurrent = current;
					current = current->link;
				}
				lastInOrder->link = firstOutOfOrder->link;
				firstOutOfOrder->link = current;
				trailCurrent->link = firstOutOfOrder;
			}
		
			firstOutOfOrder = lastInOrder->link;    //see above - repeat
		}
	}
}




		int main() 

{ 
    srand((unsigned)time(0)); 
    for(int index=0; index<40; index++){ 
        int height = (rand()%1400)+ 3000; 
		int distance = (rand()%280)+ 20; 
		cout << "Height	Distance" << endl;
		cout <<  height	<< "	"<<	distance <<  endl;
		
    } 
}




#include "coursework.h"


LinkedList::LinkedList(){
    head = NULL;
    last = NULL;
    count = 0;
}



void LinkedList::insertFirstNode(int newInfo){
	Node *newNode;
	newNode = new Node;
	newNode->data = newInfo;
	newNode->link = head; 
	head = newNode;

	count++;

	if(last == NULL){
		last = newNode;
	}
}

void LinkedList::insertLastNode(int newInfo){
	Node *newNode;
	newNode = new Node;
	newNode->data = newInfo;
	newNode->link = NULL; 
	

	count++;

	if(head == NULL){
		head = newNode;
		last = newNode;
	}
	else{
		last->link = newNode;
		last = newNode;
	}
}

int LinkedList::getNodeCount(){
	return count;
}

Node* LinkedList::getFirst(){
	return head;
}

void LinkedList::displayData(){
	Node *current = head;
	while (current != NULL){
		cout<<current->data<<endl;
		current = current->link;
	}
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

rand() never generates number in sequential order -- if it did then the numbers wouldn't be random. What you need to do is put the generated data into an array the sort the array by height.

Or put the numbers in the linked list in numerical order instead of just at the head or tail. You will need to write another function that will search the linked list to find the correct spot then insert a new node with the new hight (and other info) in that spot.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.