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

unit-iv_srb

The document provides an overview of linked lists, explaining their structure, types (singly and doubly linked lists), and key operations such as insertion, deletion, searching, and reversing. It compares linked lists with arrays, highlighting their dynamic size and efficiency in insertions and deletions, while also noting the lack of random access. Additionally, it includes code snippets for implementing various operations on linked lists.

Uploaded by

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

unit-iv_srb

The document provides an overview of linked lists, explaining their structure, types (singly and doubly linked lists), and key operations such as insertion, deletion, searching, and reversing. It compares linked lists with arrays, highlighting their dynamic size and efficiency in insertions and deletions, while also noting the lack of random access. Additionally, it includes code snippets for implementing various operations on linked lists.

Uploaded by

riddhibhabda1108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

What are Linked Lists

 A linked list is a linear data


structure.
 Nodes make up linked lists.
 Nodes are structures made up
of data and a pointer to another
node.
 Usually the pointer is called
next.
Arrays Vs Linked Lists
Arrays Linked list

Fixed size: Resizing is expensive Dynamic size

Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting

Random access i.e., efficient indexing No random access


 Not suitable for operations requiring
accessing elements by index such as sorting

No memory waste if the array is full or almost Since memory is allocated dynamically(acc. to
full; otherwise may result in much memory our need) there is no waste of memory.
waste.

Sequential access is faster [Reason: Elements in Sequential access is slow [Reason: Elements not
contiguous memory locations] in contiguous memory locations]
Types of lists
 There are two basic types of linked list

 Singly Linked list

 Doubly linked list


Singly Linked List
 Each node has only one link part

 Each link part contains the address of the next node in


the list

 Link part of the last node contains NULL value which


signifies the end of the node
Schematic representation
 Here is a singly-linked list (SLL):
Header
pointer
(*head)

a b c d

• Each node contains a value(data) and a pointer


to the next node in the list
•The header pointer which points at the first
node in the list
Basic Operations on a list

• Creating a List
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
• Reversing a list
Creating a node

