Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Упражнение 6: Задачи от 1, 2 и 3
- #include <iostream>
- using namespace std;
- struct Item {
- int info;
- Item *next;
- };
- 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(int X, Item* P) {
- while (P != NULL && 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(int X, Item* P) {
- int pos = 0;
- while (P != NULL && P->info != X){
- P = P->next;
- pos++;
- }
- return pos;
- }
- int main() {
- Item* Head = NULL;
- Head = Create(Head);
- Traverse(Head);
- int 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