Sei sulla pagina 1di 20

TOPIC: Linked List

2018-ag-5218 Sehar Manzoor

2018-ag-5230 Noor Sehar

2018-ag-5135 Warda Majeed


DATASTRUCTURE

Datastructure is the way of


organizing all data items in order
that not only elements to be stored
but also the relation between the
elements
INTRODUCTION

 DATASTRUCTURE: A data structure is


a logical representation that can be
performed on the data.
1) Linear Data Structure
2) Non Linear Data Structure

 Linear data structure is an order of data


elements. They are arrays, stacks, queued,
and linked lists.
Link

For the term “Link”-as an analogy – think of a weblink – when


you click on it, it brings you from one website to the next.
List

A list is a collection of related objects.


Think of a shopping list – it contains every item that you plan
to buy.
What is Linked List?
A linked list is a collection of nodes with various fields it
contains data field and Address field or Link field

Pointer to the first


node
Linked 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 stored 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.
Representation of Linked List in C:
 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 is a list consists of at least two parts:
1) DATA
2) Pointer to the next node Data Address of 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 C#/Java, Linked List can be represented as a class and a node
as a separate class. The LinkedList class contains a reference of
node class type.
 Write C Program to Create Linked List.

Data Address of Next Node Data Address of Next Node Data Address of Next Node

40 2000 50 3000 60 NULL


Address of 0x1000 0x2000 0x3000
Head Node Address of Last Node is NULL
Types of Linked List

There are three common type of Linked List.


 Singly Linked List
Doubly Linked List
Circular Linked List
Single Linked List
 It is the most common. Each node has data and a pointer to the next node.

HEAD Data Next Data Next Data Next NULL

Typedef struct node


{
Node Representation in
Int data;
singly Linked List
Struct node *next;
} node;
Double Linked List
Double Linked List is a linked data structure that consists of a set of sequentially
linked records called nodes.
 Each node contains different fields:
 one is data part which contain data only.
 Two other field is linked part that are point or references to the previous or to
the next node in the sequence of nodes.

HEAD
Prev Data Next Prev Data Next Prev Data Next NULL
NULL

Typedef struct node


{
Int data; Node Representation in
Struct node *next; Double Linked List
Struct node *prev;
} node;
Circualr Linked List

 Circular Linked List is a variation of Linked list, in which last node is


connected back to first node.
 Both singly linked list and doubly linked list can be made into a circular
linked list.
 Queue Data Structure can be implemented using circular linked list.

Singly Linked List as Circular


 In singly linked list, the next pointer of the last node points to the first node.
Double Linked List as Circular

 In doubly linked list, the next pointer of the last node points to the first node
and the previous pointer of the first node points to the last node making the
circular in both directions.

As per the above illustration, following are the important points to be considered.

 The last node’s next field points to the first node of the list in both cases of
singly as well as doubly linked list.
 The first node’s previous points to the last node of the list in case of doubly
linked list.
C representation of circular linked list
 We declare structure for the circular linked list in the same way as linear
linked list.

Struct node
{
int info;
Struct node*next;
};
Typedef struct node NodeType;
NodeType*star=NULL;
NodeType*last=NULL;
Basic Linked List Operations
We can treat linked list as an abstract data type and perform following basic operations:
 Creating Linked List.
 Traversing Linked List.
 Printing Linked List.
 Counting Number of Nodes in Linked List.
 Searching element in Linked List.
 Concatenating two Linked List.
 Inserting element into Linked List (Start, end and Specific Position)
 Deleting elements from Linked List (Start, End and specific position)
 And Many more operations.
Example: working with linked list
 Consider the structure of a node as follows:

Struct stud {
int roll;
char name[25];
int age;
struct stud *next;
};

/* A user-defined data type called “node” */


typedef struct stud node;
node *head;
ADVANTAGES OF LINKED LISTS
 We can dynamically allocate memory space as needed.
 We can release the unused space in the situation where the allocated space
seems to be more.
 Operation related to data elements like insertions of deletion are more
simplified.

DISADVANTAGES OF LINKED LISTS


 The memory is wasted as inter require extra memory to storage.
 Each node has to access sequentially.
 Reverse traversing is difficult.
THANK YOU

Potrebbero piacerti anche