0% found this document useful (0 votes)
16 views56 pages

Linked Data Structures Overview

The document provides an overview of linear data structures using linked organization, detailing concepts such as linked lists, their types (singly, doubly, and circular), and their implementation. It discusses the differences between arrays and linked lists, including memory allocation, access methods, and operations like insertion and deletion. Additionally, it outlines basic operations on linked lists and presents code examples for creating, displaying, and manipulating these data structures.

Uploaded by

Nitin Dhawas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views56 pages

Linked Data Structures Overview

The document provides an overview of linear data structures using linked organization, detailing concepts such as linked lists, their types (singly, doubly, and circular), and their implementation. It discusses the differences between arrays and linked lists, including memory allocation, access methods, and operations like insertion and deletion. Additionally, it outlines basic operations on linked lists and presents code examples for creating, displaying, and manipulating these data structures.

Uploaded by

Nitin Dhawas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Unit – I

Part – IV
LINEAR DATA STRUCTURES
USING LINKED ORGANIZATION

01/02/2026 1
Contents
• Concept of linked organization
• Singly linked list
• Doubly linked list
• Circular linked list
• Linked list as an ADT

01/02/2026 2
Concept of linked organization

A linked data structure is a data structure which consists of a set


of data records (nodes) linked together and organized by
references (links or pointers). The link between data can also be
called a connector.

Linking can be done in two ways – using dynamic allocation and


using array index linking.
Linked data structures include linked lists, search trees,
expression trees, and many other widely used data structures.

01/02/2026 3
Linked List Representation

Linked list can be visualized as a chain of nodes, where every node


points to the next node.

As per the above illustration, following are the important points to


be considered.
•Linked List contains a link element called first.
•Each link carries a data item(s) and a link field called next.
•Each link is linked with its next link using its next link.
•Last link carries a link as null to mark the end of the list.

01/02/2026 4
List

What is a List?
It is a sequence of ELEMENTS.
It is a List of elements of type T and it is a FINITE
SEQUENCE of elements
Example:
list of integers or list of records.
Implementation
Realization of lists: of list
There are 2 main ways :
1. Contiguous storage using array
- in which the elements are physically stored next to one
another in adjacent memory locations
2. Non Contiguous storage( Linked List)
2. Linked list
Linked list eliminates the problem encountered in Array.
• What is a Linked List?
A linked list is a collection of nodes, where each node
contains some data along with information about the
next node.
• How it works?
A linked list uses non-contiguous memory locations and
hence requires each node to remember where the next
node is.
Linked Lists

A B C 

Head

• A linked list is a series of connected nodes


• Each node contains at least
– A piece of data (any type)
– Pointer to the next node in the list
• Head: pointer to the first node
• The last node points to NULL
node
A

data pointer
•What is a NODE? Data Link
Node is a combination of DATA and LINK. 4
What is Data?
Is the part where the actual data is stored.
What is a Link?
It is the link (pointer) to the next element of the list.
Two ways of implementing link
It could be either an index to an array element (array
implementation) OR
A pointer variable containing the address of the next element
(pointer)
Representation

1) The above linked list contains 3 nodes located at different


memory locations each node has a pointer that points to the
next node.
2) The node that have a NULL indicates the it is the end of the
list (last node).
In C/C++ programs either NULL or 0 can be used to
indicate the end of the list.
Node structure

typedef struct node


{
int data;
struct node *next; Next
}node;
• *next is a pointer to next node this type of
definition

01/02/2026 12
Difference between Array and Linked List
Array Linked List
Definition Array is a collection of elements Linked list is an ordered
having same data type with collection of elements which are
common name. connected by links/pointers.

Access In array, elements can be accessed In linked list, elements can’t be


using index/subscript value, i.e. accessed randomly but can be
elements can be randomly accessed accessed only sequentially and
like arr[0], arr[3], etc. So array accessing element takes 0(n)
provides fast and random access. time.

Memory In array, elements are stored In linked list, elements can be


Structure in consecutive manner in memory. stored at any available place as
address of node is stored in
previous node.
Difference between Array and Linked List
Array Linked List
Insertion & Insertion & deletion takes more Insertion & deletion are fast &
Deletion time in array as elements are easy in linked list as only value
stored in consecutive memory of pointer is needed to change.
locations.
Memory In array, memory is allocated at In linked list, memory is
Allocation compile time i.e. Static Memory allocated at run time
Allocation. i.e. Dynamic Memory
Allocation.
Types Array can be single dimensional, Linked list can
two dimension be singly, doubly or circular link
or multidimensional. ed list.
Dependency In array, each element is In Linked list, location or
independent, no connection with address of elements is stored in
previous element or with its the link part of previous
location. element/node.
Difference between Array and Linked List
Array Linked List
Extra Space In array, no pointers are used like In linked list, adjacency between
linked list so no need of extra the elements are maintained
space in memory for pointer. using pointers or links, so
pointers are used and for that
extra memory space is needed.

