SlideShare a Scribd company logo
Data StructuresData Structures
List
Data StructuresData Structures
List
Topics to be Covered
Abstract Data Types (ADTs)
List ADT
• Array based implemetation
• Linked implementation
• Singly linked
• Doubly linked
• Circular linked
• Applications
Polynomial Manipulation
Abstract Data Types (ADTs)
List ADT
• Array based implemetation
• Linked implementation
• Singly linked
• Doubly linked
• Circular linked
• Applications
Polynomial Manipulation
Topics to be Covered
Abstract Data Types (ADTs)
In the C programming language, data types are declarations fo
memory locations or variables that determine the characteristics o
the data that may be stored and the methods (operations) o
processing that are permitted involving them.
ADT is a mathematical model for data type, which is defined from
point of view of the user of a data.
• Possible values
• Possible operations
Data structures is concrete representation of the data in the point o
view of the implementer.
In the C programming language, data types are declarations fo
memory locations or variables that determine the characteristics o
the data that may be stored and the methods (operations) o
processing that are permitted involving them.
ADT is a mathematical model for data type, which is defined from
point of view of the user of a data.
• Possible values
• Possible operations
Data structures is concrete representation of the data in the point o
view of the implementer.
Abstract Data Types (ADTs)
In the C programming language, data types are declarations fo
memory locations or variables that determine the characteristics o
the data that may be stored and the methods (operations) o
processing that are permitted involving them.
ADT is a mathematical model for data type, which is defined from
point of view of the user of a data.
• Possible values
• Possible operations
Data structures is concrete representation of the data in the point o
view of the implementer.
In the C programming language, data types are declarations fo
memory locations or variables that determine the characteristics o
the data that may be stored and the methods (operations) o
processing that are permitted involving them.
ADT is a mathematical model for data type, which is defined from
point of view of the user of a data.
• Possible values
• Possible operations
Data structures is concrete representation of the data in the point o
view of the implementer.
ADT
There is an interface between the ADT and the program(s) that use it.
The lower-level implementation details of the data structure are
hidden from view of the rest of the program.
The implementation details can be changed without altering the ADT
interface.
Abstract Data Type (ADT)
Mathematical description of an object and
the set of operations on the object
Data Types
integer, array,
pointers, …
Abstract Data Type (ADT)
Mathematical description of an object and
the set of operations on the object
Algorithms
binary search, quicksort,
…
ADT
There is an interface between the ADT and the program(s) that use it.
The lower-level implementation details of the data structure are
hidden from view of the rest of the program.
The implementation details can be changed without altering the ADT
interface.
Abstract Data Type (ADT)
Mathematical description of an object and
the set of operations on the object
Abstract Data Type (ADT)
Mathematical description of an object and
the set of operations on the object
Algorithms
binary search, quicksort,
…
The Core Operations
Every ADT should provide a way to:
• add an item
• remove an item
• find, retrieve, or access an item
Many, many more possibilities
• is it empty
• make it empty
• give a sub set of it
• and on and on and on…
Many different ways to implement these items each with associated
costs and benefits
Every ADT should provide a way to:
• add an item
• remove an item
• find, retrieve, or access an item
Many, many more possibilities
• is it empty
• make it empty
• give a sub set of it
• and on and on and on…
Many different ways to implement these items each with associated
costs and benefits
The Core Operations
Every ADT should provide a way to:
• add an item
• remove an item
• find, retrieve, or access an item
Many, many more possibilities
• is it empty
• make it empty
• give a sub set of it
• and on and on and on…
Many different ways to implement these items each with associated
costs and benefits
Every ADT should provide a way to:
• add an item
• remove an item
• find, retrieve, or access an item
Many, many more possibilities
• is it empty
• make it empty
• give a sub set of it
• and on and on and on…
Many different ways to implement these items each with associated
costs and benefits
Implementing ADTs
When implementing an ADT the operations and behaviors are
lready specified
• Interface
mplementer’s first choice is what to use as the internal storage
ontainer for the concrete data type
• the internal storage container is used to hold the items in the collection
• often an implementation of an ADT initially slim pickings for choice of
storage containers
When implementing an ADT the operations and behaviors are
lready specified
• Interface
mplementer’s first choice is what to use as the internal storage
ontainer for the concrete data type
• the internal storage container is used to hold the items in the collection
• often an implementation of an ADT initially slim pickings for choice of
storage containers
When implementing an ADT the operations and behaviors are
lready specified
• Interface
mplementer’s first choice is what to use as the internal storage
ontainer for the concrete data type
• the internal storage container is used to hold the items in the collection
• often an implementation of an ADT initially slim pickings for choice of
storage containers
When implementing an ADT the operations and behaviors are
lready specified
• Interface
mplementer’s first choice is what to use as the internal storage
ontainer for the concrete data type
• the internal storage container is used to hold the items in the collection
• often an implementation of an ADT initially slim pickings for choice of
storage containers
List ADT
A List is a dynamic ordered set of homogeneous elements.
• is a linear sequence of a number of items (need not be in specific order)
• items are of the same data type and can occur more than once
The list has one first element (the head) and one last element. Excep
for the first and the last, each element has one unique predecesso
and one unique successor.
We will consider a general list of the form A1, A2, A3, …, AN .
• Size is N
• If Size is 0 – Empty List
• Position of Ai in the list is i
A List is a dynamic ordered set of homogeneous elements.
• is a linear sequence of a number of items (need not be in specific order)
• items are of the same data type and can occur more than once
The list has one first element (the head) and one last element. Excep
for the first and the last, each element has one unique predecesso
and one unique successor.
We will consider a general list of the form A1, A2, A3, …, AN .
• Size is N
• If Size is 0 – Empty List
• Position of Ai in the list is i
List ADT
A List is a dynamic ordered set of homogeneous elements.
• is a linear sequence of a number of items (need not be in specific order)
• items are of the same data type and can occur more than once
The list has one first element (the head) and one last element. Excep
for the first and the last, each element has one unique predecesso
and one unique successor.
We will consider a general list of the form A1, A2, A3, …, AN .
• Size is N
• If Size is 0 – Empty List
• Position of Ai in the list is i
A List is a dynamic ordered set of homogeneous elements.
• is a linear sequence of a number of items (need not be in specific order)
• items are of the same data type and can occur more than once
The list has one first element (the head) and one last element. Excep
for the first and the last, each element has one unique predecesso
and one unique successor.
We will consider a general list of the form A1, A2, A3, …, AN .
• Size is N
• If Size is 0 – Empty List
• Position of Ai in the list is i
Generic Operations on a list
Create an empty list
Print all elements of a list
Construct a copy of the list
Find the position of an element in a list
Remove an element from the list
Insert an element into particular position in the list
Check if the list is empty
Remove all the elements from the list
Retrieve an element from a specific position in the list
Create an empty list
Print all elements of a list
Construct a copy of the list
Find the position of an element in a list
Remove an element from the list
Insert an element into particular position in the list
Check if the list is empty
Remove all the elements from the list
Retrieve an element from a specific position in the list
Generic Operations on a list
Create an empty list
Print all elements of a list
Construct a copy of the list
Find the position of an element in a list
Remove an element from the list
Insert an element into particular position in the list
Check if the list is empty
Remove all the elements from the list
Retrieve an element from a specific position in the list
Create an empty list
Print all elements of a list
Construct a copy of the list
Find the position of an element in a list
Remove an element from the list
Insert an element into particular position in the list
Check if the list is empty
Remove all the elements from the list
Retrieve an element from a specific position in the list
Operations on List
Create( ) – Create a List
Insert(X, p) – Insert the element X at the position p.
Delete(X) – Delete the element X
Find(X) – Return the position of X
Next(X) – Returns the element/position that succeeds X.
Next(p) – Returns the element in p+1
Previous(p) – Returns the element in p-1
Previous(X) – Returns the element/position that precedes X.
PrintList – Display the contents of the List.
MakeEmpty/DeleteAll-Makes the list empty.
IsEmpty- Returns true if the list does not have any element
IsFull- Returns true if the list cannot hold another element (full)
Create( ) – Create a List
Insert(X, p) – Insert the element X at the position p.
Delete(X) – Delete the element X
Find(X) – Return the position of X
Next(X) – Returns the element/position that succeeds X.
Next(p) – Returns the element in p+1
Previous(p) – Returns the element in p-1
Previous(X) – Returns the element/position that precedes X.
PrintList – Display the contents of the List.
MakeEmpty/DeleteAll-Makes the list empty.
IsEmpty- Returns true if the list does not have any element
IsFull- Returns true if the list cannot hold another element (full)
Operations on List
Create( ) – Create a List
Insert(X, p) – Insert the element X at the position p.
Delete(X) – Delete the element X
Find(X) – Return the position of X
Next(X) – Returns the element/position that succeeds X.
Next(p) – Returns the element in p+1
Previous(p) – Returns the element in p-1
Previous(X) – Returns the element/position that precedes X.
PrintList – Display the contents of the List.
MakeEmpty/DeleteAll-Makes the list empty.
IsEmpty- Returns true if the list does not have any element
IsFull- Returns true if the list cannot hold another element (full)
Create( ) – Create a List
Insert(X, p) – Insert the element X at the position p.
Delete(X) – Delete the element X
Find(X) – Return the position of X
Next(X) – Returns the element/position that succeeds X.
Next(p) – Returns the element in p+1
Previous(p) – Returns the element in p-1
Previous(X) – Returns the element/position that precedes X.
PrintList – Display the contents of the List.
MakeEmpty/DeleteAll-Makes the list empty.
IsEmpty- Returns true if the list does not have any element
IsFull- Returns true if the list cannot hold another element (full)
Array Implementation of List
An Array is used for storing all the list elements.
Index or subscript is used as address or position of element in the array
Even if the array is dynamically allocated, an estimate of the maximum size
of the list is required when the execution of the program starts.
In many a situations over-estimate would be done, which waste
considerable space. This could be a serious limitation, especially if there
are many lists of unknown size.
An Array is used for storing all the list elements.
Index or subscript is used as address or position of element in the array
Even if the array is dynamically allocated, an estimate of the maximum size
of the list is required when the execution of the program starts.
In many a situations over-estimate would be done, which waste
considerable space. This could be a serious limitation, especially if there
are many lists of unknown size.
Array Implementation of List
An Array is used for storing all the list elements.
Index or subscript is used as address or position of element in the array
Even if the array is dynamically allocated, an estimate of the maximum size
of the list is required when the execution of the program starts.
In many a situations over-estimate would be done, which waste
considerable space. This could be a serious limitation, especially if there
are many lists of unknown size.
An Array is used for storing all the list elements.
Index or subscript is used as address or position of element in the array
Even if the array is dynamically allocated, an estimate of the maximum size
of the list is required when the execution of the program starts.
In many a situations over-estimate would be done, which waste
considerable space. This could be a serious limitation, especially if there
are many lists of unknown size.
Array Implementation of List
de<stdio.h>
t, i, size=0, pos, ele, n; //list[100];
eate (); void insert(int,int);
nd(); void delete(); void display();
choice;
"n1. Create List 2. Insert Position 3. Display
4. Delete Position 5. Find 6. Exitn");
"Enter your choice :");
"%d",&choice);
choice != 6)
ch(choice) {
: create();break;
case 2: printf("Enter the Position :");
scanf("%d",&pos);
printf("Enter the new element to be inserted :");
scanf("%d",&ele);
insert(ele,pos);break;
case 3: display();break;
case 4: delete();break; case 5: find();break; case 6:
break;
default : printf("nInvalid Choice"); }
printf("n1. Create List 2. Insert Position 3. Display L
4. Delete Position 5. Find 6. Exitn");
printf("nEnter your choice :");
scanf("%d",&choice); } }
de<stdio.h>
t, i, size=0, pos, ele, n; //list[100];
eate (); void insert(int,int);
nd(); void delete(); void display();
choice;
"n1. Create List 2. Insert Position 3. Display
4. Delete Position 5. Find 6. Exitn");
"Enter your choice :");
"%d",&choice);
choice != 6)
ch(choice) {
: create();break;
case 2: printf("Enter the Position :");
scanf("%d",&pos);
printf("Enter the new element to be inserted :");
scanf("%d",&ele);
insert(ele,pos);break;
case 3: display();break;
case 4: delete();break; case 5: find();break; case 6:
break;
default : printf("nInvalid Choice"); }
printf("n1. Create List 2. Insert Position 3. Display L
4. Delete Position 5. Find 6. Exitn");
printf("nEnter your choice :");
scanf("%d",&choice); } }
Array Implementation of List
case 2: printf("Enter the Position :");
scanf("%d",&pos);
printf("Enter the new element to be inserted :");
scanf("%d",&ele);
insert(ele,pos);break;
case 3: display();break;
case 4: delete();break; case 5: find();break; case 6:
break;
default : printf("nInvalid Choice"); }
printf("n1. Create List 2. Insert Position 3. Display L
4. Delete Position 5. Find 6. Exitn");
printf("nEnter your choice :");
scanf("%d",&choice); } }
case 2: printf("Enter the Position :");
scanf("%d",&pos);
printf("Enter the new element to be inserted :");
scanf("%d",&ele);
insert(ele,pos);break;
case 3: display();break;
case 4: delete();break; case 5: find();break; case 6:
break;
default : printf("nInvalid Choice"); }
printf("n1. Create List 2. Insert Position 3. Display L
4. Delete Position 5. Find 6. Exitn");
printf("nEnter your choice :");
scanf("%d",&choice); } }
Array Implementation of List
oid create(void)
printf("Enter Size of List:");
scanf("%d",&size); //10
st = (int*)malloc(sizeof(int)*size);
printf("Enter No of elements to be
inserted :");
canf("%d",&n); //4
printf("Enter the elements of list :");
or(i=0;i<n;i++)
canf("%d",list+i); //45,65,23,44
oid create(void)
printf("Enter Size of List:");
scanf("%d",&size); //10
st = (int*)malloc(sizeof(int)*size);
printf("Enter No of elements to be
inserted :");
canf("%d",&n); //4
printf("Enter the elements of list :");
or(i=0;i<n;i++)
canf("%d",list+i); //45,65,23,44
45
45
45 65
45 65
Array Implementation of List
65 23 44
65
65 23
Array Implementation of List
void insert(int X,int p) //16,2
n++;
or(i=n-1;i>=p;i--)
ist[i]=list[i-1];
ist[p-1]=X;
45
void insert(int X,int p) //16,2
n++;
or(i=n-1;i>=p;i--)
ist[i]=list[i-1];
ist[p-1]=X;
Array Implementation of List
45 65 23 44
45 65 23 44 44
45 65 23 23 44
45 65 65 23 44
45 16 65 23 44
Array Implementation of List
find(void)
f("Enter the element to be searched :");
f("%d",&ele); //23
=0;i<n;i++)
[i]==ele)
f("Element %d is found in position %d",ele,i+1);
n;
f("Element is not in the list");
find(void)
f("Enter the element to be searched :");
f("%d",&ele); //23
=0;i<n;i++)
[i]==ele)
f("Element %d is found in position %d",ele,i+1);
n;
f("Element is not in the list");
Array Implementation of List
45 16 65 23 44
Array Implementation of List
oid delete(void)
rintf("Enter the Position :"); //2
canf("%d",&pos);
or(i=pos-1;i<=n;i++) list[i]=list[i+1];
--;
oid display(size)
or(i=0;i<size;i++)
rintf("%d ",list[i]);
n=5
n=5
n=5
n=4
oid delete(void)
rintf("Enter the Position :"); //2
canf("%d",&pos);
or(i=pos-1;i<=n;i++) list[i]=list[i+1];
--;
oid display(size)
or(i=0;i<size;i++)
rintf("%d ",list[i]);
n=5
n=5
n=5
n=4
Array Implementation of List
n=5
n=5
n=5
n=4
45 16 65 23 44
45 65 65 23 44
n=5
n=5
n=5
n=4
45 65 23 23 44
45 65 23 44 44
Linked Implementation of List
In order to overcome the problem of allocating oversized memory for
the list a pointer implementation of the list can be done.
Pointer implementation of the list is referred to as Linked List as
every element of the list is stored in a node that can hold the data
and a pointer to the successor node in the list.
The last node of the list will be pointing to nothing i.e. it will be made
as NULL
In order to overcome the problem of allocating oversized memory for
the list a pointer implementation of the list can be done.
Pointer implementation of the list is referred to as Linked List as
every element of the list is stored in a node that can hold the data
and a pointer to the successor node in the list.
The last node of the list will be pointing to nothing i.e. it will be made
as NULL
Linked Implementation of List
In order to overcome the problem of allocating oversized memory for
the list a pointer implementation of the list can be done.
Pointer implementation of the list is referred to as Linked List as
every element of the list is stored in a node that can hold the data
and a pointer to the successor node in the list.
The last node of the list will be pointing to nothing i.e. it will be made
as NULL
In order to overcome the problem of allocating oversized memory for
the list a pointer implementation of the list can be done.
Pointer implementation of the list is referred to as Linked List as
every element of the list is stored in a node that can hold the data
and a pointer to the successor node in the list.
The last node of the list will be pointing to nothing i.e. it will be made
as NULL
Types of Linked List
Linear Linked List
• Singly Linked
• Doubly Linked
Circular Linked List
• Singly Linked
• Doubly Linked
Linear Linked List
• Singly Linked
• Doubly Linked
Circular Linked List
• Singly Linked
• Doubly Linked
Types of Linked List
Implementation of Linked List
arations
def struct node *node_ptr;
t node
ent_type element;
_ptr next;
def node_ptr LIST;
def node_ptr position;
LIST createlist()
{
position tmpcell;
tmpcell =(struct node *)malloc(sizeof(struct
Node));
If(tmpcell == NULL)
printf(" Error : Out of Space");
tmpcell->Next = NULL;
printf("Link List Created Successfully...");
return(tmpcell);
}
arations
def struct node *node_ptr;
t node
ent_type element;
_ptr next;
def node_ptr LIST;
def node_ptr position;
LIST createlist()
{
position tmpcell;
tmpcell =(struct node *)malloc(sizeof(struct
Node));
If(tmpcell == NULL)
printf(" Error : Out of Space");
tmpcell->Next = NULL;
printf("Link List Created Successfully...");
return(tmpcell);
}
Implementation of Linked List
LIST createlist()
{
position tmpcell;
tmpcell =(struct node *)malloc(sizeof(struct
Node));
If(tmpcell == NULL)
printf(" Error : Out of Space");
tmpcell->Next = NULL;
printf("Link List Created Successfully...");
return(tmpcell);
}
LIST createlist()
{
position tmpcell;
tmpcell =(struct node *)malloc(sizeof(struct
Node));
If(tmpcell == NULL)
printf(" Error : Out of Space");
tmpcell->Next = NULL;
printf("Link List Created Successfully...");
return(tmpcell);
}
Insert (29,35,76 at beginning)
d insert( element_type x, LIST L, position p )
osition tmp_cell;
mp_cell=(position)malloc(sizeof(struct node));
( tmp_cell == NULL )
fatal_error("Out of space!!!");
se
mp_cell->element = x; tmp_cell->next = p->next;
->next = tmp_cell;
d insert( element_type x, LIST L, position p )
osition tmp_cell;
mp_cell=(position)malloc(sizeof(struct node));
( tmp_cell == NULL )
fatal_error("Out of space!!!");
se
mp_cell->element = x; tmp_cell->next = p->next;
->next = tmp_cell;
Insert (29,35,76 at beginning)
d insert( element_type x, LIST L, position p )
osition tmp_cell;
mp_cell=(position)malloc(sizeof(struct node));
( tmp_cell == NULL )
fatal_error("Out of space!!!");
se
mp_cell->element = x; tmp_cell->next = p->next;
->next = tmp_cell;
d insert( element_type x, LIST L, position p )
osition tmp_cell;
mp_cell=(position)malloc(sizeof(struct node));
( tmp_cell == NULL )
fatal_error("Out of space!!!");
se
mp_cell->element = x; tmp_cell->next = p->next;
->next = tmp_cell;
Find (73)
position find ( element_type x, LIST L )
position p;
p = L->next;
while( (p != NULL) && (p->element != x) )
p = p->next;
eturn p;
position find ( element_type x, LIST L )
position p;
p = L->next;
while( (p != NULL) && (p->element != x) )
p = p->next;
eturn p;
position find ( element_type x, LIST L )
position p;
p = L->next;
while( (p != NULL) && (p->element != x) )
p = p->next;
eturn p;
position find ( element_type x, LIST L )
position p;
p = L->next;
while( (p != NULL) && (p->element != x) )
p = p->next;
eturn p;
Find previous(56)
tion find_previous( element_type x, LIST L )
tion p;
;
e((p->next != NULL)&&(p->next->element != x))
p->next;
rn p;
tion find_previous( element_type x, LIST L )
tion p;
;
e((p->next != NULL)&&(p->next->element != x))
p->next;
rn p;
tion find_previous( element_type x, LIST L )
tion p;
;
e((p->next != NULL)&&(p->next->element != x))
p->next;
rn p;
tion find_previous( element_type x, LIST L )
tion p;
;
e((p->next != NULL)&&(p->next->element != x))
p->next;
rn p;
Delete(35)
oid delete( element_type x, LIST L )
position p, tmp_cell;
p = find_previous( x, L );
f( p->next != NULL
mp_cell = p->next;
p->next = tmp_cell->next;
ree( tmp_cell );
oid delete( element_type x, LIST L )
position p, tmp_cell;
p = find_previous( x, L );
f( p->next != NULL
mp_cell = p->next;
p->next = tmp_cell->next;
ree( tmp_cell );
Delete List
oid delete_list( LIST L )
osition p,tmp;
= L->next;
->next = NULL;
while( p != NULL )
mp=p->next;
ree( p );
p = tmp;
oid delete_list( LIST L )
osition p,tmp;
= L->next;
->next = NULL;
while( p != NULL )
mp=p->next;
ree( p );
p = tmp;
Other routines
nt is_empty( LIST L )
eturn( L->next == NULL );
nt is_last( position p, LIST L )
eturn( p->next == NULL );
true
false
If p is 800 it would return a false
If p is 712 it would return a true
nt is_empty( LIST L )
eturn( L->next == NULL );
nt is_last( position p, LIST L )
eturn( p->next == NULL );
true
false
If p is 800 it would return a false
If p is 712 it would return a true
Other routines
true
false
If p is 800 it would return a false
If p is 712 it would return a true
true
false
If p is 800 it would return a false
If p is 712 it would return a true
Implementation of Linked List
ct node;
edef int element_type;
def struct node *node_ptr;
t node
ent_type element;
_ptr next;
def node_ptr LIST;
def node_ptr position;
struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node
{ ElementType Element;
Position Next;
Position Prev; };
typedef PtrToNode List;
typedef PtrToNode Position;
ct node;
edef int element_type;
def struct node *node_ptr;
t node
ent_type element;
_ptr next;
def node_ptr LIST;
def node_ptr position;
struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node
{ ElementType Element;
Position Next;
Position Prev; };
typedef PtrToNode List;
typedef PtrToNode Position;
Implementation of Linked List
struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node
{ ElementType Element;
Position Next;
Position Prev; };
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node
{ ElementType Element;
Position Next;
Position Prev; };
typedef PtrToNode List;
typedef PtrToNode Position;
Implementation of Linked List
List CreateList()
{
Position TmpCell;
TmpCell=(List) malloc (sizeof(struct
Node));
if(TmpCell == NULL)
printf(" Error : Out of Space");
TmpCell->Next = NULL;
TmpCell->Prev = NULL;
return(TmpCell);
}
eatelist()
n tmpcell;
l =(struct node *)malloc(sizeof(struct
e));
cell == NULL)
" Error : Out of Space");
ell->Next = NULL;
"Link List Created Successfully...");
tmpcell);
List CreateList()
{
Position TmpCell;
TmpCell=(List) malloc (sizeof(struct
Node));
if(TmpCell == NULL)
printf(" Error : Out of Space");
TmpCell->Next = NULL;
TmpCell->Prev = NULL;
return(TmpCell);
}
eatelist()
n tmpcell;
l =(struct node *)malloc(sizeof(struct
e));
cell == NULL)
" Error : Out of Space");
ell->Next = NULL;
"Link List Created Successfully...");
tmpcell);
Implementation of Linked List
List CreateList()
{
Position TmpCell;
TmpCell=(List) malloc (sizeof(struct
Node));
if(TmpCell == NULL)
printf(" Error : Out of Space");
TmpCell->Next = NULL;
TmpCell->Prev = NULL;
return(TmpCell);
}
List CreateList()
{
Position TmpCell;
TmpCell=(List) malloc (sizeof(struct
Node));
if(TmpCell == NULL)
printf(" Error : Out of Space");
TmpCell->Next = NULL;
TmpCell->Prev = NULL;
return(TmpCell);
}
FIND and DELETE
on Find(ElementType X, List L)
on P; P= L->Next;
P!= NULL && P->Element != X)
->Next;
n(P);
void Delete(ElementType X, List L)
{
Position P, T;
P=Find(X,L);
T = P->Prev;
T->Next = P->Next;
if( P->Next != NULL )
P->Next->Prev = T;
free(P);
}
on Find(ElementType X, List L)
on P; P= L->Next;
P!= NULL && P->Element != X)
->Next;
n(P);
void Delete(ElementType X, List L)
{
Position P, T;
P=Find(X,L);
T = P->Prev;
T->Next = P->Next;
if( P->Next != NULL )
P->Next->Prev = T;
free(P);
}
FIND and DELETE
void Delete(ElementType X, List L)
{
Position P, T;
P=Find(X,L);
T = P->Prev;
T->Next = P->Next;
if( P->Next != NULL )
P->Next->Prev = T;
free(P);
}
void Delete(ElementType X, List L)
{
Position P, T;
P=Find(X,L);
T = P->Prev;
T->Next = P->Next;
if( P->Next != NULL )
P->Next->Prev = T;
free(P);
}
nsert(ElementType X,List L,Position P)
on TmpCell;
ell =(struct Node *)malloc(sizeof(struct Node));
pCell == NULL)
(" Error : Out of Space");
ell->Element = X;
ell->Next = P->Next ;
ext = TmpCell;
ell->Prev = P;
pCell->Next != NULL )
ell->Next->Prev = TmpCell;
nsert(ElementType X,List L,Position P)
on TmpCell;
ell =(struct Node *)malloc(sizeof(struct Node));
pCell == NULL)
(" Error : Out of Space");
ell->Element = X;
ell->Next = P->Next ;
ext = TmpCell;
ell->Prev = P;
pCell->Next != NULL )
ell->Next->Prev = TmpCell;
INSERT
DISPLAY
void PrintList(List L)
{
Position P; P=L->Next;
printf("nn List Forward :");
while(P->Next != NULL) {
printf("n %d",P->Element); P=P->Next; }
printf("n %d",P->Element);
printf("nn List backward :");
while(P != L) {
printf("n %d",P->Element); P=P->Prev; }
}
void PrintList(List L)
{
Position P; P=L->Next;
printf("nn List Forward :");
while(P->Next != NULL) {
printf("n %d",P->Element); P=P->Next; }
printf("n %d",P->Element);
printf("nn List backward :");
while(P != L) {
printf("n %d",P->Element); P=P->Prev; }
}
DISPLAY
void PrintList(List L)
{
Position P; P=L->Next;
printf("nn List Forward :");
while(P->Next != NULL) {
printf("n %d",P->Element); P=P->Next; }
printf("n %d",P->Element);
printf("nn List backward :");
while(P != L) {
printf("n %d",P->Element); P=P->Prev; }
}
void PrintList(List L)
{
Position P; P=L->Next;
printf("nn List Forward :");
while(P->Next != NULL) {
printf("n %d",P->Element); P=P->Next; }
printf("n %d",P->Element);
printf("nn List backward :");
while(P != L) {
printf("n %d",P->Element); P=P->Prev; }
}
Implementation of Linked List
ct node;
edef int element_type;
def struct node *node_ptr;
t node
ent_type element;
_ptr next;
def node_ptr LIST;
def node_ptr position;
struct node;
typedef int element_type;
typedef struct node *node_ptr;
struct node
{
element_type element;
node_ptr next;
};
typedef node_ptr LIST;
typedef node_ptr position;
ct node;
edef int element_type;
def struct node *node_ptr;
t node
ent_type element;
_ptr next;
def node_ptr LIST;
def node_ptr position;
struct node;
typedef int element_type;
typedef struct node *node_ptr;
struct node
{
element_type element;
node_ptr next;
};
typedef node_ptr LIST;
typedef node_ptr position;
Implementation of Linked List
struct node;
typedef int element_type;
typedef struct node *node_ptr;
struct node
{
element_type element;
node_ptr next;
};
typedef node_ptr LIST;
typedef node_ptr position;
struct node;
typedef int element_type;
typedef struct node *node_ptr;
struct node
{
element_type element;
node_ptr next;
};
typedef node_ptr LIST;
typedef node_ptr position;
Implementation of Linked List
LIST createlist()
{
position tmpcell;
tmpcell =(struct node *)malloc(sizeof(str
Node));
If(tmpcell == NULL)
printf(" Error : Out of Space");
tmpcell->Next = tmpcell;
printf("Link List Created Successfully...");
return(tmpcell);
}
eatelist()
n tmpcell;
l =(struct node *)malloc(sizeof(struct
e));
cell == NULL)
" Error : Out of Space");
ell->Next = NULL;
"Link List Created Successfully...");
tmpcell);
LIST createlist()
{
position tmpcell;
tmpcell =(struct node *)malloc(sizeof(str
Node));
If(tmpcell == NULL)
printf(" Error : Out of Space");
tmpcell->Next = tmpcell;
printf("Link List Created Successfully...");
return(tmpcell);
}
eatelist()
n tmpcell;
l =(struct node *)malloc(sizeof(struct
e));
cell == NULL)
" Error : Out of Space");
ell->Next = NULL;
"Link List Created Successfully...");
tmpcell);
Implementation of Linked List
LIST createlist()
{
position tmpcell;
tmpcell =(struct node *)malloc(sizeof(str
Node));
If(tmpcell == NULL)
printf(" Error : Out of Space");
tmpcell->Next = tmpcell;
printf("Link List Created Successfully...");
return(tmpcell);
}
LIST createlist()
{
position tmpcell;
tmpcell =(struct node *)malloc(sizeof(str
Node));
If(tmpcell == NULL)
printf(" Error : Out of Space");
tmpcell->Next = tmpcell;
printf("Link List Created Successfully...");
return(tmpcell);
}
Circular List (CREATE)
eate()
uct node*)malloc(sizeof(struct node));
n Enter the data:");
%d", &x->data);
= x;
x;
n If you wish to continue press 1 otherwise 0:");
%d", &c);
c != 0)
struct node*)malloc(sizeof(struct node));
n Enter the data:");
%d", &y->data);
= y;
= head;
n If you wish to continue press 1 otherwise 0:");
%d", &c);
eate()
uct node*)malloc(sizeof(struct node));
n Enter the data:");
%d", &x->data);
= x;
x;
n If you wish to continue press 1 otherwise 0:");
%d", &c);
c != 0)
struct node*)malloc(sizeof(struct node));
n Enter the data:");
%d", &y->data);
= y;
= head;
n If you wish to continue press 1 otherwise 0:");
%d", &c);
Circular List (CREATE)
eate()
uct node*)malloc(sizeof(struct node));
n Enter the data:");
%d", &x->data);
= x;
x;
n If you wish to continue press 1 otherwise 0:");
%d", &c);
c != 0)
struct node*)malloc(sizeof(struct node));
n Enter the data:");
%d", &y->data);
= y;
= head;
n If you wish to continue press 1 otherwise 0:");
%d", &c);
eate()
uct node*)malloc(sizeof(struct node));
n Enter the data:");
%d", &x->data);
= x;
x;
n If you wish to continue press 1 otherwise 0:");
%d", &c);
c != 0)
struct node*)malloc(sizeof(struct node));
n Enter the data:");
%d", &y->data);
= y;
= head;
n If you wish to continue press 1 otherwise 0:");
%d", &c);
INSERT
oid ins_at_beg()
= head;
= (struct node*)malloc(sizeof(struct node));
rintf("n Enter the data:");
canf("%d", &y->data);
while (x->link != head)
= x->link;
->link = y;
->link = head;
ead = y; }
oid ins_at_beg()
= head;
= (struct node*)malloc(sizeof(struct node));
rintf("n Enter the data:");
canf("%d", &y->data);
while (x->link != head)
= x->link;
->link = y;
->link = head;
ead = y; }
INSERT
DELETE
del_at_beg() {
ad == NULL)
tf("n List is empty");
{
= head;
= head;
hile (x->link != head)
x = x->link;
ead = y->link; x->link = head;
free(y);
}
del_at_beg() {
ad == NULL)
tf("n List is empty");
{
= head;
= head;
hile (x->link != head)
x = x->link;
ead = y->link; x->link = head;
free(y);
}
DELETE
DISPLAY
oid traverse()
f (head == NULL)
printf("n List is empty");
lse
= head;
while (x->link != head)
printf("%d->", x->data); x = x->link;
printf("%d", x->data);
oid traverse()
f (head == NULL)
printf("n List is empty");
lse
= head;
while (x->link != head)
printf("%d->", x->data); x = x->link;
printf("%d", x->data);
DISPLAY
References
www.csee.umbc.edu/courses/undergraduate/341/fall07/.../Lists/List
s1.pdf
https://courses.cs.washington.edu/courses/cse326/00wi/.../lecture1.
ppt
www.csee.umbc.edu/courses/undergraduate/341/fall07/.../Lists/List
s1.pdf
https://courses.cs.washington.edu/courses/cse326/00wi/.../lecture1.
ppt
References
www.csee.umbc.edu/courses/undergraduate/341/fall07/.../Lists/List
s1.pdf
https://courses.cs.washington.edu/courses/cse326/00wi/.../lecture1.
ppt
www.csee.umbc.edu/courses/undergraduate/341/fall07/.../Lists/List
s1.pdf
https://courses.cs.washington.edu/courses/cse326/00wi/.../lecture1.
ppt

More Related Content

PPTX
Javascript ADT - List
PDF
DSA-Lecture-05
PDF
Lecture 07 Data Structures - Basic Sorting
PPTX
Data strucutre basic introduction
PPT
Standard Template Library
PPTX
Standard template library
PPTX
Collections Training
Javascript ADT - List
DSA-Lecture-05
Lecture 07 Data Structures - Basic Sorting
Data strucutre basic introduction
Standard Template Library
Standard template library
Collections Training

What's hot (20)

PPTX
Data handling in python
PDF
STL in C++
PPT
standard template library(STL) in C++
PPTX
How to choose best containers in STL (C++)
PDF
DSA - Lecture 04
PPTX
Object Class
PPTX
Data structure and algorithms
PPTX
Generics In and Out
PPTX
Java Tutorial Lab 9
PPT
Ap Power Point Chpt6
PPTX
Collections lecture 35 40
PPTX
Java Tutorial Lab 7
ODP
C++ STL 概觀
PPTX
Java 103 intro to java data structures
PPT
Stl Containers
PPT
Stl (standard template library)
PPTX
Csci101 lect10 algorithms_iii
PDF
LPR - Week 1
Data handling in python
STL in C++
standard template library(STL) in C++
How to choose best containers in STL (C++)
DSA - Lecture 04
Object Class
Data structure and algorithms
Generics In and Out
Java Tutorial Lab 9
Ap Power Point Chpt6
Collections lecture 35 40
Java Tutorial Lab 7
C++ STL 概觀
Java 103 intro to java data structures
Stl Containers
Stl (standard template library)
Csci101 lect10 algorithms_iii
LPR - Week 1
Ad