struct node
{ // A simple node of a linked list
int data;
struct node *next; head points at the first node
}*head;
struct node *createnode()
{
struct node *newnode =(struct node*)malloc
(sizeof(struct node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=NULL;
return(newnode);
}
void accept()
{
int n;
int i;
struct node *temp;
head=NULL;
printf("how many node u want to enter");
scanf("%d",&n);
for(i=0;i<n;i++)
{
struct node *newnode = createnode();
if(head==NULL)
{
head=newnode;
}
else
{ temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
Display()
 void display()
 {
 struct node *temp=head;
 while(temp!=NULL)
 {
 printf("|%d\t|->",temp->data);
 temp=temp->next;
 }
 printf("NULL");
Searching a SLL
 Searching involves finding the required element in the
list
 We can use various techniques of searching like linear
search or binary search where binary search is more
efficient in case of Arrays
 But in case of linked list since random access is not
available it would become complex to do binary search
in it
 We can perform simple linear search traversal
Search()

 void search()
 {
 struct node *temp;
 int serno; int flag; flag=0;
 temp=head;
 IF(HEAD==NULL)
 PRINTF(“List is empty”);
 printf("\n which no u want to search");
 scanf("%d",&serno);

 while(temp!=NULL)
 {
 if(serno==temp->data)
 {
 flag=1;
 break;
 }
 temp=temp->next;
 }
 if(flag==0)
 {
 printf("number not found");
 }
 else
 {
 printf("number found");
 }
 display();
 }
Inserting the node in a SLL

There are 3 cases here:-

Insertion at the beginning

Insertion at the end


Insertion after a particular node
Insertion at the beginning
There are two steps to be followed:-

a) Make the next pointer of the node point towards the


first node of the list

b) Make the start pointer point towards this new node

 If the list is empty simply make the start pointer


point towards the new node;
Insertion at the beginning
void insert_begning()
{
struct node *newnode=createnode();
newnode->next=head;
head=newnode;
display();
}
Insert after
void insert_after()
{
struct node *newnode=createnode();
struct node *temp;
int serno;
printf(" after which node u want insert");
scanf("%d",&serno);
temp=head;
while(temp->data!=serno)
{
temp=temp->next;

}
newnode->next=temp->next;
temp->next=newnode;
display();
}
Insert end
void insert_end()
{
struct node *newnode=createnode();
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;

}
temp->next=newnode;
display();
}
Inserting at the end
Here we simply need to make the next pointer
of the last node point to the new node
void insert_end(node* p)
{
node *q=start;
if(start==NULL)
{
start=p;
cout<<”\nNode inserted successfully at the end…!!!\n”;
}
else{
while(q->link!=NULL)
q=q->link;
q->next=p;
}
}
Deleting a node in SLL
Here also we have three cases:-

 Deleting the first node

Deleting the last node

Deleting the intermediate node


Deleting the first node
Here we apply 2 steps:-

 Making the start pointer point towards the 2nd


node

 Deleting the first node using delete keyword

start

one two three


void delete_begning()
{
struct node *temp;
temp=head;
head=head->next;
temp->next=NULL;
free(temp);
display();
}
Deleting the last node
Here we apply 2 steps:-

 Making the second last node’s next pointer point


to NULL

 Deleting the last node via delete keyword

start

node1 node2 node3


void delete_end()
{
struct node *temp;
struct node *old;
temp=head;
while(temp->next!=NULL) //while start
{
old=temp;
temp=temp->next;

} //while end
old->next=NULL;
free(temp);
display();

}
Deleting a particular node
Here we make the next pointer of the node previous to
the node being deleted ,point to the successor node of
the node to be deleted and then delete the node using
delete keyword

node1 node2 node3

To be deleted
void delete_after()
{
struct node *temp; struct node *temp1; struct node *temp2;
int serno;
printf(" after which node u want delete");
scanf("%d",&serno);
temp=head;
while(temp->data!=serno)
{
temp=temp->next;

}
temp1=temp->next;
temp2=temp1->next;
temp->next=temp2;
temp1->next=NULL;
free(temp1);
display();
}
void update()
{
//printf("\n update function");
int serno;

int flag;
struct node *temp;
flag=0;
temp=head; // //10->20->30
printf("\n which no u want to update");
scanf("%d",&serno); //20
while(temp!=NULL) //while start
{
if(serno==temp->data) //if start
{ printf("enter new data");
scanf("%d",&temp->data);
flag=1;
break;
} //if end
temp=temp->next;
} //while end
if(flag==0)
{
printf("\n number not found");
}
else
{
printf("number is found It is updated successfully");
}
display();
}
void sort()
{

struct node *temp,*temp1,*temp2;


temp=head;

for(temp=head;temp!=NULL;temp=temp->next)
{
for(temp1=temp->next;temp1!=NULL;temp1=temp1->next)
{
if(temp->data>temp1->data)
{
temp2->data=temp->data;
temp->data=temp1->data;
temp1->data=temp2->data;
}
}
}
display();
}
void concatination()
{
struct node *temp,*temp1,*head1,*head2,*temp2;
// temp=head;
head1=head;
accept();
head2=head;
temp=head1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head2;
head=head1;

// display();

}
Reversing a linked list
• We can reverse a linked list by reversing the
direction of the links between 2 nodes
 We make use of 3 structure pointers say p,q,r

 At any instant q will point to the node next to p and r


will point to the node next to q

Head P p
q q
r NULL

NULL

• For next iteration p=q and q=r

• At the end we will change head to the last node


Code
void reverse()
{
struct node *temp,*old,*oldold;
temp=head;
old=NULL;
while(temp!=NULL) //while start
{
oldold=old;
old=temp;
temp=temp->next;
old->next=oldold;

} //while end
head=old;
display();
COMPLEXITY OF VARIOUS OPERATIONS
IN ARRAYS AND SLL

Operation ID-Array Complexity Singly-linked list Complexity


Insert at beginning O(n) O(1)
Insert at end O(1) O(1) if the list has tail reference
O(n) if the list has no tail reference

Insert at middle O(n) O(n)


Delete at beginning O(n) O(1)
Delete at end O(1) O(n)
Delete at middle O(n): O(n):
O(1) access followed by O(n) O(n) search, followed by O(1) delete
shift
Search O(n) linear search O(n)
O(log n) Binary search

Indexing: What is O(1) O(n)


the element at a
given position k?
Doubly Linked List
1. Doubly linked list is a linked data structure that consists of a set of
sequentially linked records called nodes.

2. Each node contains three fields ::


-: one is data part which contain data only.
-:two other field is links part that are point
or references to the previous or to the next
node in the sequence of nodes.

3. The beginning and ending nodes' previous and next


links, respectively, point to some kind of terminator,
typically a sentinel node or null to facilitate traversal
of the list.
NODE
previous data next

A B C
NULL 11 786 200 656 400 786 777 NULL

200 786 400

A doubly linked list contain three fields: an integer value, the


link to the next node, and the link to the previous node.
DLL’s compared to SLL’s

 Advantages:  Disadvantages:
 Can be traversed in either  Requires more space
direction (may be  List manipulations are
essential for some slower (because more
programs) links must be changed)
 Some operations, such as  Greater chance of having
deletion and inserting bugs (because more links
before a node, become must be manipulated)
easier
Structure of DLL
struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};

previous. .Data .next


inf
struct node
{
int data;
struct node *next;
struct node *prev;
}*head;
struct node *createnode()
{
struct node *newnode =(struct node*)malloc (sizeof(struct
node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
return(newnode);
}
void display()
{
struct node *temp=head;
while(temp!=NULL)
{
printf("|%d\t|-<-->",temp->data);
temp=temp->next;
}
printf("NULL");
}
 void search()
{

struct node *temp;


int serno;

int flag;

flag=0;

temp=head;
printf("\n search function");

printf("\n which no u want to search");


scanf("%d",&serno);

while(temp!=NULL)
{
if(serno==temp->data)
{
flag=1;
break;
}
temp=temp->next;
}
if(flag==0)
{
printf("number not found");
}
else
{
printf("number found");
}
display();
}
Inserting at beginning
void insert_begning()
{
struct node *newnode=createnode();
newnode->next=head;
head->prev=newnode;
head=newnode;
display();
}
Inserting at the end
void insert_end()
{
struct node *newnode=createnode();
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;

}
temp->next=newnode;
newnode->prev=temp;
newnode->next=NULL;
display();
}
Inserting after a node

Making next and previous pointer of the node to be


inserted point accordingly

Adjusting the next and previous pointers of the nodes b/w which
the new node accordingly
void insert_after()
{
struct node *newnode=createnode();
struct node *temp,*temp1;
int serno;
printf(" after which node u want insert");
scanf("%d",&serno);
temp=head;
while(temp->data!=serno)
{
temp=temp->next;

}
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
newnode->prev=temp;
display();
}
Deleting a node
• Node deletion from a DLL involves changing two links
• In this example,we will delete node b
myDLL

a b c

• We don’t have to do anything about the links in node b


• Garbage collection will take care of deleted nodes
• Deletion of the first node or the last node is a special
case
void delete_begning()
{
struct node *temp;
temp=head;
head=head->next;
temp->next=NULL;
head->prev=NULL;
free(temp);
display();
}
void delete_end()
{
struct node *temp;
struct node *old;
temp=head;
while(temp->next!=NULL) //while start
{
old=temp;
temp=temp->next;

} //while end
old->next=NULL;
temp->prev=NULL;
free(temp);
display();

}
void delete_after()
{
// struct node *newnode=createnode();
struct node *temp;
struct node *temp1;
struct node *temp2;
int serno;
printf(" after which node u want delete");
scanf("%d",&serno);
temp=head;
while(temp->data!=serno)
{
temp=temp->next;

}
temp1=temp->next;
temp2=temp1->next;
temp->next=temp2;
temp2->prev=temp->prev;
temp1->next=NULL;
free(temp1);
display();
}
void reverse()
{
struct node *temp,*old,*oldold;
temp=head;

while(temp->next!=NULL) //while start


{

temp=temp->next;
}

while(temp!=NULL)

{
printf("|%d\t|-<-->",temp->data);
temp=temp->prev;

} printf("NULL");

}
APPLICATIONS OF LINKED LIST
1. Applications that have an Most Recently Used(a linked list
of file names)

2. Consider the history section of web browsers, where it


creates a linked list of web-pages visited, so that when you
check history (traversal of a list) or press back button, the
previous node's data is fetched.

3. Undo functionality in Photoshop or Word (a linked list of


state)

4. A stack, hash table, and binary tree can be implemented


using a doubly linked list.
APPLICATIONS OF DOUBLY
LINKED LIST
1. double linked list was that the structure suited the
behavior of a music playlist.
 Skip Back/Forward
 Play Next Track
 Append
 Beginning/End
2. simple real life example is a Train, here each coach is
connected to its previous and next coach (Except first and
last). In terms of programming consider coach body as node
value and connectors as links to previous and next nodes.
• Polynomials
•Array Implementation:
• p1(x) = 8x3 + 3x2 + 2x + 6
• p2(x) = 23x4 + 18x - 3
p1(x) p2(x)

6 2 3 8 -3 18 0 0 23
0 2 0 2 4

Index
represents
exponents
•This is why arrays aren’t good to represent
polynomials:

• p3(x) = 16x21 - 3x5 + 2x + 6

6 2 0 0 -3 0 ………… 0 16

WASTE OF SPACE!
• Advantages of using an Array:

• only good for non-sparse polynomials.


• ease of storage and retrieval.

• Disadvantages of using an Array:

• have to allocate array size ahead of time.


• huge array size required for sparse
polynomials.
•Waste of space and runtime.
• Polynomial Representation

• Linked list Implementation:

• p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3


• p2(x) = 4x6 + 10x4 + 12x + 8

P1 23 9 18 7 41 6 18 7 3 0

TAIL (contains pointer)

P2 4 6 10 4 12 1 8 0

NODE (contains coefficient & exponent)


• Advantages of using a Linked list:

• save space (don’t have to worry about sparse


polynomials) and easy to maintain
• don’t need to allocate list size and can
declare nodes (terms) only as needed

• Disadvantages of using a Linked list :


• can’t go backwards through the list
• can’t jump to the beginning of the list from
the end.
Polynomials
A( x )  a m 1 x e m1
 am2 x e m 2
... a 0 x e0

Representation

struct polynode {
int coef;
int exp;
struct polynode * next;
};
typedef struct polynode *polyptr;

coef exp next


• Adding polynomials using a Linked list
representation: (storing the result in p3)

To do this, we have to break the process down to


cases:
• Case 1: exponent of p1 > exponent of p2
• Copy node of p1 to end of p3.
[go to next node]
• Case 2: exponent of p1 < exponent of p2
• Copy node of p2 to end of p3.
[go to next node]
• Case 3: exponent of p1 = exponent of p2
• Create a new node in p3 with the same
exponent and with the sum of the
coefficients of p1 and p2.
Example
a  3x  2 x  1
14 8

a
3 14 2 8 1 0 null

b  8 x 14  3 x 10  10 x 6
b
8 14 -3 10 10 6 null
Adding Polynomials
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 a->expon == b->expon
d
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 a->expon < b->expon
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 2 8
d
a->expon > b->expon
THANK YOU

You might also like