Thursday, March 15, 2007

Lecture 24

Linked List
  • data structure containing nodes
  • each node points to the node after it

Adding nodes to the middle

  • find the index of the node prior to the one you must add

int position = i // position where the new node gives
ListNode a = FindNum(head, position - 1);
ListNode b = new ListNode();//new object
b.setNext(a.getNext()); // 1
a.setNext(b); //2

adding position 0 = creating a new head

listNode b = new ListNode();
b.setNext(head);
head = b;



Deleting Elements

  • deletion involves reordering next pointers

ListNode a = findNum(head, position - 1);
a.setNext((a.getNext()).getNext());

  • object must not be null
  • a.getNext must not be null

Linked List as Object

public class LinkedList
{
private ListNode head;
public LinkedList(ListNode head);
public LinkedList()
public ListNode findEnd()
public ListNode findNum()
public void addNode(ListNode, newNode, int position)
public void delNode(int position)
public void addNodeToEnd(Listnode, NewNode)
public int getSize()

No comments: