-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingly_linked_list.cpp
More file actions
142 lines (127 loc) · 2.64 KB
/
Copy pathSingly_linked_list.cpp
File metadata and controls
142 lines (127 loc) · 2.64 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
#include <iostream>
#include <math.h>
using namespace std;
/* a Node of the doubly linked list */
struct Node
{
int data;
struct Node *next;
Node(){
next = NULL;
}
Node(int x){
data = x;
next = NULL;
}
};
void insert_at_head(Node** h, int x){
if(!(*h)){*h = new Node(x);return;}
Node* temp = *h;
while(temp->next){temp = temp->next;}
Node* n = new Node(x);
temp->next = n;
}
void print_list(Node** h){
Node* temp = *h;
cout << "The linked list is : ";
while(temp){
cout << temp->data << " " ;
temp = temp->next;
}
cout << endl;
}
void lens(Node** h){
Node* temp = *h;
int l = 0;
while(temp){
l++;
temp = temp->next;
}
cout << "len of linked list is " << l << endl;
}
int get_len(Node** h){
Node* temp = *h;
int l = 0;
while(temp){
l++;
temp = temp->next;
}
return l;
}
void delete_element(Node** h, int k){
Node* temp = *h;
if(temp->data == k){*h = temp->next;return ;}
while(temp->next){
if(temp->next->data == k){
temp ->next = temp->next->next;return;
}
temp = temp->next;
}
cout << "element not present in the linked list!! " << endl;
return;
}
void find_pos(Node** h, int k){
Node* temp = *h;
int pos = 0;
while(temp){
if(temp->data == k){cout << "the element " << k << " is at position " << pos << endl;return;}
temp = temp->next;
pos++;
}
cout << "element not found!!! " << endl;
}
void swap_values_of_given_indices (Node** h, int p, int q){
if(p>=get_len(h) || q>=get_len(h)){
cout << "indices out of range!! " << endl; return;
}
else if(p==q){cout << "no change !!" << endl; return;}
int c = 0;
int m = min(p,q);
int n = p+q-m;
Node* temp = *h;
int a,b;
Node* temp_1=NULL;
Node* temp_2=NULL;
while(temp){
if(m==c){
temp_1 = temp;
a = temp->data;
}
else if(n==c){
temp_2 = temp;
b = temp->data;
}
temp = temp->next;
c++;
}
temp_1->data = b;
temp_2->data = a;
}
int main(){
int n;
cout << "Enter the number of elements of the linked list: ";
cin >> n;
Node* head = NULL;
for(int i=0;i<n;i++){
int x;
cin >> x;
insert_at_head(&head,x);
}
print_list(&head);
lens(&head);
int dele;
cout << "enter the element to be deleted :" << endl;
cin >> dele;
delete_element(&head, dele);
print_list(&head);
int ch;
cout << "enter the element to be checked and find its pos : ";
cin >> ch;
find_pos(&head, ch);
int m_swap,n_swap;
cout << "Enter the indices of the linked lists to be swapped " << endl;
cin >> m_swap >> n_swap;
swap_values_of_given_indices(&head, m_swap,n_swap);
print_list(&head);
return 0;
}