Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CK_Assessment3
- {
- ///<summary>
- /// anarray based list for integers
- ///</summary>
- class IntArrayList
- {
- private int count;
- private int[] values;
- //Default Constructor setting maximum size of list to 20
- public IntArrayList()
- {
- values = new int[20];
- count= 0;
- }
- //Overloaded Constructor
- public IntArrayList(int capacity)
- {
- values = new int[capacity];
- count = 0;
- }
- //Check to see if list is empty
- public bool isEmpty()
- {
- return(count==0); //
- }
- //Checks to see if list is full
- public bool isFull()
- {
- return values.Length==count; //Return full if no more space
- }
- //Adds value to first postition of array list
- public void addFirst(int value)
- {
- if(isFull())
- {
- throw new Exception("list full"); //Error message display if list is full
- }
- if(isEmpty())
- {
- addLast(value); //Adds value to first postion
- }
- else
- {
- for(int pos =count;pos>0;pos--)
- {
- values[pos]=values[pos-1]; ////Loops through list, decrementing position in list by 1
- }
- values[0]=value; //Sets first value in list to new value entered
- count++; //Count increased by 1
- }
- }
- //Adds value to last position
- public void addLast(int value)
- {
- if(isFull())
- {
- throw new Exception("list full"); //Checks if list is full, displays message if full
- }
- values[count]=value; //Sets last value in list to new value entered
- count++;
- }
- //Removing last value
- public int removeLast()
- {
- if(isEmpty())
- {
- throw new Exception("list Empty"); //Checks if list is empty, displays message if empty
- }
- int popValue=values[count-1]; //Removes last value from list
- count--;
- return popValue; //Returns value that is removed
- }
- //Returns size of list
- public int size()
- {
- return count;
- }
- //consoleUI for testing
- public void display()
- {
- if(count==0)
- Console.WriteLine("list is empty");
- else
- {
- Console.WriteLine("list has"+ count+"items");
- for(int i = count-1;i>=0;i--) Console.WriteLine("value:"+ values[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement