-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList-Num-Sort.cpp
More file actions
95 lines (85 loc) · 1.95 KB
/
LinkedList-Num-Sort.cpp
File metadata and controls
95 lines (85 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
using namespace std;
struct Node
{
int n;
Node *next;
};
////////////////////----------------------------////////////////////////////
class LinkedList
{
public:
Node *L; // start pointer
LinkedList(); // constructor
void addAtFront(int x); // it will add a new node at the front of the list
void print(Node *x); //prints all nodes in the linked list
void sort(Node *x);
//add function deleteFromFront()
};
//////////////////-----------------------------////////////////////////////
int main(int argc, char **argv)
{
Node *L;
L = new Node;
L->n = 13;
L->next = new Node;
L->next->n = 11;
L->next->next = new Node;
L->next->next->n = 27;
L->next->next->next = new Node;
L->next->next->next->n = 6;
L->next->next->next->next = new Node;
L->next->next->next->next->n = 19;
L->next->next->next->next->next = new Node;
L->next->next->next->next->next->n = 49;
L->next->next->next->next->next->next = NULL;
//---------------------------------------------//
LinkedList list;
list.print(L);
list.sort(L);
list.print(L);
return 0;
}
////////////////--------------///////////////////
////////////////--------------///////////////////
LinkedList::LinkedList()
{
L = NULL;
}
/////////////////------------///////////////////
/////////////////------------///////////////////
//--------------------------------------------//
/////////////////------------///////////////////
/////////////////------------///////////////////
void LinkedList::sort(Node *L)
{
Node *current;
current = L;
//3 6 11 19 27 49//
while (current->next != NULL)
{
if (current->n > current->next->n)
{
int x = current->n;
current->n = current->next->n;
current->next->n = x;
current = L;
}
else
{
current = current->next;
}
}
}
///////////////////////////------------//////////////////////////
void LinkedList::print(Node *L)
{
Node *temp;
temp = L;
cout << "\n-->";
while (temp != NULL)
{
cout << " " << temp->n;
temp = temp->next;
}
}