Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

DATA STRUCTURES USING C [CSIT124]

LECTURE NOTES
MODULE- III
LINKED LIST
[INTRODUCTION]

By Dr. Nancy Girdhar


In this session we will talk… ASET

• Course outline and overview


– What is your ideas on Data Structures
– What are we going to study
– Why we will study this
– What are we going to learn
• Quizzes and assignments
• And about you all………

2
Before we Start….. ASET

• Email: ngirdhar@amity.edu

• Google Classroom Codes:

X Batch_Lab: v5t3ftw

Y Batch_Lab: tugcd3b

Theory_Lectures: dogxhmu
3
Course Content
Module 1: Introduction to Data Structures

Module 2: Stacks and Queues

Module 3: Programming with Linked Lists

Module 4: Trees

Module 5: Searching and Sorting

Module 6: Graph and their Applications


Module III: Programming with Linked Lists

• Introduction to Singly linked lists: Representation of linked lists in


memory, Traversing, Searching, Insertion into, Deletion from linked
list, Garbage collection and compaction, doubly linked list,
operations on doubly linked list, circular linked list, operations on
circular linked list, generalized list.

• Applications of Linked List-Polynomial representation using linked


list and basic operation. Stack and queue implementation using
linked list.
6

Learning Objectives​

 Impart in-depth knowledge of data structure and its implementation


in computer programs.
 Make students understand the concepts of Stack linear data
structure.
 Make students understand the applications of Stack.
Contents to be Covered​
Linked List

Definition

 Why Linked List ?

 Representation

 Types of Linked List

7
Data Structure : Linked List
Linked List
• A linked list is a linear data structure, in which the elements are NOT stored at
contiguous memory locations.
• A linked list is a linear collection of data elements, called nodes, each
pointing to the next node by means of a pointer.
• Each node contains two fields: Data & Link (Address of next node).
• A linked list is represented by a pointer to the first node called as HEAD node.
• If the linked list is EMPTY, then, HEAD is NULL.
• Last node has NULL in the link field.
Why Linked List?
Arrays can be used to store linear data of similar types, but arrays have the 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 the room has to be
created for the new elements and to create room existing elements have to be 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 and Disadvantages
Advantages
• Dynamic size
• Ease of insertion/deletion

Disadvantages
• 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.

• Extra memory space for a pointer is required with each element of the list.

• 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 the HEAD.

• If the linked list is empty, then the value of the head is NULL.

• Each node in a list consists of at least two parts:


1) Data
2) Pointer (Or Reference) to the next node
A Graphical View of a Node in a Linked List
// A linked list node
struct Node { Data Link
int data;
struct Node* next; Node
Visual Representation of Node
};
A Graphical View of a Linked List
Node Structure
struct node
{
int data; //This is where we will store data

struct node *next; //This is the link part

} *head=NULL;

/*Initially we assign “NULL” to the head for later operational purpose*/


Types of Linked List
There are three types of linked list as given below:

1.Singly Linked List

2. Doubly Linked List

3. Circular Linked List


Types of Linked List

Following are the various types of linked list.

• Simple Linked List − Item navigation is forward only.

• Doubly Linked List − Items can be navigated forward and backward.

• Circular Linked List − Last item contains link of the first element as
next and the first element has a link to the last element as previous.
18

Recommended Reading​
Textbooks: ​
• Yashwant Kanetkar,”Data Structure using C”, BPB Publication, 5th Edition ,2011
• A.Tannenbaum,Y. Lanhgsam and A.J. Augenstein ,” Data Structures Using C And C++ “,Prentice Hall of
India,2nd Edition,2009.
• Jean-Paul Tremblay, P.G Sorenson, “An Introduction to Data Structures with applications”, Mcgraw-Hill
,2nd Edition ,1984.
​Reference Book: ​
• Robert L Kruse, “Data Structure and Program Design in C”, Prentice Hall (1991).
• Noel Kalicharan ,“Data Structure in C” ,Ist Edition Create space publisher, 2008.
• Mark Allen Weiss,“Data Structure and algorithm Analysis in C”,2nd Edition AddisonWesley,1996.
• E. Balagurusamy, “Problem Solving through C language”, TMH publication, Fourth Edition, 2008.
• R.S Salaria ,“Data Structures & Algorithms using C”,Khanna Publication,4th Edition,2009
• E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C “,2nd Edition, Universities
Press,2008.

Thank you !

You might also like