0% found this document useful (0 votes)
4 views6 pages

Section18_LinkedLists+-+Java's+Dance+of+Nodes

The document discusses the LinkedList data structure in Java, highlighting its dynamic size and advantages over arrays and ArrayLists, particularly in terms of insertion and deletion efficiency. It explains the creation and manipulation of LinkedLists, including important methods and performance considerations compared to ArrayLists. Additionally, it describes the structure of LinkedLists, including nodes and references, and emphasizes their utility in scenarios requiring frequent modifications.

Uploaded by

katurivinay746
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Section18_LinkedLists+-+Java's+Dance+of+Nodes

The document discusses the LinkedList data structure in Java, highlighting its dynamic size and advantages over arrays and ArrayLists, particularly in terms of insertion and deletion efficiency. It explains the creation and manipulation of LinkedLists, including important methods and performance considerations compared to ArrayLists. Additionally, it describes the structure of LinkedLists, including nodes and references, and emphasizes their utility in scenarios requiring frequent modifications.

Uploaded by

katurivinay746
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
(ey Tinediict tlements using pointers. It's a linear data structure that ean dynamically increase or decrease in size during runtime. The LinkedList JovaLinkedList isa data structure in ava that represent lis of elements, where each elements connected tits neighboring * class in ova is pr ofthe java package, Here's an example oF how to Tate a Unked st nv LinkedList List = now Linkedhisto>()s Few important points about LinkedList: + We already used arrays and their dynamic cousin, the ArrayList class. However, arrays and array lists suffer from a major drawback. Removing an element fram the middle of an aray is expensive since all array elements beyond the removed one must be moved toward the beginning ofthe array. The same is true for inserting elements in the middle + Just ike ArrayList LinkedList class ean also contain duplicate elements and maintains insertion order + LinkedList in Java isa very useful data structure for scenarios where you need to perform frequent insertions or deletions of elements ina lst. as it doesn’t requite the same amount of memory allocation or reallocation asan array-based list + To create an LinkedList of primitive types like int, flat, and char in Java, we cannot use the primitive types directly. Instead. we need to use the corresponding wrapper class. For instance: LinkedListcint> AntNns = LinkedLLst<> (07 1 campiaton fs LinkedListeIntoger> intMane ~ now LinkedLiot<> (7 Ifwoks fae Rep LinkedList bytes Creating LinkedList List integerNums = new LinkedList>(); List doubleNums — new LinkedLiat<>()7 var countryNames = new LinkedList (); characters ~ new LinkedListo>(); jon> persons ~ new LinkedListo>(); List countryNames ~ new LinkedListo>(); countrytiames.add ("india") 7 countryNames.ada ("Canada") ; ‘canada usa Germany India count ryKamea.add ("USA") : > ul dor of pr] pe count ryNamea_add ("Germany") + countryNameg.ada ("India") ; a 2 28 ° a nodes. Each node in the LinkedList contains two parts: the data part which stores the element itself inter") that points to the next & previous nades in thelist LinkedList stores its elements usin and a reference part (alsa called a The first node in thelist is called the head of the LinkedLst and the last nade is called the tal. The head node isthe entry point to the LinkedList,and it doesn't have a previous node, while the tal nade doesn't have a next node. Iterating & Sorting of the LinkedList works same as ArrayList. We can use Iterator/Listlterator/for-each for looping and Comparable/Comparator for sorting, LinkedList Important methods while working with LinkedList add{element): adds an element to the end of the LinkedList add (index, element): inserts an element atthe specified index in the LinkedList et(index): returns the element atthe specified index inthe LinkedList removerindex): removes the element a the specified index in the Linkedtist size(): returns the numberof elements in the Linkedtist addFirst(E e):Ivis used to inserethe given element atthe beginning of list void addLast(E e): itis used to append the given element to the end ofa lst. E getFirst(): It is used to return the fist element ina list. E getlast() itis used to return the last element ina list. E peek{): Itretrieves the first element ofa list E peekFirst(): It retsieves the fist element ofa list or returns mul i itis empty E peokLast() itretrieves the last element of lst or returns nul if list is empty. E poll): It retrieves and removes the first element of lis. E pollFirst(): It retrieves and removes the frst element of alist. or returns nul if list is empty: E pollLast(): It retrieves and removes the last element of list. oF returns null fa lists empty E pop(): Itpopsan element fom the stack represented by alist. Void push(E e): It pushes an element onto the stack represented by alist. bytes LinkedList performance bytes (D) Anary ses obec references in consectve memo locaton inked Ht stares each becin a separate kino: Each hinds also stores a reference to the next link in the sequence. Inthe Java programming language. all inked lists are actually doubly inked: that is. each link also stores a reference to its predecessor. (D) Semorina element or ading an element am the mid of inked Hts an inexpensive operation—onky the ink around the element to be removed need to be updated. Below isthe illustration of the same, Tink tink? Tinks Tinks ox: (Tina on ae ow an on Ee vee (Ge see (Ge vee (Geo voc a eines reins FREER ovo revius ERG Unk? ke Link ows am on mm on a nee imo} New [Eimer vec (a evo: eons SS eo) reiows [EET] LinkedList performance bytes Accessing an random element with LinkedList is going to be expensive as they are not stored sequentially. Let's imagine you want to access the last element inside your LinkedList. Here isthe flow of steps followed, Link? Link? Links ink oe ae ox [aaa oe [iw ove [arma vee Tim vec (Gia Nee [Eimer Nee [zat reious [Ta Previous ESTRET revous ESTEE Previous ESAS STEP 1: In the ist element. details of the next link will be checked STEP 2: In the second element details of the next link will be checked STEP 3: In the third clement, details of the next link will be checked STEP 4: In the fourth clement, details of the data will be checked eaz! ArrayList vs LinkedList bytes ArrayList isan implementation of the List interface that LinkedList stores elements as nodes with a reference to the next node stores elements in a contiguous block of memory, which inthe lis. Ths allows for faster insertion and deletion of elements in ‘makes accessing elements faster than LinkedList in most the middle of the lst. as the LinkedList only needs to update the scenarios. references ofthe affected nodes. ArrayList has constant-time O(!) access time to an Accessing elements in a LinkedList takes linear time O(n) element given its index, which makes ita good because the LinkedLst has to traverse each node from the head choice when you need to access elements frequently or tal to get ta the desired element. or in a random order. ArrayList can be slower than LinkedList when it comes to This makes LinkedList a good choice when you need to frequently ‘adding or removing elements, especially rom the midale of add or remove elements from the middle of the list. or when the list thelist. This is because ArrayList hasta shiftall the elements is relatively small and the access time is not a bottleneck after the insertion or deletion point to maintain ite contiguous memory block structure. Teainrate ATHiR pray ert ecracon em oaeneerynie eateceenny OD ieee eevee ee eee Eee ree ee ee cect eel ay Seay pare eee ea ee eel eee lanes rom ne mid ets o Fane soe felts ama sented

You might also like