lec08 - CSE 12 Fundamental Data Structures: The Array and...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
08 Inserting at an index in a linked list Removing a node from a linked list Inner classes for node classes Comparing array and linked implementations CSE 12 Fundamental Data Structures: The Array and Linked Structures
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Operations on a singly-linked list Recall we are considering some different options in implementing a singly linked list: 1. head pointer points directly to first node, or null if none; no tail pointer 2. head, tail always point to 'dummy' nodes 3. head always points to 'dummy' node; no tail pointer And we looked at adding a node at the beginning of a linked list in the context of option 1 Next we will look at adding a node at an arbitrary index in a linked list, using option 1
Background image of page 2
Inserting an element at an index in the list (using option 1) public void add(int index, E theElement) { 1. check index >=0 && index <= size if(index < 0 || index > size) throw new IOBException(); 2. advance cursor to point to node just before insertion point SLNode<E> cursor = head; while(--index > 0) cursor = cursor.getSuccessor(); 4. Create new node SLNode<E> newnode = new SLNode<E>(theElement, null); 5. Set the new node’s next to be same as cursor's next newnode.setSuccessor(cursor.getSuccessor()); 6. Set cursor's next to point to the new node cursor.setSuccessor(newnode); 7. Increment size by 1 size++; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Adding at index 2 in a list: Step 4 SLNode<E> newnode = new SLNode<E>( theElement, null ); 4. Create the new SLNode head size 4 next data element newnode next data next data next data next data 15 10 40 77 cursor
Background image of page 4
Adding at index 2 in a list: Step 5 newnode.setSuccessor(cursor.getSuccessor()); 5. Set newnode's next to be same as cursor's next head size 4 next data element newnode next data next data next data next data 15 10 40 77 cursor
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Adding at index 2 in a list: Step 6 cursor.setSuccessor(newnode); size++; 6. Set cursor's next to point to the new node, increment size head size 5 next data element newnode next data next data next data next data 15 10 40 77 cursor
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 21

lec08 - CSE 12 Fundamental Data Structures: The Array and...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online