+ All Categories
Home > Documents > Lecture Linked List

Lecture Linked List

Date post: 07-Apr-2018
Category:
Upload: nagasravika-bodapati
View: 218 times
Download: 0 times
Share this document with a friend

of 20

Transcript
  • 8/3/2019 Lecture Linked List

    1/20

    Dr.Aruna Malapati

    Asst Professor

    Dept of CS & IT

    BITS Pilani, Hyderabad Campus

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

    Linked List

    Todays Agenda

  • 8/3/2019 Lecture Linked List

    2/20

    Abstract Data Type (ADT)

    Data type a set of objects + a set of operations

    Example: integer

    set of whole numbers

    operations: +, -, x, /

    Can this be generalized? (e.g. procedures generalize the notion of an operator)

    Yes!

    Abstractdata type high-level abstractions (managing complexity through abstraction)

    Encapsulation

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    3/20

    The List ADT

    A sequence of zero or more elements

    A1, A2, A3, AN

    N: length of the list

    A1: first element

    AN: last element

    Ai: position i

    If N=0, then empty list

    Linearly ordered

    Ai precedes Ai+1

    Ai follows Ai-1

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    4/20

    Implementation of an ADT

    Choose a data structureto represent the ADT

    E.g. arrays, records, etc.

    Each operation associated with the ADT is

    implemented by one or more subroutines

    Two standard implementations for the list ADT

    Array-based

    Linked list

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    5/20

    Array Implementation

    Elements are stored in contiguous array positions

    Requires an estimate of the maximum size of the list waste space

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    6/20

    Arrays: pluses and minuses

    + Fast element access.

    -- Impossible to resize.

    Many applications require resizing!

    Required size not always immediately available.

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    7/20

    Pointer Implementation

    (Linked List)

    Ensure that the list is not stored

    contiguously

    use a linked list a series of structures that are not

    necessarily adjacent in memory

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    8/20

    A linked list is a series of connected nodes (or

    links) where each node is a data structure.

    Dynamically allocated data structures can be

    linked togetherto form a chain.

    A linked list can grow or shrink in size as theprogram runs. This is possible because the nodes

    in a linked list are dynamically allocated.

    Linked List ADT

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    9/20

    Linked Lists

    Stores a collection of items non-contiguously. Each node stores

    element

    link to the next node Head: pointer to the first node

    The last node points to NULL

    A

    Head

    B C

    A

    data pointer

    node

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    10/20

    Hence a Linked List is a collection of structures ordered not by their

    physical placement in memory (like an array) but by logical links

    that are stored as part of the data in the structure itself.

    Such structure is represented as follows:

    struct node

    {

    int item;struct node *next;

    }

    node

    item next

    Self-referentialstructure

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    11/20

    The general node structure

    struct tag_name

    {

    type member1;

    type member2;..

    struct tag_name*next;

    }

    next

    next

    member1 member2 Member N

    The structure may contain more thanone item with different types

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    12/20

    Linked-listalso called as one-way list is a collection of data

    elements called as nodes.

    The linear order is given bypointers.

    Each node has two parts.(Data & link field)

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    13/20

  • 8/3/2019 Lecture Linked List

    14/20

    Example

    struct link_list

    {

    int age;

    struct link_list *next;

    } ;

    main()

    {

    struct link_list node1,node2;

    node1.next=&node2;node1.age=25;

    node2.age=56;

    }

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    15/20

    struct node

    {

    int value;

    struct node *next;

    } *head;

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    16/20

    Advantages oflinked lists

    Its a dynamic data structure

    Hence it can grow or shrink as needed

    It can be made just as long as required

    No wastage of memory space

    Not necessary to specify the number of nodes

    Easy to insert and delete items and rearrangement is very easy

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    17/20

    Limitation oflinked lists

    Accessing an arbitrary item is cumbersome and time consuming.

    Uses more storage than the array structure. This because of the

    additional link field.

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    18/20

    Types of Linked Lists

    1. One-way or linear singly linked lists

    2. Circular linked lists

    3. Two-way or Doubly linked lists

    4. Circular doubly linked lists

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    19/20

    Types of Linked Lists

    8 12 5 0 Linear list

    2 4 8 Circular list

    2 4 8 00

    Two way linked list

    2 4 8

    Two way circular listFirst semester 2011-12BITS Pilani Hyderabad campus

  • 8/3/2019 Lecture Linked List

    20/20

    1. What is wrong with the following

    declaration?

    struct element

    { double value;

    struct element link;} ;

    TA C252 COMPUTER PROGRAMMING IIFirst semester 2011-12BITS Pilani Hyderabad campus


Recommended