-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
102 lines (96 loc) · 2.41 KB
/
Copy pathLinkedList.cpp
File metadata and controls
102 lines (96 loc) · 2.41 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
96
97
98
99
100
101
102
#include <iostream>
#include <string>
class Node{
public:
char data;
struct Node* next;
Node(char elem): data(elem),next(nullptr){};
};
class LinkedList{
public:
Node* head;
public:
LinkedList():head(nullptr){};
LinkedList(const std::string& inputString) : head(nullptr) {
for (char elem : inputString) {
insert(elem);
}
}
void insert(char c){
Node* newNode=new Node(c);
if(!head){
head = newNode;
}
else{
Node* current=head;
while(current->next){
current=current->next;
}
current->next=newNode;
}
}
void display(){
Node* current=head;
while(current){
std::cout << current->data;
current=current->next;
}
std::cout << std::endl;
}
~LinkedList(){
Node* current=head;
while(current){
Node* temp=current;
current=current->next;
delete temp;
}
}
void ring(){
Node* current=head;
while(current->next){
current=current->next;
}
current->next=head;
}
void swap(Node** n1, Node** n2) {
Node* temp = *n1;
*n1 = *n2;
*n2 = temp;
temp=(*(*n2)).next;
(*(*n2)).next=(*(*n1)).next;
(*(*n1)).next=temp;
}
Node* getHead(){
return head;
}
void sort(){
Node** node1 = &(head);
while (*node1 != nullptr) {
Node** temp = &((*node1)->next);
Node** minNode=node1;
while ((*temp) != nullptr) {
if(((*temp)->data)<((*minNode)->data)){
minNode=temp;
}
temp=&((*temp)->next);
}
if(minNode!=node1){
swap(minNode,node1);
}
node1=&((*node1)->next);
}
}
};
int main() {
LinkedList list("gfedcba");
list.sort();
list.display();
return 0;
}
int getMaxIndex(){
current=head;
int index=0;
while(current){
current=
}
}