You are on page 1of 20

Todays Agenda

Linked List
Dr.Aruna Malapati Asst Professor Dept of CS & IT BITS Pilani, Hyderabad Campus
First semester 2011-12 BITS Pilani Hyderabad campus TA C252 COMPUTER PROGRAMMING II

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! high-level abstractions (managing complexity through abstraction) Encapsulation

Abstract data type


First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

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

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

Implementation of an ADT
Choose a data structure to 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

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

Array Implementation
Elements are stored in contiguous array positions

Requires an estimate of the maximum size of the list


waste space

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

Arrays: pluses and minuses


+ Fast element access. -- Impossible to resize. Many applications require resizing! Required size not always immediately available.

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

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

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

Linked List ADT


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 together to form a chain. A linked list can grow or shrink in size as the program runs. This is possible because the nodes in a linked list are dynamically allocated.
First semester 2011-12 BITS Pilani Hyderabad campus TA C252 COMPUTER PROGRAMMING II

Linked Lists

A Head

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

node

A
data pointer

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

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; } Self-referential structure


First semester 2011-12 BITS Pilani Hyderabad campus

node

item

next

TA C252 COMPUTER PROGRAMMING II

The general node structure

struct tag_name { type member1; type member2; .. struct tag_name*next; }

The structure may contain more than one item with different types

member1

member2

Member N

next next

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

Linked-list also called as one-way list is a collection of data elements called as nodes. The linear order is given by pointers. Each node has two parts.(Data & link field)

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

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


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

struct node { int value; struct node *next; } *head;

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

Advantages of linked 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

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

Limitation of linked lists


Accessing an arbitrary item is cumbersome and time consuming. Uses more storage than the array structure. This because of the additional link field.

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

Types of Linked Lists


1. 2. 3. 4. One-way or linear singly linked lists Circular linked lists Two-way or Doubly linked lists Circular doubly linked lists

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

Types of Linked Lists


8 12 5 0

Linear list

Circular list

Two way linked list


2 4 8

First semester 2011-12 BITS Pilani Hyderabad campus

Two way circular list

1. What is wrong with the following declaration? struct element { double value; struct element link; };

First semester 2011-12 BITS Pilani Hyderabad campus

TA C252 COMPUTER PROGRAMMING II

You might also like