0% found this document useful (0 votes)
4 views

Linked list

The document provides an overview of linked lists, including their definitions, types, and operations such as insertion and deletion. It covers various implementations like singly linked lists, circular linked lists, and doubly linked lists, along with their respective algorithms and C code examples. Additionally, it contrasts linked lists with contiguous lists, highlighting their dynamic nature and memory allocation methods.

Uploaded by

dipeshcrestra7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Linked list

The document provides an overview of linked lists, including their definitions, types, and operations such as insertion and deletion. It covers various implementations like singly linked lists, circular linked lists, and doubly linked lists, along with their respective algorithms and C code examples. Additionally, it contrasts linked lists with contiguous lists, highlighting their dynamic nature and memory allocation methods.

Uploaded by

dipeshcrestra7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Linked List

a. Concept and Definition


b. Inserting and deleting nodes
c. Linked implementation of a stack (PUSH/POP)
d. Linked implementation of a queue (Insert/Remove)
e. Circular List
• Stack as a circular list (PUSH/POP)
• Queue as a circular list (Insert/Remove)
f. Doubly Linked List (Insert/Remove)
List
• A list is a set of items organized sequentially.
For example, an array is a list where the
sequential organization is provided implicitly
by its index.
• We can represent a list in two ways:
– Static Implementation(Array)Contiguous List
– Dynamic Implementation(Linked List)
Contiguous List
• A contiguous list is a list in array implementation.
• In this representation, each item is contiguous to
next item.
• The operations of insertion and deletion of items
can be done to/from anywhere within the list.
• The operations that are commonly performed in
contiguous list are: creation(), Insertion(),
Deletion(), Modification(), Traversal(), Merging().
Implementation of Contiguous List
To implement a contiguous 8 0
list, we need to define a 9 1
large enough array to hold 5 2
all the items of the list. The 10 3
first item of the list is 12 4
placed at 0th position of 15 5
array and successive items 20 6
in successive positions of
the array. The last item of
18 7 <-last_index
the list is indicated by 8
“last_index”. For example,
in the figure last_index=7
Implementation of Contiguous List…
Assumptions
 MAXLIST is defined.
 CList[MAXLIST] is defined as an array to hold the items of the list.
 last_index is defined to indicate index the last element.
 Initially, last_index=-1.
 Insertion:
1. Read item x to insert.
2. If last_index==MAXLIST-1, declare list full and return.
3. If last_index==-1, increment last_index and perform Clist[0]=x and return.
4. Read position pos to insert.
5. If pos>last_index+1, declare invalid and return.
6. i=last_index;
7. while(i>=pos)
CList[i+1]=CList[i];
i=i-1;
8. CList(pos)=x;
9. Increment last_index
10. Return
Implementation of Contiguous List…
• Assumptions
– Deletion:
1. If last_index==-1, declare list empty and return.
2. Read position pos to delete.
3. If pos>last_index, declare invalid and return.
4. x=CList[pos];
5. i=pos;
6. while(i<last_index)
CList[i]=CList[i+1];
i=i+1;
7. Decrement last_index
8. Return x as deleted item
C code for Contiguous List
#define MAXLIST 100
void insert(struct CList *, int );
int remove(struct CList *);
void display(struct CList *);

struct CList
{
int items[MAXLIST];
int last_index;
}list;
void main() break;
{ case 2:
int x; x=remove(&list);
int option; printf("\n The removed item is: %d", x);
char ch='y'; break;
list.last_index=-1; case 3:
printf("\n What do you want to do?"); display(&list);
printf("\n1.Add an item"); break;
printf("\n2.Delete an item"); case 4:
printf("\n3.Display List"); exit();
printf("\n4.Exit"); default:
while(ch=='y') printf("Invalid Option");
{ }
printf("\n Enter your option:"); printf("\n Do you want to continue(y/n)?");
scanf("%d", &option); scanf(" %c", &ch);
switch(option) }
{ getch();
case 1: }
printf("\n Enter item to insert:");
scanf("%d", &x);
insert(&list, x);
void insert(struct CList *list, int x) if(pos>(list->last_index)+1)
{ {
int pos; printf("\n Invalid");
int i; return;
if(list->last_index==MAXLIST-1) }
{ else
printf("\n List if Full."); {
return; i=list->last_index;
} while(i>=pos)
if(list->last_index==-1) {
{ list->items[i+1]=list->items[i];
++(list->last_index); i=i-1;
list->items[0]=x; }
return; list->items[pos]=x;
} (list->last_index)++;
return;
printf("\n Enter position to insert:"); }
scanf("%d", &pos); }
int remove(struct CList *list) else
{ {
int pos; x=list->items[pos];
int i; i=pos;
int x; while(i<list->last_index)
if(list->last_index==-1) {
{ list->items[i]=list->items[i+1];
printf("\n List is empty."); i=i+1;
return -1; }
} (list->last_index)--;
else return x;
{ }
printf("\n Enter position to delete:"); }
scanf("%d", &pos); }

if(pos>list->last_index) void display(struct CList *list)


{ {
printf("\n Invalid"); int i;
return -1; for(i=0;i<=list->last_index;i++)
} printf("\n CList[%d]=%d", i, list->items[i]);
}
Linked List
• A linked list is defined as a collection of nodes in which
each node has two parts:
– (i) information part and
– (ii) link part
• To store a set of items in linked list representation, the
information part of a node contains the actual item of
the list while the link part of the node contains the
address of the next node in the list. The address of the
last node of a linked list is NULL.

Fig: Linked list with two nodes


In C, a linked list template is created using a self-referential structure
and nodes of the linked list are created and removed using dynamic
memory allocation functions like malloc() and free(). We consider
headnode as an external pointer. This pointer helps in creating and
accessing other nodes in the linked list.
The following code defines the structure for linked list and creates a
headnode.
struct linkedlist
{
int info;
struct linkedlist *link;
};
struct linkedlist *head;
head=NULL;
Note: The pointer member variable link points to (i.e. contains address
of) a structure variable of the structure type linkedlist.
Self Referential Structure
• It is a structure which contains reference to it self.
• A structure whose one of the member is pointer whose
data type is structure it self.
• This can be defined as:
struct structure_name
{
member1;
Member2;
--------------
--------------
Srtuct strucure_name *variable_name;
};
Operations on linked list
1. Inserting a node to the linked list
 Inserting a node at the first position
 Inserting a node at the end.
 Inserting a node at the specified position.
2. Deleting a node from the linked list
 Deleting a node from the first position.
 Deleting a node from the end
 Deleting a node from specified position.
3. Displaying a content of the linked list
4. Searching a value in linked list.
5. Counting the number of nodes in linked list.
Types of Linked Lists
• Linked lists are of following four types:
1. Singly Linked List
2. Circular Linked List
3. Doubly Linked List
4. Circular Doubly Linked List
Singly Linked List
A singly linked list is one in which all nodes are linked together in
some sequential manner by an explicit pointer. Hence, it is also
called linear linked list.
Nodes of linked list contains two fields: data field and pointer field.
Data field contains the actual data item and pointer field contains
the address to the next node.
The pointer field of last node contains the NULL .
startnode 1022 6
1011 5 1033 7 NULL
1011 1022 1033

Problem: We cannot access the predecessor node from the


current node. This problem can be overcome by using doubly
linked list.
Structure of node of singly linked list
struct SLL
{
Int data;
struct SLL *ptr;
};
struct SLL *head;
head=NULL;
Algorithm: Add at begining
• Let *head be the pointer to the first node and *newnode be another
pointer of structure type
1. Start
2. Create a new node using malloc() function.
i.e newnode=(struct SLL*)malloc(size of(struct SLL));
3. Read the number say num
4. Assign num to data field
i.e newnode->data=num;
5. Check the head pointer. If it points to NULL then
Set: head pointer to newnode
i.e head=newnode;
6. otherwise:
Set: pointer field of newnode to first node
i.e newnode->ptr=head
Set: head pointer to newnode
i.e head=newnode;
7. stop
Add at end Algorithm
• Let *head be pointer to first node, *newnode and
*temp be two pointers of type structure.
1. Start
2. Allocate memory for newnode
i.e newnode=(struct SLL*)malloc(size of(struct SLL));
3. Read the number say: num
4. Assign num to data field of newnode.
i.e newnode->data=num;
5. Check the head pointer. If it points to NULL then
Set: head pointer to newnode
i.e head=newnode;
6. otherwise: traverse up to last node
i.e set: temp =head;
while(temp->ptr!=NULL)
Set: temp=temp->ptr
7. Set pointer field of last node to newnode
i.e temp->ptr=newnode
8. stop
Add at specified Position
• Let *head be pointer to first node, *newnode and
*temp be two pointers of type structure and pos
be position.
1. Start
2. Allocate memory for newnode
i.e newnode=(struct SLL*)malloc(size of(struct SLL));
3. Read the number say: num
4. Assign num to data field of newnode.
i.e newnode->data=num;
5. Check the head pointer. If it points to NULL then
Set: head pointer to newnode
i.e head=newnode;
6. otherwise: read the position
traverse up to position
i.e set: temp =head;
for(i=1;i<pos-1;i++)
Set: temp=temp->ptr
7. Set pointer field of temp to newnode
i.e temp->ptr=newnode
8. Set pointer field of newnode to pointer field of
temp
i.e newnode->ptr=temp->ptr
9. stop
Delete from begining
• Let *head be the pointer to the first node and *temp be the
pointer variable of structure type.
1. Start
2. Check the head pointer and if it is NULL then display the
message: list does not exist and exit.
i.e if (*head==NULL)
print: linked list does not exist
3. Store address of first node to temp
i.e temp=head
4. Set head to ptr field of head
i.e head=head->ptr
5. Free the memory reserved by temp
6. stop
Delete from end
• Let *head be the pointer to the first node and *temp be the pointer variable of
structure type.
1. Start
2. Check the head pointer and if it is NULL then display the message: list does not exist
and exit.
i.e if (*head==NULL)
print: linked list does not exist
3. Store address of first node to temp
i.e temp=head
4. Traverse up to last node such that last node becomes temp and second last node
becomes temp1
Set: temp=head
While(temp!=NULL)
Set: temp1=temp
Set temp=temp->ptr
5. Free the memory reserved by temp
6. Set ptr field of second last node to NULL
i.e temp1->ptr=NULL
7. stop
Display the Content
1. Start
2. If head== NULL then
Display: linked list does not exist and exit.
3. set: temp=head
4. While temp!=NULL
Display: data field of temp
Set: temp=temp->ptr
5. stop
Circular Linked List
• A circular linked list is just a singly linked list in which
the link field of the last node contains the address of the
first node of the list. That is, the link field of the last
node does not point to NULL, rather it points back to the
beginning of the linked list.
• It has no beginning and end (It must be established).
• Problem: Cannot traverse list in backward direction.
lastnode
1033