Figure
Types of Linked List

Following are the various types of linked list.


Singly Linked List − Item navigation is forward only.
Doubly Linked List − Items can be navigated in forward and
backward directions.
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.

01/02/2026 16
Types of Lists
• Depending on the way in which the links are used to
maintain adjacency, several different types of linked lists
are possible.

– Linear singly-linked list (or simply linear list)


• One we have discussed so far.

head

A B C

01/02/2026 17
– Circular linked list
• The pointer from the last element in the list points back
to the first element.

head

A B C

01/02/2026 18
– Doubly linked list
• Pointers exist between adjacent nodes in both directions.
• The list can be traversed either forward or backward.
• Usually two pointers are maintained to keep track of the
list, head and tail.

head tail

A B C

01/02/2026 19
Basic Operations on a List
• Creating a list
• Display the list
• Traversing / Searching the list
• Inserting an item in the list
• Deleting an item from the list

01/02/2026 20
List is an Abstract Data Type

• What is an abstract data type?


– It is a data type defined by the user.
– Typically more complex than simple data types like int, float,
etc.
• Why abstract?
– Because details of the implementation are hidden.
– When you do some operation on the list, say insert an
element, you just call a function.
– Details of how the list is implemented or how the insert
function is written is no longer required.

01/02/2026 21
Creating a List
• To start with, we have to create a node (the first node),
and make head point to it.
head = (node *) malloc(sizeof(node));

head
Data next

typedef struct node


{
int data;
struct node *next;
}node;
01/02/2026 22
Contd.
• If there are n number of nodes in the initial linked list:
– Allocate n records, one by one.
– Read in the fields of the records.
– Modify the links of the records so that the chain is formed.

head

A B C

01/02/2026 23
typedef struct node{
int data;
struct node *next;
}node;
create empty node
1. function node *getnode()
2. Create pointer variable and allocate memory for it
node *p;
p=(node*)malloc(sizeof(node));
p->next=NULL;
3. Create a node return p;
01/02/2026 24
Create List with [Link] nodes
node *create()
{
node *head=NULL,*p,*last;
int i,n;
printf("\nEnter no of nodes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p=getnode();
printf("\nEnter [%d] node:",i+1);
scanf("%d", &p->data);
if(head==NULL)
head=last=p;
else {
last->next=p;
last=p;
}//end if }//end for
return head; } 25
Functions to display list display (node *head)
[Link] node *temp=head;
2. Check list is empty or not
i. if (head!=NULL)
not empty then scan all element up to end
Repeat while(temp->next!=NULL)
print “temp->data”
temp=temp->next;
End while
to print last node
print “temp->data”

01/02/2026 26
Function insert_begin()
1. Initialize struct node *p;
2. allocation of memory
p=getnode();
// < p=(struct node *)malloc(sizeof(struct node));
p->next=NULL>
printf("\nEnter node to be inserted:" );
accept from user “ p->data”
3. i. if first element of list mean
if(head==NULL)
head=p;
ii. otherwise else
p->next=head;
head=p;

01/02/2026 27
Function insert_END () Linked List
1. Initialize node *temp,*ptr;
2. Memory allocation using malloc call getnode function
/* p=(struct node *)malloc(sizeof(struct node));
p->next=NULL*/
i. otherwise Accept Data from user
“p->data”
3. i. if inserting first node of list
if(head==NULL)
head=p;
ii. otherwise else
temp=head;
repeat while(temp->next!=NULL)
temp=temp->next;
temp->next=p;

01/02/2026 28
node *ins_any(node *head)
{ node *temp,*p;
int x;
printf("\nInsert at any position\n");
if(head==NULL)
printf("\nList is Empty");
else {
printf("\nEnter node after which new node to be inserted: ");
scanf("%d",&x);
temp=head;
while(temp!=NULL && temp->data!=x)
temp=temp->next;
if(temp!=NULL)
{
p=getnode();
printf("\nEnter node to be inserted:" );
scanf("%d",&p->data);
p->next=temp->next;
temp->next=p; }
else
01/02/2026 printf("\nData not found"); } 29
return head; }
Delete at first
node *del_first(node *head)
{
node *temp;
temp=head;
head=head->next;
free(temp);
return head;
}

01/02/2026 30
node *del_end(node *head)
{ node *temp,*prev;
if(head->next==NULL)
{
temp=head;
head=head->next;
free(temp);
return head;
}
else
{ temp=head;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
free(temp);
}
return head; }
01/02/2026 31
Delete ANY NODE
else
node *del_any(node *head) {
{ temp=head;
int x; while(temp!=NULL && temp->data!=x)
node *temp,*prev; {
printf("\nEnter node to be deleted: "); prev=temp;
scanf("%d", &x); temp=temp->next;
if(head->data==x)
{ }
temp=head; if(temp!=NULL)
head=head->next; {
free(temp); prev->next=temp->next;
} free(temp);
}
else
printf("\nData not found");
}
return head;
}
01/02/2026 32
– Doubly linked list
• Pointers exist between adjacent nodes in both directions.
• The list can be traversed either forward or backward.
• Usually two pointers are maintained to keep track of the list, head
and tail.

head tail

A B C

01/02/2026 33
• SLL

• DLL
3000

01/02/2026 struct node *prev; 34


};
Doubly Linked list
typedef struct DLL
{
int data;
struct DLL *prev,*next;
}node;
node * getnode()
{
node *p;
p=(node*)malloc(sizeof(node));
p->next=NULL;
p->prev=NULL;
return p;
}
01/02/2026 35
Create List for(i=0;i<n;i++)
{
p=getnode();
node *create()
printf("\nEnter [%d] record:",i+1);
{ scanf("%d",&p->data);
node head=NULL,*p,*last; if(head==NULL)
int i,n; head=last=p;
printf("\nEnter no of records: "); else
{
scanf("%d",&n);
last->next=p;
p->prev=last;
last=p;
}
}
return head;
}
01/02/2026 36
01/02/2026 37
/Inserts a Node at head of doubly linked list void
1. Function to insert at first
P=getnode();
if(head == NULL)
{
head = P; //P is newNode in LL
return;
}
head->prev = P;
P->next = head;
head = P;

