SlideShare a Scribd company logo
List
Objectives
• Describe a list
• How a list can be implemented by linked
structure
• Implement the various operations on
linked list
List
• List is homogeneous collection of
elements, with linear relationship
between the elements, the list can be
ordered or unordered.
• Implementing list
List can be implemented using
1. Linear array
2. Linked list
Array implementation of lists
• The linear array can be created at the compilation time by
using type declaration statement of type
int a[100];
• Which create linear array with 100 elements
• Since each elements takes two bytes of memory, the
compiler allocates 200 bytes for the array.
• The above array can be created at run time with the
following declaration statement
int *a;
a=(int*)malloc(100*sizeof(int));
In the above declaration, the malloc() function allocates 200
bytes of memory and assign the address of the first byte to
the pointer variable a.
Limitations of an array
• Insertion and deletion operations are
expensive.
• Inserting at first position requires first
pushing the entire array down by one
position to make room.
• Deleting the first element requires shifting
all the elements in the list one position up.
• So worst case of these operation is O(n).
• Size of the list must be known in advance.
Linked implementation of list
• To avoid the linear cost of insertion and deletion
operations, we need to ensure that the elements
of the list are not stored contiguously.
• Linked list basically consists of series of
structures, which are not necessarily adjacent in
memory
• Each structure contains an element of the list
and a pointer to structure containing its
successor.
• Linked implementation allow traverse and
search operations to be carried out in linear
time.
• Insertion and deletion operations can be
implemented efficiently as it requires only
rearrangement of pointers.
Linked list defined
A linked list is a linear collection of data
elements, called nodes, the linear order is
given by pointers. Each node is divided
into two or more parts. Linked list can be
of following types:
• Linear linked list or one way list
• Doubly linked list or two way list
• Circular linked list
• Header linked list
Linear linked list
In a linear linked list, also called singly linked list or one
way linked list, each node is divided into two parts.
• First parts contain the information of the element
• Second part called the linked field or next pointer field,
contains the address of the next node in the list.
head
1200 1201 1202 1203 X
Next pointer field of 2nd
node
Information field of second node
•head is used to hold the address of first element of the list.
•Last element of the linked list have NULL value in the next pointer field to mark the
end of the list
Representation of Linear linked
list
Suppose we want to store the list of integer numbers, then
the linear linked list can be represented in memory with
the following declarations.
typedef struct nodetype
{
int info;
struct nodetype *next;
}node;
node *head;
The above declaration define a new data type, whose each
element is of type nodetype and gives it a name node.
Operation on Linear linked lists
• Creating an empty list
• Traversing a list
• Searching an element
• Inserting an element
• Deleting an element
Creating an empty list
• In the previous declaration, the variable head is declared
as pointer to node data type.
• Variable head is not yet given a value.
• This variable is used to point to the first element of the
list
• Since the list will be empty in the beginning, the variable
head is assigned a sentinel value to indicate the list is
empty.
void createemptylist(node **head)
{
*head=NULL;
}
Traversing a list
Linear list can be traversed in two ways
• In order traversal
• Reverse order traversal
In order traversal:
To traverse the linear linked list, we move along the pointer, and
process each element till we reach the last element.
void traverseinorder(node *head)
{
while(head!=NULL)
{
printf(“%dn”,head->info);
head=head->next;
}
}
Traversing a list
Reverse order traversal:
To traverse the linear linked list in reverse order, we move along the
pointer till we reach the last element. The last element is processed
first, then the second last and so on and finally the first element of
the list
To implement this we use either stack (LIFO) or recursion.
Void traversereverseorder(node *head)
{
if(head->next!=NULL)
{
traversereverseorder(head->next);
printf(“%dn”,head->info);
}
}
Searching an element
• In linear linked list, only linear searching
is possible.
• This is one of the limitation of the linked
list as there is no way to find the location
of the middle element of the list
List can be
1. Sorted
2. Unsorted
Searching an element
List is unsorted:
We traverse the list from the beginning, and compare
each element of the list with the given element say
item to be searched.
node *searchunsortedlist(node *head, int item)
{
while((head!=NULL) &&(head->info!=item))
head=head->next;
return head;
}
Searching an element
List is sorted:
If the list is sorted say in ascending order then we traverse the list from
beginning and compare each element of list with item to be
searched. If the match occurs, the location of the element is
returned. If we reach the element that is greater than item or end of
the list NULL value is returned.
node *searchinsortedlist(node *head, int item)
{
while(head!=NULL)
{
if(head->info==item)
return head;
else if (item<head->info)
return NULL;
else
head=head->next;
}
return NULL;
}
Inserting an element
To insert an element in the list, the first task is to
get a free node, assign the element to be
inserted to the info field of the node, and then
new node is placed at the appropriate position
by adjusting the appropriate pointer. The
insertion in the list can take place at the
following positions:
• At the beginning of the list
• At the end of the list
• After a given element
Inserting an element
Insert at the beginning of the list:
First test whether the linked list is initially empty, if yes,
then the element is inserted as the first and only one
element by performing the following steps:
• Assign NULL value to the next pointer field of the new
node
• Assign address of new node to head
If the list is not empty, then the element is inserted as the
first element of the list by performing the following steps:
• Assign value of head variable to the next pointer field of
the new node.
• Assign address of the new node to the head.
Inserting an element
Insert at the beginning of the list:
Void insertatbegining(node **head,int item)
{
node *ptr;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*head==NULL)
ptr->next=NULL;
else
ptr->next=*head;
*head=ptr;
}
Inserting an element
Inserting at the end of the list:
First test whether the linked list is initially empty, if yes,
then the element is inserted as the first and only one
element by performing the following steps:
• Assign NULL value to the next pointer field of the new
node
• Assign address of new node to head
If the list is not empty, then the list is traversed to reach the
last element, and then element is inserted as the last
element of the list by performing the following steps:
• Assign NULL value to the next pointer field of the new
node
• Assign address of the new node to the next pointer field
of the last node.
Inserting an element
Void insertatend(node **head, int item)
{
node *ptr, *loc;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
ptr->next=NULL;
if(*head==NULL)
*head=ptr;
else
{
loc=*head;
while (loc->next!=NULL)
loc=loc->next;
loc->next=ptr;
}
}
Inserting an element
Inserting after given element:
To insert the new element after the given element,
first we find the location, say loc, of the given
element in the list, and then the element is
inserted in the list by performing following steps:
• Assign the next pointer field of the node pointed
by loc to the next pointer field of the new node.
• Assign address of the new node to the next
pointer field of the node pointed by loc.
Inserting an element
Void insertafterelement(node *head, int item,int after)
{
node *ptr, *loc;
loc=search(head,after);
if(loc==(node*)NULL) /*element after not found*/
return;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
ptr->next=loc->next;
loc->next=ptr;
}
Deleting an element
• To delete an element from the list, first
the pointer are set properly and then the
memory occupied by the node to be
deleted is deallocated(free).
• The deletion in the list can take place at
the following positions.
1. At the beginning of the list
2. At the end of the list
3. After a given element
Deleting from the beginning of the
list
An element from the beginning of the lists can be
deleted by performing following steps:
• Assign the value of head ( address of the first
element of the list) to a temporary variable (say
ptr)
• Assign the value of the next pointer field of the
first node to head.
• Deallocate the memory occupied by the node
pointed to by ptr.
Deleting from the beginning of the
list
Void deletefrombegining( node **head)
{
node *ptr;
if(*head==NULL)
return;
else
{
ptr=*head;
*head=(*head)->next;
free(ptr);
}
}
Deleting from the end of the list
To delete from the end of the list, we first traverse
to the second last element of the list. Then the
last element can be deleted by performing
following steps:
• Assign the next pointer field of the second last
node to a temporary variable ( say ptr).
• Assign value NULL to the next pointer field of
the second last node of the list.
• Deallocate the memory occupied by the node
pointed to by ptr.
Deleting from the end of the listVoid deletefromend( node **head)
{
node *ptr,*loc;
if (*head==NULL)
return;
else if ((*head)->next==(node*) NULL)
{
ptr=*head;
*head=NULL;
free(ptr);
}
else
{
loc=*head;
ptr=(*head)->next;
while(ptr->next!=NULL)
{
loc=ptr;
ptr=ptr->next;
}
loc->next=NULL;
free(ptr);
}
}
Deleting after a given element
To delete an element after a given element, first
we find the location say (loc) of the element after
which the element can be deleted by performing
the following steps:
• Assign next pointer field of the node pointed by
the loc to temporary variable (say ptr).
• Assign the next pointer field of the node to be
deleted to the node pointed to by loc
• Deallocate the memory occupied by the node
pointed to by ptr.
Deleting after a given element
Void deleteafterelement( node*head, int after)
{
node *ptr, *loc;
loc=search(head,after);
if(loc==(node*)NULL) /*element ‘after’ not found*/
return;
ptr=loc->next;
loc->next=ptr->next;
free(ptr);
}
Deleting Entire list
Before the program terminates, the entire list must
be deletedso that the memory occupied by the
nodes of the list can be used for other purposes.
This task can be accomplished by performing
the following steps:
• Assign the head pointer to a temporary variable,
say ptr.
• Advance the head pointer to the next node.
• Deallocate the memory occupied by the node
pointed to by ptr.
The above steps are repeated till the entire list is
deleted.
Deleting Entire list
Void deletelist(node **head)
{
node *ptr;
while(*head!=NULL)
{
ptr=*head;
*head=(*head)->next;
free(ptr);
}
}
Doubly Linked List
In doubly linked list, also called the two way list,
each node is divided into three parts:
• The first part called, previous pointer field,
contains the address of preceding element in the
list.
• The second part contains the information of the
list.
• The third part, called next pointer field, contains
the address of the succeeding element in the
list.
In addition, two pointer variables, e.g. head and
tail, are used that contains the address of first
element and the address of last element of the
list.
Doubly Linked List
head
X 1200 1201 1203 X
Next pointer field of 2nd
node
Information field of second node
tail
Previous pointer field of 2nd
node
Representation of doubly linked list
• Suppose we want to store list of integer.
typedef struct nodetype
{
struct nodetype *prev;
int info;
struct nodetype *next;
}node;
node *head,*tail;
The above declaration defines a new data type, whose
each element is of type nodetype and gives it name
node.
Operation on Doubly linked lists
• Creating an empty list
• Traversing a list
• Searching an element
• Inserting an element
• Deleting an element
Creating an Empty list
• In the previous declaration, the variable head and tail are
declared as pointer to a node data type.
• These Variables are not yet given a value.
• The head is used to point to the first element of the list
and tail is used to point to the last element of the list.
• Since the list will be empty in the beginning, the variable
head and tail are assigned a sentinel value to indicate
the list is empty.
void createemptylist(node **head, node **tail)
{
*head=*tail=NULL;
}
Traversing a list
Doubly linked list can be traversed in both way and that too very
conveniently.
• In order traversal
• Reverse order traversal
In order traversal:
To traverse the doubly linked list, we move along the pointer, and
process each element till we reach the last element.
void traverseinorder(node *head)
{
while(head!=NULL)
{
printf(“%dn”,head->info);
head=head->next;
}
}
Traversing a list
Reverse order traversal:
The following listing shows the various steps required for
traversing a doubly linked list in the backward direction.
Void traversereverseorder(node *tail)
{
if(tail!=NULL)
{
printf(“%dn”,tail->info);
tail=tail->prev;
}
}
Searching an element
The doubly linked list can be traversed in any order to
reach the given element. The following listing shows the
various steps required for searching an element from the
beginning.
node *search (node *head, int item)
{
while(head!=NULL)
{
if(head->info==item)
return head;
head=head->next;
}
return NULL;
}
Inserting an element
To insert an element in the list, the first task is to
get a free node, assign the element to be
inserted to the info field of the node, and then
new node is placed at the appropriate position
by adjusting the appropriate pointer. The
insertion in the list can take place at the
following positions:
• At the beginning of the list
• At the end of the list
• After a given element
• Before a given element
Inserting an element
Insert at the beginning of the list:
First test whether the linked list is initially empty, if yes, then the
element is inserted as the first and only one element by performing
the following steps:
• Assign NULL value to the next pointer and prev pointer field of the
new node
• Assign address of new node to head and tail pointer variables.
If the list is not empty, then the element is inserted as the first element
of the list by performing the following steps:
• Assign NULL value to the prev pointer field of the new node.
• Assign value of head variable (the address of the first element of the
existing list) to the next pointer field of the new node.
• Assign address of the new node to prev pointer field of the node
currently pointed by head variable, i. e. first element of the existing
list.
• Finally Assign address of the new node to the head variable.
Inserting an element
Insert at the beginning of the list:
Void insertatbegining (node **head, node **tail, int item)
{
node *ptr;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*head==NULL)
ptr->next=ptr->prev=NULL;
*head=*tail=ptr;
else
{
ptr->prev=NULL;
ptr->next=*head;
(*head)->prev=ptr;
*head=ptr;
}
}
Inserting an element
Inserting at the end of the list
First test whether the linked list is initially empty, if yes, then the
element is inserted as the first and only one element by
performing the following steps:
• Assign NULL value to the next pointer and prev pointer field of
the new node
• Assign address of new node to head and tail pointer variable.
If the list is not empty, then element is inserted as the last element
of the list by performing the following steps:
• Assign NULL value to the next pointer field of the new node.
• Assign value of the tail variable (the address of the last element
of the existing list) to the prev pointer field of the new node.
• Assign address of the new node to the next pointer field of the
node currently pointed by tail variable i.e last element of the
existing list.
• Finally assign the address of the new node to tail variable.
Inserting an element
Insert at the end of the list:
Void insertatend (node **head, node **tail, int item)
{
node *ptr;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*head==NULL)
ptr->next=ptr->prev=NULL;
*head=*tail=ptr;
else
{
ptr->next=NULL;
ptr->prev=*tail;
(*tail)->next=ptr;
*tail=ptr;
}
}
Inserting an element
Inserting after a given element:
Void insert afterelement (node *head, node **tail, int item, int after)
{
node *ptr, *loc;
ptr=head;
loc=search(ptr,after);
if(loc==NULL)
return;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(loc->next==NULL)
{
ptr->next=NULL;
loc->next=ptr;
ptr->prev=*tail;
*tail=ptr;
}
else
{
ptr->prev=loc;
ptr->next=loc->next;
(loc->next)->prev=ptr;
loc->next=ptr;
}
}
Inserting an element
Inserting before a given element:
Void insertbeforeelement (node **head, int item, int before)
{
node *ptr, *loc;
ptr=*head;
loc=search(ptr,before);
if(loc==NULL)
return;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(loc->prev==NULL)
{
ptr->prev=NULL;
loc->prev=ptr;
ptr->next=*head;
*head=ptr;
}
else
{
ptr->prev=loc->prev;
ptr->next=loc;
(loc->prev)->next=ptr;
loc->prev=ptr;
}
}
Deleting an element
• To delete an element from the list, first the
pointer are set properly and then the memory
occupied by the node to be deleted is
deallocated (freed).
• The deletion in the list can take place at the
following positions.
1.At the beginning of the list
2.At the end of the list
3.After a given element
4.Before a given element
Deleting an element
Deleting from the beginning of the list:
An element from the beginning of the lists can be
deleted by performing following steps:
• Assign the value of head ( address of the first
element of the list) to a temporary variable (say ptr)
• Further there are two cases:
1. If there is only one element in the existing list, both
head and tail variable are set to NULL.
2. If there are more than one element in the list then
following steps are given below:
– Assign NULL value to the prev pointer field of the second
node.
– Assign address of the second node to head.
3. Deallocate the memory occupied by the node pointed to by
ptr.
Deleting an element
Deleting from the beginning of the list:
Void deletefrombegining( node **head, node **tail)
{
node *ptr;
if(*head==NULL)
return;
ptr=*head;
if(*head==*tail) /*one element only*/
*head=*tail=NULL;
else
{
(ptr->next)->prev=NULL;
*head=ptr->next;
}
free(ptr);
}
Deleting an element
Deleting from the end of the list:
An element from the end of the list can be deleted by
performing following steps:
• Assign the value of tail ( address of the last element of
the list) to a temporary variable (say ptr)
• Further there are two cases:
1.If there is only one element in the existing list, both
head and tail variable are set to NULL.
2.If there are more than one element in the list then
following steps are given below:
– Assign NULL value to the next pointer field of the second
last node.
– Assign address of the second last node to tail.
3.Deallocate the memory occupied by the node pointed to by
ptr.
Deleting an element
Deleting from the end of the list:
Void deletefromend( node **head, node **tail)
{
node *ptr;
if(*head==NULL)
return;
ptr=*tail;
if(*head==*tail) /*one element only*/
*head=*tail=NULL;
else
{
(ptr->prev)->next=NULL;
*tail=ptr->prev;
}
free(ptr);
}
Deleting an element
Deleting after a given element:
Void ideleteafterelement (node *head, node **tail, int item, int after)
{
node *ptr, *loc;
ptr=head;
loc=search(ptr,after);
if(loc==NULL)
return;
else if((loc->next)->next==NULL)
{
ptr=loc->next;
loc->next=NULL;
*tail=loc;
free(ptr);
}
else
{
ptr=loc->next;
loc->next=ptr->next;
(ptr->next)->prev=loc;
free(ptr);
}
}
Deleting an element
Deleting before a given element:
Void ideleteafterelement (node **head, int item, int before)
{
node *ptr, *loc;
ptr=head;
loc=search(ptr,before);
if(loc==NULL)
return;
else if((loc->prev)->prev==NULL)
{
ptr=loc->prev;
loc->prev=NULL;
*head=loc;
free(ptr);
}
else
{
ptr=loc->prev;
loc->prev=ptr->prev;
(ptr->prev)->next=loc;
free(ptr);
}
}
Deleting entire list
The doubly linked list can be deleted either by
heading from the beginning or from the end.
The list can be deleted from the beginning by
performing the following steps:
• Assign the head pointer to a temporary
variable, say ptr.
• Advance the head pointer to the next node.
• Deallocate the memory occupied by the node
pointed to by ptr.
The above steps are repeated till the entire list
is deleted. Finally the tail pointer is set to
NULL value.
Deleting entire list
Void deletelist(node **head, node **tail)
{
node *ptr;
while(*head!=NULL)
{
ptr=*head;
*head=(*head)->next;
free(ptr);
}
*tail=NULL;
}
Circular Linked List
A circular list is a linear linked list, except that the
last element points to the first element. For non
empty circular linked list there are no NULL
pointer.
The memory declarations for representing circular
linked lists are the same as for linear linked lists
Properties:
• Can reach entire list from any node
• Need special test for end of list
• Used as buffer
Circular Linked List
• A Circular Linked List is a special type of
Linked List
• It supports traversing from the end of the
list to the beginning by making the last
node point back to the head of the list.
• Circular linked lists are usually sorted
• Circular linked lists are useful for playing
video and sound files in “looping” mode.
• They are also a stepping stone to
implementing graphs, an important topic in
computer graphics.
Representation of Circular linked
list
Suppose we want to store the list of integer numbers, then
the circular linked list can be represented in memory with
the following declarations.
typedef struct nodetype
{
int info;
struct nodetype *next;
}node;
node *head;
The above declaration define a new data type, whose each
element is of type nodetype and gives it a name node.
Circular Linked List
All operations performed on linear linked list can be easily
extended for circular linked lists with following
exceptions:
1. While inserting new node at the end of the lists, its
next pointer field is made to point to the first node.
2. While testing for the end of the lists, we compare the
next pointer field with the address of the first node.
head
1201 1234 1345
Circular Linked with 3 Nodes
Operation on Linear linked lists
• Creating an empty list
• Traversing a list
• Searching an element
• Inserting an element
• Deleting an element
Header Linked List
• A header list is a linked list, which
always contains a special node, called
header node, at the beginning of the
linked list.
• This header node usually contains
vital information about the linked list
such as the number of nodes in the list,
whether the list is sorted or not.
Header Linked List
head
1234 1345 1346 X
Header Node
Header Linked List
Types of header linked list:
• Header linear linked list
• Circular header list
• Two way header list
• Two way circular header list
Header Linked List
head
1234 1345 1346 X
Header Node
Header linear linked list
Header Linked List
head
1234 1345 1346
Header Node
Circular header list
Header Linked List
X 1200 1201 1203 X
head
Header node
Two way header list
Header Linked List
1200 1201 1203
head
Header node
Applications of linked lists
• To implement the other data structures
such as stacks, queues, trees and graphs.
• To maintain a directory of names.
• To perform arithmetic operation on long
integers.
• To manipulate polynomial.
• To represent sparse matrices.
Polynomial Manipulation
A polynomial of type
4x3
+6x2
+10x+6
can be represented using following linked
list
4 3 6 2 10 1 6 0 X
Poly coefficient
power
In the above list, each node has the following structure
Coefficient of the term Power of x Link to the next node
Polynomial Manipulation
The required memory declarations for the
representation of a polynomial with integer
coefficients are
typedef struct nodetype
{
int coeff;
int power;
struct nodetype *next;
}node;
node *poly;
 Examples
