|
| 1 | +#include<iostream> |
| 2 | +using namespace std; |
| 3 | +struct Node { |
| 4 | + int data; |
| 5 | + struct Node* next; |
| 6 | +}*frist=NULL; |
| 7 | + |
| 8 | +void Create(int A[], int n) { |
| 9 | + int i; |
| 10 | + struct Node* t, * last; |
| 11 | + frist = new Node; |
| 12 | + frist->data = A[0]; |
| 13 | + frist->next = NULL; |
| 14 | + last = frist; |
| 15 | + |
| 16 | + for (i = 1; i < n; i++) { |
| 17 | + t = new Node; |
| 18 | + t->data = A[i]; |
| 19 | + t->next = NULL; |
| 20 | + last->next = t; |
| 21 | + last = t; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +void Display(struct Node* p) { |
| 26 | + while (p) { |
| 27 | + cout << p->data << "->"; |
| 28 | + p = p->next; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +void RDisplay(struct Node* p) { |
| 33 | + if (p) |
| 34 | + { |
| 35 | + cout << p->data << "->"; |
| 36 | + RDisplay(p->next); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +int Count(struct Node* p) |
| 41 | +{ |
| 42 | + int count = 0; |
| 43 | + while (p) { |
| 44 | + count += 1; |
| 45 | + p = p->next; |
| 46 | + } |
| 47 | + return count; |
| 48 | +} |
| 49 | + |
| 50 | +int RCount(struct Node* p) { |
| 51 | + if (p == NULL) |
| 52 | + return 0; |
| 53 | + else |
| 54 | + return RCount(p->next) + 1; |
| 55 | +} |
| 56 | + |
| 57 | +int Max(struct Node* p) { |
| 58 | + int max = INT_MIN; |
| 59 | + while (p) { |
| 60 | + if (p->data > max) |
| 61 | + max = p->data; |
| 62 | + p = p->next; |
| 63 | + } |
| 64 | + return max; |
| 65 | +} |
| 66 | + |
| 67 | +int RMax(struct Node* p) { |
| 68 | + int x = 0; |
| 69 | + if (p == 0) |
| 70 | + return INT_MIN; |
| 71 | + x = RMax(p->next); |
| 72 | + if (x > p->data) |
| 73 | + return x; |
| 74 | + else |
| 75 | + return p->data; |
| 76 | +} |
| 77 | + |
| 78 | +int Min(struct Node* p) |
| 79 | +{ |
| 80 | + int min = INT_MAX; |
| 81 | + while (p) { |
| 82 | + if (p->data < min) |
| 83 | + min = p->data; |
| 84 | + p = p->next; |
| 85 | + } |
| 86 | + return min; |
| 87 | +} |
| 88 | +int RMin(struct Node* p) { |
| 89 | + int x = 0; |
| 90 | + if (p == 0) |
| 91 | + return INT_MAX; |
| 92 | + x = RMin(p->next); |
| 93 | + if (x < p->data) |
| 94 | + return x; |
| 95 | + else |
| 96 | + return p->data; |
| 97 | +} |
| 98 | + |
| 99 | +int Sum(struct Node* p) { |
| 100 | + int sum = 0; |
| 101 | + while (p) |
| 102 | + { |
| 103 | + sum += p->data; |
| 104 | + p = p->next; |
| 105 | + } |
| 106 | + return sum; |
| 107 | +} |
| 108 | + |
| 109 | +int RSum(struct Node* p) |
| 110 | +{ |
| 111 | + if (p == 0) |
| 112 | + return 0; |
| 113 | + else |
| 114 | + return Sum(p->next) + p->data; |
| 115 | +} |
| 116 | + |
| 117 | +float Avg(struct Node* p) |
| 118 | +{ |
| 119 | + return Sum(frist) / Count(frist); |
| 120 | +} |
| 121 | + |
| 122 | +// binary search is not applicable in linkedlist |
| 123 | +// only linear search is applicable in the linkedlist |
| 124 | + |
| 125 | +struct Node* Search(struct Node* p, int key) { |
| 126 | + while (p) |
| 127 | + { |
| 128 | + if (key == p->data) |
| 129 | + return (p); |
| 130 | + p = p->next; |
| 131 | + } |
| 132 | + return NULL; |
| 133 | +} |
| 134 | + |
| 135 | +// move to head |
| 136 | +struct Node* Improved_Search(struct Node *p, int key) { |
| 137 | + Node* q = NULL; // take a extra pointer |
| 138 | + while (p) { |
| 139 | + if (key == p->data) { |
| 140 | + q->next = p->next; // q is present one before the key element so make q->next to the next of key element so that there will be a breakage of link |
| 141 | + p->next = frist; // then point key's element next to 1st so that it will become the 1st element of the linkedlist |
| 142 | + frist = p; // now move frist on p; |
| 143 | + return p; |
| 144 | + } |
| 145 | + q = p; |
| 146 | + p = p->next; |
| 147 | + } |
| 148 | + return NULL; |
| 149 | +} |
| 150 | + |
| 151 | +int main() { |
| 152 | + int A[] = { 545445,500,4,5,8,1 }; |
| 153 | + int n = sizeof(A) / sizeof(int); |
| 154 | + Create(A, n); |
| 155 | + //Display(frist); |
| 156 | + //RDisplay(frist); |
| 157 | + //cout << "The length of the linkedlist is ! = " << Count(frist); |
| 158 | + //cout << "The length of the linkedlist is ! = " << RCount(frist); |
| 159 | + //cout << "The maximum element in the linkedlist is ! = " << Max(frist); |
| 160 | + //cout << "The maximum element in the linkedlist is ! = " << RMax(frist); |
| 161 | + //cout<< "The minimum element in the linkedlist is ! = " << Min(frist); |
| 162 | + //cout<< "The minimum element in the linkedlist is ! = " << RMin(frist); |
| 163 | + //cout<< "The sum of the element in the linkedlist is ! = " << Sum(frist); |
| 164 | + //cout<< "The sum of the element in the linkedlist is ! = " << RSum(frist); |
| 165 | + //cout<< "The Average of the elements in the linkedlist is ! = " << Avg(frist); |
| 166 | + |
| 167 | + |
| 168 | + /*struct Node* temp = Search(frist, 5); |
| 169 | + if (temp) |
| 170 | + cout << temp->data << " is present in the list "; |
| 171 | + else |
| 172 | + cout << "key is not found ";*/ |
| 173 | + |
| 174 | + struct Node* temp = Improved_Search(frist,1); |
| 175 | + if (temp) |
| 176 | + cout << temp->data << " Is present in the list "; |
| 177 | + else |
| 178 | + cout << "Element not found"; |
| 179 | + |
| 180 | + return 0; |
| 181 | +} |
0 commit comments