01/02/2026 38
Function for InsertAtTail
1. Initialize Node* temp = head,*P;
[Link] memory for new node P using malloc
3. Insert first node if(head == NULL)
head = P;
return;
[Link] while(temp->next != NULL)
temp = temp->next; // Go To last Node
temp->next = P;
P->prev = temp;

01/02/2026 39
//Prints all elements in linked list in reverse traversal order.
function ReversePrint()
[Link] Node* temp = head;
2. i. Check whether list is empty or not
if(temp == NULL)
return; // empty list, exit
// Going to last Node
ii. Repeat to search end
while(temp->next != NULL)
temp = temp->next;
3.// Traversing backward using prev pointer "Reverse: “
i. repeat while(temp != NULL)
{ printf("%d ",temp->data);
temp = temp->prev; }
01/02/2026 40
Delete at start
void deleteFromBeginning()
1. Initialize temp variable node * temp;
2. i. Check list is empty or not
if(head == NULL)
printf("Unable to delete. List is empty.\n");
ii. otherwise else
temp = head;
head = head->next;
head->prev = NULL; // Remove the link to previous node
free(temp);

01/02/2026 41
void delend(node *head)
{
node *temp;
if(head==NULL)
List empty
else
{ temp=head;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
free(temp);
01/02/2026 42
}
else
DELETE { temp=head;
while(temp!=NULL && temp->data!=x)
if(head==NULL) temp=temp->next; if(temp!
printf("\nList is Empty"); else
=NULL)
{
printf("\n Enter element to delete "); {
scanf("%d",&x); if(temp->next==NULL)
//list only one node {
if(head->data==x && temp->prev->next=NULL;
head->next==NULL) free(temp);
{ }
temp=head; else
head=head->next; {
free(temp);
temp->prev->next=temp->next;
}
else if(head->data==x)//search at first loc temp->next->prev=temp->prev;
{ free(temp);
temp=head; }
head=head->next ; }
head->prev=NULL; else
free(temp); printf("\nData not found");
} }
01/02/2026
} return head; }
if(temp!=NULL)
{
Insert at any location p=getnode();
printf("\nEnter new Record “);
if(head==NULL) scanf("%d", &p->data);
{ if(temp->next==NULL)//last element in ll
printf("\nList is Empty"); {
} temp->next=p;
else p->prev=temp;
{ }
printf("\n Enter After which to be else
inserted:" ); {
scanf("%d", &x); p->next=temp->next;
temp=head; temp->next->prev=p;
temp->next=p;
while(temp!=NULL && temp->data!=x) p->prev=temp;
temp=temp->next; }
}
else
printf("\nData not found");
01/02/2026 } return head; }
Circular Linked List

node *getnode()
{
node *p;
p=(node*)malloc(sizeof(node));
p->next=p;
return p;
45
}
Insert at Beginning CLL
void insertAtBeginning(int value)
else
struct Node *newNode;
{
/* newNode = (struct Node*) malloc (sizeof
(struct Node)); Node *temp = head;
newNode -> data = value; */ while(temp -> next != head)
newNode =getnode(); temp = temp -> next;
if(head == NULL) newNode -> next = head;
{ head = newNode;
head = newNode; temp -> next = head;
newNode -> next = head; }
} printf("\n Insertion
success!!!");
}
01/02/2026 46
Insert at End CLL
void insertAtEnd() else
{ {
Node *newNode; Node *temp = head;
newNode= getnode(); while(temp -> next != head)
scanf(“%d”, newNode -> data ); temp = temp -> next;
if(head == NULL) temp -> next = newNode;
{ newNode -> next = head;
head = newNode; }
newNode -> next = head; printf("\nInsertion success!!!");
} }

01/02/2026 47
void del_at_beg()
{
if (head == NULL)
printf("\n List is empty");
else
{
x = head;
temp = head;
while (x->next != head)
x = x->next;
head = temp->next;
x->next = head;
free(temp);
}
}
01/02/2026 48
void display()
{ if(head == NULL)
printf ("\n List is Empty!!!");
else
{
struct Node *temp = head;
printf("\n List elements are: \n");
while(temp -> next != head)
{
printf("%d ---> ",temp -> data);
}
printf("%d ---> %d", temp -> data);
}
} 49
Generalized Linked List
• A generalized linked list contains structures or
elements with every one containing its own pointer.
• It's generalized if the list can have any deletions,
insertions, and similar inserted effectively into it.

01/02/2026 50
• A linear list is defined as finite sequence of elements
a1,a2,a3….an which is denoted by
A=(a1,a2,a3…an)
Elements are atomic in nature
Elements address by its index

1.A=() an empty list


2.B=(a,b,c)-list of 3 elements with
B(1)=a ,B(1)=b B(1)=c
GL each element is either atom or list
3. c=(a(b, c),d)

01/02/2026 51
Node of LL
Flag Data or Link Next Link

If Flag=0 Node is data node

0 Data Next Link

If Flag=1 Node is head node

1 Link Next Link

01/02/2026 52
DS to represent a node of GLL
typedef struct node
{
int flag;
struct node *next;
union
{
char data;
struct node *sublist;
}shared;
}node;

01/02/2026 53
void insert_pos()
[Link] struct node *ptr,*temp;
int i, pos, Data;
2. i. Memory allocation
temp=(struct node *) malloc (sizeof(struct node));
ii. Accept the position on which num will insert pos
iii. Enter the data value of the node Data
temp->info=Data
temp->next=NULL;
3. i. Insert Element at first position mean if(pos==0)
temp->next=start;
start=temp;
ii. otherwise else Repeat for(i=0,ptr=start; i<pos-1; i++)
ptr=ptr->next;
then if(ptr==NULL)
Print “Position not found” return;
“Found position’”
01/02/2026
temp->next =ptr->next ; ptr->next=temp; 54
Generic Linked Lists
• Unlike C++ and Java, C doesn’t support generics. How
to create a linked list in C that can be used for any data
type?
• In C, we can use void pointer and function pointer to
implement the same functionality.
• The great thing about void pointer is it can be used to
point to any data type. Also, size of all types of pointers
is always is same, so we can always allocate a linked list
node. Function pointer is needed process actual content
stored at address pointed by void pointer.

01/02/2026 55
// C program for generic linked list
#include<stdio.h>
#include<stdlib.h>
/* A linked list node */
struct Node
{
// Any data type can be stored in this node
void *data;
struct Node *next;
};

01/02/2026 56
/* Function to add a node at the beginning of Linked List. This function
expects a pointer to the data to be added and size of the data type */
void push(struct Node** head_ref, void *new_data, size_t data_size)
{
// Allocate memory for node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = malloc(data_size);
new_node->next = (*head_ref);
// Copy contents of new_data to newly allocated memory.
// Assumption: char takes 1 byte.
int i;
for (i=0; i<data_size; i++)
*(char *)(new_node->data + i) = *(char *)(new_data + i);
// Change head pointer as new node is added at the beginning
(*head_ref) = new_node;
}

01/02/2026 57

You might also like