Skip to content

Instantly share code, notes, and snippets.

@dbc2201
Created January 18, 2019 04:47
Show Gist options
  • Save dbc2201/1768d0758107b8f4d6abdd3f039a80bc to your computer and use it in GitHub Desktop.
Save dbc2201/1768d0758107b8f4d6abdd3f039a80bc to your computer and use it in GitHub Desktop.
code for List ADT in class 2E List is for primitive integers
package list;
import java.util.Arrays;
public class ListADT
{
int[] list = new int[10];
int bottom = -1;
void insert(int value)
{
if (!isFull())
{
for (int i = 0; i < list.length; i++)
{
if (list[i] == 0)
{
list[i] = value;
bottom++;
break;
}
}
}
else
{
System.out.println("Overflow!");
}
}
boolean isEmpty()
{
if (bottom == -1)
{
return true;
}
else
{
return false;
}
}
boolean isFull()
{
if (bottom == list.length - 1 && list[bottom] != 0)
{
return true;
}
else
{
return false;
}
}
int remove(int index)
{
list[index] = 0;
int i;
for (i = index; i < list.length - 1; i++)
{
// list[index] = list[index + 1];
list[i] = list[i + 1];
System.out.println("i = " + i);
// list[0] = list[0 + 1];
}
list[i] = 0;
// return value;
return 0;
}
public static void main(String[] args)
{
ListADT list1 = new ListADT();
System.out.println(Arrays.toString(list1.list));
for (int i = 0; i < list1.list.length; i++)
{
list1.insert(i);
}
System.out.println(Arrays.toString(list1.list));
System.out.println(list1.isFull());
list1.remove(4);
System.out.println(Arrays.toString(list1.list));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment