Advertisement
Huskymeister

Ordered List

Feb 14th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. /*
  2. Mark Brown
  3. Data Structures - Ordered List
  4. 5-2-2016
  5. */
  6.  
  7. #include "stdafx.h"
  8. #include <iostream>
  9.  
  10. #define SIZE 5
  11. #define TRUE 1
  12. #define FALSE 0
  13.  
  14. using namespace std;
  15.  
  16. struct list
  17. {
  18.     int tail = 0;
  19.     int content[SIZE];
  20. };
  21.  
  22. int menu();
  23. void init(struct list*);
  24. void add(struct list*, int n);
  25. int remove(struct list*, int i);
  26. int get(struct list, int i);
  27. int search(struct list, int n);
  28. bool isFull(struct list);
  29. bool isEmpty(struct list);
  30.  
  31. int main()
  32. {
  33.     list myList;
  34.     init(&myList);
  35.     int choice;
  36.     while ((choice = menu()) != 0)
  37.     {
  38.         int val = 0;
  39.         int pos;
  40.  
  41.         switch (choice)
  42.         {
  43.         // Adds an item
  44.         case 1:
  45.             system("cls");
  46.  
  47.            
  48.             cout << "Enter value to add..   ";
  49.             cin >> val;
  50.             cout << endl;
  51.  
  52.             add(&myList, val);
  53.             break;
  54.        
  55.         // Removes a value at position
  56.         case 2:
  57.             system("cls");
  58.  
  59.             cout << "Remove value at position..   ";
  60.             cin >> val;
  61.             cout << endl;
  62.  
  63.             cout << "Removed value was..   " << remove(&myList, val) << endl;
  64.             system("pause");
  65.             break;
  66.  
  67.         // Returns value at position
  68.         case 3:
  69.             system("cls");
  70.  
  71.             cout << "Get value at position..   ";
  72.             cin >> val;
  73.             cout << endl;
  74.  
  75.             cout << "Value at position..   " << val << "   ..is..   " << get(myList, val) << endl;;
  76.             system("pause");
  77.             break;
  78.  
  79.         // Searches for a number
  80.         case 4:
  81.             system("cls");
  82.  
  83.             cout << "Search for number..   ";
  84.             cin >> val;
  85.             cout << endl;
  86.  
  87.             pos = search(myList, val);
  88.             if (pos > 0){
  89.                 cout << "Found at position..   " << pos << endl;
  90.             }
  91.             else
  92.             {
  93.                 cout << "Not found." << endl;
  94.             }
  95.             system("pause");
  96.             break;
  97.  
  98.         // Prints the list
  99.         case 5:
  100.             system("cls");
  101.  
  102.             cout << "List.. " << endl;
  103.             for (int i = 0; i < myList.tail; i++)
  104.             {
  105.                 cout << i + 1 << ". " << get(myList, i) << endl;
  106.             }
  107.             system("pause");
  108.         }
  109.     };
  110.     return 0;
  111. }
  112.  
  113. // Displays Menu for user to use
  114. int menu()
  115. {
  116.     system("cls");
  117.  
  118.     cout << "Menu" << endl;
  119.     cout << "1. Add item." << endl;
  120.     cout << "2. Remove item at.." << endl;
  121.     cout << "3. Get item at.. " << endl;
  122.     cout << "4. Search for.. " << endl;
  123.     cout << "5. Print List." << endl;
  124.     cout << "0. Exit." << endl;
  125.  
  126.     int choice;
  127.     cin >> choice;
  128.     return choice;
  129. }
  130.  
  131. // Fills the list with 0s
  132. void init(struct list* l)
  133. {
  134.     for (int i = 0; i < SIZE; i++)
  135.     {
  136.         l->content[i] = 0;
  137.     }
  138. }
  139.  
  140. // Adds a value to the end of the list then sorts the list
  141. void add(struct list* l, int n)
  142. {
  143.     l->content[l->tail++] = n;
  144.  
  145.     int i = l->tail-2;
  146.     while (n < l->content[i])
  147.     {
  148.         int tmp = l->content[i];
  149.         l->content[i] = l->content[i + 1];
  150.         l->content[i + 1] = tmp;
  151.         i--;
  152.     }
  153.  
  154. }
  155.  
  156. // Removes the value in the list at the position
  157. int remove(struct list* l, int i)
  158. {
  159.     if (i < l->tail)
  160.     {
  161.         int tmp = l->content[i];
  162.  
  163.         for (i; i < l->tail; i++)
  164.         {
  165.             l->content[i] = l->content[i + 1];
  166.         }
  167.  
  168.         l->tail--;
  169.         return tmp;
  170.     }
  171.     else
  172.     {
  173.         return 0;
  174.     }
  175. }
  176.  
  177. // Returns the value at a position of the list
  178. int get(struct list l, int i)
  179. {
  180.     return l.content[i];
  181. }
  182.  
  183. // Searches the list for a value in the list then returns the position if found otherwise it returns -1
  184. int search(struct list l, int n)
  185. {
  186.  
  187.     for (int i = 0; i < l.tail; i++)
  188.     {
  189.         if (l.content[i] == n){
  190.             return i;
  191.         }
  192.     }
  193.     return -1;
  194. }
  195.  
  196. // Checks if the given list is full
  197. bool isFull(struct list l)
  198. {
  199.     if (l.tail > SIZE)
  200.     {
  201.         return TRUE;
  202.     }
  203.     else
  204.     {
  205.         return FALSE;
  206.     }
  207. }
  208.  
  209. // checks if the list is empty
  210. bool isEmpty(struct list l)
  211. {
  212.     if (l.tail == 0)
  213.     {
  214.         return TRUE;
  215.     }
  216.     else
  217.     {
  218.         return FALSE;
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement