Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists CSM 1220 - Linked Lists.

Similar presentations


Presentation on theme: "Linked Lists CSM 1220 - Linked Lists."— Presentation transcript:

1 Linked Lists CSM Linked Lists

2 ‘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 Linked Lists

3 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 Linked Lists

4 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 Linked Lists

5 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 Linked Lists

6 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 Linked Lists

7 Intialization Node head, Tail; Head=tail=null; class Node { int data;
Node next; }

8 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;

9 Head == null Head Tail

10 Head == null Current Head A Tail Node current=new Node (element);

11 head=current; tail=current; Head A Tail

12 if (head !=null) Node current=new Node (B); Head B A current Tail

13 current.next=head; head=current; Head B A Tail

14 current.next=head; head=current; Head C B A current Tail

15 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;

16 tail.next=current; tail=current; Head A B current Tail

17 tail.next=current; tail=current; Head A B Tail Tail

18 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 Linked Lists

19 Removing an element from a linked list
public int DeleteNode(int position) { int ptr=1; if (position==1) { head=head.next; return(1); } CSM Linked Lists

20 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);

21 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

22 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

23 For eg: A= 3x14 + 2x8 +1 would be stored as

24 We have two polynomial. A= 3x14 + 2x8 +1 B= 8x14 – 3x x6 Their sum will be 11x14 – 3x10 + 2x8 +10x6 + 1.

25 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).

26 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.

27 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

28

29 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

30 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)

31 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

32 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

33 Thank you……


Download ppt "Linked Lists CSM 1220 - Linked Lists."

Similar presentations


Ads by Google