Hello, I am relatively new to C++ and am trying to bubble sort my linked list that creates 100 random integers. Everything works, but I am unsure how to continue this to include a bubble sorting method. Thanks for all the help!
#include "stdafx.h"
#include <iostream>
usingnamespace std;
class Node{
public:
int data; //set data
Node * next; // set node next
Node(int x){
data = x; //data = whatever
next = NULL; //next is null
}
Node(int x, Node *y){ //helps set the next step for the linkedlist
data = x;
next = y;
}
};
class linkedList{ // my linked list class
Node*head;
public:
linkedList(){
head = NULL;
}
void addNode(int value){ //adds nodes
Node *p;
if (head == NULL)
head = new Node(value, NULL);
else{
p = head;
while (p->next != NULL)
p = p->next;
p->next = new Node(value, NULL);
}
}
void print(){ // calls to print nodes
Node * p;
p = head;
while (p != NULL){
cout << p->data << "\n";
p = p->next;
}
}
};
int main(void){ //main
int random;
linkedList get; //get linked list
for (int x = 0; x < 20; x++){ //random integers loop
random = rand() % 100 + 1;
get.addNode(random);
}
get.print();
return(0);
}