Knowledge in linked lists

data structures and algorithms

data structures and algorithms includes introduction,array,sorting,searching,linked lists,graphs and stacks and queues.

linked list introduction and creation of link list

Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers. Why Linked List? Arrays can be used to store linear data of similar types, but arrays have following limitations. 1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage. 2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted. For example, in a system if we maintain a sorted list of IDs in an array id[]. id[] = [1000, 1010, 1050, 2000, 2040].   And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000). Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved. Advantages over arrays 1) Dynamic size 2) Ease of insertion/deletion Drawbacks: 1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists efficiently with its default implementation. Read about it here. 2) Extra memory space for a pointer is required with each element of the list. 3) Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not there in case of linked lists. Representation: A linked list is represented by a pointer to the first node of the linked list. The first node is called head. If the linked list is empty, then value of head is NULL. Each node in a list consists of at least two parts: 1) data 2) Pointer (Or Reference) to the next node In C, we can represent a node using structures. Below is an example of a linked list node with an integer data. In Java or C#, LinkedList can be represented as a class and a Node as a separate class. The LinkedList class contains a reference of Node class type. C CPP Java Python C# filter_none brightness_4 // A linked list node struct Node {   int data;   struct Node *next; }; First Simple Linked List in C Let us create a simple linked list with 3 nodes. C++ C Java Python C# filter_none brightness_4 // A simple CPP program to introduce  // a linked list  #include <bits/stdc++.h> using namespace std;    class Node  {      public:     int data;      Node *next;  };    // Program to create a simple linked  // list with 3 nodes  int main()  {      Node* head = NULL;      Node* second = NULL;      Node* third = NULL;         // allocate 3 nodes in the heap      head = new Node();     second = new Node();     third = new Node();    /* Three blocks have been allocated dynamically.      We have pointers to these three blocks as first,      second and third          head         second         third          |             |             |          |             |             |      +---+-----+     +----+----+     +----+----+      | # | # |     | # | # |     | # | # |      +---+-----+     +----+----+     +----+----+         # represents any random value.  Data is random because we haven’t assigned  anything yet */        head->data = 1; //assign data in first node  head->next = second; // Link first node with                      // the second node         /* data has been assigned to data part of first      block (block pointed by head). And next      pointer of first block points to second.      So they both are linked.         head         second         third          |             |             |          |             |             |      +---+---+     +----+----+     +-----+----+      | 1 | o----->| # | # |     | # | # |      +---+---+     +----+----+     +-----+----+      */        // assign data to second node  second->data = 2;     // Link second node with the third node  second->next = third;         /* data has been assigned to data part of second      block (block pointed by second). And next      pointer of the second block points to third      block. So all three blocks are linked.             head         second         third          |             |             |          |             |             |      +---+---+     +---+---+     +----+----+      | 1 | o----->| 2 | o-----> | # | # |      +---+---+     +---+---+     +----+----+     */            third->data = 3; //assign data to third node  third->next = NULL;         /* data has been assigned to data part of third      block (block pointed by third). And next pointer      of the third block is made NULL to indicate      that the linked list is terminated here.         We have the linked list ready.             head                  |              |          +---+---+     +---+---+     +----+------+          | 1 | o----->| 2 | o-----> | 3 | NULL |          +---+---+     +---+---+     +----+------+                        Note that only head is sufficient to represent      the whole list. We can traverse the complete      list by following next pointers. */        return 0;  } 

What is "program listing" in Java?

JAVA LIST INTERFACESome of the important points about Java List are:·       List Interface is the sub interface of Collection. ·       It contains index-based methods to insert and delete elements. ·       It is an ordered collection of objects in which duplicate values can be stored. ·       Since List preserves the insertion order, it allows positional access and insertion of elements.·       List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.·       List allows you to add duplicate elements.·       List allows you to have ‘null’ elements.·       List indexes start from 0, just like arrays.1. List Interface declaration·       public interface List<E> extends Collection<E>  2. Java List Example1.   import java.util.*;  2.   public class ListExample{  3.   public static void main(String args[]){  4.   List<String> al=new ArrayList<String>();  5.   al.add("Amit");  6.   al.add("Vijay");  7.   al.add("Kumar");  8.   al.add(1,"Sachin");  9.   System.out.println("An element at 2nd position: "+al.get(2));  10.     for(String s:al){  11.     System.out.println(s);  12.     }  13.     }  14.     }   Output: An element at 2nd position: Vijay Amit Sachin Vijay Kuma Java List MethodsSome of the useful Java List methods are;1.     int size(): to get the number of elements in the list.2.     boolean isEmpty(): to check if list is empty or not.3.     boolean contains(Object o): Returns true if this list contains the specified element.4.     Iterator<E> iterator(): Returns an iterator over the elements in this list in proper sequence.5.     Object[] toArray(): Returns an array containing all of the elements in this list in proper sequence6.     boolean add(E e): Appends the specified element to the end of this list.7.     boolean remove(Object o): Removes the first occurrence of the specified element from this list.8.     boolean retainAll(Collection<?> c): Retains only the elements in this list that are contained in the specified collection.9.     void clear(): Removes all the elements from the list.10. E get(int index): Returns the element at the specified position in the list.11. E set(int index, E element): Replaces the element at the specified position in the list with the specified element.12. ListIterator<E> listIterator(): Returns a list iterator over the elements in the list.13. List<E> subList(int fromIndex, int toIndex): Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.Some of the default methods added to List in Java 8 are;1. default void replaceAll(UnaryOperator<E> operator): Replaces each element of this list with the result of applying the operator to that element.2. default void sort(Comparator<super E> c): Sorts this list according to the order induced by the specified Comparator.3. default Spliterator<E> spliterator(): Creates a Spliterator over the elements in this list. 

Python Programming - Lists

In this PDF there is detailed concepts and programs related to python Programming regarding lists .You will be able to learn how to use in daily life.

linked list in c

linked list complete descripyion with example

List of all IIT in India

This documents file contents information of all IIT in india...

LINKED LIST

you will get to know how to store data in a linked list.

Reema Thareja Second Edition Data Structures Using C

Reema Thareja Second Edition Data Structures Using C Detailed Contents *Introduction to C *Introduction to Data Structures and Algorithms *Arrays *Strings *Linked Lists

Java: array lists

this file will give you the basic way of using arrays in java. it includes how to declare it , how to use it, how to access it, and all the basics you need to know about array lists to use in java. do refer to this and enjoy learning

HTML Lists

HTML List Example An Unordered List: • Item • Item • Item • Item An Ordered List: 1. First item 2. Second item 3. Third item 4. Fourth item Unordered HTML List An unordered list starts with the tag. Each list item starts with the tag. The list items will be marked with bullets (small black circles) by default: Example Coffee Tea Milk Try it Yourself »

Linked Lists

Introduction to Linked List Operations Creation Traversal Insertion of Node Deletion of Node Variants of Linked list Singly Doubly Circular Applications

Linked Lists - DSA notes

This pdf Contains notes on Topic Linked Lists - DSA notes . This is part of book DSA made easy.