Linked List

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Mr.

Abhijit Sarkar
Asst. Professor
Dept. of CSE (Cyber Security)
Haldia Institute of Technology
 Static Memory Allocation:- Memory is allocated during the compile time of a program.
(Ex: Array)

 Dynamic Memory Allocation:- Memory space for any variable is allocated at the time of
execution(runtime) of the program.
(Ex: Linked List)
There are 4 library functions provided by C defined under <stdlib.h> header file to
facilitate dynamic memory allocation in C programming.
They are:

 malloc()
 calloc()
 realloc()
 free()
malloc method:-
 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.

Syntax:-
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the pointer ptr
holds the address of the first byte in the allocated memory. If space is insufficient, allocation fails and
returns a NULL pointer.
calloc() method:-
 “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 parameters which is number of elements and size of each element.
 It initializes each block with a default value „0‟.
Syntax:
ptr=(cast-type*)calloc(n,element-size);
n is the number of elements and element-size is the size of each element.
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size of the float. If
space is insufficient, allocation fails and returns a NULL pointer.
ptr = realloc(ptr, newSize); where ptr is reallocated with new size 'newSize'

Realloc method:-
“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.
Syntax:
ptr=realloc(ptr, newsize);
where ptr is reallocated with updated size ‘newsize’.
If space is insufficient, allocation fails and returns a NULL pointer.
ptr = realloc(ptr, newSize); where ptr is reallocated with new size 'newSize'

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);
 It‟s a special list of some data elements linked to one another.
 The logical ordering is represented by having each element pointing to the next
element.
 Each element is called a node which has 2 parts:- INFO part which stores the
information and POINTER part which points to the next element.
 The last node in a linked list holds a special information NULL at its address part.
Advantage:-
 Dynamic data structure.
 Efficient memory allocation.
 Insertion and deletion are easier and efficient

Disadvantage:-
 If the number of fields are more, more memory is needed.
 Accessing a data item is time consuming.
 All nodes are linked together in some sequential manner.
 It has the beginning and the end,
 Nodes are traversed either from left to right in general.
 We cannot access the predecessor of node from the current node.
Structure definition and head creation:-
struct node
{
int info;
struct node *next; //Pointer to node
};
typedef struct node NODE; //typedef making it ADT and giving alias
NODE *start=NULL; //Initially there is a start pointer/node without any
information
Insert a Node at beginning:-
 Allocate memory for new node.
 Assign value to the data/INFO field of new node.
 Make the link field of new node to point to the starting node.
 Set the external pointer(start/Head) to point to the new node.
Insert a Node at beginning:-
void insertatbegin(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
if(start==NULL)
ptr->next=NULL;
else
ptr->next=start;
start=ptr;
}
Insert a Node at the end:-
 If the list is empty, create the new node.
 If the list is not empty, then go/traverse to the last node and then insert the new
node after the last node.
 Make the link field of new node to point to NULL.
Insert a Node at the end:-
void insertatend(int item)
{
NODE *ptr,*loc;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=NULL;
if(start==NULL)
start=ptr;
else
loc=start;
while(loc->next !=NULL)
loc=loc->next;
loc->next=ptr;
}
Insert a Node at specified position:-
 Allocate memory for the new node.
 Assign value to the data field of the new node.
 Make the link field of the new node to point to node B.
 Make the link field of node A to point to new node.
Insert a Node at specified position:-
void insert_spec()
{
NODE *ptr,*loc;
int temp,item;
printf(“Enter item:”);
scanf(“%d”,&item);
printf(“After which element”);
scanf(“%d”,&temp);
loc=start;
while(loc->info != temp)
{
loc=loc->next;
}
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=loc->next;
loc->next=ptr;
return;
}
Deleting the First Node:-
If the list is not empty then check if element belongs to the first node.
Move the start pointer to the second node.
Free the first node.
Deleting the First Node:-
void dele_beg(void)
{
NODE *ptr;
if(start==NULL)
{
printf(“Empty list\n”);
}
else
{
ptr=start;
start=start->next;
printf(“%d”,ptr->info);
free(ptr);
}
}
Deleting the Last Node:-
If the linked list is not empty, go on traversing the list till the last
node.
Set the link field of the last node‟s previous node to NULL.
Free the last node.
Deleting the Last Node:-
Deleting Node from specific position:-
If the linked list is not empty, then make the link field of Node A to
point to Node B.
Free the node between Node A and Node B.
Deleting Node from specific position:-
void dele_spe() else
{ {
NODE *ptr,*loc; loc=ptr;
int temp; ptr=ptr->next;
printf("Which element?"); while(ptr!=NULL)
scanf("%d",&temp); {
ptr=start; if(ptr->info==temp)
if(start==NULL)
{
{
printf("Empty list");
loc->next=ptr->next;
} printf("Deleted Item: %d",ptr->info);
else if(ptr->info==temp) free(ptr);
{ return;
start=ptr->next; }
printf("Deleted Item: %d",ptr->info); loc=ptr;
free(ptr); ptr=ptr->next;
} }
}
}
Element Definition of Stack:-
struct stack
{
int no;
struct stack *next;
};
typedef struct stack S;
S *top=NULL;
Algorithm for Push( ) using Linked List:- Algorithm for Pop( ) using Linked List:-

