-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication_on_linkedList.cpp
121 lines (111 loc) · 2.19 KB
/
application_on_linkedList.cpp
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
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
class Node *next;
};
void traversalOfElements(Node *head)
{
while (head != NULL) // head head != 0
{
cout << (*head).data << " ";
head = head->next;
}
}
Node *insert_ele_at_begining(int ele, Node *head)
{
Node *newNode = new Node;
(*newNode).data = ele;
newNode->next = head;
head = newNode;
return head;
}
void linear_search(Node *head, int ele)
{
while (head != NULL)
{
if (head->data == ele)
{
cout << "Found";
return;
}
head = head->next;
}
}
Node *improved_linear_seach(Node *head, int ele)
{
Node *p = head;
Node *q;
while (p)
{
if (p->data == ele)
{
cout << "Found";
q->next = p->next;
p->next = head;
return p;
}
else
{
q = p;
p = p->next;
}
}
}
//Deleting Dublicats from sorted linked list
void dub_delete(Node *head)
{
Node *p = head;
Node *q = p->next;
while (q)
{
if (p->data != q->data)
{
p = q;
q = q->next;
}
else
{
p->next = q->next;
delete q;
q = p->next;
}
}
}
//Here we are reversing links
Node *reversing_linked_list(Node *head)
{
Node *p = head;
Node *q = NULL;
Node *r = NULL;
//r->next = NULL;
while (p != NULL)
{
r = q;
q = p;
p = p->next;
q->next = r;
}
return q;
}
int main()
{
Node *head = new Node; //head Node pointer of linked list
head->data = 0;
head->next = NULL;
int arr[] = {1, 2, 2, 2, 2, 2, 3, 4, 5, 2, 2, 6, 7, 8, 9}; //taking array for input in linked list
int n = sizeof(arr) / sizeof(arr[0]); //size of array
for (int i = 0; i < n; i++)
{
head = insert_ele_at_begining(arr[i], head);
}
traversalOfElements(head);
cout << endl;
//head = improved_linear_seach(head, 5);
//dub_delete(head);
head = reversing_linked_list(head);
cout << endl;
traversalOfElements(head);
return 0;
}