ECE 242 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece242/ Linked List I Lecture 10 Prof. Eric Polizzi
Logistics ECE242 Recommendations The goal of projects is to help you practice and master the concepts that we see in class. Projects aim at providing a rewarding and useful experience. However, focusing your efforts only on projects will not be enough to succeed in the 242 exams (mid term, and final). Exams will be heavily based on your understanding of the lecture notes, textbook, and HW practices. In order to become more comfortable with the class materials, I encourage you to: Practice the HW before discussion Ask questions to TA (by emails, in discussion, in Office hours) Get a complementary look at the textbook 2
The big picture... so far DATA STRUCTURES ALGORITHMS Array Linked List Unordered array (unsorted list) Ordered array Ordered Array (sorted list) Stacks Queues Priority Q insert, delete, linear search, shuffle, sorting insert, delete, shuffle, binary search push, pop, peek, etc. enqueue, dequeue, peek, etc. 3
Arrays Several advantages: Easy to access a particular item... array[i] Insertion is fast in unordered array Searching is fast in ordered array Data locality in memory (good for performance in scientific computing) Main disadvantages: Some algorithms require large number of shifting/moves... costly The size of an array cannot be changed once it is created... lack of flexibility If the size is too large... waste of memory If the size is too small, it cannot be expanded dynamically. 4
Linked list Versatile mechanism that can be used in place of an array based implementation for various data structures (list, stack, queues, etc.) Take as much memory as needed (allocate memory dynamically if needed) No need to move items around In a linked list each item is embedded in a link (sometimes called a node), and each link contains a reference to the link. We obtain a chain of objects that are connected link link link a b c a b c : Items/objects : reference 5
Linked list Succession of links from first link to the last link that points to reference The class Link contains some data and a reference to the link The class Linked List contains the reference to the first link and all major methods that operate on the list (ex: add, remove, display, search, etc.) 6
Linked list: Class Link Remark: in Java (2 steps to define an object) MyObject o; // 1- Creation of reference/pointer to object o= new MyObject();// 2- Creation of the actual object 'new' Definition of Class Link (example with two data items: String and int) class Link { public String name; // data public int age; // data public Link ; // reference to link // Constructor public Link(String name,int age){ this.name=name; this.age=age; = ; // optional This class is self referential since it contains a field () of the same type as itself 7
Linked list: Example with 2 links Link 1stlink = new Link(name1,age1); data name1 age1 Link 2ndlink = new Link(name2,age2); name2 age2 1stlink. = 2ndlink; name1 age1 name2 age2 reference to link 8
Linked list: Consequences Move from one link to another by references Number of links can be expanded dynamically The objects/items of the link can be anywhere in memory (no data locality) Insertion/removal does not require moving items a b c e d To do: Test Java applet LinkList.html 9
Linked list: Class LinkList Creation of a Linked list for the objects data defined in the class Link class LinkList { // variables // constructors // methods private Link first;// Reference to the first // link on the list public LinkList() { first = ; // no item in the list yet public boolean isempty() { return(first==);//true if list is empty //////////// other methods such as: public void insertfirst(string name,int age){ // insert new link at the beginning public Link deletefirst() {// to complete public void displaylist() {// to complete 10
Linked list: insertfirst method Before insertion LinkList first Han 40 Luke 20 Obiwan 55 After Insertion LinkList first Han 40 Luke 20 Obiwan 55 2 Yoda 400 1 11
Linked list: insertfirst method // insert at the beginning of the list public void insertfirst(string name,int age) { Link newlink = new Link(name, age); // create new link newlink. = first; // newlink old first (step 1) first = newlink; // first newlink (step 2) LinkList first Han 40 Luke 20 Obiwan 55 2 Yoda 400 1 12
Linked list: deletefirst method Before deletion (at the beginning of the list) LinkList first Han 40 Luke 20 Obiwan 55 After deletion LinkList first Luke 20 Obiwan 55 Remark: the object removed from the list, still exists in memory somewhere, but it is not the user responsibility to deal with it (garbage collection process) 13
Linked list: deletefirst method // delete at the beginning of the list and return first Link public Link deletefirst(){ if (!isempty()){ // check that the list is not empty Link temp = first; // save reference of first link first = first.; // first old (remove link) return temp; // return deleted link else return ; After deletion LinkList first Luke 20 Obiwan 55 Remark: the object removed from the list, still exists in memory somewhere, but it is not the user responsibility to deal with it (garbage collection process) 14
Linked list: displaylist method 'current' probe moves from link to link using current=current.; LinkL ist first Han 40 Luke 20 Obiwan 55 LinkL ist first Han 40 Luke 20 Obiwan 55 current current public void displaylist() { Link current = first; // start at the beginning of the list while (current!=) // until the end of the list { System.out.println(current.name+"'s age is "+current.age); current = current.; // move to Link 15
Linked List Application Example class LinkListApp { public static void main(string[] args) { LinkList mylist = new LinkList(); mylist.insertfirst("obiwan",55); mylist.insertfirst("luke",20); mylist.insertfirst("han",40); mylist.displaylist(); while(!mylist.isempty()){ Link del = mylist.deletefirst(); System.out.println("link of "+del.name+" deleted"); Han's age is 40 Luke's age is 20 Obiwan's age is 55 link of Han deleted link of Luke deleted link of Obiwan deleted 16