a x x= + +3 2 114 8
b x x x= − +8 3 1014 10 6
3 14 2 8 1 0
a
null
8 14 -3 10 10 6
b
null
Polynomial Representation: Example
 Adding Polynomials: Figure 4:19 c = a + b
4.6.2 Adding Polynomials: c = a + b
Case 1: p->exp = q->exp
3 14 2 8 1 0
p
8 14 -3 10 10 6
q
11 14
a
b
c
Adding Polynomials : c = a + b (cont.)
Case 2: p->exp < q->exp
3 14 2 8 1 0
p
8 14 -3 10 10 6
q
11 14 -3 10
a
b
c
3 14 2 8 1 0
p
8 14 -3 10 10 6
q
11 14 -3 10 2 8
Case 3: p->exp > q->exp
Adding Polynomials: c = a + b (cont.)
Representing sparse matrices
• inadequate of sequential schemes
(1) # of nonzero terms will vary after some matrix computation
(2) matrix just represents intermediate results
• new scheme
• Each column (row): a circular linked list with a head node
0 0 0 2 0 0
0 0 1 0 0 5
0 4 0 0 0 0
0 0 0 0 0 0
0 0 0 0 7 0
A 5X6 sparse matrices
Sparse Matrix
The description of this representation is as
• It contains one header node that has four fields….
--#of rows
--#of cols
--#of non zero elements
--head i.e. pointer to 1st
row containing at least one non zero element.
• A linked list of rows containing at least one non zero term, in the ascending
order of their row values. each node of this list has three fields
--row (row number for corresponding row list)
--next (pointer to next node in the row list)
--first (a pointer to first column in a row having non zero item)
• A linked list of columns containing nonzero terms, in the ascending order of
their column values. Each node of this list has three fields
--col (column number for corresponding row list)
--term ( a non zero value in column col)
--link (a pointer to next column having non zero element)
Sparse Matrix
5 6 5
1 4 2 X
2 3 1
3 2 4 X
5 X 5 7 X
6 5 X
A linked representation of sparse matrix
Required memory declaration
Structure of column node:
typedef struct columnnodetype
{
int col;
float element;
struct columnnodetype *link;
}columnnode;
Required memory declaration
Structure of row node:
typedef struct rownodetype
{
int row;
struct rownodetype *next;
struct columnnode *first;
}rownode;
Required memory declaration
Structure of header node:
typedef struct headernodetype
{
int nrow;
int ncol;
int num;
struct rownode *head;
}headernode;
Linked list Problems
1. Write a function that return value 1 if the
linear linked list is sorted in the ascending
order otherwise it returns value 0.
2. Write a function that makes copies of
given linear linked list.
3. Write a function that merge two sorted
linear linked list.
4. Write a function that sorts a linear linked
list of integer values in the descending
order.
Josephus Problem
It consists of a group of soldiers surrounded by a heavy
enemy force. There is only one horse to escape.
Therefore, only one soldier can escape. In order to
determine which soldier will escape, they form a circle
and pick up a number n from a hat. A name is also
picked up from the hat. They start counting clockwise
around the circle from a soldier whose name is picked up
from the hat. And the soldier on which count reaches n
is removed from the circle. And the count again starts
from the soldier who was next to the soldier who is
removed from the circle. This process goes on till only
one soldier is left. This soldier will take the horse and
escapes. Write a program to solve this problem. Input to
your program is list of names of soldiers and number n
and the output should be the name of the soldier left.
Josephus Problem
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char info[25];
struct node *NEXT;
};
struct node *HEAD;
struct node *TAIL;
char name[25];
void myInit()
{
HEAD=(struct node *)
malloc(sizeof(struct node));
TAIL=(struct node *)
malloc(sizeof(struct node));
HEAD->NEXT=TAIL;
TAIL->NEXT=HEAD;
}
Josephus Problem
void myInsert()
{
struct node *TEMP;
TEMP=(struct
node*)malloc(sizeof(struc
t node));
strcpy(TEMP->info,name);
TEMP->NEXT=HEAD-
>NEXT;
HEAD->NEXT=TEMP;
}
void myRemove()
{
struct node *TEMP;
TEMP=(struct node*)
malloc(sizeof(struct
node));
TEMP=HEAD->NEXT;
HEAD->NEXT=HEAD-
>NEXT->NEXT;
strcpy(name,TEMP->info);
}
Josephus Problem
void main()
{
clrscr();
int numSol, counter;
myInit();
printf("Enter the number of
soldier: ");
scanf("%d",&numSol);
for(int i=1;i<=numSol; i++)
{
printf("Pls. Type a Name:
");
scanf("%s",&name);
myInsert();
}
printf("Value for Counter: ");
scanf("%d",&counter);
for(int j=1;j<=numSol;j++)
{
for(int k=1;k<=counter;k++)
{
myRemove();
if(k==counter)
{
puts(name);
getch();
} } } }

More Related Content

What's hot

Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
Tushar Aneyrao
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
J.T.A.JONES
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
Hafiz Umair
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
student
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
Aakash deep Singhal
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Reazul Islam
 
Ch 1 intriductions
Ch 1 intriductionsCh 1 intriductions
Ch 1 intriductions
irshad17
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
Dr.Umadevi V
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
Dabbal Singh Mahara
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Linked list
Linked listLinked list
Linked list
Md. Afif Al Mamun
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
Linked list
Linked listLinked list
Linked list
akshat360
 

What's hot (16)

Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Ch 1 intriductions
Ch 1 intriductionsCh 1 intriductions
Ch 1 intriductions
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
Linked List
Linked ListLinked List
Linked List
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Linked list
Linked listLinked list
Linked list
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Linked list
Linked listLinked list
Linked list
 

Viewers also liked

Data visualization
Data visualizationData visualization
Data visualization
Young Alista
 
Maven
MavenMaven
Cryptography
CryptographyCryptography
Cryptography
Young Alista
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discovery
Young Alista
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cache
Young Alista
 
Network
NetworkNetwork
Network
Young Alista
 
Learning python
Learning pythonLearning python
Learning python
Young Alista
 
Motivation for multithreaded architectures
Motivation for multithreaded architecturesMotivation for multithreaded architectures
Motivation for multithreaded architectures
Young Alista
 
Computer security
Computer securityComputer security
Computer security
Young Alista
 
Prolog resume
Prolog resumeProlog resume
Prolog resume
Young Alista
 
Overview prolog
Overview prologOverview prolog
Overview prolog
Young Alista
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data mining
Young Alista
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Young Alista
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Young Alista
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherence
Young Alista
 
Abstract class
Abstract classAbstract class
Abstract class
Young Alista
 
Game theory
Game theoryGame theory
Game theory
Young Alista
 
Google appenginejava.ppt
Google appenginejava.pptGoogle appenginejava.ppt
Google appenginejava.ppt
Young Alista
 
Basic dns-mod
Basic dns-modBasic dns-mod
Basic dns-mod
Young Alista
 
Hashfunction
HashfunctionHashfunction
Hashfunction
Young Alista
 

Viewers also liked (20)

Data visualization
Data visualizationData visualization
Data visualization
 
Maven
MavenMaven
Maven
 
Cryptography
CryptographyCryptography
Cryptography
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discovery
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cache
 
Network
NetworkNetwork
Network
 
Learning python
Learning pythonLearning python
Learning python
 
Motivation for multithreaded architectures
Motivation for multithreaded architecturesMotivation for multithreaded architectures
Motivation for multithreaded architectures
 
Computer security
Computer securityComputer security
Computer security
 
Prolog resume
Prolog resumeProlog resume
Prolog resume
 
Overview prolog
Overview prologOverview prolog
Overview prolog
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data mining
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherence
 
Abstract class
Abstract classAbstract class
Abstract class
 
Game theory
Game theoryGame theory
Game theory
 
Google appenginejava.ppt
Google appenginejava.pptGoogle appenginejava.ppt
Google appenginejava.ppt
 
Basic dns-mod
Basic dns-modBasic dns-mod
Basic dns-mod
 
Hashfunction
HashfunctionHashfunction
Hashfunction
 

Similar to Linked list

Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
kasthurimukila
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
NirmalPandey23
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
single linked list
single linked listsingle linked list
single linked list
Sathasivam Rangasamy
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
msohail37
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
Amar Jukuntla
 
LinkedDoublyLists.ppt
LinkedDoublyLists.pptLinkedDoublyLists.ppt
LinkedDoublyLists.ppt
veenatanmaipatlolla
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
Mandeep Singh
 
Data structures2
Data structures2Data structures2
Data structures2
Parthipan Parthi
 
3.linked list
3.linked list3.linked list
3.linked list
Chandan Singh
 
Data structures
Data structuresData structures
Data structures
Sneha Chopra
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
ChrisSosaJacob
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
Lifna C.S
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
Ain-ul-Moiz Khawaja
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
kiran Patel
 

Similar to Linked list (20)

Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
single linked list
single linked listsingle linked list
single linked list
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
LinkedDoublyLists.ppt
LinkedDoublyLists.pptLinkedDoublyLists.ppt
LinkedDoublyLists.ppt
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
 
Data structures2
Data structures2Data structures2
Data structures2
 
3.linked list
3.linked list3.linked list
3.linked list
 
Data structures
Data structuresData structures
Data structures
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 

More from Young Alista

Serialization/deserialization
Serialization/deserializationSerialization/deserialization
Serialization/deserialization
Young Alista
 
Business analytics and data mining
Business analytics and data miningBusiness analytics and data mining
Business analytics and data mining
Young Alista
 
Cache recap
Cache recapCache recap
Cache recap
Young Alista
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching works
Young Alista
 
Object model
Object modelObject model
Object model
Young Alista
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessors
Young Alista
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Young Alista
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
Young Alista
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Young Alista
 
Inheritance
InheritanceInheritance
Inheritance
Young Alista
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and python
Young Alista
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
Young Alista
 
Programming for engineers in python
Programming for engineers in pythonProgramming for engineers in python
Programming for engineers in python
Young Alista
 
Api crash
Api crashApi crash
Api crash
Young Alista
 
Python basics
Python basicsPython basics
Python basics
Young Alista
 
Extending burp with python
Extending burp with pythonExtending burp with python
Extending burp with python
Young Alista
 
Python your new best friend
Python your new best friendPython your new best friend
Python your new best friend
Young Alista
 
Python language data types
Python language data typesPython language data types
Python language data types
Young Alista
 
Rest api to integrate with your site
Rest api to integrate with your siteRest api to integrate with your site
Rest api to integrate with your site
Young Alista
 
Poo java
Poo javaPoo java
Poo java
Young Alista
 

More from Young Alista (20)

Serialization/deserialization
Serialization/deserializationSerialization/deserialization
Serialization/deserialization
 
Business analytics and data mining
Business analytics and data miningBusiness analytics and data mining
Business analytics and data mining
 
Cache recap
Cache recapCache recap
Cache recap
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching works
 
Object model
Object modelObject model
Object model
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessors
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Inheritance
InheritanceInheritance
Inheritance
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and python
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Programming for engineers in python
Programming for engineers in pythonProgramming for engineers in python
Programming for engineers in python
 
Api crash
Api crashApi crash
Api crash
 
Python basics
Python basicsPython basics
Python basics
 
Extending burp with python
Extending burp with pythonExtending burp with python
Extending burp with python
 
Python your new best friend
Python your new best friendPython your new best friend
Python your new best friend
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Rest api to integrate with your site
Rest api to integrate with your siteRest api to integrate with your site
Rest api to integrate with your site
 
Poo java
Poo javaPoo java
Poo java
 

Recently uploaded

Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 

Recently uploaded (20)

Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 

Linked list

  • 1. List Objectives • Describe a list • How a list can be implemented by linked structure • Implement the various operations on linked list
  • 2. List • List is homogeneous collection of elements, with linear relationship between the elements, the list can be ordered or unordered. • Implementing list List can be implemented using 1. Linear array 2. Linked list
  • 3. Array implementation of lists • The linear array can be created at the compilation time by using type declaration statement of type int a[100]; • Which create linear array with 100 elements • Since each elements takes two bytes of memory, the compiler allocates 200 bytes for the array. • The above array can be created at run time with the following declaration statement int *a; a=(int*)malloc(100*sizeof(int)); In the above declaration, the malloc() function allocates 200 bytes of memory and assign the address of the first byte to the pointer variable a.
  • 4. Limitations of an array • Insertion and deletion operations are expensive. • Inserting at first position requires first pushing the entire array down by one position to make room. • Deleting the first element requires shifting all the elements in the list one position up. • So worst case of these operation is O(n). • Size of the list must be known in advance.
  • 5. Linked implementation of list • To avoid the linear cost of insertion and deletion operations, we need to ensure that the elements of the list are not stored contiguously. • Linked list basically consists of series of structures, which are not necessarily adjacent in memory • Each structure contains an element of the list and a pointer to structure containing its successor. • Linked implementation allow traverse and search operations to be carried out in linear time. • Insertion and deletion operations can be implemented efficiently as it requires only rearrangement of pointers.
  • 6. Linked list defined A linked list is a linear collection of data elements, called nodes, the linear order is given by pointers. Each node is divided into two or more parts. Linked list can be of following types: • Linear linked list or one way list • Doubly linked list or two way list • Circular linked list • Header linked list
  • 7. Linear linked list In a linear linked list, also called singly linked list or one way linked list, each node is divided into two parts. • First parts contain the information of the element • Second part called the linked field or next pointer field, contains the address of the next node in the list. head 1200 1201 1202 1203 X Next pointer field of 2nd node Information field of second node •head is used to hold the address of first element of the list. •Last element of the linked list have NULL value in the next pointer field to mark the end of the list
  • 8. Representation of Linear linked list Suppose we want to store the list of integer numbers, then the linear linked list can be represented in memory with the following declarations. typedef struct nodetype { int info; struct nodetype *next; }node; node *head; The above declaration define a new data type, whose each element is of type nodetype and gives it a name node.
  • 9. Operation on Linear linked lists • Creating an empty list • Traversing a list • Searching an element • Inserting an element • Deleting an element
  • 10. Creating an empty list • In the previous declaration, the variable head is declared as pointer to node data type. • Variable head is not yet given a value. • This variable is used to point to the first element of the list • Since the list will be empty in the beginning, the variable head is assigned a sentinel value to indicate the list is empty. void createemptylist(node **head) { *head=NULL; }
  • 11. Traversing a list Linear list can be traversed in two ways • In order traversal • Reverse order traversal In order traversal: To traverse the linear linked list, we move along the pointer, and process each element till we reach the last element. void traverseinorder(node *head) { while(head!=NULL) { printf(“%dn”,head->info); head=head->next; } }
  • 12. Traversing a list Reverse order traversal: To traverse the linear linked list in reverse order, we move along the pointer till we reach the last element. The last element is processed first, then the second last and so on and finally the first element of the list To implement this we use either stack (LIFO) or recursion. Void traversereverseorder(node *head) { if(head->next!=NULL) { traversereverseorder(head->next); printf(“%dn”,head->info); } }
  • 13. Searching an element • In linear linked list, only linear searching is possible. • This is one of the limitation of the linked list as there is no way to find the location of the middle element of the list List can be 1. Sorted 2. Unsorted
  • 14. Searching an element List is unsorted: We traverse the list from the beginning, and compare each element of the list with the given element say item to be searched. node *searchunsortedlist(node *head, int item) { while((head!=NULL) &&(head->info!=item)) head=head->next; return head; }
  • 15. Searching an element List is sorted: If the list is sorted say in ascending order then we traverse the list from beginning and compare each element of list with item to be searched. If the match occurs, the location of the element is returned. If we reach the element that is greater than item or end of the list NULL value is returned. node *searchinsortedlist(node *head, int item) { while(head!=NULL) { if(head->info==item) return head; else if (item<head->info) return NULL; else head=head->next; } return NULL; }
  • 16. Inserting an element To insert an element in the list, the first task is to get a free node, assign the element to be inserted to the info field of the node, and then new node is placed at the appropriate position by adjusting the appropriate pointer. The insertion in the list can take place at the following positions: • At the beginning of the list • At the end of the list • After a given element
  • 17. Inserting an element Insert at the beginning of the list: First test whether the linked list is initially empty, if yes, then the element is inserted as the first and only one element by performing the following steps: • Assign NULL value to the next pointer field of the new node • Assign address of new node to head If the list is not empty, then the element is inserted as the first element of the list by performing the following steps: • Assign value of head variable to the next pointer field of the new node. • Assign address of the new node to the head.
  • 18. Inserting an element Insert at the beginning of the list: Void insertatbegining(node **head,int item) { node *ptr; ptr=(node*)malloc(sizeof(node)); ptr->info=item; if(*head==NULL) ptr->next=NULL; else ptr->next=*head; *head=ptr; }
  • 19. Inserting an element Inserting at the end of the list: First test whether the linked list is initially empty, if yes, then the element is inserted as the first and only one element by performing the following steps: • Assign NULL value to the next pointer field of the new node • Assign address of new node to head If the list is not empty, then the list is traversed to reach the last element, and then element is inserted as the last element of the list by performing the following steps: • Assign NULL value to the next pointer field of the new node • Assign address of the new node to the next pointer field of the last node.
  • 20. Inserting an element Void insertatend(node **head, int item) { node *ptr, *loc; ptr=(node*)malloc(sizeof(node)); ptr->info=item; ptr->next=NULL; if(*head==NULL) *head=ptr; else { loc=*head; while (loc->next!=NULL) loc=loc->next; loc->next=ptr; } }
  • 21. Inserting an element Inserting after given element: To insert the new element after the given element, first we find the location, say loc, of the given element in the list, and then the element is inserted in the list by performing following steps: • Assign the next pointer field of the node pointed by loc to the next pointer field of the new node. • Assign address of the new node to the next pointer field of the node pointed by loc.
  • 22. Inserting an element Void insertafterelement(node *head, int item,int after) { node *ptr, *loc; loc=search(head,after); if(loc==(node*)NULL) /*element after not found*/ return; ptr=(node*)malloc(sizeof(node)); ptr->info=item; ptr->next=loc->next; loc->next=ptr; }
  • 23. Deleting an element • To delete an element from the list, first the pointer are set properly and then the memory occupied by the node to be deleted is deallocated(free). • The deletion in the list can take place at the following positions. 1. At the beginning of the list 2. At the end of the list 3. After a given element
  • 24. Deleting from the beginning of the list An element from the beginning of the lists can be deleted by performing following steps: • Assign the value of head ( address of the first element of the list) to a temporary variable (say ptr) • Assign the value of the next pointer field of the first node to head. • Deallocate the memory occupied by the node pointed to by ptr.
  • 25. Deleting from the beginning of the list Void deletefrombegining( node **head) { node *ptr; if(*head==NULL) return; else { ptr=*head; *head=(*head)->next; free(ptr); } }
  • 26. Deleting from the end of the list To delete from the end of the list, we first traverse to the second last element of the list. Then the last element can be deleted by performing following steps: • Assign the next pointer field of the second last node to a temporary variable ( say ptr). • Assign value NULL to the next pointer field of the second last node of the list. • Deallocate the memory occupied by the node pointed to by ptr.
  • 27. Deleting from the end of the listVoid deletefromend( node **head) { node *ptr,*loc; if (*head==NULL) return; else if ((*head)->next==(node*) NULL) { ptr=*head; *head=NULL; free(ptr); } else { loc=*head; ptr=(*head)->next; while(ptr->next!=NULL) { loc=ptr; ptr=ptr->next; } loc->next=NULL; free(ptr); } }
  • 28. Deleting after a given element To delete an element after a given element, first we find the location say (loc) of the element after which the element can be deleted by performing the following steps: • Assign next pointer field of the node pointed by the loc to temporary variable (say ptr). • Assign the next pointer field of the node to be deleted to the node pointed to by loc • Deallocate the memory occupied by the node pointed to by ptr.
  • 29. Deleting after a given element Void deleteafterelement( node*head, int after) { node *ptr, *loc; loc=search(head,after); if(loc==(node*)NULL) /*element ‘after’ not found*/ return; ptr=loc->next; loc->next=ptr->next; free(ptr); }
  • 30. Deleting Entire list Before the program terminates, the entire list must be deletedso that the memory occupied by the nodes of the list can be used for other purposes. This task can be accomplished by performing the following steps: • Assign the head pointer to a temporary variable, say ptr. • Advance the head pointer to the next node. • Deallocate the memory occupied by the node pointed to by ptr. The above steps are repeated till the entire list is deleted.
  • 31. Deleting Entire list Void deletelist(node **head) { node *ptr; while(*head!=NULL) { ptr=*head; *head=(*head)->next; free(ptr); } }
  • 32. Doubly Linked List In doubly linked list, also called the two way list, each node is divided into three parts: • The first part called, previous pointer field, contains the address of preceding element in the list. • The second part contains the information of the list. • The third part, called next pointer field, contains the address of the succeeding element in the list. In addition, two pointer variables, e.g. head and tail, are used that contains the address of first element and the address of last element of the list.
  • 33. Doubly Linked List head X 1200 1201 1203 X Next pointer field of 2nd node Information field of second node tail Previous pointer field of 2nd node
  • 34. Representation of doubly linked list • Suppose we want to store list of integer. typedef struct nodetype { struct nodetype *prev; int info; struct nodetype *next; }node; node *head,*tail; The above declaration defines a new data type, whose each element is of type nodetype and gives it name node.
  • 35. Operation on Doubly linked lists • Creating an empty list • Traversing a list • Searching an element • Inserting an element • Deleting an element
  • 36. Creating an Empty list • In the previous declaration, the variable head and tail are declared as pointer to a node data type. • These Variables are not yet given a value. • The head is used to point to the first element of the list and tail is used to point to the last element of the list. • Since the list will be empty in the beginning, the variable head and tail are assigned a sentinel value to indicate the list is empty. void createemptylist(node **head, node **tail) { *head=*tail=NULL; }
  • 37. Traversing a list Doubly linked list can be traversed in both way and that too very conveniently. • In order traversal • Reverse order traversal In order traversal: To traverse the doubly linked list, we move along the pointer, and process each element till we reach the last element. void traverseinorder(node *head) { while(head!=NULL) { printf(“%dn”,head->info); head=head->next; } }
  • 38. Traversing a list Reverse order traversal: The following listing shows the various steps required for traversing a doubly linked list in the backward direction. Void traversereverseorder(node *tail) { if(tail!=NULL) { printf(“%dn”,tail->info); tail=tail->prev; } }
  • 39. Searching an element The doubly linked list can be traversed in any order to reach the given element. The following listing shows the various steps required for searching an element from the beginning. node *search (node *head, int item) { while(head!=NULL) { if(head->info==item) return head; head=head->next; } return NULL; }
  • 40. Inserting an element To insert an element in the list, the first task is to get a free node, assign the element to be inserted to the info field of the node, and then new node is placed at the appropriate position by adjusting the appropriate pointer. The insertion in the list can take place at the following positions: • At the beginning of the list • At the end of the list • After a given element • Before a given element
  • 41. Inserting an element Insert at the beginning of the list: First test whether the linked list is initially empty, if yes, then the element is inserted as the first and only one element by performing the following steps: • Assign NULL value to the next pointer and prev pointer field of the new node • Assign address of new node to head and tail pointer variables. If the list is not empty, then the element is inserted as the first element of the list by performing the following steps: • Assign NULL value to the prev pointer field of the new node. • Assign value of head variable (the address of the first element of the existing list) to the next pointer field of the new node. • Assign address of the new node to prev pointer field of the node currently pointed by head variable, i. e. first element of the existing list. • Finally Assign address of the new node to the head variable.
  • 42. Inserting an element Insert at the beginning of the list: Void insertatbegining (node **head, node **tail, int item) { node *ptr; ptr=(node*)malloc(sizeof(node)); ptr->info=item; if(*head==NULL) ptr->next=ptr->prev=NULL; *head=*tail=ptr; else { ptr->prev=NULL; ptr->next=*head; (*head)->prev=ptr; *head=ptr; } }
  • 43. Inserting an element Inserting at the end of the list First test whether the linked list is initially empty, if yes, then the element is inserted as the first and only one element by performing the following steps: • Assign NULL value to the next pointer and prev pointer field of the new node • Assign address of new node to head and tail pointer variable. If the list is not empty, then element is inserted as the last element of the list by performing the following steps: • Assign NULL value to the next pointer field of the new node. • Assign value of the tail variable (the address of the last element of the existing list) to the prev pointer field of the new node. • Assign address of the new node to the next pointer field of the node currently pointed by tail variable i.e last element of the existing list. • Finally assign the address of the new node to tail variable.
  • 44. Inserting an element Insert at the end of the list: Void insertatend (node **head, node **tail, int item) { node *ptr; ptr=(node*)malloc(sizeof(node)); ptr->info=item; if(*head==NULL) ptr->next=ptr->prev=NULL; *head=*tail=ptr; else { ptr->next=NULL; ptr->prev=*tail; (*tail)->next=ptr; *tail=ptr; } }
  • 45. Inserting an element Inserting after a given element: Void insert afterelement (node *head, node **tail, int item, int after) { node *ptr, *loc; ptr=head; loc=search(ptr,after); if(loc==NULL) return; ptr=(node*)malloc(sizeof(node)); ptr->info=item; if(loc->next==NULL) { ptr->next=NULL; loc->next=ptr; ptr->prev=*tail; *tail=ptr; } else { ptr->prev=loc; ptr->next=loc->next; (loc->next)->prev=ptr; loc->next=ptr; } }
  • 46. Inserting an element Inserting before a given element: Void insertbeforeelement (node **head, int item, int before) { node *ptr, *loc; ptr=*head; loc=search(ptr,before); if(loc==NULL) return; ptr=(node*)malloc(sizeof(node)); ptr->info=item; if(loc->prev==NULL) { ptr->prev=NULL; loc->prev=ptr; ptr->next=*head; *head=ptr; } else { ptr->prev=loc->prev; ptr->next=loc; (loc->prev)->next=ptr; loc->prev=ptr; } }
  • 47. Deleting an element • To delete an element from the list, first the pointer are set properly and then the memory occupied by the node to be deleted is deallocated (freed). • The deletion in the list can take place at the following positions. 1.At the beginning of the list 2.At the end of the list 3.After a given element 4.Before a given element
  • 48. Deleting an element Deleting from the beginning of the list: An element from the beginning of the lists can be deleted by performing following steps: • Assign the value of head ( address of the first element of the list) to a temporary variable (say ptr) • Further there are two cases: 1. If there is only one element in the existing list, both head and tail variable are set to NULL. 2. If there are more than one element in the list then following steps are given below: – Assign NULL value to the prev pointer field of the second node. – Assign address of the second node to head. 3. Deallocate the memory occupied by the node pointed to by ptr.
  • 49. Deleting an element Deleting from the beginning of the list: Void deletefrombegining( node **head, node **tail) { node *ptr; if(*head==NULL) return; ptr=*head; if(*head==*tail) /*one element only*/ *head=*tail=NULL; else { (ptr->next)->prev=NULL; *head=ptr->next; } free(ptr); }
  • 50. Deleting an element Deleting from the end of the list: An element from the end of the list can be deleted by performing following steps: • Assign the value of tail ( address of the last element of the list) to a temporary variable (say ptr) • Further there are two cases: 1.If there is only one element in the existing list, both head and tail variable are set to NULL. 2.If there are more than one element in the list then following steps are given below: – Assign NULL value to the next pointer field of the second last node. – Assign address of the second last node to tail. 3.Deallocate the memory occupied by the node pointed to by ptr.
  • 51. Deleting an element Deleting from the end of the list: Void deletefromend( node **head, node **tail) { node *ptr; if(*head==NULL) return; ptr=*tail; if(*head==*tail) /*one element only*/ *head=*tail=NULL; else { (ptr->prev)->next=NULL; *tail=ptr->prev; } free(ptr); }
  • 52. Deleting an element Deleting after a given element: Void ideleteafterelement (node *head, node **tail, int item, int after) { node *ptr, *loc; ptr=head; loc=search(ptr,after); if(loc==NULL) return; else if((loc->next)->next==NULL) { ptr=loc->next; loc->next=NULL; *tail=loc; free(ptr); } else { ptr=loc->next; loc->next=ptr->next; (ptr->next)->prev=loc; free(ptr); } }
  • 53. Deleting an element Deleting before a given element: Void ideleteafterelement (node **head, int item, int before) { node *ptr, *loc; ptr=head; loc=search(ptr,before); if(loc==NULL) return; else if((loc->prev)->prev==NULL) { ptr=loc->prev; loc->prev=NULL; *head=loc; free(ptr); } else { ptr=loc->prev; loc->prev=ptr->prev; (ptr->prev)->next=loc; free(ptr); } }
  • 54. Deleting entire list The doubly linked list can be deleted either by heading from the beginning or from the end. The list can be deleted from the beginning by performing the following steps: • Assign the head pointer to a temporary variable, say ptr. • Advance the head pointer to the next node. • Deallocate the memory occupied by the node pointed to by ptr. The above steps are repeated till the entire list is deleted. Finally the tail pointer is set to NULL value.
  • 55. Deleting entire list Void deletelist(node **head, node **tail) { node *ptr; while(*head!=NULL) { ptr=*head; *head=(*head)->next; free(ptr); } *tail=NULL; }
  • 56. Circular Linked List A circular list is a linear linked list, except that the last element points to the first element. For non empty circular linked list there are no NULL pointer. The memory declarations for representing circular linked lists are the same as for linear linked lists Properties: • Can reach entire list from any node • Need special test for end of list • Used as buffer
  • 57. Circular Linked List • A Circular Linked List is a special type of Linked List • It supports traversing from the end of the list to the beginning by making the last node point back to the head of the list. • Circular linked lists are usually sorted • Circular linked lists are useful for playing video and sound files in “looping” mode. • They are also a stepping stone to implementing graphs, an important topic in computer graphics.
  • 58. Representation of Circular linked list Suppose we want to store the list of integer numbers, then the circular linked list can be represented in memory with the following declarations. typedef struct nodetype { int info; struct nodetype *next; }node; node *head; The above declaration define a new data type, whose each element is of type nodetype and gives it a name node.
  • 59. Circular Linked List All operations performed on linear linked list can be easily extended for circular linked lists with following exceptions: 1. While inserting new node at the end of the lists, its next pointer field is made to point to the first node. 2. While testing for the end of the lists, we compare the next pointer field with the address of the first node. head 1201 1234 1345 Circular Linked with 3 Nodes
  • 60. Operation on Linear linked lists • Creating an empty list • Traversing a list • Searching an element • Inserting an element • Deleting an element
  • 61. Header Linked List • A header list is a linked list, which always contains a special node, called header node, at the beginning of the linked list. • This header node usually contains vital information about the linked list such as the number of nodes in the list, whether the list is sorted or not.
  • 62. Header Linked List head 1234 1345 1346 X Header Node
  • 63. Header Linked List Types of header linked list: • Header linear linked list • Circular header list • Two way header list • Two way circular header list
  • 64. Header Linked List head 1234 1345 1346 X Header Node Header linear linked list
  • 65. Header Linked List head 1234 1345 1346 Header Node Circular header list
  • 66. Header Linked List X 1200 1201 1203 X head Header node Two way header list
  • 67. Header Linked List 1200 1201 1203 head Header node
  • 68. Applications of linked lists • To implement the other data structures such as stacks, queues, trees and graphs. • To maintain a directory of names. • To perform arithmetic operation on long integers. • To manipulate polynomial. • To represent sparse matrices.
  • 69. Polynomial Manipulation A polynomial of type 4x3 +6x2 +10x+6 can be represented using following linked list 4 3 6 2 10 1 6 0 X Poly coefficient power In the above list, each node has the following structure Coefficient of the term Power of x Link to the next node
  • 70. Polynomial Manipulation The required memory declarations for the representation of a polynomial with integer coefficients are typedef struct nodetype { int coeff; int power; struct nodetype *next; }node; node *poly;
  • 71.  Examples a x x= + +3 2 114 8 b x x x= − +8 3 1014 10 6 3 14 2 8 1 0 a null 8 14 -3 10 10 6 b null Polynomial Representation: Example
  • 72.  Adding Polynomials: Figure 4:19 c = a + b 4.6.2 Adding Polynomials: c = a + b Case 1: p->exp = q->exp 3 14 2 8 1 0 p 8 14 -3 10 10 6 q 11 14 a b c
  • 73. Adding Polynomials : c = a + b (cont.) Case 2: p->exp < q->exp 3 14 2 8 1 0 p 8 14 -3 10 10 6 q 11 14 -3 10 a b c
  • 74. 3 14 2 8 1 0 p 8 14 -3 10 10 6 q 11 14 -3 10 2 8 Case 3: p->exp > q->exp Adding Polynomials: c = a + b (cont.)
  • 75. Representing sparse matrices • inadequate of sequential schemes (1) # of nonzero terms will vary after some matrix computation (2) matrix just represents intermediate results • new scheme • Each column (row): a circular linked list with a head node 0 0 0 2 0 0 0 0 1 0 0 5 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 A 5X6 sparse matrices
  • 76. Sparse Matrix The description of this representation is as • It contains one header node that has four fields…. --#of rows --#of cols --#of non zero elements --head i.e. pointer to 1st row containing at least one non zero element. • A linked list of rows containing at least one non zero term, in the ascending order of their row values. each node of this list has three fields --row (row number for corresponding row list) --next (pointer to next node in the row list) --first (a pointer to first column in a row having non zero item) • A linked list of columns containing nonzero terms, in the ascending order of their column values. Each node of this list has three fields --col (column number for corresponding row list) --term ( a non zero value in column col) --link (a pointer to next column having non zero element)
  • 77. Sparse Matrix 5 6 5 1 4 2 X 2 3 1 3 2 4 X 5 X 5 7 X 6 5 X A linked representation of sparse matrix
  • 78. Required memory declaration Structure of column node: typedef struct columnnodetype { int col; float element; struct columnnodetype *link; }columnnode;
  • 79. Required memory declaration Structure of row node: typedef struct rownodetype { int row; struct rownodetype *next; struct columnnode *first; }rownode;
  • 80. Required memory declaration Structure of header node: typedef struct headernodetype { int nrow; int ncol; int num; struct rownode *head; }headernode;
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94. Linked list Problems 1. Write a function that return value 1 if the linear linked list is sorted in the ascending order otherwise it returns value 0. 2. Write a function that makes copies of given linear linked list. 3. Write a function that merge two sorted linear linked list. 4. Write a function that sorts a linear linked list of integer values in the descending order.
  • 95. Josephus Problem It consists of a group of soldiers surrounded by a heavy enemy force. There is only one horse to escape. Therefore, only one soldier can escape. In order to determine which soldier will escape, they form a circle and pick up a number n from a hat. A name is also picked up from the hat. They start counting clockwise around the circle from a soldier whose name is picked up from the hat. And the soldier on which count reaches n is removed from the circle. And the count again starts from the soldier who was next to the soldier who is removed from the circle. This process goes on till only one soldier is left. This soldier will take the horse and escapes. Write a program to solve this problem. Input to your program is list of names of soldiers and number n and the output should be the name of the soldier left.
  • 96. Josephus Problem #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> struct node { char info[25]; struct node *NEXT; }; struct node *HEAD; struct node *TAIL; char name[25]; void myInit() { HEAD=(struct node *) malloc(sizeof(struct node)); TAIL=(struct node *) malloc(sizeof(struct node)); HEAD->NEXT=TAIL; TAIL->NEXT=HEAD; }
  • 97. Josephus Problem void myInsert() { struct node *TEMP; TEMP=(struct node*)malloc(sizeof(struc t node)); strcpy(TEMP->info,name); TEMP->NEXT=HEAD- >NEXT; HEAD->NEXT=TEMP; } void myRemove() { struct node *TEMP; TEMP=(struct node*) malloc(sizeof(struct node)); TEMP=HEAD->NEXT; HEAD->NEXT=HEAD- >NEXT->NEXT; strcpy(name,TEMP->info); }
  • 98. Josephus Problem void main() { clrscr(); int numSol, counter; myInit(); printf("Enter the number of soldier: "); scanf("%d",&numSol); for(int i=1;i<=numSol; i++) { printf("Pls. Type a Name: "); scanf("%s",&name); myInsert(); } printf("Value for Counter: "); scanf("%d",&counter); for(int j=1;j<=numSol;j++) { for(int k=1;k<=counter;k++) { myRemove(); if(k==counter) { puts(name); getch(); } } } }