void pop()
void push()
{
{ S *temp;
S *node; temp=top;
node=(S *)malloc(sizeof(S)); if(top==NULL)
printf("Enter the element: "); {
printf("Stack Empty");
scanf("%d",&node->no);
}
node->next=top;
else{
top=node; top=top->next;
} printf("Deleted element:%d",temp->no);
free(temp);
}
}
Structure Definition of Queue Element:-

struct queue
{
int no;
struct queue *next;
};
typedef struct queue Q;
Q *start=NULL;
Algorithm for Enqueue( ) using Linked List:- Algorithm for Dequeue( ) using Linked
List:-
 It has no beginning and no end.
 Last node in the list points to the first node.

Advantages:-
 Nodes can be accessed easily.
 Deletion of nodes is easier.
Disadvantages:-
 Backward traversing is not possible.
Insert Node at beginning:-
 Allocate memory for the new node.
 Assign value to data field of new node.
 If the list is empty then set First=Rear=new node
 Otherwise, make the link field of the new node to point to the First node. Also, make the link field
of Rear node to point to new node and finally set First to new node.
Insert Node at beginning:-
void insertatbegin(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
if(first==NULL)
{
first=rear=ptr;
ptr->next=first;
}
else
{
ptr->next=first;
first=ptr;
rear->next=first;
}
}
Insert Node at the end:-
 Allocate the memory for new node.
 Assign value to the data field of new node.
 If list is empty, set first=rear=new node.
 Otherwise, make the link field of rear node to point to new node. Make the link field of new node
to point to first node. Set rear to new node.
Insert Node at the end:-
void insertatend(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
if(first==NULL)
{
first=rear=ptr;
ptr->next=first;
}
else
{
ptr->next=first;
rear->next=ptr;
rear=ptr;
}
}
Delete Node from the Beginning:-
 If the list is empty, output error.
 If first=rear, then free first node and set first=rear=NULL.
 Otherwise, make temporary pointer to point to first node and make the first to point to its next
node. Set link field of rear to point to wherever first is pointing to. Free the node pointed to by
temporary pointer.
Delete Node from the Beginning:-
void deletebeg(void)
{
NODE *ptr;
if(first==NULL)
printf("List empty");
else if(first==rear)
{
printf("Deleted Item:%d",first->info);
ptr=first;
free(ptr);
first=rear=NULL;
}
else
{
ptr=first;
first=first->next;
rear->next=first;
free(ptr);
}
}
Delete Node from the End:-
 If the list is empty, display “Empty List”.
 If first=rear, then free the first node and set first=rear=NULL.
 Otherwise, go on traversing till the rear but one node and set its link field to point back to first
node. Then, free the rear node.
Delete Node from the End:-
 A Doubly Linked List contains an extra pointer, typically called previous pointer,
together with next pointer and information part.

Advantages:-
 Insertion and deletion are simple.
 Efficient utilization of memory.
 Bi-directional traversal helps in efficient and easy accessibility of nodes.
Disadvantages:-
 Uses two pointers, which results in more memory space requirement.
Insert Node at beginning:-
 Allocate memory for the new node.
 Assign value to the data field of the new node.
 Assign left links to NULL.
 Make the right link of new node to point to the head node of the list, make left link of the head
node to point to new node.
 Finally, reset the head pointer. Make it point to the new node which has been inserted at
beginning.
Insert Node at beginning:-
void insertatbegin(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
if(head==NULL)
{
ptr->prev=ptr->next=NULL;
head=tail=ptr;
}
else
{
ptr->prev=NULL;
ptr->next=head;
head->prev=ptr;
head=ptr;
}
}
Insert Node at the End:-
 Allocate memory for the new node and assign value to the data field of the new node.
 Set right links of the new node to NULL.
 If the list is not empty, then traverse till the last and make the right link of last node to point to the
new node and left link of the new node to point to the last node.
 Finally, reset the tail pointer. Make it point to the new node which has been inserted at end.
Insert Node at the End:-
void insertatend(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
if(tail==NULL)
{
ptr->prev=ptr->next=NULL;
head=tail=ptr;
}
else{
ptr->next=NULL;
ptr->prev=tail;
tail->next=ptr;
tail=ptr;
}
}
Deleting Node from Beginning:-
 If the list is empty, then deletion not possible.
 Otherwise, make the head pointer to point to the second node and if the second node is not NULL
then make its left link to point to NULL.
 Free the first node.
Deleting Node from Beginning:-
void deletebeg(void)
{
NODE *ptr;
if(head==NULL)
return;
else if(head->next==NULL)
{
ptr=head;
head=tail=NULL;
free(ptr);
}
else{
ptr=head;
head=head->next;
head->prev=NULL;
free(ptr);
}
}
Deleting Node from the End:-
 If the list is empty, then deletion not possible.
 Otherwise, make the last but one node the new tail.
 Make the right link of the last but one node to point to NULL.
 Free the last node.
Deleting Node from the End:-
void deleteend(void)
{
NODE *ptr;
if(tail==NULL)
return;
else if(tail->prev==NULL)
{
ptr=tail;
head=tail=NULL;
free(ptr);
}
else
{
ptr=tail;
tail=tail->prev;
tail->next=NULL;
free(ptr);
}
}

You might also like