Unit 3 Chapter 5 Linked Lists Code Flashcards
1
Q
sorted list
A
//////////////////////////////////////////////////////////////// class Link { public long dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; } // ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); } } // end class Link //////////////////////////////////////////////////////////////// class SortedList { private Link first; // ref to first item // ------------------------------------------------------------- public SortedList() // constructor { first = null; } // ------------------------------------------------------------- public boolean isEmpty() // true if no links { return (first==null); } // ------------------------------------------------------------- public void insert(long key) // insert, in order { Link newLink = new Link(key); // make new link Link previous = null; // start at first Link current = first; // until end of list, while(current != null && key > current.dData) { // or key > current, previous = current; current = current.next; // go to next item } if(previous==null) // at beginning of list first = newLink; // first --> newLink else // not at beginning previous.next = newLink; // old prev --> newLink newLink.next = current; // newLink --> old currnt } // end insert() // ------------------------------------------------------------- public Link remove() // return & delete first link { // (assumes non-empty list) Link temp = first; // save first first = first.next; // delete first return temp; // return value } // ------------------------------------------------------------- public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } } // end class SortedList //////////////////////////////////////////////////////////////// class SortedListApp { public static void main(String[] args) { // create new list SortedList theSortedList = new SortedList(); theSortedList.insert(20); // insert 2 items theSortedList.insert(40);
theSortedList.displayList(); // display list theSortedList.insert(10); // insert 3 more items theSortedList.insert(30); theSortedList.insert(50); theSortedList.displayList(); // display list theSortedList.remove(); // remove an item
theSortedList.displayList(); // display list } // end main() } // end class SortedListApp ////////////////////////////////////////////////////////////////
2
Q
insertion sort
A
//////////////////////////////////////////////////////////////// class Link { public long dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; } // ------------------------------------------------------------- } // end class Link //////////////////////////////////////////////////////////////// class SortedList { private Link first; // ref to first item on list // ------------------------------------------------------------- public SortedList() // constructor (no args) { first = null; } // initialize list // ------------------------------------------------------------- public SortedList(Link[] linkArr) // constructor (array { // as argument) first = null; // initialize list for(int j=0; j current.dData) { // or key > current, previous = current; current = current.next; // go to next item } if(previous==null) // at beginning of list first = k; // first --> k else // not at beginning previous.next = k; // old prev --> k k.next = current; // k --> old currnt } // end insert() // ------------------------------------------------------------- public Link remove() // return & delete first link { // (assumes non-empty list) Link temp = first; // save first first = first.next; // delete first return temp; // return value } // ------------------------------------------------------------- } // end class SortedList //////////////////////////////////////////////////////////////// class ListInsertionSortApp { public static void main(String[] args) { int size = 10; // create array of links Link[] linkArray = new Link[size];
for(int j=0; j); } // end main() } // end class ListInsertionSortApp ////////////////////////////////////////////////////////////////
3
Q
linkStack
A
//////////////////////////////////////////////////////////////// class Link { public long dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; } // ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print(dData + " "); } } // end class Link //////////////////////////////////////////////////////////////// class LinkList { private Link first; // ref to first item on list // ------------------------------------------------------------- public LinkList() // constructor { first = null; } // no items on list yet // ------------------------------------------------------------- public boolean isEmpty() // true if list is empty { return (first==null); } // ------------------------------------------------------------- public void insertFirst(long dd) // insert at start of list { // make new link Link newLink = new Link(dd); newLink.next = first; // newLink --> old first first = newLink; // first --> newLink } // ------------------------------------------------------------- public long deleteFirst() // delete first item { // (assumes list not empty) Link temp = first; // save reference to link first = first.next; // delete it: first-->old next return temp.dData; // return deleted link } // ------------------------------------------------------------- public void displayList() { Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } // ------------------------------------------------------------- } // end class LinkList //////////////////////////////////////////////////////////////// class LinkStack { private LinkList theList; //-------------------------------------------------------------- public LinkStack() // constructor { theList = new LinkList(); } //-------------------------------------------------------------- public void push(long j) // put item on top of stack { theList.insertFirst(j); } //-------------------------------------------------------------- public long pop() // take item from top of stack { return theList.deleteFirst(); } //-------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return ( theList.isEmpty() ); } //-------------------------------------------------------------- public void displayStack() { System.out.print("Stack (top-->bottom): "); theList.displayList(); } //-------------------------------------------------------------- } // end class LinkStack //////////////////////////////////////////////////////////////// class LinkStackApp { public static void main(String[] args) { LinkStack theStack = new LinkStack(); // make stack
theStack.push(20); // push items theStack.push(40); theStack.displayStack(); // display stack theStack.push(60); // push items theStack.push(80); theStack.displayStack(); // display stack theStack.pop(); // pop items theStack.pop();
theStack.displayStack(); // display stack } // end main() } // end class LinkStackApp ////////////////////////////////////////////////////////////////
4
Q
link queue
A
//////////////////////////////////////////////////////////////// class Link { public long dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(long d) // constructor { dData = d; } // ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); } // ------------------------------------------------------------- } // end class Link //////////////////////////////////////////////////////////////// class FirstLastList { private Link first; // ref to first item private Link last; // ref to last item // ------------------------------------------------------------- public FirstLastList() // constructor { first = null; // no items on list yet last = null; } // ------------------------------------------------------------- public boolean isEmpty() // true if no links { return first==null; } // ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else last.next = newLink; // old last --> newLink last = newLink; // newLink old next return temp; } // ------------------------------------------------------------- public void displayList() { Link current = first; // start at beginning while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } // ------------------------------------------------------------- } // end class FirstLastList //////////////////////////////////////////////////////////////// class LinkQueue { private FirstLastList theList; //-------------------------------------------------------------- public LinkQueue() // constructor { theList = new FirstLastList(); } // make a 2-ended list //-------------------------------------------------------------- public boolean isEmpty() // true if queue is empty { return theList.isEmpty(); } //-------------------------------------------------------------- public void insert(long j) // insert, rear of queue { theList.insertLast(j); } //-------------------------------------------------------------- public long remove() // remove, front of queue { return theList.deleteFirst(); } //-------------------------------------------------------------- public void displayQueue() { System.out.print("Queue (front-->rear): "); theList.displayList(); } //-------------------------------------------------------------- } // end class LinkQueue //////////////////////////////////////////////////////////////// class LinkQueueApp { public static void main(String[] args) { LinkQueue theQueue = new LinkQueue(); theQueue.insert(20); // insert items theQueue.insert(40);
theQueue.displayQueue(); // display queue theQueue.insert(60); // insert items theQueue.insert(80); theQueue.displayQueue(); // display queue theQueue.remove(); // remove items theQueue.remove();
theQueue.displayQueue(); // display queue } // end main() } // end class LinkQueueApp ////////////////////////////////////////////////////////////////
5
Q
linkList2 adds methods to search a linked list and delete an item.
A
// ------------------------------------------------------------- public Link find(int key) // find link with given key { // (assumes non-empty list) Link current = first; // start at 'first' while(current.iData != key) // while no match, { if(current.next == null) // if end of list, return null; // didn't find it else // not end of list, current = current.next; // go to next link } return current; // found it } // ------------------------------------------------------------- public Link delete(int key) // delete link with given key { // (assumes non-empty list) Link current = first; // search for link Link previous = first; while(current.iData != key) { if(current.next == null) return null; // didn't find it else { previous = current; // go to next link current = current.next; } } // found it if(current == first) // if first link, first = first.next; // change first else // otherwise, previous.next = current.next; // bypass it return current; } // -------------------------------------------------------------
6
Q
linkList
A
//////////////////////////////////////////////////////////////// class Link { public int iData; // data item public double dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(int id, double dd) // constructor { iData = id; // initialize data dData = dd; // ('next' is automatically } // set to null) // ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print("{" + iData + ", " + dData + "} "); } } // end class Link //////////////////////////////////////////////////////////////// class LinkList { private Link first; // ref to first link on list
// ------------------------------------------------------------- public LinkList() // constructor { first = null; // no links on list yet } // ------------------------------------------------------------- public boolean isEmpty() // true if list is empty { return (first==null); } // ------------------------------------------------------------- // insert at start of list public void insertFirst(int id, double dd) { // make new link Link newLink = new Link(id, dd); newLink.next = first; // newLink --> old first first = newLink; // first --> newLink } // ------------------------------------------------------------- public Link deleteFirst() // delete first item { // (assumes list not empty) Link temp = first; // save reference to link first = first.next; // delete it: first-->old next return temp; // return deleted link } // ------------------------------------------------------------- public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } // ------------------------------------------------------------- } // end class LinkList //////////////////////////////////////////////////////////////// class LinkListApp { public static void main(String[] args) { LinkList theList = new LinkList(); // make new list
theList.insertFirst(22, 2.99); // insert four items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99); theList.insertFirst(88, 8.99); theList.displayList(); // display list while( !theList.isEmpty() ) // until it's empty, { Link aLink = theList.deleteFirst(); // delete link System.out.print("Deleted "); // display it aLink.displayLink(); System.out.println(""); } theList.displayList(); // display list } // end main() } // end class LinkListApp ////////////////////////////////////////////////////////////////
7
Q
interIterator
A
import java.io.*; // for I/O //////////////////////////////////////////////////////////////// class Link { public long dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; } // ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print(dData + " "); } } // end class Link //////////////////////////////////////////////////////////////// class LinkList { private Link first; // ref to first item on list
// ------------------------------------------------------------- public LinkList() // constructor { first = null; } // no items on list yet // ------------------------------------------------------------- public Link getFirst() // get value of first { return first; } // ------------------------------------------------------------- public void setFirst(Link f) // set first to new link { first = f; } // ------------------------------------------------------------- public boolean isEmpty() // true if list is empty { return first==null; } // ------------------------------------------------------------- public ListIterator getIterator() // return iterator { return new ListIterator(this); // initialized with } // this list // ------------------------------------------------------------- public void displayList() { Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } // ------------------------------------------------------------- } // end class LinkList //////////////////////////////////////////////////////////////// class ListIterator { private Link current; // current link private Link previous; // previous link private LinkList ourList; // our linked list //-------------------------------------------------------------- public ListIterator(LinkList list) // constructor { ourList = list; reset(); } //-------------------------------------------------------------- public void reset() // start at 'first' { current = ourList.getFirst(); previous = null; } //-------------------------------------------------------------- public boolean atEnd() // true if last link { return (current.next==null); } //-------------------------------------------------------------- public void nextLink() // go to next link { previous = current; current = current.next; } //-------------------------------------------------------------- public Link getCurrent() // get current link { return current; } //-------------------------------------------------------------- public void insertAfter(long dd) // insert after { // current link Link newLink = new Link(dd);
if( ourList.isEmpty() ) // empty list { ourList.setFirst(newLink); current = newLink; } else // not empty { newLink.next = current.next; current.next = newLink; nextLink(); // point to new link } } //-------------------------------------------------------------- public void insertBefore(long dd) // insert before { // current link Link newLink = new Link(dd);
if(previous == null) // beginning of list { // (or empty list) newLink.next = ourList.getFirst(); ourList.setFirst(newLink); reset(); } else // not beginning { newLink.next = previous.next; previous.next = newLink; current = newLink; } } //-------------------------------------------------------------- public long deleteCurrent() // delete item at current { long value = current.dData; if(previous == null) // beginning of list { ourList.setFirst(current.next); reset(); } else // not beginning { previous.next = current.next; if( atEnd() ) reset(); else current = current.next; } return value; } //-------------------------------------------------------------- } // end class ListIterator //////////////////////////////////////////////////////////////// class InterIterApp { public static void main(String[] args) throws IOException { LinkList theList = new LinkList(); // new list ListIterator iter1 = theList.getIterator(); // new iter long value; iter1. insertAfter(20); // insert items iter1. insertAfter(40); iter1. insertAfter(80); iter1. insertBefore(60);
while(true) { System.out.print("Enter first letter of show, reset, "); System.out.print("next, get, before, after, delete: "); System.out.flush(); int choice = getChar(); // get user's option switch(choice) { case 's': // show list if( !theList.isEmpty() ) theList.displayList(); else System.out.println("List is empty"); break; case 'r': // reset (to first) iter1.reset(); break; case 'n': // advance to next item if( !theList.isEmpty() && !iter1.atEnd() ) iter1.nextLink(); else System.out.println("Can't go to next link"); break; case 'g': // get current item if( !theList.isEmpty() ) { value = iter1.getCurrent().dData; System.out.println("Returned " + value); } else System.out.println("List is empty"); break; case 'b': // insert before current System.out.print("Enter value to insert: "); System.out.flush(); value = getInt(); iter1.insertBefore(value); break; case 'a': // insert after current System.out.print("Enter value to insert: "); System.out.flush(); value = getInt(); iter1.insertAfter(value); break; case 'd': // delete current item if( !theList.isEmpty() ) { value = iter1.deleteCurrent(); System.out.println("Deleted " + value); } else System.out.println("Can't delete"); break; default: System.out.println("Invalid entry"); } // end switch } // end while } // end main() //-------------------------------------------------------------- public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //------------------------------------------------------------- public static char getChar() throws IOException { String s = getString(); return s.charAt(0); }
//------------------------------------------------------------- public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); } //------------------------------------------------------------- } // end class InterIterApp ////////////////////////////////////////////////////////////////
8
Q
firstLastLIst
A
//////////////////////////////////////////////////////////////// class Link { public long dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(long d) // constructor { dData = d; } // ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); } // ------------------------------------------------------------- } // end class Link //////////////////////////////////////////////////////////////// class FirstLastList { private Link first; // ref to first link private Link last; // ref to last link // ------------------------------------------------------------- public FirstLastList() // constructor { first = null; // no links on list yet last = null; } // ------------------------------------------------------------- public boolean isEmpty() // true if no links { return first==null; } // ------------------------------------------------------------- public void insertFirst(long dd) // insert at front of list { Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list, last = newLink; // newLink old first first = newLink; // first --> newLink } // ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else last.next = newLink; // old last --> newLink last = newLink; // newLink old next return temp; } // ------------------------------------------------------------- public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } // ------------------------------------------------------------- } // end class FirstLastList //////////////////////////////////////////////////////////////// class FirstLastApp { public static void main(String[] args) { // make a new list FirstLastList theList = new FirstLastList();
theList.insertFirst(22); // insert at front theList.insertFirst(44); theList.insertFirst(66); theList.insertLast(11); // insert at rear theList.insertLast(33); theList.insertLast(55); theList.displayList(); // display the list theList.deleteFirst(); // delete first two items theList.deleteFirst();
theList.displayList(); // display again } // end main() } // end class FirstLastApp ////////////////////////////////////////////////////////////////
9
Q
doublyLinked
A
//////////////////////////////////////////////////////////////// class Link { public long dData; // data item public Link next; // next link in list public Link previous; // previous link in list // ------------------------------------------------------------- public Link(long d) // constructor { dData = d; } // ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); } // ------------------------------------------------------------- } // end class Link //////////////////////////////////////////////////////////////// class DoublyLinkedList { private Link first; // ref to first item private Link last; // ref to last item // ------------------------------------------------------------- public DoublyLinkedList() // constructor { first = null; // no items on list yet last = null; } // ------------------------------------------------------------- public boolean isEmpty() // true if no links { return first==null; } // ------------------------------------------------------------- public void insertFirst(long dd) // insert at front of list { Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list, last = newLink; // newLink old first first = newLink; // first --> newLink } // ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else { last.next = newLink; // old last --> newLink newLink.previous = last; // old last old next return temp; } // ------------------------------------------------------------- public Link deleteLast() // delete last link { // (assumes non-empty list) Link temp = last; if(first.next == null) // if only one item first = null; // first --> null else last.previous.next = null; // old previous --> null last = last.previous; // old previous old next else // not first // old previous --> old next current.previous.next = current.next;
if(current==last) // last item? last = current.previous; // old previous ); } // ------------------------------------------------------------- } // end class DoublyLinkedList //////////////////////////////////////////////////////////////// class DoublyLinkedApp { public static void main(String[] args) { // make a new list DoublyLinkedList theList = new DoublyLinkedList();
theList.insertFirst(22); // insert at front theList.insertFirst(44); theList.insertFirst(66); theList.insertLast(11); // insert at rear theList.insertLast(33); theList.insertLast(55); theList.displayForward(); // display list forward theList.displayBackward(); // display list backward theList.deleteFirst(); // delete first item theList.deleteLast(); // delete last item theList.deleteKey(11); // delete item with key 11 theList.displayForward(); // display list forward theList.insertAfter(22, 77); // insert 77 after 22 theList.insertAfter(33, 88); // insert 88 after 33
theList.displayForward(); // display list forward } // end main() } // end class DoublyLinkedApp ////////////////////////////////////////////////////////////////