Department of CSE and IT, JUIT Waknaghat
Practical No.3
________________________________________________________________________
AIM: Getting acquainted with Linked list ( Linear Linked List).
OBJECTIVE
The goal of this lab is to help you become acquainted with how to create linked list, insert
elements into linked list, traverse the linked list, delete elements from the 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 List:
• 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.
Department of CSE and IT, JUIT Waknaghat
• 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
Department of CSE and IT, JUIT Waknaghat
• 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(“%d\n”,head->info);
head=head->next;
}
}
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 sack (LIFO) or recursion.
Void traversereverseorder(node *head)
{
if(head->next!=NULL)
{
traversereverseorder(head->next);
printf(“%d\n”,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
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;
}
Department of CSE and IT, JUIT Waknaghat
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
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.
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 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
Department of CSE and IT, JUIT Waknaghat
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.
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 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.
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)
Department of CSE and IT, JUIT Waknaghat
• 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 list
Void 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:
Department of CSE and IT, JUIT Waknaghat
• 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);
}
}
STUDENT TASKS:
1. WAP to implement the Linear linked list. Perform the following operations on the
linked list:
Addition of numbers after a particular location.
Deletion of the number given by the user.
Counting the no of nodes.
Displaying the linked list.
Adding the numbers at the beginning of the linked list.
Appending the numbers given by the users.
2.WAP for reversing the linked list.
Department of CSE and IT, JUIT Waknaghat
Practical No. 4
________________________________________________________________________
AIM: Getting acquainted with Linked list ( Linear Linked List).
OBJECTIVE
The goal of this lab is to help you become acquainted with how to create linked list, insert
elements into linked list, traverse the linked list, delete elements from the linked list.
STUDENT TASKS:
1. Two linked lists are given as
A: 1, 2, 3, 4, 5, 6
B: 7, 8, 9, 10, 11, 12
Make a third linked list so that it is in the following order.
C: 1,7,2,8,…..,6,12
2. WAP for printing the common elements in two given linked lists.
3. WAP that traverse a list implemented using linked list and delete all nodes whose keys
are negative.
4. A linked list contains some positive numbers and some negative numbers. Use this
linked list to WAP for creating two more linked list, one containing all positive no and
other all negative nos.