Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct elem {
- int key;
- elem * next;
- }* start;
- void init();
- void add_b(int n);
- void add_mid1(int n, int k);
- void add_mid2(int n, int k);
- void add_e(int n);
- int del_b(int & n);
- int del_mid1(int & n, int k);
- int del_mid2(int & n, int k);
- int del_e(int & n);
- int search_iter(int n);
- void list();
- void destroy(elem *&listt);
- void addtolist(int array[], int l, elem *&s);
- void add_e(elem *&s, int n);
- int main() {
- int num, k, ch;
- init();
- do {
- cout << "\n Menu \n";
- cout << "1. Add the first element\n";
- cout << "2. Add element before k\n";
- cout << "3. Add element after k\n";
- cout << "4. Add the last element\n";
- cout << "5. Del the first element\n";
- cout << "6. Del element before k\n";
- cout << "7. Del element after k \n";
- cout << "8. Del the last element\n";
- cout << "9. Search element\n";
- cout << "10. Show the list\n";
- cout << "11. Destroy the list\n";
- cout << "12. Add to list\n";
- cout << "0. Exit\n";
- cout << "Your choice:";
- cin >> ch;
- switch (ch) {
- case 1:
- cout << "num=";
- cin >> num;
- add_b(num);
- break;
- case 2:
- cout << "num=";
- cin >> num;
- cout << "k=";
- cin >> k;
- add_mid1(num, k);
- break;
- case 3:
- cout << "num=";
- cin >> num;
- cout << "k=";
- cin >> k;
- add_mid2(num, k);
- break;
- case 4:
- cout << "num=";
- cin >> num;
- add_e(num);
- break;
- case 5:
- if (del_b(num))
- list();
- break;
- case 6:
- cout << "num=";
- cin >> num;
- cout << "k=";
- cin >> k;
- if (del_mid1(num, k))
- list();
- break;
- case 7:
- cout << "num=";
- cin >> num;
- cout << "k=";
- cin >> k;
- if (del_mid2(num, k))
- list();
- break;
- case 8:
- if (del_e(num))
- list();
- break;
- case 9:
- cout << "num=";
- cin >> num;
- if (search_iter(num))
- cout << "\n" << num << " is found!\n";
- else
- cout << "\n " << num << " is not found!\n";
- break;
- case 10:
- list();
- break;
- case 11:
- destroy(start);
- break;
- case 12:
- {
- int array[10] = { 10, 20, 30, 55, 66, 20, 54, 554, 534, 5325};
- addtolist(array, 10, start);
- }
- break;
- }
- } while (ch != 0);
- return 0;
- system("pause");
- }
- void add_e(elem *&s, int n)
- {
- elem *p = s, *q = s;
- q = new elem;
- q->key = n;
- q->next = NULL;
- if (s)
- {
- while (p -> next)
- p = p -> next;
- p -> next = q;
- } else
- s = q;
- }
- void addtolist(int array[], int l, elem *&s)
- {
- for (int i = 0; i < l; i++)
- {
- add_e(s, array[i]);
- }
- }
- void destroy(elem *&listt)
- {
- while (listt)
- {
- elem *t = listt;
- cout << "Deleting: " << listt->key << endl;
- listt = listt->next;
- delete t;
- }
- }
- void init() {
- start = NULL;
- }
- void add_b(int n) {
- elem * p = start;
- start = new elem;
- start -> key = n;
- start -> next = p;
- }
- void add_mid1(int n, int k) {
- elem * q = NULL, * p = start;
- while (p -> key != k)
- p = p -> next;
- q = new elem;
- q -> next = p -> next;
- q -> key = p -> key;
- p -> next = q;
- p -> key = n;
- }
- void add_mid2(int n, int k) {
- elem * q = NULL, * p = start;
- while (p -> key != k)
- p = p -> next;
- q = new elem;
- q -> next = p -> next;
- q -> key = n;
- p -> next = q;
- }
- void add_e(int n) {
- elem * p = start, * q = start;
- q = new elem;
- q -> key = n;
- q -> next = NULL;
- if (start) {
- while (p -> next)
- p = p -> next;
- p -> next = q;
- } else
- start = q;
- }
- int del_b(int & n) {
- elem * p = start;
- if (start) {
- n = start -> key;
- start = start -> next;
- delete p;
- return 1;
- } else
- return 0;
- }
- int del_mid1(int & n, int k) {
- elem * q = NULL, * p = start;
- if (start) {
- while (p -> key != k) {
- q = p;
- p = p -> next;
- }
- n = q -> key;
- q -> next = p -> next;
- q -> key = p -> key;
- delete p;
- return 1;
- } else
- return 0;
- }
- int del_mid2(int & n, int k) {
- elem * q = NULL, * p = start;
- if (start) {
- while (p -> key != k)
- p = p -> next;
- q = p -> next;
- n = q -> key;
- p -> next = q -> next;
- delete q;
- return 1;
- } else
- return 0;
- }
- int del_e(int & n) {
- elem * q = NULL, * p = start;
- if (start) {
- if (p -> next) {
- while (p -> next) {
- q = p;
- p = p -> next;
- }
- q -> next = NULL;
- } else
- start = NULL;
- n = p -> key;
- delete p;
- return 1;
- } else
- return 0;
- }
- int search_iter(int n) {
- elem * p = start;
- if (start) {
- while ((p -> key != n) && (p -> next))
- p = p -> next;
- if (p -> key == n)
- return 1;
- else
- return 0;
- }
- return 0;
- }
- void list()
- {
- elem *p = start;
- if (start)
- while (p)
- {
- cout << p->key << "\t";
- p = p->next;
- }
- cout << "\n\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement