-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinklist.cpp
More file actions
211 lines (196 loc) · 4.4 KB
/
Copy pathlinklist.cpp
File metadata and controls
211 lines (196 loc) · 4.4 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <bits/stdc++.h>
using namespace std;
typedef struct node *nodePtr;
typedef struct node
{
int data;
nodePtr next;
} node;
void printList(node *head) //This function prints the list
{
while (head->next != NULL)
{
cout << head->data << "->";
head = head->next;
}
cout << head->data;
cout << endl;
return;
}
void atLast(int n, nodePtr *head) //this function adds the list from last
{
nodePtr temp = *head;
nodePtr newPtr = (nodePtr)malloc(sizeof(node));
newPtr->data = n;
newPtr->next = NULL;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newPtr;
return;
}
nodePtr pushAtFirst(int n, nodePtr head) //this function adds the list from the begining this is not a void function it returns the adress of the newly created link list
{
nodePtr newptr = (nodePtr)malloc(sizeof(node));
newptr->data = n;
newptr->next = head;
return newptr;
}
void insertAtPos(int n, int pos, nodePtr *head) //this function inserts at the given position as per '0' indexing
{
nodePtr temp = *head;
nodePtr newnode = (nodePtr)malloc(sizeof(node));
newnode->data = n;
int po = 0;
while (po < pos - 1)
{
temp = temp->next;
po++;
}
nodePtr temp2 = temp->next;
temp = *head;
po = 0;
while (po < pos - 1)
{
temp = temp->next;
po++;
}
temp->next = newnode;
newnode->next = temp2;
return;
}
void insertAfter(int n, int m, nodePtr *head) //this function adds the new list after the targeted data
{
nodePtr temp = *head;
nodePtr newNode = (nodePtr)malloc(sizeof(node));
newNode->data = m;
int datas;
while (temp->data != n)
{
datas = temp->data;
temp = temp->next;
if (temp == NULL)
return;
}
newNode->next = temp->next;
temp->next = newNode;
return;
}
void del(int n, nodePtr *head) //this function deletes the list with the value if there are multiple values then it deletes the first occurance of it
{
nodePtr temp = *head;
int data1;
while (temp->data != n)
{
data1 = temp->data;
temp = temp->next;
}
nodePtr temp2 = temp->next;
temp = *head;
while (temp->data != data1)
{
temp = temp->next;
}
temp->next = temp2;
return;
}
nodePtr findList(int n, nodePtr head) //find list find the data and returns it's adress if present;
{
while (head->data != n)
{
head = head->next;
if (head == NULL)
return NULL;
}
return head;
}
int length(nodePtr head) //finds the length of the list
{
int length = 0;
while (head != NULL)
{
length++;
head = head->next;
}
return length;
}
int minimum(nodePtr head) //returns the minimum of the list
{
int min = INT_MAX;
while (head != NULL)
{
int dat = head->data;
min = min < dat ? min : dat;
head = head->next;
}
return min;
}
int maximum(nodePtr head) //returns the maximum of the list
{
int min = INT_MAX;
while (head != NULL)
{
int dat = head->data;
min = min > dat ? min : dat;
head = head->next;
}
return min;
}
void sortAcc(nodePtr *head)//bubble sort technique to sort the list in ascending order
{
nodePtr temp = *head;
nodePtr curr = temp;
nodePtr next = temp->next;
while(curr)
{
while(next)
{
if(curr->data > next->data)
{
swap(curr->data,next->data);
}
next = next->next;
}
curr = curr->next;
}
}
void sortdec(nodePtr *head)//bubble sort technique to sort the list in decending order
{
nodePtr temp = *head;
nodePtr curr = temp;
nodePtr next = temp->next;
while(curr)
{
next = curr->next;
while(next)
{
if(curr->data < next->data)
{
swap(curr->data,next->data);
}
next = next->next;
}
curr = curr->next;
}
}
int main()
{
nodePtr head = (nodePtr)malloc(sizeof(node));
nodePtr first = (nodePtr)malloc(sizeof(node));
head->data = 1;
head->next = first;
first->data = 2;
first->next = NULL;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int t;
cin >> t;
atLast(t, &head);
}
sortAcc(&head);
printList(head);
return 0;
}