SlideShare une entreprise Scribd logo
1  sur  49
LINKED LIST 
aND 
ITS TYPES 
Trupti Agrawal 1
LINKED LIST 
In computer science, a linked list is a data structure consisting of a group 
of nodes which together represent a sequence. Under the simplest form, 
each node is composed of a data and a reference (in other words, a link) to 
the next node in the sequence; more complex variants add additional links. 
This structure allows for efficient insertion or removal of elements from 
any position in the sequence. 
Fig.: A linked list whose nodes contain two fields: an integer value and a 
link to the next node. The last node is linked to a terminator used to signify 
the end of the list. 
Trupti Agrawal 2
Singly Linked Lists 
Linked lists are drawn as an order sequence of nodes 
with links represented as arrows (Figure 4.1). 
The name of the pointer to the first node in the list is the name 
of the list. (the list of Figure 4.1 is called ptr.) 
 Notice that we do not explicitly put in the values of pointers, but simply 
draw allows to indicate that they are there. 
Trupti Agrawal 3
Singly Linked Lists [cont…] 
The nodes do not resident in sequential locations 
The locations of the nodes may change on different 
runs 
Link Field 
Data Field 
Node 
. . . Null 
ptr 
chain 
Trupti Agrawal 4
Singly Linked Lists [cont…] 
Why it is easier to make arbitrary insertions and deletions using a linked 
list? 
To insert the word mat between cat can sat, we must: 
 Get a node that is currently unused; let its address be paddr. 
 Set the data field of this node to mat. 
 Set paddr’s link field to point to the address found in the link field of 
the node containing cat. 
 Set the link field of the node containing cat to point to paddr. 
Trupti Agrawal 5
Singly Linked Lists [cont…] 
Delete mat from the list: 
We only need to find the element that immediately precedes 
mat, which is cat, and set its link field to point to mat’s link 
(Figure 4.3). 
We have not moved any data, and although the link field of 
mat still points to sat, mat is no longer in the list. 
Trupti Agrawal 6
Singly Linked Lists [cont…] 
We need the following capabilities to make linked 
representations possible: 
Defining a node’s structure, that is, the fields it contains. We 
use self-referential structures, discussed in Section 2.2 to do 
this. 
Create new nodes when we need them. (malloc) 
Remove nodes that we no longer need. (free) 
Trupti Agrawal 7
Singly Linked Lists [cont…] 
2.2.4 Self-Referential Structures 
One or more of its components is a pointer to itself. 
typedef struct list { 
char data; 
list *link; 
} 
Construct a list with three nodes 
item1.link=&item2; 
item2.link=&item3; 
malloc: obtain a node (memory) 
free: release memory 
list item1, item2, item3; 
item1.data=‘a’; 
item2.data=‘b’; 
item3.data=‘c’; 
item1.link=item2.link=item3.link=NULL; 
a b c 
Trupti Agrawal 8
Singly Linked Lists [cont…] 
Example [List of words ending in at]: 
Declaration 
typedef struct list_node *list_pointer; 
struct list_node { 
char data [4]; 
list_pointer link; 
}; 
Creation 
list_pointer ptr =NULL; 
Testing 
#define IS_EMPTY(ptr) (!(ptr)) 
Allocation 
ptr=(list_pointer) malloc (sizeof(list_node)); 
Return the spaces: 
free(ptr); Trupti Agrawal 9
Singly Linked Lists [cont…] 
e -> name  (*e).name 
strcpy(ptr -> data, “bat”); 
ptr -> link = NULL; 
address of 
first node 
ptr data ptr link 
 b a t 0 NULL 
ptr 
Figure 4.4:Referencing the fields of a node(p.142) 
Trupti Agrawal 10
Singly Linked Lists [cont…] 
Example 4.2 [Two-node linked list]: 
typedef struct list_node *list_pointer; 
typedef struct list_node { 
int data; 
list_pointer link; 
}; 
list_pointer ptr =NULL; 
Program 4.2: Create a two-node list 
list_pointer create2( ) 
{ 
/* create a linked list with two nodes */ 
list_pointer first, second; 
first = (list_pointer) malloc(sizeof(list_node)); 
second = (list_pointer) malloc(sizeof(list_node)); 
second -> link = NULL; 
second -> data = 20; 
ptr 
first -> data = 10; 
first ->link = second; 
return first; 
10 20 
} #define IS_FULL(ptr) (! 
(ptr)) 
When returns NULL if there 
is no more memory. 
 NULL 
Trupti Agrawal 11
Singly Linked Lists [cont…] 
• Insertion 
Observation 
 insert a new node with data = 50 into the list ptr after node 
node 
10 20 
50 
NULL 
temp 
ptr 
Trupti Agrawal 12
Singly Linked Lists [cont…] 
Implement Insertion: 
void insert(list_pointer *ptr, List_pointer node) 
{ 
/* insert a new node with data = 50 into the list ptr after 
node */ 
list_pointer temp; 
temp=(list_pointer)malloc(sizeof(list_node)); 
if(IS_FULL(temp)){ 
fprintf(stderr, “The memory is fulln”); 
exit(1); 
} 
temp->data=50; 
temp 50 
Trupti Agrawal 13
Singly Linked Lists [cont…] 
if(*ptr){ //nonempty list 
temp->link = node->link; 
node->link = temp; 
} 
else{ //empty list 
temp->link = NULL; 
*ptr = temp; 
} 
} 
node 
10 20 
50 
NULL 
temp 
ptr 
Trupti Agrawal 14
Singly Linked Lists [cont…] 
Deletion 
Observation: delete node from the list 
ptr trial node 
10 50 20 NULL 
ptr trial node 
10 50 20 NULL 
ptr 
10 20 NULL 
Trupti Agrawal 15
Singly Linked Lists [cont…] 
Implement Deletion: 
void delete(list_pointer *ptr, list_pointer trail, 
list_pointer node) 
{ 
/* delete node from the list, trail is the preceding node 
ptr is the head of the list */ 
if(trail) 
trail->link = node->link; 
else 
*ptr = (*ptr)->link; 
free(node); 
} 
ptr trial node 
10 50 20 NULL 
Trupti Agrawal 16
Singly Linked Lists [cont…] 
Print out a list (traverse a list) 
Program 4.5: Printing a list 
void print_list(list_pointer ptr) 
{ 
printf(“The list contains: “); 
for ( ; ptr; ptr = ptr->link) 
printf(“%4d”, ptr->data); 
printf(“n”); 
} 
Trupti Agrawal 17
Dynamically Linked Stacks and Queues 
When several stacks and queues coexisted, there was no 
efficient way to represent them sequentially. 
Notice that direction of links for both stack and the queue 
facilitate easy insertion and deletion of nodes. 
 Easily add or delete a node form the top of the stack. 
 Easily add a node to the rear of the queue and add or delete a node at the 
front of a queue. 
Trupti Agrawal 18
Dynamically Linked 
Stacks and Queues [cont…] 
Represent n stacks 
top item link 
link 
... 
NULL 
Stack 
Trupti Agrawal 19
Dynamically Linked 
Stacks and Queues [cont…] 
Push in the linked stack 
void add(stack_pointer *top, element item){ 
/* add an element to the top of the stack */ Push 
stack_pointer temp = (stack_pointer) malloc (sizeof (stack)); 
if (IS_FULL(temp)) { 
fprintf(stderr, “ The memory is fulln”); 
exit(1); 
}t 
emp->item = item; 
temp->link = *top; 
*top= temp; 
} 
temp item link 
top link 
link 
... 
NULL 
Trupti Agrawal 20
Dynamically Linked 
Stacks and Queues [cont…] 
• Pop from the linked stack 
element delete(stack_pointer *top) { 
/* delete an element from the stack */ Pop 
stack_pointer temp = *top; 
element item; 
if (IS_EMPTY(temp)) { 
fprintf(stderr, “The stack is emptyn”); 
exit(1); 
}i 
tem = temp->item; 
*top = temp->link; 
free(temp); 
return item; 
} 
item link 
link 
link 
... 
NULL 
temp 
top 
Trupti Agrawal 21
Dynamically Linked 
Stacks and Queues [cont…] 
Represent n queues 
front item link 
link 
... 
NULL 
Queue 
rear 
Delete from 
Add to 
Trupti Agrawal 22
Dynamically Linked 
Stacks and Queues [cont…] 
enqueue in the linked queue 
front link 
... 
NULL 
temp item 
link 
rear 
NULL 
Trupti Agrawal 23
Dynamically Linked 
Stacks and Queues [cont…] 
dequeue from the linked queue (similar to push) 
front item link 
link 
link 
... 
NULL 
temp 
rear 
Trupti Agrawal 24
Dynamically Linked 
Stacks and Queues [cont…] 
The solution presented above to the n-stack, m-queue 
problem is both computationally and 
conceptually simple. 
We no longer need to shift stacks or queues to make 
space. 
Computation can proceed as long as there is memory 
available. 
Trupti Agrawal 25
Polynomials 
A polynomial is an expression that contains more than two 
terms. A term is made up of coefficient and exponent. An 
example of polynomial is 
P(x) = 4x3+6x2+7x+9 
Trupti Agrawal 26
Polynomials [cont…] 
A polynomial thus may be represented using arrays or linked 
lists. 
Array representation assumes that the exponents of the given 
expression are arranged from 0 to the highest value (degree), 
which is represented by the subscript of the array beginning 
with 0. The coefficients of the respective exponent are placed 
at an appropriate index in the array. The array representation 
for the above polynomial expression is given below: 
Trupti Agrawal 27
Polynomials [cont…] 
A polynomial may also be represented using a linked list. A 
structure may be defined such that it contains two parts- one is 
the coefficient and second is the corresponding exponent. The 
structure definition may be given as shown below: 
ccooeeff eexxppoonn lliinnkk 
 struct polynomial 
{i 
nt coefficient; 
int exponent; 
struct polynomial *next; 
}; 
Trupti Agrawal 28
Polynomials [cont…] 
Thus the above polynomial may be represented 
using linked list as shown below: 
Trupti Agrawal 29
Polynomials [cont…] 
Addition of two Polynomials: 
o For adding two polynomials using arrays is straightforward method, since both the arrays 
may be added up element wise beginning from 0 to n-1, resulting in addition of two 
polynomials. 
o Addition of two polynomials using linked list requires comparing the exponents, and 
wherever the exponents are found to be same, the coefficients are added up. For terms with 
different exponents, the complete term is simply added to the result thereby making it a 
part of addition result. 
Multiplication of two Polynomials: 
o Multiplication of two polynomials however requires manipulation of each node such that 
the exponents are added up and the coefficients are multiplied. 
o After each term of first polynomial is operated upon with each term of the second 
polynomial, then the result has to be added up by comparing the exponents and adding the 
coefficients for similar exponents and including terms as such with dissimilar exponents in 
the result. 
Trupti Agrawal 30
Polynomials [cont…] 
Attach a node to the end of a list 
void attach(float coefficient, int exponent, poly_pointer *ptr){ 
/* create a new node with coef = coefficient and expon = exponent, attach 
it to the node pointed to by ptr. Ptr is updated to point to this new node 
*/ 
poly_pointer temp; 
temp = (poly_pointer) malloc(sizeof(poly_node)); 
/* create new node */ 
if (IS_FULL(temp)) { 
fprintf(stderr, “The memory is fulln”); 
exit(1); 
} 
temp->coef = coefficient; /* copy item to the new node */ 
temp->expon = exponent; 
(*ptr)->link = temp; /* attach */ 
*ptr = temp; /* move ptr to the end of the list */ 
} Trupti Agrawal 31
Polynomials [cont…] 
 Analysis of PADD (Polynomial addition) 
f f 
e e 
m A x = a x m- + ××× + a x + B x = b x n- + + b x 
( )( 1 0 ) ( )( 1 0 ) 
- -  
n 
1 0 1 0 
1. coefficient additions 
0 £ additions £ min(m, n) 
where m (n) denotes the number of terms in A (B). 
2. exponent comparisons extreme case: 
em-1 > fm-1 > em-2 > fm-2 > … > e1 > f1 > e0 > f0 
m+n-1 comparisons 
3. creation of new nodes extreme case: maximum number of 
terms in d is m+n m + n new nodes 
summary: O(m+n) 
Trupti Agrawal 32
Polynomials [cont…] 
A Suite for Polynomials 
e(x) = a(x) * b(x) + d(x) 
poly_pointer a, b, d, e; 
... 
a = read_poly(); 
b = read_poly(); 
d = read_poly(); 
temp = pmult(a, b); 
e = padd(temp, d); 
print_poly(e); 
read_poly() 
print_poly() 
padd() 
psub() 
pmult() 
temp is used to hold a partial result. 
By returning the nodes of temp, we 
may use it to hold other polynomials 
Trupti Agrawal 33
Polynomials [cont…] 
Erase Polynomials 
erase frees the nodes in temp 
void erase (poly_pointer *ptr){ 
/* erase the polynomial pointed to by ptr */ 
poly_pointer temp; 
while ( *ptr){ 
temp = *ptr; 
*ptr = (*ptr) -> link; 
free(temp); 
} 
} 
Trupti Agrawal 34
Circularly Linked Lists 
Circular Linked list 
The link field of the last node points to the first node in 
the list. 
Example 
Represent a polynomial ptr = 3x14+2x8+1 as a circularly 
linked list. 
ptr 33 1144 22 88 11 00 
Trupti Agrawal 35
Circularly Linked Lists [cont…] 
Maintain an Available List 
We free nodes that are no longer in use so that we may 
reuse these nodes later 
We can obtain an efficient erase algorithm for circular 
lists, by maintaining our own list (as a chain) of nodes 
that have been “freed”. 
Instead of using malloc and free, we now use get_node 
(program 4.13) and ret_node (program 4.14). 
avail ... NULL 
List of freed nodes 
Trupti Agrawal 36
Circularly Linked Lists [cont…] 
Maintain an Available List (cont’d) 
When we need a new node, we examine this list. 
If the list is not empty, then we may use one of its nodes. 
Only when the 
list is empty we 
do need to use 
malloc to 
create a new 
node. 
Trupti Agrawal 37
Circularly Linked Lists [cont…] 
Maintain an Available List (cont’d) 
Insert ptr to the front of this list 
Let avail be a variable of type poly_pointer that points 
to the first node in our list of freed nodes. 
Henceforth, we call this list the available space list or 
avail list. 
Initially, we set avail to NULL 
Trupti Agrawal 38
Circularly Linked Lists [cont…] 
Maintain an Available List 
Erase a circular list in a fixed 
amount (constant) of time 
O(1) independent of the 
number of nodes in the list 
using cerase 
      
temp 
ptr 
avail      N U LL 
Trupti Agrawal 39
Circularly Linked Lists [cont…] 
We must handle the zero polynomial as a special 
case. To avoid it, we introduce a head node into each 
polynomial 
each polynomial, zero or nonzero, contains one additional 
node. 
The expon and coef fields of this node are irrelevant. 
Why ? 
So ! 
Trupti Agrawal 40
Circularly Linked 
Lists [cont…] 
For fit the 
circular list with 
head node 
representation 
We may remove 
the test for (*ptr) 
from cerase 
Changes the 
original padd to 
cpadd 
/* head node */ 
/*a->expon=-1, so b->expont > -1 */ 
/* link to the first node */ 
Trupti Agrawal 41
Circularly Linked Lists [cont…] 
Operations for circularly linked lists 
Question: 
What happens when we want to insert a new node at the 
front of the circular linked list ptr? 
ptr x1 x2 x3 
Answer: 
 move down the entire length of ptr. 
Possible Solution: 
x1 x2 x3 ptr 
Trupti Agrawal 42
Circularly Linked Lists [cont…] 
 Insert a new node 
at the front of a 
circular list 
 To insert node at 
the rear, we only 
need to add the 
additional 
statement *ptr = 
node to the else 
clause of 
insert_front 
x1 x2 x3 ptr 
node 
Trupti Agrawal 43
Circularly Linked Lists [cont…] 
Finding the length of a circular list 
Trupti Agrawal 44
Doubly Linked Lists [cont…] 
Singly linked lists pose problems because we can 
move easily only in the direction of the links 
Doubly linked list has at least three fields 
left link field(llink), data field(item), right link 
field(rlink). 
The necessary declarations: 
typedef struct node *node_pointer; 
typedef struct node{ 
node_pointer llink; 
element item; 
node_pointer rlink; 
}; 
... NULL 
? ptr 
Trupti Agrawal 45
Doubly Linked Lists [cont…] 
Sample 
doubly linked circular with head node: (Figure 4.23) 
empty double linked circular list with head node 
(Figure 4.24) 
suppose that ptr points to any node in a doubly 
linked list, then: 
 ptr = ptr -> llink -> rlink = ptr -> rlink -> llink 
Trupti Agrawal 46
Doubly Linked Lists [cont…] 
Insert node 
Head node 
llink item rlink 
New node 
node 
Trupti Agrawal 47
Doubly Linked Lists [cont…] 
Delete node 
Head node 
llink item rlink 
deleted node 
Trupti Agrawal 48
THANK YOU….. !!! 
Trupti Agrawal 49

Contenu connexe

Tendances

Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked ListNinad Mankar
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structureAbrish06
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure pptNalinNishant3
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.pptTirthika Bandi
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)Huba Akhtar
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy TutorialAfzal Badshah
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queueSrajan Shukla
 

Tendances (20)

Binary Search
Binary SearchBinary Search
Binary Search
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
stack & queue
stack & queuestack & queue
stack & queue
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
single linked list
single linked listsingle linked list
single linked list
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Linked List
Linked ListLinked List
Linked List
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Linked list
Linked listLinked list
Linked list
 

En vedette

Doubly linked list
Doubly linked listDoubly linked list
Doubly linked listFahd Allebdi
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8sumitbardhan
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structureTushar Aneyrao
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Lovelyn Rose
 
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)John C. Havens
 
Double linked list
Double linked listDouble linked list
Double linked listraviahuja11
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Trees data structure
Trees data structureTrees data structure
Trees data structureSumit Gupta
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAbhishek L.R
 
Open Legal Data Workshop at Stanford
Open Legal Data Workshop at StanfordOpen Legal Data Workshop at Stanford
Open Legal Data Workshop at StanfordHarry Surden
 
Harry Surden - Artificial Intelligence and Law Overview
Harry Surden - Artificial Intelligence and Law OverviewHarry Surden - Artificial Intelligence and Law Overview
Harry Surden - Artificial Intelligence and Law OverviewHarry Surden
 
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017Carol Smith
 

En vedette (18)

Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
7 Myths of AI
7 Myths of AI7 Myths of AI
7 Myths of AI
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
linked list
linked list linked list
linked list
 
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
 
Cyber Crime
Cyber CrimeCyber Crime
Cyber Crime
 
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Open Legal Data Workshop at Stanford
Open Legal Data Workshop at StanfordOpen Legal Data Workshop at Stanford
Open Legal Data Workshop at Stanford
 
Harry Surden - Artificial Intelligence and Law Overview
Harry Surden - Artificial Intelligence and Law OverviewHarry Surden - Artificial Intelligence and Law Overview
Harry Surden - Artificial Intelligence and Law Overview
 
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
 

Similaire à Linked list

Operations on linked list
Operations on linked listOperations on linked list
Operations on linked listSumathi Kv
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationProf Ansari
 
Data structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LISTData structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LISTbinakasehun2026
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptshashankbhadouria4
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDev Chauhan
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked ListReazul Islam
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 
CH5_Linked List.ppt
CH5_Linked List.pptCH5_Linked List.ppt
CH5_Linked List.pptArifKamal36
 
CH5_Linked List.pptx
CH5_Linked List.pptxCH5_Linked List.pptx
CH5_Linked List.pptxSaralaT3
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysMaulen Bale
 

Similaire à Linked list (20)

Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
03-Lists.ppt
03-Lists.ppt03-Lists.ppt
03-Lists.ppt
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
3.ppt
3.ppt3.ppt
3.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Data structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LISTData structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LIST
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Ch17
Ch17Ch17
Ch17
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
CH5_Linked List.ppt
CH5_Linked List.pptCH5_Linked List.ppt
CH5_Linked List.ppt
 
CH5_Linked List.pptx
CH5_Linked List.pptxCH5_Linked List.pptx
CH5_Linked List.pptx
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 

Plus de Trupti Agrawal

Plus de Trupti Agrawal (7)

Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Arrays
ArraysArrays
Arrays
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 

Dernier

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 

Dernier (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 

Linked list

  • 1. LINKED LIST aND ITS TYPES Trupti Agrawal 1
  • 2. LINKED LIST In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence. Fig.: A linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of the list. Trupti Agrawal 2
  • 3. Singly Linked Lists Linked lists are drawn as an order sequence of nodes with links represented as arrows (Figure 4.1). The name of the pointer to the first node in the list is the name of the list. (the list of Figure 4.1 is called ptr.)  Notice that we do not explicitly put in the values of pointers, but simply draw allows to indicate that they are there. Trupti Agrawal 3
  • 4. Singly Linked Lists [cont…] The nodes do not resident in sequential locations The locations of the nodes may change on different runs Link Field Data Field Node . . . Null ptr chain Trupti Agrawal 4
  • 5. Singly Linked Lists [cont…] Why it is easier to make arbitrary insertions and deletions using a linked list? To insert the word mat between cat can sat, we must:  Get a node that is currently unused; let its address be paddr.  Set the data field of this node to mat.  Set paddr’s link field to point to the address found in the link field of the node containing cat.  Set the link field of the node containing cat to point to paddr. Trupti Agrawal 5
  • 6. Singly Linked Lists [cont…] Delete mat from the list: We only need to find the element that immediately precedes mat, which is cat, and set its link field to point to mat’s link (Figure 4.3). We have not moved any data, and although the link field of mat still points to sat, mat is no longer in the list. Trupti Agrawal 6
  • 7. Singly Linked Lists [cont…] We need the following capabilities to make linked representations possible: Defining a node’s structure, that is, the fields it contains. We use self-referential structures, discussed in Section 2.2 to do this. Create new nodes when we need them. (malloc) Remove nodes that we no longer need. (free) Trupti Agrawal 7
  • 8. Singly Linked Lists [cont…] 2.2.4 Self-Referential Structures One or more of its components is a pointer to itself. typedef struct list { char data; list *link; } Construct a list with three nodes item1.link=&item2; item2.link=&item3; malloc: obtain a node (memory) free: release memory list item1, item2, item3; item1.data=‘a’; item2.data=‘b’; item3.data=‘c’; item1.link=item2.link=item3.link=NULL; a b c Trupti Agrawal 8
  • 9. Singly Linked Lists [cont…] Example [List of words ending in at]: Declaration typedef struct list_node *list_pointer; struct list_node { char data [4]; list_pointer link; }; Creation list_pointer ptr =NULL; Testing #define IS_EMPTY(ptr) (!(ptr)) Allocation ptr=(list_pointer) malloc (sizeof(list_node)); Return the spaces: free(ptr); Trupti Agrawal 9
  • 10. Singly Linked Lists [cont…] e -> name  (*e).name strcpy(ptr -> data, “bat”); ptr -> link = NULL; address of first node ptr data ptr link  b a t 0 NULL ptr Figure 4.4:Referencing the fields of a node(p.142) Trupti Agrawal 10
  • 11. Singly Linked Lists [cont…] Example 4.2 [Two-node linked list]: typedef struct list_node *list_pointer; typedef struct list_node { int data; list_pointer link; }; list_pointer ptr =NULL; Program 4.2: Create a two-node list list_pointer create2( ) { /* create a linked list with two nodes */ list_pointer first, second; first = (list_pointer) malloc(sizeof(list_node)); second = (list_pointer) malloc(sizeof(list_node)); second -> link = NULL; second -> data = 20; ptr first -> data = 10; first ->link = second; return first; 10 20 } #define IS_FULL(ptr) (! (ptr)) When returns NULL if there is no more memory.  NULL Trupti Agrawal 11
  • 12. Singly Linked Lists [cont…] • Insertion Observation  insert a new node with data = 50 into the list ptr after node node 10 20 50 NULL temp ptr Trupti Agrawal 12
  • 13. Singly Linked Lists [cont…] Implement Insertion: void insert(list_pointer *ptr, List_pointer node) { /* insert a new node with data = 50 into the list ptr after node */ list_pointer temp; temp=(list_pointer)malloc(sizeof(list_node)); if(IS_FULL(temp)){ fprintf(stderr, “The memory is fulln”); exit(1); } temp->data=50; temp 50 Trupti Agrawal 13
  • 14. Singly Linked Lists [cont…] if(*ptr){ //nonempty list temp->link = node->link; node->link = temp; } else{ //empty list temp->link = NULL; *ptr = temp; } } node 10 20 50 NULL temp ptr Trupti Agrawal 14
  • 15. Singly Linked Lists [cont…] Deletion Observation: delete node from the list ptr trial node 10 50 20 NULL ptr trial node 10 50 20 NULL ptr 10 20 NULL Trupti Agrawal 15
  • 16. Singly Linked Lists [cont…] Implement Deletion: void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { /* delete node from the list, trail is the preceding node ptr is the head of the list */ if(trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); } ptr trial node 10 50 20 NULL Trupti Agrawal 16
  • 17. Singly Linked Lists [cont…] Print out a list (traverse a list) Program 4.5: Printing a list void print_list(list_pointer ptr) { printf(“The list contains: “); for ( ; ptr; ptr = ptr->link) printf(“%4d”, ptr->data); printf(“n”); } Trupti Agrawal 17
  • 18. Dynamically Linked Stacks and Queues When several stacks and queues coexisted, there was no efficient way to represent them sequentially. Notice that direction of links for both stack and the queue facilitate easy insertion and deletion of nodes.  Easily add or delete a node form the top of the stack.  Easily add a node to the rear of the queue and add or delete a node at the front of a queue. Trupti Agrawal 18
  • 19. Dynamically Linked Stacks and Queues [cont…] Represent n stacks top item link link ... NULL Stack Trupti Agrawal 19
  • 20. Dynamically Linked Stacks and Queues [cont…] Push in the linked stack void add(stack_pointer *top, element item){ /* add an element to the top of the stack */ Push stack_pointer temp = (stack_pointer) malloc (sizeof (stack)); if (IS_FULL(temp)) { fprintf(stderr, “ The memory is fulln”); exit(1); }t emp->item = item; temp->link = *top; *top= temp; } temp item link top link link ... NULL Trupti Agrawal 20
  • 21. Dynamically Linked Stacks and Queues [cont…] • Pop from the linked stack element delete(stack_pointer *top) { /* delete an element from the stack */ Pop stack_pointer temp = *top; element item; if (IS_EMPTY(temp)) { fprintf(stderr, “The stack is emptyn”); exit(1); }i tem = temp->item; *top = temp->link; free(temp); return item; } item link link link ... NULL temp top Trupti Agrawal 21
  • 22. Dynamically Linked Stacks and Queues [cont…] Represent n queues front item link link ... NULL Queue rear Delete from Add to Trupti Agrawal 22
  • 23. Dynamically Linked Stacks and Queues [cont…] enqueue in the linked queue front link ... NULL temp item link rear NULL Trupti Agrawal 23
  • 24. Dynamically Linked Stacks and Queues [cont…] dequeue from the linked queue (similar to push) front item link link link ... NULL temp rear Trupti Agrawal 24
  • 25. Dynamically Linked Stacks and Queues [cont…] The solution presented above to the n-stack, m-queue problem is both computationally and conceptually simple. We no longer need to shift stacks or queues to make space. Computation can proceed as long as there is memory available. Trupti Agrawal 25
  • 26. Polynomials A polynomial is an expression that contains more than two terms. A term is made up of coefficient and exponent. An example of polynomial is P(x) = 4x3+6x2+7x+9 Trupti Agrawal 26
  • 27. Polynomials [cont…] A polynomial thus may be represented using arrays or linked lists. Array representation assumes that the exponents of the given expression are arranged from 0 to the highest value (degree), which is represented by the subscript of the array beginning with 0. The coefficients of the respective exponent are placed at an appropriate index in the array. The array representation for the above polynomial expression is given below: Trupti Agrawal 27
  • 28. Polynomials [cont…] A polynomial may also be represented using a linked list. A structure may be defined such that it contains two parts- one is the coefficient and second is the corresponding exponent. The structure definition may be given as shown below: ccooeeff eexxppoonn lliinnkk  struct polynomial {i nt coefficient; int exponent; struct polynomial *next; }; Trupti Agrawal 28
  • 29. Polynomials [cont…] Thus the above polynomial may be represented using linked list as shown below: Trupti Agrawal 29
  • 30. Polynomials [cont…] Addition of two Polynomials: o For adding two polynomials using arrays is straightforward method, since both the arrays may be added up element wise beginning from 0 to n-1, resulting in addition of two polynomials. o Addition of two polynomials using linked list requires comparing the exponents, and wherever the exponents are found to be same, the coefficients are added up. For terms with different exponents, the complete term is simply added to the result thereby making it a part of addition result. Multiplication of two Polynomials: o Multiplication of two polynomials however requires manipulation of each node such that the exponents are added up and the coefficients are multiplied. o After each term of first polynomial is operated upon with each term of the second polynomial, then the result has to be added up by comparing the exponents and adding the coefficients for similar exponents and including terms as such with dissimilar exponents in the result. Trupti Agrawal 30
  • 31. Polynomials [cont…] Attach a node to the end of a list void attach(float coefficient, int exponent, poly_pointer *ptr){ /* create a new node with coef = coefficient and expon = exponent, attach it to the node pointed to by ptr. Ptr is updated to point to this new node */ poly_pointer temp; temp = (poly_pointer) malloc(sizeof(poly_node)); /* create new node */ if (IS_FULL(temp)) { fprintf(stderr, “The memory is fulln”); exit(1); } temp->coef = coefficient; /* copy item to the new node */ temp->expon = exponent; (*ptr)->link = temp; /* attach */ *ptr = temp; /* move ptr to the end of the list */ } Trupti Agrawal 31
  • 32. Polynomials [cont…]  Analysis of PADD (Polynomial addition) f f e e m A x = a x m- + ××× + a x + B x = b x n- + + b x ( )( 1 0 ) ( )( 1 0 ) - -  n 1 0 1 0 1. coefficient additions 0 £ additions £ min(m, n) where m (n) denotes the number of terms in A (B). 2. exponent comparisons extreme case: em-1 > fm-1 > em-2 > fm-2 > … > e1 > f1 > e0 > f0 m+n-1 comparisons 3. creation of new nodes extreme case: maximum number of terms in d is m+n m + n new nodes summary: O(m+n) Trupti Agrawal 32
  • 33. Polynomials [cont…] A Suite for Polynomials e(x) = a(x) * b(x) + d(x) poly_pointer a, b, d, e; ... a = read_poly(); b = read_poly(); d = read_poly(); temp = pmult(a, b); e = padd(temp, d); print_poly(e); read_poly() print_poly() padd() psub() pmult() temp is used to hold a partial result. By returning the nodes of temp, we may use it to hold other polynomials Trupti Agrawal 33
  • 34. Polynomials [cont…] Erase Polynomials erase frees the nodes in temp void erase (poly_pointer *ptr){ /* erase the polynomial pointed to by ptr */ poly_pointer temp; while ( *ptr){ temp = *ptr; *ptr = (*ptr) -> link; free(temp); } } Trupti Agrawal 34
  • 35. Circularly Linked Lists Circular Linked list The link field of the last node points to the first node in the list. Example Represent a polynomial ptr = 3x14+2x8+1 as a circularly linked list. ptr 33 1144 22 88 11 00 Trupti Agrawal 35
  • 36. Circularly Linked Lists [cont…] Maintain an Available List We free nodes that are no longer in use so that we may reuse these nodes later We can obtain an efficient erase algorithm for circular lists, by maintaining our own list (as a chain) of nodes that have been “freed”. Instead of using malloc and free, we now use get_node (program 4.13) and ret_node (program 4.14). avail ... NULL List of freed nodes Trupti Agrawal 36
  • 37. Circularly Linked Lists [cont…] Maintain an Available List (cont’d) When we need a new node, we examine this list. If the list is not empty, then we may use one of its nodes. Only when the list is empty we do need to use malloc to create a new node. Trupti Agrawal 37
  • 38. Circularly Linked Lists [cont…] Maintain an Available List (cont’d) Insert ptr to the front of this list Let avail be a variable of type poly_pointer that points to the first node in our list of freed nodes. Henceforth, we call this list the available space list or avail list. Initially, we set avail to NULL Trupti Agrawal 38
  • 39. Circularly Linked Lists [cont…] Maintain an Available List Erase a circular list in a fixed amount (constant) of time O(1) independent of the number of nodes in the list using cerase       temp ptr avail      N U LL Trupti Agrawal 39
  • 40. Circularly Linked Lists [cont…] We must handle the zero polynomial as a special case. To avoid it, we introduce a head node into each polynomial each polynomial, zero or nonzero, contains one additional node. The expon and coef fields of this node are irrelevant. Why ? So ! Trupti Agrawal 40
  • 41. Circularly Linked Lists [cont…] For fit the circular list with head node representation We may remove the test for (*ptr) from cerase Changes the original padd to cpadd /* head node */ /*a->expon=-1, so b->expont > -1 */ /* link to the first node */ Trupti Agrawal 41
  • 42. Circularly Linked Lists [cont…] Operations for circularly linked lists Question: What happens when we want to insert a new node at the front of the circular linked list ptr? ptr x1 x2 x3 Answer:  move down the entire length of ptr. Possible Solution: x1 x2 x3 ptr Trupti Agrawal 42
  • 43. Circularly Linked Lists [cont…]  Insert a new node at the front of a circular list  To insert node at the rear, we only need to add the additional statement *ptr = node to the else clause of insert_front x1 x2 x3 ptr node Trupti Agrawal 43
  • 44. Circularly Linked Lists [cont…] Finding the length of a circular list Trupti Agrawal 44
  • 45. Doubly Linked Lists [cont…] Singly linked lists pose problems because we can move easily only in the direction of the links Doubly linked list has at least three fields left link field(llink), data field(item), right link field(rlink). The necessary declarations: typedef struct node *node_pointer; typedef struct node{ node_pointer llink; element item; node_pointer rlink; }; ... NULL ? ptr Trupti Agrawal 45
  • 46. Doubly Linked Lists [cont…] Sample doubly linked circular with head node: (Figure 4.23) empty double linked circular list with head node (Figure 4.24) suppose that ptr points to any node in a doubly linked list, then:  ptr = ptr -> llink -> rlink = ptr -> rlink -> llink Trupti Agrawal 46
  • 47. Doubly Linked Lists [cont…] Insert node Head node llink item rlink New node node Trupti Agrawal 47
  • 48. Doubly Linked Lists [cont…] Delete node Head node llink item rlink deleted node Trupti Agrawal 48
  • 49. THANK YOU….. !!! Trupti Agrawal 49