1 / 14

2. Program Construction in Java

18 Linked Lists. 2. Program Construction in Java. They can only hold one data type They are static (fixed in size) – need more space, you must create another array; don't use it all and you are wasting memory. Disadvantages of arrays.

jasia
Download Presentation

2. Program Construction in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 18 Linked Lists 2. Program Construction in Java

  2. They can only hold one data type They are static (fixed in size) – need more space, you must create another array; don't use it all and you are wasting memory. Disadvantages of arrays

  3. This is the order in which students arrived:String [] names = {"Josh","Anne","Christophe","Suzanne", "Hugh","David","-", "-"};int [] age = {13,15,21,10,11,14,-1,-1};int [] pointers = {3,2,5,-1,0,4,-1,-1};int start = 1; // points at Anne The pointers array keeps track of their order by name. Order in arrays

  4. Although now no need to sort the other arrays, difficult to handle pointers in arrays (they easily become detached) Easier with objects since the pointer never gets detached. Pointers

  5. Pointers and nodes • Each object in the list is a nodepublic class ListNode{ public String name; public int age; // etc. public ListNode next;}

  6. Pointers and nodes • Note how each pointer is created recursively • The first object (root node) can be created with a nqme such as front. • The second node is therefore front.next, the third is front.next.next, etc.

  7. Runners • This method of traversing the list could quickly become clumsy, so better to create temporary nodes called runners to shuffle along the list until the required item is found. • Conventionally, they are called current and previous.

  8. Traversing the list • Remember to check if front is empty (null). • If not, let previous = front and current = previous.next • If current is not what is required, let previous = current and current = current.next and loop.

  9. Print and append • Print: traverse the list, outputting at each point • Append: traverse the list until current = null, then create new node and set previous.next

  10. Searching and counting • Search: (only linear possible) traverse the list, comparing current to wanted and output when equal. • Count: traverse the list until current = null, incrementing a counter at each step

  11. Insert and delete • Insert: traverse the list to current > newNode and make previous.next = newNode and newNode.next = current • Delete: traverse the list until current matches the unwanted item, then make previous.next = current.next

  12. Sorting • Sorting algoritms not needed since the list is inherently in order. • Dual linked lists have two sets of pointers in their nodes and so have two natural order

  13. Stacks and queues • Linked lists can be used to implement stacks, queues and circular queues. • Use the algorithms defined above to pop, push, enqueue and dequeue.

  14. Sequential access – the only possible search is a linear one from the front of the list Disadvantages of linked lists

More Related