5 1022 6 1033 7 1011


1011 1022 1033
Structure of Node
struct cll
{
int data;
struct cll *next;
};
struct cll *head, *tail;
head=NULL;
tail=NULL;
Add at begining
1. Start
2. Create a newnode using malloc()
i.e. newnode=(struct cll*)malloc(sizeof(struct cll));
3. Read a number say num
4. set: newnode->data=num
5. If head==NULL then,
Set: newnode->next=newnode
Set: head=tail=newnode
Else:
Set: newnode->next=head
head=newnode
tail->next=head
6. stop
Add at End
1. Start
2. Create a newnode using malloc()
i.e. newnode=(struct cll*)malloc(sizeof(struct cll));
3. Read a number say: num
4. set: newnode->data=num
5. If head==NULL then,
Set: newnode->next=newnode
Set: head=tail=newnode
Else:
Set: tail->next=newnode
tail=newnode
tail->next=head
6. stop
Display the Content
1. Start
2. If head== NULL then
Display: linked list does not exist and exit.
3. set: temp=head
4. While temp!=tail
Display: data field of temp
Set: temp=temp->next
5. stop
Delete from beginning
1. Start
2. If head==NULL then
Display: linked list does not exist and exit.
Else
Display: deleted item is: head->data
Set: head=head->next
Set: tail->next=head
3. stop
Delete from end
1. Start
2. If head==NULL then
Display: linked list does not exist and exit.
Else
Set: temp=head
Traverse up to last
i.e. while(temp->next!=tail)
Set: temp=temp->next
3. Display: deleted item is: tail->data
4. Set: tail=temp
5. Set: tail->next=head
6. stop
Doubly Linked List
• A doubly linked list is one in which all nodes are linked
together by multiple links which help in accessing both
the successor node (i.e. next node) and the predecessor
node (i.e. previous node) from any arbitrary node within
the list.
• Therefore each node in a doubly linked list requires two
pointers: one to the left node (previous node) and other
to the right node (next node). This helps to traverse the
list in the forward direction and also to the backward
direction (bi-directional traversing).
p re v l i n k i n fo n ex t l i n k

Fig: A node in doubly linked list


• 1011

• NULL
• 5• 1022 • 1011• 6 • 1033• 1022• 7 • NULL

• 1011 • 1022 • 1033

• Fig: A doubly linked list


Structure of node

struct dll
{
int data;
struct dll *next;
struct dll *prev;
};
struct cll *head;
head=NULL;
Add at begining

• Let *head be the pointer to the first node and *newnode be another pointer
of structure type
1. Start
2. Create a new node using malloc() function.
i.e newnode=(struct SLL*)malloc(size of(struct SLL));
3. Read the number say num
4. Assign num to data field
i.e newnode->data=num;
5. Check the head pointer. If it points to NULL then
Set: head pointer to newnode
i.e head=newnode;
Set: newnode->next=newnode->prev=NULL
6. otherwise:
Set: next pointer field of newnode to first node
i.e newnode->next=head
Set: head->prev=newnode
7. Set: head pointer to newnode
i.e head=newnode;
7. stop

You might also like