Linked Lists CSM 1220 - Linked Lists
‘Variable-length arrays’? Arrays are fixed-length To expand them, you create a new, longer array, and copy the contents of the old array into the new array This is slow! Solution: Attach a pointer to each item in the array, which points to the next item This is a linked list An data item plus its pointer is called a node CSM 1220 - Linked Lists
The Linked List data structure [0] [1] [2] array A B C Array node linked A B C Linked list Linked lists are unbounded (maximum number of items limited only by memory) CSM 1220 - Linked Lists
The Node data structure Contains two things: A piece of data (any object) The next node in the list Write the code for a node… CSM 1220 - Linked Lists
The Linked List data structure Same operations as for arrays. Here are the ones we know: addToFront, addToBack: add an object to the front/back of the list removeFromFront/Back: remove an object from the front/back getFront/Back: examine the object at the front/back (no removal) isEmpty: determines whether or not the list is empty length: returns the number of objects in the list equals: tests two lists for equality toString: converts a list to a String 'Ordered' means that the order matters CSM 1220 - Linked Lists
linked list Some common examples of a linked list Hash tables use linked lists for collision resolution Any "File Requester" dialog uses a linked list Binary Trees Stacks and Queues can be implemented with a doubly linked list Relational Databases (eg. Microsoft Access) Exercise: method signatures for the class (see previous slide) CSM 1220 - Linked Lists
Intialization Node head, Tail; Head=tail=null; class Node { int data; Node next; }
2. Adding a Node Adding an element to the head of a linked list public void CreateNode (int element) { Node current=new Node (element); if (head==null) head=current; tail=current; } else current.next=head;
Head == null Head Tail
Head == null Current Head A Tail Node current=new Node (element);
head=current; tail=current; Head A Tail
if (head !=null) Node current=new Node (B); Head B A current Tail
current.next=head; head=current; Head B A Tail
current.next=head; head=current; Head C B A current Tail
Adding an element to the tail public void CreateNode(int element) { Node current=new Node(element); current.next=null; if (head==null) head=current; tail=current; } else tail.next=current;
tail.next=current; tail=current; Head A B current Tail
tail.next=current; tail=current; Head A B Tail Tail
removeFromHead head=head.next; Initial list Remove Head (C) Head C B A Tail Remove Head (C) Implementation is left as an exercise. Remember to return the removed object. head=head.next; Head B A Tail CSM 1220 - Linked Lists
Removing an element from a linked list public int DeleteNode(int position) { int ptr=1; if (position==1) { head=head.next; return(1); } CSM 1220 - Linked Lists
current=head; while (current.next!=null) { if(ptr==position-1) break; current=current.next; ptr++; } if (ptr==position-1) { current.next=current.next.next; return (1); else return (-1);
Consider the polynomial expression: A(x)= amxem+…….+a1xe1 , Polynomial Addition Consider the polynomial expression: A(x)= amxem+…….+a1xe1 , where ai are non-zero coefficients and ei are exponents
The polynomial can be represented using linked list. Each term in the expression will be represented by a node. A node will be of fixed size having 3 fields which represent the coefficient and exponent of a term plus a pointer to the next term. Coeff Exp Next
For eg: A= 3x14 + 2x8 +1 would be stored as
We have two polynomial. A= 3x14 + 2x8 +1 B= 8x14 – 3x10 + 10x6 Their sum will be 11x14 – 3x10 + 2x8 +10x6 + 1.
We have two pointers p and q that points to A and B respectively. if the exponents of the two terms are equal , then the coefficients are added and a new term is created for the result. If the exponents of the current term in A is less than the current term in B, then a duplicate of B is created and attached to C. the pointer q is advanced to the next term. Similar action is taken on A if exp(p) > exp(q).
Each time a new node is generated its coeff and exp fields are set and it is appened to the end of the list C. d is a pointer that points to the last node in the resultant node.
Procedure ATTACH(c,e,d) //c – coeff, e – exp, d – pointer call GETNODE(I) exp(I) e coeff (I) c link(d) I d I end ATTACH
procedure PADD(A,B,C) // A and B – singly linked list are summed to form the new // list name C p A q B call GETNODE (C) d C while p 0 and q 0 do
case : exp (p) = exp(q) x coeff(p) + coeff(q) if x 0 then call ATTACH( x, exp(p), d) p link(p) q link(q)
case : exp (p) < exp (q) call ATTACH( coeff (q), exp(q), d) q link(q) else call ATTACH( coeff (p), exp(p), d) p link(p) end case endwhile
while p 0 do call ATTACH( coeff(p), exp(p), d) p link(p) endwhile while q 0 do call ATTACH( coeff(q), exp(q), d) q link(q) link(d) 0;t C ; C link(C) call RET(T) end PADD
Thank you……