Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Mark Brown
- Data Structures - Ordered List
- 5-2-2016
- */
- #include "stdafx.h"
- #include <iostream>
- #define SIZE 5
- #define TRUE 1
- #define FALSE 0
- using namespace std;
- struct list
- {
- int tail = 0;
- int content[SIZE];
- };
- int menu();
- void init(struct list*);
- void add(struct list*, int n);
- int remove(struct list*, int i);
- int get(struct list, int i);
- int search(struct list, int n);
- bool isFull(struct list);
- bool isEmpty(struct list);
- int main()
- {
- list myList;
- init(&myList);
- int choice;
- while ((choice = menu()) != 0)
- {
- int val = 0;
- int pos;
- switch (choice)
- {
- // Adds an item
- case 1:
- system("cls");
- cout << "Enter value to add.. ";
- cin >> val;
- cout << endl;
- add(&myList, val);
- break;
- // Removes a value at position
- case 2:
- system("cls");
- cout << "Remove value at position.. ";
- cin >> val;
- cout << endl;
- cout << "Removed value was.. " << remove(&myList, val) << endl;
- system("pause");
- break;
- // Returns value at position
- case 3:
- system("cls");
- cout << "Get value at position.. ";
- cin >> val;
- cout << endl;
- cout << "Value at position.. " << val << " ..is.. " << get(myList, val) << endl;;
- system("pause");
- break;
- // Searches for a number
- case 4:
- system("cls");
- cout << "Search for number.. ";
- cin >> val;
- cout << endl;
- pos = search(myList, val);
- if (pos > 0){
- cout << "Found at position.. " << pos << endl;
- }
- else
- {
- cout << "Not found." << endl;
- }
- system("pause");
- break;
- // Prints the list
- case 5:
- system("cls");
- cout << "List.. " << endl;
- for (int i = 0; i < myList.tail; i++)
- {
- cout << i + 1 << ". " << get(myList, i) << endl;
- }
- system("pause");
- }
- };
- return 0;
- }
- // Displays Menu for user to use
- int menu()
- {
- system("cls");
- cout << "Menu" << endl;
- cout << "1. Add item." << endl;
- cout << "2. Remove item at.." << endl;
- cout << "3. Get item at.. " << endl;
- cout << "4. Search for.. " << endl;
- cout << "5. Print List." << endl;
- cout << "0. Exit." << endl;
- int choice;
- cin >> choice;
- return choice;
- }
- // Fills the list with 0s
- void init(struct list* l)
- {
- for (int i = 0; i < SIZE; i++)
- {
- l->content[i] = 0;
- }
- }
- // Adds a value to the end of the list then sorts the list
- void add(struct list* l, int n)
- {
- l->content[l->tail++] = n;
- int i = l->tail-2;
- while (n < l->content[i])
- {
- int tmp = l->content[i];
- l->content[i] = l->content[i + 1];
- l->content[i + 1] = tmp;
- i--;
- }
- }
- // Removes the value in the list at the position
- int remove(struct list* l, int i)
- {
- if (i < l->tail)
- {
- int tmp = l->content[i];
- for (i; i < l->tail; i++)
- {
- l->content[i] = l->content[i + 1];
- }
- l->tail--;
- return tmp;
- }
- else
- {
- return 0;
- }
- }
- // Returns the value at a position of the list
- int get(struct list l, int i)
- {
- return l.content[i];
- }
- // Searches the list for a value in the list then returns the position if found otherwise it returns -1
- int search(struct list l, int n)
- {
- for (int i = 0; i < l.tail; i++)
- {
- if (l.content[i] == n){
- return i;
- }
- }
- return -1;
- }
- // Checks if the given list is full
- bool isFull(struct list l)
- {
- if (l.tail > SIZE)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- // checks if the list is empty
- bool isEmpty(struct list l)
- {
- if (l.tail == 0)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement