VARDHAMAN COLLEGE OF ENGINEERING
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO 9001:2015 Certified
Kacharam, Shamshabad - 501 218, Hyderabad, Telangana, India
Department of Computer Science and Engineering (Data Science)
A8505 –DATA STRUCTURE
Module-II
Dynamic Memory Allocation
The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at
runtime. Dynamic memory allocation in c language is possible by 4 functions of <stdlib.h> header file.
I. malloc()
II. calloc()
III. realloc()
IV. free()
Difference between static memory allocation and dynamic memory allocation
Static Memory Allocation Dynamic Memory Allocation
Memory is allocated at compile time, before Memory is allocated at run time, after compilation.
execution.
Memory can’t be increased while executing program. Memory can be increased while executing program.
Insertion is not always possible Insertion is always possible
Deletion of causes wastage of memory No wastage of memory
Used in array. Used in linked list.
Dynamic Memory Allocation functions:
malloc() Allocates single block of requested memory.
calloc() allocates multiple blocks of requested memory.
realloc() reallocates the memory occupied by malloc() or calloc() functions.
free() frees the dynamically allocated memory.
malloc() function in C:
❖ The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of
memory with the specified size.
❖ It returns a pointer of type void which can be cast into a pointer of any form.
❖ It doesn’t initialize memory at execution time so that it has initialized each block with the default garbage
value initially.
❖ It returns NULL if memory is not sufficient.
Syntax:
ptr=(data_type*)malloc(n*sizeof(data_type));
Example:
ptr = (int*) malloc(5 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 20 bytes of memory. And, the pointer ptr holds the address
of the first byte in the allocated memory.
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 1
#include <stdio.h>
#include <stdlib.h>
void main()
{ // This pointer will hold the base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the array
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(n * sizeof(int));
// Get the elements of the array
for (i = 0; i < n; ++i)
scanf(“%d”,ptr+ i);
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
}
Output:
Enter number of elements: 5
The elements of the array are: 1, 2, 3, 4, 5,
calloc() :
❖ “calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of
blocks of memory of the specified type.
❖ it is very much similar to malloc() but has two different points and these are:
❖ It initializes each block with a default value ‘0’.
❖ It has two parameters or arguments as compare to malloc().
Syntax :
ptr=(cast-type*)calloc(number, byte-size);
here, n is the no. of elements and element-size is the size of each element.
Example:
ptr = (int*) calloc(5, sizeof(int));
This statement allocates contiguous space in memory for 5 elements each with the size of the int ( i. e 4 bytes).
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 2
#include <stdio.h>
#include <stdlib.h>
void main()
{ // This pointer will hold the base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the array
n = 5;
printf("Enter number of elements: %d\n", n);
// Dynamically allocate memory using calloc()
ptr = (int*)calloc(n, sizeof(int));
// Check if the memory has been successfully allocated by calloc() or not
// Memory has been successfully allocated
printf("Memory successfully allocated using calloc()\n");
// Get the elements of the array
for (i = 0; i < n; ++i)
scanf(“%d”,ptr+i);
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", *(ptr+i));
}
Output:
Enter number of elements: 5
The elements of the array are: 1, 2, 3, 4, 5,
realloc() :
❖ “realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of a
previously allocated memory.
❖ In other words, if the memory previously allocated with the help of
malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory.
❖ re-allocation of memory maintains the already present value and new blocks will be initialized with the
default garbage value.
Syntax:
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 3
If space is insufficient, allocation fails and returns a NULL pointer.
#include <stdio.h>
#include <stdlib.h>
void main()
{ // This pointer will hold the
// base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the array
n = 5;
printf("Enter number of elements: %d\n", n);
// Dynamically allocate memory using calloc()
ptr = (int*)calloc(n, sizeof(int));
// Get the elements of the array
for (i = 0; i < n; ++i) {
scanf(“%d”,ptr+i);
}
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", *(ptr+i));
// Get the new size for the array
n = 10;
printf("\n\nEnter the new size of the array: %d\n", n);
// Dynamically re-allocate memory using realloc()
ptr = realloc(ptr, n * sizeof(int));
// Memory has been successfully allocated
printf("Memory successfully re-allocated using realloc.\n");
// Get the new elements of the array
for (i = 5; i < n; ++i) {
scanf(“%d”,ptr+i);
}
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", *(ptr+i));
}
free(ptr);
}
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 4
Output:
Enter number of elements: 5
The elements of the array are: 1, 2, 3, 4, 5,
Enter the new size of the array: 10
Memory successfully re-allocated using realloc. The elements of the array are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
free() method:
❖ “free” method in C is used to dynamically de-allocate the memory.
❖ The memory allocated using functions malloc() and calloc() is not
de-allocated on their own.
❖ Hence the free() method is used, whenever the dynamic memory
allocation takes place.
❖ It helps to reduce wastage of memory by freeing it.
Syntax:
free(ptr);
#include <stdio.h>
#include <stdlib.h>
int main()
{// This pointer will hold the
// base address of the block created
int *ptr, *ptr1;
int n, i;
// Get the number of elements for the array
n = 5;
printf("Enter number of elements: %d\n", n);
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(n * sizeof(int));
// Dynamically allocate memory using calloc()
ptr1 = (int*)calloc(n, sizeof(int));
// Check if the memory has been successfully
// allocated by malloc or not
if (ptr == NULL || ptr1 == NULL) {
printf("Memory not allocated.\n");
exit(0); }
else { // Memory has been successfully allocated
printf("Memory successfully allocated using malloc.\n");
// Free the memory
free(ptr);
printf("Malloc Memory successfully freed.\n");
// Memory has been successfully allocated
printf("\nMemory successfully allocated using calloc.\n");
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 5
// Free the memory
free(ptr1);
printf("Calloc Memory successfully freed.\n");
}
return 0;
}
Output:
Enter number of elements: 5
Memory successfully allocated using malloc.
Malloc Memory successfully freed.
Memory successfully allocated using calloc.
Calloc Memory successfully freed.
Self-Referential Structures
Self-Referential structures are those structures that have one or more pointers which point to the same
type of structure, as their member.
//this pogram is to implement self- referential structure
#include<stdio.h>
#include<malloc.h>
struct list
{
int data;
struct list *link;
}*first=NULL;
void create();
void display();
int count=0;
void main()
{
int choice;
while(1)
{
printf(“1.create\n2.display\n3.exit\n“);
printf(“Enter your choice: “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 6
display();
break;
case 3:
exit();
default:
printf(“Wrong choice. Try again”);
}
}
}
void create()
{
struct list *node,*current;
node=(struct list *) malloc(sizeof(struct list));
printf("enter the data to be inserted\n");
scanf("%d",&node->data);
node->link=NULL;
if(first==NULL)
first=current=node;
else
{
current->link=node;
current=node;
count++;
}
}
void display()
{
struct list *temp;
printf("The number of nodes in the Linked List are %d\n ",count);
if(first==NULL)
printf("Linked List is Empty\n");
else
{
temp=first;
printf("The Elements of Linked List are.......\n");
while(temp!=NULL)
{
printf("%3d",temp->data);
temp=temp->link;
}
printf("\n");
}
}
Linked List Concepts:
❑ A linked list is a non-sequential collection of data items. It is a dynamic data structure. For every
data item in a linked list, there is an associated pointer that would give the memory location of the
next data item in the linked list.
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 7
❑ The data items in the linked list are not in consecutive memory locations. They may be anywhere,
but the accessing of these data items is easier as each data item contains the address of the next
data item.
Advantages of linked lists:
❑ Linked lists are dynamic data structures. i.e., they can grow or shrink during the execution of a
program.
❑ Insertion and Deletions are easier and efficient. Linked lists provide flexibility in inserting a data
item at a specified position and deletion of the data item from the given position.
❑ Linked lists have efficient memory utilization. Here, memory is not pre-allocated. Memory is
allocated whenever it is required and it is de-allocated (removed) when it is no longer needed.
❑ Many complex applications can be easily carried out with linked lists.
Disadvantages of linked lists:
❑ It consumes more space because every node requires a additional pointer to store address of the
next node.
❑ Searching a particular element in list is difficult and also time consuming.
Types of Linked Lists:
1. Single Linked List
2. Double Linked List.
3. Circular Linked List.
4. Circular Double Linked List.
Single Linked List
❖ Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which
points to the next node in the sequence of nodes.
❖ The operations we can perform on singly linked lists are insertion, deletion and traversal.
Circular Doubly Linked List
❑ Circular doubly linked list is a more complexed type of data structure in which a node contain
pointers to its previous node as well as the next node.
❑ Circular doubly linked list doesn't contain NULL in any of the node.
❑ The last node of the list contains the address of the first node of the list.
❑ The first node of the list also contain address of the last node in its previous pointer.
❑ Due to the fact that a circular doubly linked list contains three parts in its structure therefore, it
demands more space per node and more expensive basic operations.
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 8
However, a circular doubly linked list provides easy manipulation of the pointers and the searching
becomes twice as efficient
Doubly Linked List
In a doubly linked list, each node contains a data part and two addresses, one for the previous node and
one for the next node.
In circular linked list the last node of the list holds the address of the first node hence forming a circular
chain.
Comparison between array and linked list:
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 9
Single Linked List:
What is a Node?
❖ A Node in a linked list holds the data value and the pointer which points to the location of the next
node in the linked list.
Single Linked List:
❑ A linked list allocates space for each element separately in its own block of memory called a
"node".
❑ The list gets an overall structure by using pointers to connect all its nodes together like the links in
a chain.
❑ Each node contains two fields; a "data" field to store whatever element, and a "next" field which is
a pointer used to link to the next node.
❑ Each node is allocated in the heap using malloc(), so the node memory continues to exist until it is
explicitly de-allocated using free(). The front of the list is a pointer to the “start” node.
Implementation of Single Linked List:
Before writing the code to build the above list, we need to create a start node, used to create and
access other nodes in the linked list. The following structure definition will do
• Creating a structure with one data item and a next pointer, this will be pointing to next node of the
list. This is called as self-referential structure.
• Initialize the start pointer to be NULL.
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 10
Figure: Structure definition, single link node and empty list
Basic operations in a single linked list
The basic operations in a single linked list are:
• Creation.
• Insertion.
• Deletion.
• Traversing.
Single linked list algorithm
create()
1. node<- create node
2. Enter no
3. Node[data]<-no
4. Node[next]<-NULL
5. if(head = NULL)
6. head <-node
7. else
8. temp<-head
9. while(temp[next] ≠ NULL)
10. temp <- temp[next]
11. temp[next]<-node
display()
1. if(head = NULL)
2. print “Empty List"
3. else
4. temp <- head
5. while(temp ≠ NULL)
6. print temp[data]
7. temp<-temp[next]
count()
1. count=0
2. temp<- head
3. if(head = NULL)
4. print “Empty List"
5. else
6. while(temp ≠ NULL)
7. count<-count+1
8. temp <- temp[next]
9. print “Number of elements in the list=count”
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 11
firstinsert()
1. Node<-create node
2. Enter no
3. node[data]<-no
4. node[next]<-head
5. head<-node
lastinsert()
1. node<-create node
2. enter no
3. node[data] <- no
4. node[next]<-NULL
5. temp<- head
6. while(temp[next] ≠ NULL)
7. temp<- temp[next]
8. temp[next]<-node
anyinsert()
1. node<-create node
2. enter no and pos
3. node[data]<-no;
4. temp <- head;
5. for i<-1 to pos-2
6. temp<-temp[next]
7. node[next]<-temp[next]
8. temp[next]<-node
firstdelete()
1. if(head = NULL)
2. print "No nodes are exist."
3. else
4. temp<-head
5. head<-temp[next]
6. free(temp)
lastdelete()
1. if(head = NULL)
2. print "Empty List."
3. else
4. temp <- head
5. while(temp[next] ≠ NULL)
6. temp1<-temp
7. temp<- temp[next]
8. temp1[next]<- NULL
9. free(temp)
anydelete()
1. if(head = NULL)
2. print "Empty List."
3. else
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 12
4. print "Enter position of node to delete: "
5. temp <- head
6. for i<- 1 to pos-2
7. temp<- temp[next]
8. temp1<- temp[next]
9. temp[next] <- temp1[next]
10. free(temp1)
C program
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
struct list
{
int data;
struct list *next;
}*head = NULL;
void create();
void display();
void count();
void firstinsert();
void lastinsert();
void anyinsert();
void firstdelete();
void lastdelete();
void anydelete();
void sort();
void reverse();
void main()
{
int choice,no,pos;
while(1)
{
printf("\n
1.Create\n2.Display\n3.count\n4.firstinsert\n5.lastinsert\n6.anyinsert\n7.firstdelete\n
8.lastdelete\n9.anydelete\n10.sort\n11.reverse\n12.exit\n”);
printf(“Enter your choice: “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
count();
break;
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 13
case 4:
firstinsert();
break;
case 5:
lastinsert();
break;
case 6:
anyinsert();
break;
case 7:
firstdelete();
break;
case 8:
lastdelete();
break;
case 9:
anydelete();
break;
case 10:
sort();
break;
case 11:
reverse();
break;
case 12:
exit(0);
default:
printf(“Wrong choice. Try again”);
}
}
}
void create()
{
struct list *node,*temp;
int no;
node=(struct list*)malloc(sizeof(struct list));
printf(“Enter a number: “);
scanf(“%d”,&no);
node->data=no;
node->next=NULL;
if(head == NULL)
head = node;
else
{
temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = node;
}
}
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 14
void display()
{
struct list *temp;
temp = head;
if(head == NULL)
printf("\n Empty List");
else
{
while(temp != NULL)
{
printf("%d-->", temp -> data);
temp = temp -> next;
}
}
}
void count()
{
struct list *temp;
int count=0;
temp = head;
if(head == NULL)
printf("\n Empty List");
else
{
while(temp != NULL)
{
count++;
temp = temp -> next;
}
printf("Number of elements in the list is %d", count);
}
}
void firstinsert()
{
struct list *node;
int no;
node=(struct list*)malloc(sizeof(struct list));
printf(“Enter a number: “);
scanf(“%d”,&no);
node->data=no;
node->next=head;
head=node;
}
void lastinsert()
{
struct list *node,*temp;
int no;
node=(struct list*)malloc(sizeof(struct list));
printf(“Enter a number: “);
scanf(“%d”,&no);
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 15
node->data=no;
node->next=NULL;
temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = node;
}
void anyinsert()
{
struct list *node,*temp;
int no,pos,i;
node=(struct list*)malloc(sizeof(struct list));
printf(“Enter a number and position: “);
scanf(“%d%d”,&no,&pos);
node->data=no;
temp=head;
for(i=1;i<=pos-2;i++)
temp=temp->next;
node->next=temp->next;
temp->next=node;
}
void firstdelete()
{
struct list *temp;
if(head == NULL)
printf("\n No nodes are exist..");
else
{
temp = head;
head = temp -> next;
free(temp);
printf("\n Node deleted ");
}
}
void lastdelete()
{
struct list node *temp,*temp1;
if(head == NULL)
printf("\n Empty List..");
else
{
temp = head;
while(temp -> next != NULL)
{
temp1=temp;
temp = temp -> next;
}
temp1 -> next = NULL;
free(temp);
printf("\n Node deleted ");
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 16
}
}
void anydelete()
{
int i;
struct list node *temp,*temp1;
if(head == NULL)
printf("\n Empty List..");
else
{
printf("\n Enter position of node to delete: ");
scanf("%d", &pos);
temp=head;
for(i=1;i<=pos-2;i++)
temp=temp->next;
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
}
}
void reverse()
{
struct list *temp,*temp1,*pre=NULL;
temp=head;
while(temp!=NULL)
{
temp1=temp->next;
temp->next=pre;
pre=temp;
temp=temp1;
}
head=pre;
}
Double linked list algorithm
create()
1. node<- create node
2. Enter no
3. Node[data]<-no
4. Node[next]<-NULL
5. if(head = NULL)
6. head <-node
7. node[prev] <- NULL
8. else
9. temp<-head
10. while(temp[next] ≠ NULL)
11. temp <- temp[next]
12. temp[next]<-node
13. node[prev] <- temp
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 17
display()
1. if(head = NULL)
2. print “Empty List"
3. else
4. temp <- head
5. while(temp ≠ NULL)
6. print temp[data]
7. temp<-temp[next]
count()
10. count=0
11. temp<- head
12. if(head = NULL)
13. print “Empty List"
14. else
15. while(temp ≠ NULL)
16. count<-count+1
17. temp <- temp[next]
18. print “Number of elements in the list=count”
firstinsert()
1. node<-create node
2. Enter no
3. node[data]<-no
4. head[prev]<- node
5. node[next]<-head
6. node[prev]<-NULL
7. head<-node
lastinsert()
1. node<-create node
2. enter no
3. node[data] <- no
4. node[next]<-NULL
5. temp<- head
6. while(temp[next] ≠ NULL)
7. temp<- temp[next]
8. temp[next]<-node
9. node[prev]<- temp
anyinsert()
1. node<-create node
2. enter no and pos
3. node[data]<-no;
4. temp <- head;
5. for i<-1 to pos-2
6. temp<-temp[next]
7. node[next]<-temp[next]
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 18
8. node[prev]<-temp
9. [temp[next]][prev]<-node
10. temp[next] <-node
firstdelete()
1. if(head = NULL)
2. print "No nodes are exist."
3. else
4. temp<-head
5. head<-temp[next]
6. head[prev]<- NULL
7. free(temp)
lastdelete()
1. if(head = NULL)
2. print "Empty List."
3. else
4. temp <- head
5. while(temp[next] ≠ NULL)
6. temp1<-temp
7. temp<- temp[next]
8. temp1[next]<- NULL
9. free(temp)
anydelete()
1. if(head = NULL)
2. print "Empty List."
3. else
4. print "Enter position of node to delete: "
5. temp <- head
6. for i<- 1 to pos-2
7. temp<- temp[next]
8. temp1<- temp[next]
9. temp1[next][prev]<-temp
10. temp[next] <- temp1[next]
11. free(temp1)
C program
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
struct list
{
int data;
struct list *next;
}*head = NULL;
void create();
void display();
void count();
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 19
void firstinsert();
void lastinsert();
void anyinsert();
void firstdelete();
void lastdelete();
void anydelete();
void main()
{
int choice,no,pos;
while(1)
{
printf("\n
1.Create\n2.Display\n3.count\n4.firstinsert\n5.lastinsert\n6.anyinsert\n7.firstdelete\n
8.lastdelete\n9.anydelete\n10.sort\n11.reverse\n12.exit\n”);
printf(“Enter your choice: “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
count();
break;
case 4:
firstinsert();
break;
case 5:
lastinsert();
break;
case 6:
anyinsert();
break;
case 7:
firstdelete();
break;
case 8:
lastdelete();
break;
case 9:
anydelete();
break;
case 10:
sort();
break;
case 11:
reverse();
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 20
break;
case 12:
exit(0);
default:
printf(“Wrong choice. Try again”);
}
}
}
void create()
{
struct list *node,*temp;
int no;
node=(struct list*)malloc(sizeof(struct list));
printf(“Enter a number: “);
scanf(“%d”,&no);
node->data=no;
node->next=NULL;
if(head == NULL)
head = node;
node->prev=NULL;
else
{
temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = node;
node->prev=temp;
}
}
void display()
{
struct list *temp;
temp = head;
if(head == NULL)
printf("\n Empty List");
else
{
while(temp != NULL)
{
printf("%d-->", temp -> data);
temp = temp -> next;
}
}
}
void count()
{
struct list *temp;
int count=0;
temp = head;
if(head == NULL)
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 21
printf("\n Empty List");
else
{
while(temp != NULL)
{
count++;
temp = temp -> next;
}
printf("Number of elements in the list is %d", count);
}
}
void firstinsert()
{
struct list *node;
int no;
node=(struct list*)malloc(sizeof(struct list));
printf(“Enter a number: “);
scanf(“%d”,&no);
node->data=no;
head->prev=node;
node->next=head;
node->prev=NULL
head=node;
}
void lastinsert()
{
struct list *node,*temp;
int no;
node=(struct list*)malloc(sizeof(struct list));
printf(“Enter a number: “);
scanf(“%d”,&no);
node->data=no;
node->next=NULL;
temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = node;
node->prev=temp;
}
void anyinsert()
{
struct list *node,*temp;
int no,pos,i;
node=(struct list*)malloc(sizeof(struct list));
printf(“Enter a number and position: “);
scanf(“%d%d”,&no,&pos);
node->data=no;
temp=head;
for(i=1;i<=pos-2;i++)
temp=temp->next;
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 22
node->next=temp->next;
node->prev=temp;
node->next->prev=node;
temp->next=node;
}
void firstdelete()
{
struct list *temp;
if(head == NULL)
printf("\n No nodes are exist..");
else
{
temp = head;
head = temp -> next;
head->prev=NULL;
free(temp);
printf("\n Node deleted ");
}
}
void lastdelete()
{
struct list node *temp,*temp1;
if(head == NULL)
printf("\n Empty List..");
else
{
temp = head;
while(temp -> next != NULL)
{
temp1=temp;
temp = temp -> next;
}
temp1 -> next = NULL;
free(temp);
printf("\n Node deleted ");
}
}
void anydelete()
{
int i;
struct list node *temp,*temp1;
if(head == NULL)
printf("\n Empty List..");
else
{
printf("\n Enter position of node to delete: ");
scanf("%d", &pos);
temp=head;
for(i=1;i<=pos-2;i++)
temp=temp->next;
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 23
temp1=temp->next;
temp1->next->prev=temp
temp->next=temp1->next;
free(temp1);
}
}
Mr. Bijaya Kumar Sethi, Assistant Professor Page | 24