SlideShare a Scribd company logo
Linked Lists
EENG 212
ALGORITHMS
And
DATA STRUCTURES
Linked Lists
 A linked list is a linear collection of data
elements, called nodes, where the linear
order is given by means of pointers.
 Each node is divided into two parts:
 The first part contains the information of the
element and
 The second part contains the address of the next
node (link /next pointer field) in the list.
Linked Lists
info next
list
info next info next
Linear linked list
null
Adding an Element to the front
of a Linked List
5
info next
list
info next info next
3 8 null
Some Notations for use in
algorithm (Not in C programs)
 p: is a pointer
 node(p): the node pointed to by p
 info(p): the information portion of the node
 next(p): the next address portion of the node
 getnode(): obtains an empty node
 freenode(p): makes node(p) available for
reuse even if the value of the pointer p is
changed.
Adding an Element to the front
of a Linked List
5
info next
list
info next info next
3 8
info next
p p = getnode()
null
Adding an Element to the front
of a Linked List
5
info next
list
info next info next
3 8
info next
p 6 info(p) = 6
null
Adding an Element to the front
of a Linked List
5
info next info next info next
3 8
info next
p 6
list
next(p) = list
null
Adding an Element to the front
of a Linked List
5
info next info next info next
3 8
info next
6
p
list list = p
null
Adding an Element to the front
of a Linked List
5
info next info next info next
3 8
info next
list 6 null
Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
list 6 null
Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
6
list
p
p = list
null
Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
6
list
p list = next(p)
null
Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
6
list
p x = info(p)
x = 6
null
Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
p
x = 6
freenode(p)
list null
Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
list
x = 6 null
Linked List Implementation of
Stacks – PUSH(S,X)
 The first node of the list is the top of the
stack. If an external pointer s points to such a
linked list, the operation push(s,x) may be
implemented by
p=getnode();
info(p)=x;
next(p)=s;
s=p;
Linked List Implementation of
Stacks – POP(S)
 The operation x=pop(s) removes the first node from a nonempty
list and signals underflow if the list is empty:
if (empty(s)){ /* checks whether s equals null */
printf(‘stack underflow’);
exit(1);
}
else {
p =s;
s=next(p);
x = info(p);
freenode(p);
}
Linked List Implemantation of
QUEUES
5
info next info next info next
3 8
info next
6
front null
rear
5
info next info next info next
3 8
info next
6
front
info next
null
rear
12
Linked List Implemantation of
QUEUES
 A queue q consists of a list and two pointers, q.front and q.rear. The operations
empty(q) and x=remove(q) are completely analogous to empty(s) and x=pop(s),
with the pointer q.front replacing s.
if(empty(q)){
printf(“queue undeflow”);
exit(1);
}
p=q.front;
x=info(p);
q.front=next(p);
if(q.front==null)
q.rear=null;
freenode(p);
return(x);
Linked List Implemantation of
QUEUES
 The operation insert(q,x) is implemented by
p= getnode();
info(p)=x;
next(p)=null;
if(q.front==null)
q.front=p;
else
next(q.rear)=p;
q.rear=p;
Linked List as a Data Structure
 An item is accesses in a linked list by
traversing the list from its beginning.
 An array implementation allows acccess to
the nth item in a group using single operation,
whereas a list implementation requires n
operations.
 The advantage of a list over an array occurs
when it is necessary to insert or delete an
element in the middle of a group of other
elements.
Element x is inserted between the
third an fourth elements in an array
X0
X1
X2
X3
X4
X5
X6
X0
X1
X2
X3
X4
X5
X6
X0
X1
X2
X3
X4
X5
X6
x
Inserting an item x into a list
after a node pointed to by p
X0 X1 X2 X3 X4 X5 X6 null
list
X0 X1 X2 X3 X4 X5 X6 null
list
p
p
x
q
Inserting an item x into a list
after a node pointed to by p
q=getnode();
info(q)=x;
next(q)=next(p);
next(p)=q;
Deleting an item x from a list
after a node pointed to by p
X0 X1 X2 X3 X4 X5 X6 null
list
p q
X0 X1 X2 X4 X5 X6 null
list
p
x =X3
X3
Deleting an item x from a list
after a node pointed to by p
q=next(p);
x=info(q);
next(p)=next(q);
freenode(q);
LINKED LISTS USING
DYNAMIC VARIABLES
 In array implementation of the linked lists a fixed set of nodes
represented by an array is established at the beginning of the execution
 A pointer to a node is represented by the relative position of the node
within the array.
 In array implementation, it is not possible to determine the number of
nodes required for the linked list. Therefore;
 Less number of nodes can be allocated which means that the program will
have overflow problem.
 More number of nodes can be allocated which means that some amount of
the memory storage will be wasted.
 The solution to this problem is to allow nodes that are dynamic, rather
than static.
 When a node is required storage is reserved/allocated for it and when a
node is no longerneeded, the memory storage is released/freed.
ALLOCATING AND FREEING
DYNAMIC VARIABLES
 C library function malloc() is used for dynamically
allocating a space to a pointer. Note that the
malloc() is a library function in <stdlib.h> header file.
 The following lines allocate an integer space from
the memory pointed by the pointer p.
int *p;
p = (int *) malloc(sizeof(int));
 Note that sizeof() is another library function that returns the
number of bytes required for the operand. In this example,
4 bytes for the int.
ALLOCATING AND FREEING
DYNAMIC VARIABLES
 Allocate floating point number space for a
float pointer f.
float *f;
f = (float *) malloc(sizeof(float));
Question:What is the output of
the following lines?
int *p, *q;
int x;
p = (int *) malloc(sizeof(int));
*p = 3;
x = 6;
q = (int *) malloc(sizeof(int));
*q=x;
printf(“%d %d n”, *p, *q);
 The above lines will print 3 and 6.
p
p 3
6
x
q
q 6
malloc() and free()
The following lines and the proceeding figure shows the
effectiveness of the free() function.
int *p, *q;
p = (int *) malloc(sizeof(int));
*p = 5;
q = (int *) malloc(sizeof(int));
*q = 8;
free(p);
p = q;
q = (int *) malloc(sizeof(int));
*q = 6;
printf(“%d %d n”, *p, *q);
LINKED LISTS STRUCTURES
AND BASIC FUNCTIONS
 The value zero can be used in a C program as the null pointer. You
can use the following line to declare the NULL constant. Note that a
NULL pointer is considered NOT to point any storage location.
#define NULL 0
 The following node structure can be used to implement Linked Lists.
Note that the info field, which can be some other data type (not
necessarily int), keeps the data of the node and the pointer next links
the node to the next node in the Linked List.
struct node{
int info;
struct node *next;
};
typedef struct node *NODEPTR;
LINKED LISTS STRUCTURES
AND BASIC FUNCTIONS
 When a new node is required (e.g. to be inserted
into the list) the following function, getnode, can be
used to make a new node to be available for the list.
NODEPTR getnode(void)
{
NODEPTR p;
p = (NODEPTR) malloc(sizeof(struct node));
return p;
}
LINKED LISTS STRUCTURES
AND BASIC FUNCTIONS
When a new node is no longer used (e.g. to
be deleted from the list) the following
function, freenode, can be used to release
the node back to the memory.
void freenode(NODEPTR p)
{
free(p);
}
PRIMITIVE FUNCTIONS FOR
LINEAR LINKED LISTS
 The following functions insertafter(p,x) and
delafter(p,px) are primitive functions that can
be used for the dynamic implementation of a
linked list. Assume that list is a pointer
variable pointing the first node of a list (if any)
and equals NULL in the case of an empty list.
void insertafter(NODEPTR p, int x)
{
NODEPTR q;
if(p == NULL){
printf("void insertionn");
exit(1);
}
q=getnode();
q->info = x;
q->next = p->next;
p->next = q;
}
void delafter(NODEPTR p , int *px)
{
NODEPTR q;
if((p == NULL) || (p->next == NULL)){
printf("void deletionn");
exit(1);
}
q = p->next;
*px = q->info;
p->next = q->next;
freenode(q);
}
Searching through the linked
list.
 The following function searches through the
linked list and returns a pointer the first
occurrence of the search key or returns NULL
pointer if the search key is not in the list. Note
that the linked list contains integer data items.
NODEPTR searchList(NODEPTR plist, int key)
{
NODEPTR p;
p = plist;
while(p != NULL){
if(p->info == key)
return p;
p = p->next;
}
return NULL;
}
Displaying the linked list
elements
 Write a function to display the student with highest
CGPA in a linked list containing student data. Use
the following node structure for your linked list.
struct node{
int stNo;
float CGPA;
struct node *next;
};
typedef struct node *NODEPTR;
void DisplayMax(NODEPTR plist)
{
NODEPTR p;
float maxCGPA=-1.0;
int maxstNo;
p = plist; /*current node*/
if(p == NULL){
printf(“no node/data is available in the listn”);
return;
}
do{
if(p->CGPA > maxCGPA){
maxCGPA = p->CGPA;
maxstNo = p->stNo;
}
p = p->next;
} while(p!= NULL);
printf(“The student number with max CGPA: %dn”, maxstNo);
printf(“The student’s CGPA: %dn”, maxCGPA);
}

More Related Content

Similar to 2.ppt

Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
KasthuriKAssistantPr
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
Hanif Durad
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
PoonamPatil120
 
Linked lists
Linked listsLinked lists
Linked lists
Himadri Sen Gupta
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queuestech4us
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
Anaya Zafar
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
Rana junaid Rasheed
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Getachew Ganfur
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
Samsil Arefin
 
Write a JAVA LinkedListRec class that has the following methods siz.pdf
Write a JAVA LinkedListRec class that has the following methods siz.pdfWrite a JAVA LinkedListRec class that has the following methods siz.pdf
Write a JAVA LinkedListRec class that has the following methods siz.pdf
info785431
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
JAGDEEPKUMAR23
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
Kirill Kozlov
 
Circular_Linked_List.ppt
Circular_Linked_List.pptCircular_Linked_List.ppt
Circular_Linked_List.ppt
SLekshmiNair
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
Poojith Chowdhary
 

Similar to 2.ppt (20)

Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Linked lists
Linked listsLinked lists
Linked lists
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
List
ListList
List
 
Ds notes
Ds notesDs notes
Ds notes
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
 
Write a JAVA LinkedListRec class that has the following methods siz.pdf
Write a JAVA LinkedListRec class that has the following methods siz.pdfWrite a JAVA LinkedListRec class that has the following methods siz.pdf
Write a JAVA LinkedListRec class that has the following methods siz.pdf
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Circular_Linked_List.ppt
Circular_Linked_List.pptCircular_Linked_List.ppt
Circular_Linked_List.ppt
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
 

More from ArifKamal36

03-arrays-pointers.ppt
03-arrays-pointers.ppt03-arrays-pointers.ppt
03-arrays-pointers.ppt
ArifKamal36
 
html-lists-tables.ppt
html-lists-tables.ppthtml-lists-tables.ppt
html-lists-tables.ppt
ArifKamal36
 
HTMLTables.ppt
HTMLTables.pptHTMLTables.ppt
HTMLTables.ppt
ArifKamal36
 
Basic-HTML.9526794.powerpoint.pptx
Basic-HTML.9526794.powerpoint.pptxBasic-HTML.9526794.powerpoint.pptx
Basic-HTML.9526794.powerpoint.pptx
ArifKamal36
 
Lecture 1. Data Structure & Algorithm.pptx
Lecture 1. Data Structure & Algorithm.pptxLecture 1. Data Structure & Algorithm.pptx
Lecture 1. Data Structure & Algorithm.pptx
ArifKamal36
 
DS1.pptx
DS1.pptxDS1.pptx
DS1.pptx
ArifKamal36
 
topic11LinkedLists.ppt
topic11LinkedLists.ppttopic11LinkedLists.ppt
topic11LinkedLists.ppt
ArifKamal36
 
3.ppt
3.ppt3.ppt
1.ppt
1.ppt1.ppt
Games.ppt
Games.pptGames.ppt
Games.ppt
ArifKamal36
 
IAT334-Lec01-Intro.pptx
IAT334-Lec01-Intro.pptxIAT334-Lec01-Intro.pptx
IAT334-Lec01-Intro.pptx
ArifKamal36
 
9916167.ppt
9916167.ppt9916167.ppt
9916167.ppt
ArifKamal36
 
e3-chap-01.ppt
e3-chap-01.ppte3-chap-01.ppt
e3-chap-01.ppt
ArifKamal36
 

More from ArifKamal36 (14)

03-arrays-pointers.ppt
03-arrays-pointers.ppt03-arrays-pointers.ppt
03-arrays-pointers.ppt
 
html-lists-tables.ppt
html-lists-tables.ppthtml-lists-tables.ppt
html-lists-tables.ppt
 
HTMLTables.ppt
HTMLTables.pptHTMLTables.ppt
HTMLTables.ppt
 
Basic-HTML.9526794.powerpoint.pptx
Basic-HTML.9526794.powerpoint.pptxBasic-HTML.9526794.powerpoint.pptx
Basic-HTML.9526794.powerpoint.pptx
 
Lecture 1. Data Structure & Algorithm.pptx
Lecture 1. Data Structure & Algorithm.pptxLecture 1. Data Structure & Algorithm.pptx
Lecture 1. Data Structure & Algorithm.pptx
 
DS1.pptx
DS1.pptxDS1.pptx
DS1.pptx
 
topic11LinkedLists.ppt
topic11LinkedLists.ppttopic11LinkedLists.ppt
topic11LinkedLists.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
1.ppt
1.ppt1.ppt
1.ppt
 
Games.ppt
Games.pptGames.ppt
Games.ppt
 
IAT334-Lec01-Intro.pptx
IAT334-Lec01-Intro.pptxIAT334-Lec01-Intro.pptx
IAT334-Lec01-Intro.pptx
 
9916167.ppt
9916167.ppt9916167.ppt
9916167.ppt
 
e3-chap-01.ppt
e3-chap-01.ppte3-chap-01.ppt
e3-chap-01.ppt
 

Recently uploaded

UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 

Recently uploaded (20)

UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 

2.ppt

  • 2. Linked Lists  A linked list is a linear collection of data elements, called nodes, where the linear order is given by means of pointers.  Each node is divided into two parts:  The first part contains the information of the element and  The second part contains the address of the next node (link /next pointer field) in the list.
  • 3. Linked Lists info next list info next info next Linear linked list null
  • 4. Adding an Element to the front of a Linked List 5 info next list info next info next 3 8 null
  • 5. Some Notations for use in algorithm (Not in C programs)  p: is a pointer  node(p): the node pointed to by p  info(p): the information portion of the node  next(p): the next address portion of the node  getnode(): obtains an empty node  freenode(p): makes node(p) available for reuse even if the value of the pointer p is changed.
  • 6. Adding an Element to the front of a Linked List 5 info next list info next info next 3 8 info next p p = getnode() null
  • 7. Adding an Element to the front of a Linked List 5 info next list info next info next 3 8 info next p 6 info(p) = 6 null
  • 8. Adding an Element to the front of a Linked List 5 info next info next info next 3 8 info next p 6 list next(p) = list null
  • 9. Adding an Element to the front of a Linked List 5 info next info next info next 3 8 info next 6 p list list = p null
  • 10. Adding an Element to the front of a Linked List 5 info next info next info next 3 8 info next list 6 null
  • 11. Removing an Element from the front of a Linked List 5 info next info next info next 3 8 info next list 6 null
  • 12. Removing an Element from the front of a Linked List 5 info next info next info next 3 8 info next 6 list p p = list null
  • 13. Removing an Element from the front of a Linked List 5 info next info next info next 3 8 info next 6 list p list = next(p) null
  • 14. Removing an Element from the front of a Linked List 5 info next info next info next 3 8 info next 6 list p x = info(p) x = 6 null
  • 15. Removing an Element from the front of a Linked List 5 info next info next info next 3 8 info next p x = 6 freenode(p) list null
  • 16. Removing an Element from the front of a Linked List 5 info next info next info next 3 8 list x = 6 null
  • 17. Linked List Implementation of Stacks – PUSH(S,X)  The first node of the list is the top of the stack. If an external pointer s points to such a linked list, the operation push(s,x) may be implemented by p=getnode(); info(p)=x; next(p)=s; s=p;
  • 18. Linked List Implementation of Stacks – POP(S)  The operation x=pop(s) removes the first node from a nonempty list and signals underflow if the list is empty: if (empty(s)){ /* checks whether s equals null */ printf(‘stack underflow’); exit(1); } else { p =s; s=next(p); x = info(p); freenode(p); }
  • 19. Linked List Implemantation of QUEUES 5 info next info next info next 3 8 info next 6 front null rear 5 info next info next info next 3 8 info next 6 front info next null rear 12
  • 20. Linked List Implemantation of QUEUES  A queue q consists of a list and two pointers, q.front and q.rear. The operations empty(q) and x=remove(q) are completely analogous to empty(s) and x=pop(s), with the pointer q.front replacing s. if(empty(q)){ printf(“queue undeflow”); exit(1); } p=q.front; x=info(p); q.front=next(p); if(q.front==null) q.rear=null; freenode(p); return(x);
  • 21. Linked List Implemantation of QUEUES  The operation insert(q,x) is implemented by p= getnode(); info(p)=x; next(p)=null; if(q.front==null) q.front=p; else next(q.rear)=p; q.rear=p;
  • 22. Linked List as a Data Structure  An item is accesses in a linked list by traversing the list from its beginning.  An array implementation allows acccess to the nth item in a group using single operation, whereas a list implementation requires n operations.  The advantage of a list over an array occurs when it is necessary to insert or delete an element in the middle of a group of other elements.
  • 23. Element x is inserted between the third an fourth elements in an array X0 X1 X2 X3 X4 X5 X6 X0 X1 X2 X3 X4 X5 X6 X0 X1 X2 X3 X4 X5 X6 x
  • 24. Inserting an item x into a list after a node pointed to by p X0 X1 X2 X3 X4 X5 X6 null list X0 X1 X2 X3 X4 X5 X6 null list p p x q
  • 25. Inserting an item x into a list after a node pointed to by p q=getnode(); info(q)=x; next(q)=next(p); next(p)=q;
  • 26. Deleting an item x from a list after a node pointed to by p X0 X1 X2 X3 X4 X5 X6 null list p q X0 X1 X2 X4 X5 X6 null list p x =X3 X3
  • 27. Deleting an item x from a list after a node pointed to by p q=next(p); x=info(q); next(p)=next(q); freenode(q);
  • 28. LINKED LISTS USING DYNAMIC VARIABLES  In array implementation of the linked lists a fixed set of nodes represented by an array is established at the beginning of the execution  A pointer to a node is represented by the relative position of the node within the array.  In array implementation, it is not possible to determine the number of nodes required for the linked list. Therefore;  Less number of nodes can be allocated which means that the program will have overflow problem.  More number of nodes can be allocated which means that some amount of the memory storage will be wasted.  The solution to this problem is to allow nodes that are dynamic, rather than static.  When a node is required storage is reserved/allocated for it and when a node is no longerneeded, the memory storage is released/freed.
  • 29. ALLOCATING AND FREEING DYNAMIC VARIABLES  C library function malloc() is used for dynamically allocating a space to a pointer. Note that the malloc() is a library function in <stdlib.h> header file.  The following lines allocate an integer space from the memory pointed by the pointer p. int *p; p = (int *) malloc(sizeof(int));  Note that sizeof() is another library function that returns the number of bytes required for the operand. In this example, 4 bytes for the int.
  • 30. ALLOCATING AND FREEING DYNAMIC VARIABLES  Allocate floating point number space for a float pointer f. float *f; f = (float *) malloc(sizeof(float));
  • 31. Question:What is the output of the following lines? int *p, *q; int x; p = (int *) malloc(sizeof(int)); *p = 3; x = 6; q = (int *) malloc(sizeof(int)); *q=x; printf(“%d %d n”, *p, *q);  The above lines will print 3 and 6. p p 3 6 x q q 6
  • 32. malloc() and free() The following lines and the proceeding figure shows the effectiveness of the free() function. int *p, *q; p = (int *) malloc(sizeof(int)); *p = 5; q = (int *) malloc(sizeof(int)); *q = 8; free(p); p = q; q = (int *) malloc(sizeof(int)); *q = 6; printf(“%d %d n”, *p, *q);
  • 33. LINKED LISTS STRUCTURES AND BASIC FUNCTIONS  The value zero can be used in a C program as the null pointer. You can use the following line to declare the NULL constant. Note that a NULL pointer is considered NOT to point any storage location. #define NULL 0  The following node structure can be used to implement Linked Lists. Note that the info field, which can be some other data type (not necessarily int), keeps the data of the node and the pointer next links the node to the next node in the Linked List. struct node{ int info; struct node *next; }; typedef struct node *NODEPTR;
  • 34. LINKED LISTS STRUCTURES AND BASIC FUNCTIONS  When a new node is required (e.g. to be inserted into the list) the following function, getnode, can be used to make a new node to be available for the list. NODEPTR getnode(void) { NODEPTR p; p = (NODEPTR) malloc(sizeof(struct node)); return p; }
  • 35. LINKED LISTS STRUCTURES AND BASIC FUNCTIONS When a new node is no longer used (e.g. to be deleted from the list) the following function, freenode, can be used to release the node back to the memory. void freenode(NODEPTR p) { free(p); }
  • 36. PRIMITIVE FUNCTIONS FOR LINEAR LINKED LISTS  The following functions insertafter(p,x) and delafter(p,px) are primitive functions that can be used for the dynamic implementation of a linked list. Assume that list is a pointer variable pointing the first node of a list (if any) and equals NULL in the case of an empty list.
  • 37. void insertafter(NODEPTR p, int x) { NODEPTR q; if(p == NULL){ printf("void insertionn"); exit(1); } q=getnode(); q->info = x; q->next = p->next; p->next = q; }
  • 38. void delafter(NODEPTR p , int *px) { NODEPTR q; if((p == NULL) || (p->next == NULL)){ printf("void deletionn"); exit(1); } q = p->next; *px = q->info; p->next = q->next; freenode(q); }
  • 39. Searching through the linked list.  The following function searches through the linked list and returns a pointer the first occurrence of the search key or returns NULL pointer if the search key is not in the list. Note that the linked list contains integer data items.
  • 40. NODEPTR searchList(NODEPTR plist, int key) { NODEPTR p; p = plist; while(p != NULL){ if(p->info == key) return p; p = p->next; } return NULL; }
  • 41. Displaying the linked list elements  Write a function to display the student with highest CGPA in a linked list containing student data. Use the following node structure for your linked list. struct node{ int stNo; float CGPA; struct node *next; }; typedef struct node *NODEPTR;
  • 42. void DisplayMax(NODEPTR plist) { NODEPTR p; float maxCGPA=-1.0; int maxstNo; p = plist; /*current node*/ if(p == NULL){ printf(“no node/data is available in the listn”); return; } do{ if(p->CGPA > maxCGPA){ maxCGPA = p->CGPA; maxstNo = p->stNo; } p = p->next; } while(p!= NULL); printf(“The student number with max CGPA: %dn”, maxstNo); printf(“The student’s CGPA: %dn”, maxCGPA); }