Similar to Data structures list (20)

PPTX
unit 1_Linked list.pptx
PPT
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
PPTX
A singly linked list is a linear data structure
DOCX
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
PDF
UNIT I LINEAR DATA STRUCTURES – LIST
PPT
Merriam-Webster's Definition merriam.ppt
PPTX
Lecture1 classes4
PDF
UNITIII LDS.pdf
PPT
Data Structures and Algorithms en anglais
PPT
data structurer and algorithm EE 2204.ppt
PPT
dsa3.ppt
PPT
Abstract data types
PPT
Data Structure And Algorithm Presentation
PPT
dsa.ppt
PPT
dsa.ppt
PPT
dsa (1).ppt
PPT
data structures algorithm-AVL tresss.ppt
PPT
dsa.ppt
PDF
M v bramhananda reddy dsa complete notes
unit 1_Linked list.pptx
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
A singly linked list is a linear data structure
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
UNIT I LINEAR DATA STRUCTURES – LIST
Merriam-Webster's Definition merriam.ppt
Lecture1 classes4
UNITIII LDS.pdf
Data Structures and Algorithms en anglais
data structurer and algorithm EE 2204.ppt
dsa3.ppt
Abstract data types
Data Structure And Algorithm Presentation
dsa.ppt
dsa.ppt
dsa (1).ppt
data structures algorithm-AVL tresss.ppt
dsa.ppt
M v bramhananda reddy dsa complete notes
Ad

Recently uploaded (20)

PPTX
Welding lecture in detail for understanding
PDF
PPT on Performance Review to get promotions
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
web development for engineering and engineering
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Welding lecture in detail for understanding
PPT on Performance Review to get promotions
CYBER-CRIMES AND SECURITY A guide to understanding
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
R24 SURVEYING LAB MANUAL for civil enggi
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Automation-in-Manufacturing-Chapter-Introduction.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
web development for engineering and engineering
UNIT 4 Total Quality Management .pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
bas. eng. economics group 4 presentation 1.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx

Data structures list

  • 1. Data StructuresData Structures List Data StructuresData Structures List
  • 2. Topics to be Covered Abstract Data Types (ADTs) List ADT • Array based implemetation • Linked implementation • Singly linked • Doubly linked • Circular linked • Applications Polynomial Manipulation Abstract Data Types (ADTs) List ADT • Array based implemetation • Linked implementation • Singly linked • Doubly linked • Circular linked • Applications Polynomial Manipulation Topics to be Covered
  • 3. Abstract Data Types (ADTs) In the C programming language, data types are declarations fo memory locations or variables that determine the characteristics o the data that may be stored and the methods (operations) o processing that are permitted involving them. ADT is a mathematical model for data type, which is defined from point of view of the user of a data. • Possible values • Possible operations Data structures is concrete representation of the data in the point o view of the implementer. In the C programming language, data types are declarations fo memory locations or variables that determine the characteristics o the data that may be stored and the methods (operations) o processing that are permitted involving them. ADT is a mathematical model for data type, which is defined from point of view of the user of a data. • Possible values • Possible operations Data structures is concrete representation of the data in the point o view of the implementer. Abstract Data Types (ADTs) In the C programming language, data types are declarations fo memory locations or variables that determine the characteristics o the data that may be stored and the methods (operations) o processing that are permitted involving them. ADT is a mathematical model for data type, which is defined from point of view of the user of a data. • Possible values • Possible operations Data structures is concrete representation of the data in the point o view of the implementer. In the C programming language, data types are declarations fo memory locations or variables that determine the characteristics o the data that may be stored and the methods (operations) o processing that are permitted involving them. ADT is a mathematical model for data type, which is defined from point of view of the user of a data. • Possible values • Possible operations Data structures is concrete representation of the data in the point o view of the implementer.
  • 4. ADT There is an interface between the ADT and the program(s) that use it. The lower-level implementation details of the data structure are hidden from view of the rest of the program. The implementation details can be changed without altering the ADT interface. Abstract Data Type (ADT) Mathematical description of an object and the set of operations on the object Data Types integer, array, pointers, … Abstract Data Type (ADT) Mathematical description of an object and the set of operations on the object Algorithms binary search, quicksort, … ADT There is an interface between the ADT and the program(s) that use it. The lower-level implementation details of the data structure are hidden from view of the rest of the program. The implementation details can be changed without altering the ADT interface. Abstract Data Type (ADT) Mathematical description of an object and the set of operations on the object Abstract Data Type (ADT) Mathematical description of an object and the set of operations on the object Algorithms binary search, quicksort, …
  • 5. The Core Operations Every ADT should provide a way to: • add an item • remove an item • find, retrieve, or access an item Many, many more possibilities • is it empty • make it empty • give a sub set of it • and on and on and on… Many different ways to implement these items each with associated costs and benefits Every ADT should provide a way to: • add an item • remove an item • find, retrieve, or access an item Many, many more possibilities • is it empty • make it empty • give a sub set of it • and on and on and on… Many different ways to implement these items each with associated costs and benefits The Core Operations Every ADT should provide a way to: • add an item • remove an item • find, retrieve, or access an item Many, many more possibilities • is it empty • make it empty • give a sub set of it • and on and on and on… Many different ways to implement these items each with associated costs and benefits Every ADT should provide a way to: • add an item • remove an item • find, retrieve, or access an item Many, many more possibilities • is it empty • make it empty • give a sub set of it • and on and on and on… Many different ways to implement these items each with associated costs and benefits
  • 6. Implementing ADTs When implementing an ADT the operations and behaviors are lready specified • Interface mplementer’s first choice is what to use as the internal storage ontainer for the concrete data type • the internal storage container is used to hold the items in the collection • often an implementation of an ADT initially slim pickings for choice of storage containers When implementing an ADT the operations and behaviors are lready specified • Interface mplementer’s first choice is what to use as the internal storage ontainer for the concrete data type • the internal storage container is used to hold the items in the collection • often an implementation of an ADT initially slim pickings for choice of storage containers When implementing an ADT the operations and behaviors are lready specified • Interface mplementer’s first choice is what to use as the internal storage ontainer for the concrete data type • the internal storage container is used to hold the items in the collection • often an implementation of an ADT initially slim pickings for choice of storage containers When implementing an ADT the operations and behaviors are lready specified • Interface mplementer’s first choice is what to use as the internal storage ontainer for the concrete data type • the internal storage container is used to hold the items in the collection • often an implementation of an ADT initially slim pickings for choice of storage containers
  • 7. List ADT A List is a dynamic ordered set of homogeneous elements. • is a linear sequence of a number of items (need not be in specific order) • items are of the same data type and can occur more than once The list has one first element (the head) and one last element. Excep for the first and the last, each element has one unique predecesso and one unique successor. We will consider a general list of the form A1, A2, A3, …, AN . • Size is N • If Size is 0 – Empty List • Position of Ai in the list is i A List is a dynamic ordered set of homogeneous elements. • is a linear sequence of a number of items (need not be in specific order) • items are of the same data type and can occur more than once The list has one first element (the head) and one last element. Excep for the first and the last, each element has one unique predecesso and one unique successor. We will consider a general list of the form A1, A2, A3, …, AN . • Size is N • If Size is 0 – Empty List • Position of Ai in the list is i List ADT A List is a dynamic ordered set of homogeneous elements. • is a linear sequence of a number of items (need not be in specific order) • items are of the same data type and can occur more than once The list has one first element (the head) and one last element. Excep for the first and the last, each element has one unique predecesso and one unique successor. We will consider a general list of the form A1, A2, A3, …, AN . • Size is N • If Size is 0 – Empty List • Position of Ai in the list is i A List is a dynamic ordered set of homogeneous elements. • is a linear sequence of a number of items (need not be in specific order) • items are of the same data type and can occur more than once The list has one first element (the head) and one last element. Excep for the first and the last, each element has one unique predecesso and one unique successor. We will consider a general list of the form A1, A2, A3, …, AN . • Size is N • If Size is 0 – Empty List • Position of Ai in the list is i
  • 8. Generic Operations on a list Create an empty list Print all elements of a list Construct a copy of the list Find the position of an element in a list Remove an element from the list Insert an element into particular position in the list Check if the list is empty Remove all the elements from the list Retrieve an element from a specific position in the list Create an empty list Print all elements of a list Construct a copy of the list Find the position of an element in a list Remove an element from the list Insert an element into particular position in the list Check if the list is empty Remove all the elements from the list Retrieve an element from a specific position in the list Generic Operations on a list Create an empty list Print all elements of a list Construct a copy of the list Find the position of an element in a list Remove an element from the list Insert an element into particular position in the list Check if the list is empty Remove all the elements from the list Retrieve an element from a specific position in the list Create an empty list Print all elements of a list Construct a copy of the list Find the position of an element in a list Remove an element from the list Insert an element into particular position in the list Check if the list is empty Remove all the elements from the list Retrieve an element from a specific position in the list
  • 9. Operations on List Create( ) – Create a List Insert(X, p) – Insert the element X at the position p. Delete(X) – Delete the element X Find(X) – Return the position of X Next(X) – Returns the element/position that succeeds X. Next(p) – Returns the element in p+1 Previous(p) – Returns the element in p-1 Previous(X) – Returns the element/position that precedes X. PrintList – Display the contents of the List. MakeEmpty/DeleteAll-Makes the list empty. IsEmpty- Returns true if the list does not have any element IsFull- Returns true if the list cannot hold another element (full) Create( ) – Create a List Insert(X, p) – Insert the element X at the position p. Delete(X) – Delete the element X Find(X) – Return the position of X Next(X) – Returns the element/position that succeeds X. Next(p) – Returns the element in p+1 Previous(p) – Returns the element in p-1 Previous(X) – Returns the element/position that precedes X. PrintList – Display the contents of the List. MakeEmpty/DeleteAll-Makes the list empty. IsEmpty- Returns true if the list does not have any element IsFull- Returns true if the list cannot hold another element (full) Operations on List Create( ) – Create a List Insert(X, p) – Insert the element X at the position p. Delete(X) – Delete the element X Find(X) – Return the position of X Next(X) – Returns the element/position that succeeds X. Next(p) – Returns the element in p+1 Previous(p) – Returns the element in p-1 Previous(X) – Returns the element/position that precedes X. PrintList – Display the contents of the List. MakeEmpty/DeleteAll-Makes the list empty. IsEmpty- Returns true if the list does not have any element IsFull- Returns true if the list cannot hold another element (full) Create( ) – Create a List Insert(X, p) – Insert the element X at the position p. Delete(X) – Delete the element X Find(X) – Return the position of X Next(X) – Returns the element/position that succeeds X. Next(p) – Returns the element in p+1 Previous(p) – Returns the element in p-1 Previous(X) – Returns the element/position that precedes X. PrintList – Display the contents of the List. MakeEmpty/DeleteAll-Makes the list empty. IsEmpty- Returns true if the list does not have any element IsFull- Returns true if the list cannot hold another element (full)
  • 10. Array Implementation of List An Array is used for storing all the list elements. Index or subscript is used as address or position of element in the array Even if the array is dynamically allocated, an estimate of the maximum size of the list is required when the execution of the program starts. In many a situations over-estimate would be done, which waste considerable space. This could be a serious limitation, especially if there are many lists of unknown size. An Array is used for storing all the list elements. Index or subscript is used as address or position of element in the array Even if the array is dynamically allocated, an estimate of the maximum size of the list is required when the execution of the program starts. In many a situations over-estimate would be done, which waste considerable space. This could be a serious limitation, especially if there are many lists of unknown size. Array Implementation of List An Array is used for storing all the list elements. Index or subscript is used as address or position of element in the array Even if the array is dynamically allocated, an estimate of the maximum size of the list is required when the execution of the program starts. In many a situations over-estimate would be done, which waste considerable space. This could be a serious limitation, especially if there are many lists of unknown size. An Array is used for storing all the list elements. Index or subscript is used as address or position of element in the array Even if the array is dynamically allocated, an estimate of the maximum size of the list is required when the execution of the program starts. In many a situations over-estimate would be done, which waste considerable space. This could be a serious limitation, especially if there are many lists of unknown size.
  • 11. Array Implementation of List de<stdio.h> t, i, size=0, pos, ele, n; //list[100]; eate (); void insert(int,int); nd(); void delete(); void display(); choice; "n1. Create List 2. Insert Position 3. Display 4. Delete Position 5. Find 6. Exitn"); "Enter your choice :"); "%d",&choice); choice != 6) ch(choice) { : create();break; case 2: printf("Enter the Position :"); scanf("%d",&pos); printf("Enter the new element to be inserted :"); scanf("%d",&ele); insert(ele,pos);break; case 3: display();break; case 4: delete();break; case 5: find();break; case 6: break; default : printf("nInvalid Choice"); } printf("n1. Create List 2. Insert Position 3. Display L 4. Delete Position 5. Find 6. Exitn"); printf("nEnter your choice :"); scanf("%d",&choice); } } de<stdio.h> t, i, size=0, pos, ele, n; //list[100]; eate (); void insert(int,int); nd(); void delete(); void display(); choice; "n1. Create List 2. Insert Position 3. Display 4. Delete Position 5. Find 6. Exitn"); "Enter your choice :"); "%d",&choice); choice != 6) ch(choice) { : create();break; case 2: printf("Enter the Position :"); scanf("%d",&pos); printf("Enter the new element to be inserted :"); scanf("%d",&ele); insert(ele,pos);break; case 3: display();break; case 4: delete();break; case 5: find();break; case 6: break; default : printf("nInvalid Choice"); } printf("n1. Create List 2. Insert Position 3. Display L 4. Delete Position 5. Find 6. Exitn"); printf("nEnter your choice :"); scanf("%d",&choice); } } Array Implementation of List case 2: printf("Enter the Position :"); scanf("%d",&pos); printf("Enter the new element to be inserted :"); scanf("%d",&ele); insert(ele,pos);break; case 3: display();break; case 4: delete();break; case 5: find();break; case 6: break; default : printf("nInvalid Choice"); } printf("n1. Create List 2. Insert Position 3. Display L 4. Delete Position 5. Find 6. Exitn"); printf("nEnter your choice :"); scanf("%d",&choice); } } case 2: printf("Enter the Position :"); scanf("%d",&pos); printf("Enter the new element to be inserted :"); scanf("%d",&ele); insert(ele,pos);break; case 3: display();break; case 4: delete();break; case 5: find();break; case 6: break; default : printf("nInvalid Choice"); } printf("n1. Create List 2. Insert Position 3. Display L 4. Delete Position 5. Find 6. Exitn"); printf("nEnter your choice :"); scanf("%d",&choice); } }
  • 12. Array Implementation of List oid create(void) printf("Enter Size of List:"); scanf("%d",&size); //10 st = (int*)malloc(sizeof(int)*size); printf("Enter No of elements to be inserted :"); canf("%d",&n); //4 printf("Enter the elements of list :"); or(i=0;i<n;i++) canf("%d",list+i); //45,65,23,44 oid create(void) printf("Enter Size of List:"); scanf("%d",&size); //10 st = (int*)malloc(sizeof(int)*size); printf("Enter No of elements to be inserted :"); canf("%d",&n); //4 printf("Enter the elements of list :"); or(i=0;i<n;i++) canf("%d",list+i); //45,65,23,44 45 45 45 65 45 65 Array Implementation of List 65 23 44 65 65 23
  • 13. Array Implementation of List void insert(int X,int p) //16,2 n++; or(i=n-1;i>=p;i--) ist[i]=list[i-1]; ist[p-1]=X; 45 void insert(int X,int p) //16,2 n++; or(i=n-1;i>=p;i--) ist[i]=list[i-1]; ist[p-1]=X; Array Implementation of List 45 65 23 44 45 65 23 44 44 45 65 23 23 44 45 65 65 23 44 45 16 65 23 44
  • 14. Array Implementation of List find(void) f("Enter the element to be searched :"); f("%d",&ele); //23 =0;i<n;i++) [i]==ele) f("Element %d is found in position %d",ele,i+1); n; f("Element is not in the list"); find(void) f("Enter the element to be searched :"); f("%d",&ele); //23 =0;i<n;i++) [i]==ele) f("Element %d is found in position %d",ele,i+1); n; f("Element is not in the list"); Array Implementation of List 45 16 65 23 44
  • 15. Array Implementation of List oid delete(void) rintf("Enter the Position :"); //2 canf("%d",&pos); or(i=pos-1;i<=n;i++) list[i]=list[i+1]; --; oid display(size) or(i=0;i<size;i++) rintf("%d ",list[i]); n=5 n=5 n=5 n=4 oid delete(void) rintf("Enter the Position :"); //2 canf("%d",&pos); or(i=pos-1;i<=n;i++) list[i]=list[i+1]; --; oid display(size) or(i=0;i<size;i++) rintf("%d ",list[i]); n=5 n=5 n=5 n=4 Array Implementation of List n=5 n=5 n=5 n=4 45 16 65 23 44 45 65 65 23 44 n=5 n=5 n=5 n=4 45 65 23 23 44 45 65 23 44 44
  • 16. Linked Implementation of List In order to overcome the problem of allocating oversized memory for the list a pointer implementation of the list can be done. Pointer implementation of the list is referred to as Linked List as every element of the list is stored in a node that can hold the data and a pointer to the successor node in the list. The last node of the list will be pointing to nothing i.e. it will be made as NULL In order to overcome the problem of allocating oversized memory for the list a pointer implementation of the list can be done. Pointer implementation of the list is referred to as Linked List as every element of the list is stored in a node that can hold the data and a pointer to the successor node in the list. The last node of the list will be pointing to nothing i.e. it will be made as NULL Linked Implementation of List In order to overcome the problem of allocating oversized memory for the list a pointer implementation of the list can be done. Pointer implementation of the list is referred to as Linked List as every element of the list is stored in a node that can hold the data and a pointer to the successor node in the list. The last node of the list will be pointing to nothing i.e. it will be made as NULL In order to overcome the problem of allocating oversized memory for the list a pointer implementation of the list can be done. Pointer implementation of the list is referred to as Linked List as every element of the list is stored in a node that can hold the data and a pointer to the successor node in the list. The last node of the list will be pointing to nothing i.e. it will be made as NULL
  • 17. Types of Linked List Linear Linked List • Singly Linked • Doubly Linked Circular Linked List • Singly Linked • Doubly Linked Linear Linked List • Singly Linked • Doubly Linked Circular Linked List • Singly Linked • Doubly Linked Types of Linked List
  • 18. Implementation of Linked List arations def struct node *node_ptr; t node ent_type element; _ptr next; def node_ptr LIST; def node_ptr position; LIST createlist() { position tmpcell; tmpcell =(struct node *)malloc(sizeof(struct Node)); If(tmpcell == NULL) printf(" Error : Out of Space"); tmpcell->Next = NULL; printf("Link List Created Successfully..."); return(tmpcell); } arations def struct node *node_ptr; t node ent_type element; _ptr next; def node_ptr LIST; def node_ptr position; LIST createlist() { position tmpcell; tmpcell =(struct node *)malloc(sizeof(struct Node)); If(tmpcell == NULL) printf(" Error : Out of Space"); tmpcell->Next = NULL; printf("Link List Created Successfully..."); return(tmpcell); } Implementation of Linked List LIST createlist() { position tmpcell; tmpcell =(struct node *)malloc(sizeof(struct Node)); If(tmpcell == NULL) printf(" Error : Out of Space"); tmpcell->Next = NULL; printf("Link List Created Successfully..."); return(tmpcell); } LIST createlist() { position tmpcell; tmpcell =(struct node *)malloc(sizeof(struct Node)); If(tmpcell == NULL) printf(" Error : Out of Space"); tmpcell->Next = NULL; printf("Link List Created Successfully..."); return(tmpcell); }
  • 19. Insert (29,35,76 at beginning) d insert( element_type x, LIST L, position p ) osition tmp_cell; mp_cell=(position)malloc(sizeof(struct node)); ( tmp_cell == NULL ) fatal_error("Out of space!!!"); se mp_cell->element = x; tmp_cell->next = p->next; ->next = tmp_cell; d insert( element_type x, LIST L, position p ) osition tmp_cell; mp_cell=(position)malloc(sizeof(struct node)); ( tmp_cell == NULL ) fatal_error("Out of space!!!"); se mp_cell->element = x; tmp_cell->next = p->next; ->next = tmp_cell; Insert (29,35,76 at beginning) d insert( element_type x, LIST L, position p ) osition tmp_cell; mp_cell=(position)malloc(sizeof(struct node)); ( tmp_cell == NULL ) fatal_error("Out of space!!!"); se mp_cell->element = x; tmp_cell->next = p->next; ->next = tmp_cell; d insert( element_type x, LIST L, position p ) osition tmp_cell; mp_cell=(position)malloc(sizeof(struct node)); ( tmp_cell == NULL ) fatal_error("Out of space!!!"); se mp_cell->element = x; tmp_cell->next = p->next; ->next = tmp_cell;
  • 20. Find (73) position find ( element_type x, LIST L ) position p; p = L->next; while( (p != NULL) && (p->element != x) ) p = p->next; eturn p; position find ( element_type x, LIST L ) position p; p = L->next; while( (p != NULL) && (p->element != x) ) p = p->next; eturn p; position find ( element_type x, LIST L ) position p; p = L->next; while( (p != NULL) && (p->element != x) ) p = p->next; eturn p; position find ( element_type x, LIST L ) position p; p = L->next; while( (p != NULL) && (p->element != x) ) p = p->next; eturn p;
  • 21. Find previous(56) tion find_previous( element_type x, LIST L ) tion p; ; e((p->next != NULL)&&(p->next->element != x)) p->next; rn p; tion find_previous( element_type x, LIST L ) tion p; ; e((p->next != NULL)&&(p->next->element != x)) p->next; rn p; tion find_previous( element_type x, LIST L ) tion p; ; e((p->next != NULL)&&(p->next->element != x)) p->next; rn p; tion find_previous( element_type x, LIST L ) tion p; ; e((p->next != NULL)&&(p->next->element != x)) p->next; rn p;
  • 22. Delete(35) oid delete( element_type x, LIST L ) position p, tmp_cell; p = find_previous( x, L ); f( p->next != NULL mp_cell = p->next; p->next = tmp_cell->next; ree( tmp_cell ); oid delete( element_type x, LIST L ) position p, tmp_cell; p = find_previous( x, L ); f( p->next != NULL mp_cell = p->next; p->next = tmp_cell->next; ree( tmp_cell );
  • 23. Delete List oid delete_list( LIST L ) osition p,tmp; = L->next; ->next = NULL; while( p != NULL ) mp=p->next; ree( p ); p = tmp; oid delete_list( LIST L ) osition p,tmp; = L->next; ->next = NULL; while( p != NULL ) mp=p->next; ree( p ); p = tmp;
  • 24. Other routines nt is_empty( LIST L ) eturn( L->next == NULL ); nt is_last( position p, LIST L ) eturn( p->next == NULL ); true false If p is 800 it would return a false If p is 712 it would return a true nt is_empty( LIST L ) eturn( L->next == NULL ); nt is_last( position p, LIST L ) eturn( p->next == NULL ); true false If p is 800 it would return a false If p is 712 it would return a true Other routines true false If p is 800 it would return a false If p is 712 it would return a true true false If p is 800 it would return a false If p is 712 it would return a true
  • 25. Implementation of Linked List ct node; edef int element_type; def struct node *node_ptr; t node ent_type element; _ptr next; def node_ptr LIST; def node_ptr position; struct Node; typedef int ElementType; typedef struct Node *PtrToNode; struct Node { ElementType Element; Position Next; Position Prev; }; typedef PtrToNode List; typedef PtrToNode Position; ct node; edef int element_type; def struct node *node_ptr; t node ent_type element; _ptr next; def node_ptr LIST; def node_ptr position; struct Node; typedef int ElementType; typedef struct Node *PtrToNode; struct Node { ElementType Element; Position Next; Position Prev; }; typedef PtrToNode List; typedef PtrToNode Position; Implementation of Linked List struct Node; typedef int ElementType; typedef struct Node *PtrToNode; struct Node { ElementType Element; Position Next; Position Prev; }; typedef PtrToNode List; typedef PtrToNode Position; struct Node; typedef int ElementType; typedef struct Node *PtrToNode; struct Node { ElementType Element; Position Next; Position Prev; }; typedef PtrToNode List; typedef PtrToNode Position;
  • 26. Implementation of Linked List List CreateList() { Position TmpCell; TmpCell=(List) malloc (sizeof(struct Node)); if(TmpCell == NULL) printf(" Error : Out of Space"); TmpCell->Next = NULL; TmpCell->Prev = NULL; return(TmpCell); } eatelist() n tmpcell; l =(struct node *)malloc(sizeof(struct e)); cell == NULL) " Error : Out of Space"); ell->Next = NULL; "Link List Created Successfully..."); tmpcell); List CreateList() { Position TmpCell; TmpCell=(List) malloc (sizeof(struct Node)); if(TmpCell == NULL) printf(" Error : Out of Space"); TmpCell->Next = NULL; TmpCell->Prev = NULL; return(TmpCell); } eatelist() n tmpcell; l =(struct node *)malloc(sizeof(struct e)); cell == NULL) " Error : Out of Space"); ell->Next = NULL; "Link List Created Successfully..."); tmpcell); Implementation of Linked List List CreateList() { Position TmpCell; TmpCell=(List) malloc (sizeof(struct Node)); if(TmpCell == NULL) printf(" Error : Out of Space"); TmpCell->Next = NULL; TmpCell->Prev = NULL; return(TmpCell); } List CreateList() { Position TmpCell; TmpCell=(List) malloc (sizeof(struct Node)); if(TmpCell == NULL) printf(" Error : Out of Space"); TmpCell->Next = NULL; TmpCell->Prev = NULL; return(TmpCell); }
  • 27. FIND and DELETE on Find(ElementType X, List L) on P; P= L->Next; P!= NULL && P->Element != X) ->Next; n(P); void Delete(ElementType X, List L) { Position P, T; P=Find(X,L); T = P->Prev; T->Next = P->Next; if( P->Next != NULL ) P->Next->Prev = T; free(P); } on Find(ElementType X, List L) on P; P= L->Next; P!= NULL && P->Element != X) ->Next; n(P); void Delete(ElementType X, List L) { Position P, T; P=Find(X,L); T = P->Prev; T->Next = P->Next; if( P->Next != NULL ) P->Next->Prev = T; free(P); } FIND and DELETE void Delete(ElementType X, List L) { Position P, T; P=Find(X,L); T = P->Prev; T->Next = P->Next; if( P->Next != NULL ) P->Next->Prev = T; free(P); } void Delete(ElementType X, List L) { Position P, T; P=Find(X,L); T = P->Prev; T->Next = P->Next; if( P->Next != NULL ) P->Next->Prev = T; free(P); }
  • 28. nsert(ElementType X,List L,Position P) on TmpCell; ell =(struct Node *)malloc(sizeof(struct Node)); pCell == NULL) (" Error : Out of Space"); ell->Element = X; ell->Next = P->Next ; ext = TmpCell; ell->Prev = P; pCell->Next != NULL ) ell->Next->Prev = TmpCell; nsert(ElementType X,List L,Position P) on TmpCell; ell =(struct Node *)malloc(sizeof(struct Node)); pCell == NULL) (" Error : Out of Space"); ell->Element = X; ell->Next = P->Next ; ext = TmpCell; ell->Prev = P; pCell->Next != NULL ) ell->Next->Prev = TmpCell; INSERT
  • 29. DISPLAY void PrintList(List L) { Position P; P=L->Next; printf("nn List Forward :"); while(P->Next != NULL) { printf("n %d",P->Element); P=P->Next; } printf("n %d",P->Element); printf("nn List backward :"); while(P != L) { printf("n %d",P->Element); P=P->Prev; } } void PrintList(List L) { Position P; P=L->Next; printf("nn List Forward :"); while(P->Next != NULL) { printf("n %d",P->Element); P=P->Next; } printf("n %d",P->Element); printf("nn List backward :"); while(P != L) { printf("n %d",P->Element); P=P->Prev; } } DISPLAY void PrintList(List L) { Position P; P=L->Next; printf("nn List Forward :"); while(P->Next != NULL) { printf("n %d",P->Element); P=P->Next; } printf("n %d",P->Element); printf("nn List backward :"); while(P != L) { printf("n %d",P->Element); P=P->Prev; } } void PrintList(List L) { Position P; P=L->Next; printf("nn List Forward :"); while(P->Next != NULL) { printf("n %d",P->Element); P=P->Next; } printf("n %d",P->Element); printf("nn List backward :"); while(P != L) { printf("n %d",P->Element); P=P->Prev; } }
  • 30. Implementation of Linked List ct node; edef int element_type; def struct node *node_ptr; t node ent_type element; _ptr next; def node_ptr LIST; def node_ptr position; struct node; typedef int element_type; typedef struct node *node_ptr; struct node { element_type element; node_ptr next; }; typedef node_ptr LIST; typedef node_ptr position; ct node; edef int element_type; def struct node *node_ptr; t node ent_type element; _ptr next; def node_ptr LIST; def node_ptr position; struct node; typedef int element_type; typedef struct node *node_ptr; struct node { element_type element; node_ptr next; }; typedef node_ptr LIST; typedef node_ptr position; Implementation of Linked List struct node; typedef int element_type; typedef struct node *node_ptr; struct node { element_type element; node_ptr next; }; typedef node_ptr LIST; typedef node_ptr position; struct node; typedef int element_type; typedef struct node *node_ptr; struct node { element_type element; node_ptr next; }; typedef node_ptr LIST; typedef node_ptr position;
  • 31. Implementation of Linked List LIST createlist() { position tmpcell; tmpcell =(struct node *)malloc(sizeof(str Node)); If(tmpcell == NULL) printf(" Error : Out of Space"); tmpcell->Next = tmpcell; printf("Link List Created Successfully..."); return(tmpcell); } eatelist() n tmpcell; l =(struct node *)malloc(sizeof(struct e)); cell == NULL) " Error : Out of Space"); ell->Next = NULL; "Link List Created Successfully..."); tmpcell); LIST createlist() { position tmpcell; tmpcell =(struct node *)malloc(sizeof(str Node)); If(tmpcell == NULL) printf(" Error : Out of Space"); tmpcell->Next = tmpcell; printf("Link List Created Successfully..."); return(tmpcell); } eatelist() n tmpcell; l =(struct node *)malloc(sizeof(struct e)); cell == NULL) " Error : Out of Space"); ell->Next = NULL; "Link List Created Successfully..."); tmpcell); Implementation of Linked List LIST createlist() { position tmpcell; tmpcell =(struct node *)malloc(sizeof(str Node)); If(tmpcell == NULL) printf(" Error : Out of Space"); tmpcell->Next = tmpcell; printf("Link List Created Successfully..."); return(tmpcell); } LIST createlist() { position tmpcell; tmpcell =(struct node *)malloc(sizeof(str Node)); If(tmpcell == NULL) printf(" Error : Out of Space"); tmpcell->Next = tmpcell; printf("Link List Created Successfully..."); return(tmpcell); }
  • 32. Circular List (CREATE) eate() uct node*)malloc(sizeof(struct node)); n Enter the data:"); %d", &x->data); = x; x; n If you wish to continue press 1 otherwise 0:"); %d", &c); c != 0) struct node*)malloc(sizeof(struct node)); n Enter the data:"); %d", &y->data); = y; = head; n If you wish to continue press 1 otherwise 0:"); %d", &c); eate() uct node*)malloc(sizeof(struct node)); n Enter the data:"); %d", &x->data); = x; x; n If you wish to continue press 1 otherwise 0:"); %d", &c); c != 0) struct node*)malloc(sizeof(struct node)); n Enter the data:"); %d", &y->data); = y; = head; n If you wish to continue press 1 otherwise 0:"); %d", &c); Circular List (CREATE) eate() uct node*)malloc(sizeof(struct node)); n Enter the data:"); %d", &x->data); = x; x; n If you wish to continue press 1 otherwise 0:"); %d", &c); c != 0) struct node*)malloc(sizeof(struct node)); n Enter the data:"); %d", &y->data); = y; = head; n If you wish to continue press 1 otherwise 0:"); %d", &c); eate() uct node*)malloc(sizeof(struct node)); n Enter the data:"); %d", &x->data); = x; x; n If you wish to continue press 1 otherwise 0:"); %d", &c); c != 0) struct node*)malloc(sizeof(struct node)); n Enter the data:"); %d", &y->data); = y; = head; n If you wish to continue press 1 otherwise 0:"); %d", &c);
  • 33. INSERT oid ins_at_beg() = head; = (struct node*)malloc(sizeof(struct node)); rintf("n Enter the data:"); canf("%d", &y->data); while (x->link != head) = x->link; ->link = y; ->link = head; ead = y; } oid ins_at_beg() = head; = (struct node*)malloc(sizeof(struct node)); rintf("n Enter the data:"); canf("%d", &y->data); while (x->link != head) = x->link; ->link = y; ->link = head; ead = y; } INSERT
  • 34. DELETE del_at_beg() { ad == NULL) tf("n List is empty"); { = head; = head; hile (x->link != head) x = x->link; ead = y->link; x->link = head; free(y); } del_at_beg() { ad == NULL) tf("n List is empty"); { = head; = head; hile (x->link != head) x = x->link; ead = y->link; x->link = head; free(y); } DELETE
  • 35. DISPLAY oid traverse() f (head == NULL) printf("n List is empty"); lse = head; while (x->link != head) printf("%d->", x->data); x = x->link; printf("%d", x->data); oid traverse() f (head == NULL) printf("n List is empty"); lse = head; while (x->link != head) printf("%d->", x->data); x = x->link; printf("%d", x->data); DISPLAY