Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Упражнение 6: Задачa 4
- #include <iostream>
- #include <cmath>
- #define EPSILON 0.000001
- using namespace std;
- struct Item {
- double info;
- Item *next;
- };
- bool areEquals(double a, double b) {
- return fabs(a - b) < EPSILON;
- }
- Item* Create(Item* Head){
- Item* Last=NULL, *P;
- char ch;
- cout << " enter new element (Y/N) ?:";
- cin >> ch;
- while (ch == 'Y' || ch == 'y') {
- P = new Item;
- cout << " enter integer value of new element : ";
- cin >> P->info;
- P->next = NULL;
- if (Head == NULL) Head = P;
- else Last->next = P;
- Last = P;
- cout << " enter new element (Y/N) ?:";
- cin >> ch;
- }
- return Head;
- }
- void Traverse(Item* P) {
- while (P != NULL) { //или while (P) {
- cout << "next el:" << P->info << "\n";
- P = P->next;
- }
- }
- Item* Search(double X, Item* P) {
- while (P != NULL && !areEquals(P->info, X)){
- P = P->next;
- }
- return P;
- }
- int ListSize(Item* P){
- int cnt = 0;
- while (P != NULL){
- P = P->next;
- cnt++;
- }
- return cnt;
- }
- int SearchPos(double X, Item* P) {
- int pos = 0;
- while (P != NULL && !areEquals(P->info, X)){
- P = P->next;
- pos++;
- }
- return pos;
- }
- int main() {
- Item* Head = NULL;
- Head = Create(Head);
- Traverse(Head);
- double num;
- cout << "Enter element for searching: ";
- cin >> num;
- Item* El = Search(num, Head);
- if (El != NULL){
- cout << "Found. Position: " << SearchPos(num, Head) << endl;
- } else {
- cout << "Not found" << endl;
- }
- cout << "List size: " << ListSize(Head) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement