Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LLIndexList {
- private Customer info; // information stored in each node
- private LLNode<Customer> linList; // Link to the following node
- private int totalElements = 0;
- public LLNode<Customer> getPointerTo(int index) {
- LLNode<Customer> temp = linList;
- for (int i = 0; i < index; i++) {
- temp = temp.getLink();
- }
- return temp;
- }
- public LLIndexList() {
- linList = null;
- totalElements = 0;
- }
- public int size() {
- return totalElements;
- }
- public void printList() {
- LLNode<Customer> temp = linList;
- int count = 0;
- while (temp != null) {
- System.out.println(count + ": " + temp.getInfo());
- temp = temp.getLink();
- count++;
- }
- }
- Customer getItem(int index) {
- if (index == 0)
- return linList.getInfo();
- else
- return getPointerTo(index).getInfo();
- }
- public void insert(int index, Customer item) {
- LLNode<Customer> newNode = new LLNode<Customer>(item);
- LLNode<Customer> prevPtr = linList;
- // int count = 1;
- if (prevPtr == null || index == 0) {
- newNode.setLink(linList);
- linList = newNode;
- totalElements++;
- } else {
- prevPtr = getPointerTo(index - 1);
- newNode.setLink(prevPtr.getLink());
- prevPtr.setLink(newNode);
- totalElements++;
- }
- }
- public void deleteItem(int index) {
- LLNode<Customer> prevPtr = linList;
- if (linList == null)
- return;
- prevPtr = getPointerTo(index - 1);
- if (index == 0) {
- linList = linList.getLink();
- totalElements--;
- } else {
- LLNode<Customer> deleteMe = prevPtr.getLink();
- prevPtr.setLink(deleteMe.getLink());
- totalElements--;
- }
- }
- public int findInSorted(Customer item) {
- LLNode<Customer> temp = linList;
- for (int index = 0; index < totalElements; index++) {
- temp = getPointerTo(index);
- if (temp.getInfo().getKey() == item.getKey()) {
- return index;
- }
- }
- return -1;
- }
- public int locationToInsert(Customer item) {
- LLNode<Customer> temp = linList;
- int count = 0;
- while (temp != null && temp.getInfo().getKey() < item.getKey()) {
- temp = temp.getLink();
- count++;
- }
- return count;
- }
- public void insertIntoSorted(Customer cust) {
- int index = locationToInsert(cust);
- insert(index, cust);
- }
- public void deleteFromSorted(Customer cust) {
- int index = findInSorted(cust);
- deleteItem(index);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement