0% found this document useful (0 votes)
31 views

Data Structures

The document discusses dynamic memory allocation and linked lists. It explains the advantages of dynamic allocation and techniques like malloc, calloc and realloc. It also defines linked lists and their types and operations like insertion, deletion and searching.

Uploaded by

hrishabhjoshi123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Data Structures

The document discusses dynamic memory allocation and linked lists. It explains the advantages of dynamic allocation and techniques like malloc, calloc and realloc. It also defines linked lists and their types and operations like insertion, deletion and searching.

Uploaded by

hrishabhjoshi123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

DATA STRUCTURES AND ALGORITHMS

USING C

UNIT II

Swati Jain, Assistant Professor, VSIT, VIPS - TC


DYNAMIC MEMORY ALLOCATION

• Dynamic memory allocation is a fundamental concept in data structures and programming.


It allows programs to allocate memory at runtime, providing flexibility and
efficiency when working with data structures of varying sizes.
• In most programming languages, including C++, memory can be classified into two
categories: stack memory and heap memory.
• Local variables and function calls are stored in the stack memory, whereas the more
adaptable heap memory can be allocated and released at runtime.
• The process of allocating and releasing memory from the heap is known as dynamic
memory allocation. It allows the programmer to manage memory explicitly, providing
the ability to create data structures of varying sizes and adjust memory requirements
dynamically.

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DYNAMIC MEMORY ALLOCATION

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DYNAMIC MEMORY ALLOCATION

• Reasons and Advantage of allocating memory dynamically:

• When we do not know how much amount of memory would be needed for the program
beforehand.
• When we want data structures without any upper limit of memory space.
• When you want to use your memory space more efficiently. Example: If you have
allocated memory space for a 1D array as array[20] and you end up using only 10
memory spaces then the remaining 10 memory spaces would be wasted and this wasted
memory cannot even be utilized by other program variables.
• Dynamically created lists insertions and deletions can be done very easily just by
the manipulation of addresses whereas in case of statically allocated memory
insertions and deletions lead to more movements and wastage of memory.
• When you want you to use the concept of structures and linked list in programming,
dynamic memory allocation is a must
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
DYNAMIC MEMORY ALLOCATION TECHNIQUES:

• 1. malloc:
• The malloc (memory allocation) function is used to allocate a specified number of
bytes in memory.
• When memory is allocated successfully, it returns a pointer to that block, otherwise
it returns NULL.
• By dividing the necessary number of elements by each one's individual size, the
block's size is calculated.
• For example:
• int* ptr = (int*) malloc(5 * sizeof(int)); // Allocates memory for 5 integers

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DYNAMIC MEMORY ALLOCATION TECHNIQUES:

• 2. 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().
• int* ptr = (int*) calloc(5, sizeof(int)); // Allocates memory for an
array of 5 integers

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DYNAMIC MEMORY ALLOCATION TECHNIQUES:

• 3. 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.
• ptr = realloc(ptr, newSize); // where
ptr is reallocated with new size 'newSize'.
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
DYNAMIC VS STATIC MEMORY ALLOCATION

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DYNAMIC VS STATIC MEMORY ALLOCATION

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
LINKED LIST

• A linked list is a linear data structure, in which the elements are not stored at
contiguous memory locations.
• In simple words, a linked list consists of nodes where each node contains a data
field and a reference(link) to the next node in the list.
• The elements in a linked list are linked using pointers as shown in the below image:

• It is basically chains of nodes, each node contains information such as data and a pointer to
the next node in the chain. In the linked list there is a head pointer, which points to the
first element of the linked list, and if the list is empty then it simply points to null or
nothing.
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
LINKED LIST

• Why linked list data structure needed?


• Here are a few advantages of a linked list that is listed below, it will help you
understand why it is necessary to know.
• Dynamic Data structure: The size of memory can be allocated or de-allocated at run
time based on the operation insertion or deletion.
• Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than
arrays since no elements need to be shifted after insertion and deletion, Just the
address needed to be updated.
• Efficient Memory Utilization: As we know Linked List is a dynamic data structure the
size increases or decreases as per the requirement so this avoids the wastage of
memory.
• Implementation: Various advanced data structures can be implemented using a linked
list like a stack, queue, graph, hash maps, etc.

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
Swati Jain, Assistant Professor, VSIT, VIPS - TC
Swati Jain, Assistant Professor, VSIT, VIPS - TC
LINKED LIST

• Types of linked lists:


• There are mainly three types of linked lists:
• Single-linked list
• Double linked list
• Circular linked list

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST

• Traversal of items can be done in the forward direction only due to the
linking of every node to its next node.

// A Single linked list node


struct Node {
int data;
struct Node* next;
};
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
DOUBLY LINKED LIST

• Traversal of items can be done in both forward and backward directions


as every node contains an additional prev pointer that points to the
previous node.

/* Node of a doubly linked list */


struct Node {
int data;
struct Node* next; // Pointer to next node in DLL
struct Node* prev; // Pointer to previous node in
DLL
}; Swati Jain, Assistant Professor, VSIT,
VIPS - TC
CIRCULAR LINKED LIST

• A circular linked list is a type of linked list in which the first and
the last nodes are also connected to each other to form a circle, there
is no NULL at the end.

// A Single linked list node


struct Node {
int data;
struct Node* next;
};
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
OPERATIONS ON LINKED LIST:

• The following operations are performed on a Single Linked List


• Insertion: The insertion operation can be performed in three ways. They are as
follows…
• Inserting At the Beginning of the list
• Inserting At End of the list
• Inserting At Specific location in the list
• Deletion: The deletion operation can be performed in three ways. They are as
follows…
• Deleting from the Beginning of the list
• Deleting from the End of the list
• Deleting a Specific Node
• Search: It is a process of determining and retrieving a specific node either from the
front, the end or anywhere in the list.
• Display: This process displays the elements of a Single-linked list.
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
LINKED LIST - INSERTION OPERATION

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
LINKED LIST - INSERTION OPERATION

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
LINKED LIST - INSERTION OPERATION

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- INSERTION AT BEGINNING

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- INSERTION AT BEGINNING

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- INSERTION AT BEGINNING
#include<stdio.h>
#include<stdlib.h>
void beginsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
beginsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
} Swati Jain, Assistant Professor, VSIT,
VIPS - TC
void beginsert(int item)
SINGLY LINKED LIST- INSERTION AT END

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- INSERTION AT END

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- INSERTION AT END

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- INSERTION AT END
#include<stdio.h>
#include<stdlib.h>
void lastinsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
lastinsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void lastinsert(int item)
{
struct node *ptr = (struct node*)malloc(sizeof(struct node));
struct node *temp;
if(ptr == NULL)
{ Swati Jain, Assistant Professor, VSIT,
printf("\nOVERFLOW"); VIPS - TC
SINGLY LINKED LIST- INSERTION AT SPECIFIC POSITION

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- INSERTION AT SPECIFIC POSITION

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- INSERTION AT SPECIFIC POSITION

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- INSERTION AT END
#include<stdio.h>
#include<stdlib.h>
void randominsert(int);
void create(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item,loc;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
if(head == NULL)
{
create(item);
}
else
{
randominsert(item);
} Swati Jain, Assistant Professor, VSIT,
printf("\nPress 0 to insert more ?\n"); VIPS - TC
SINGLY LINKED LIST- DELETION AT THE BEGINNING

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- DELETION AT THE BEGINNING

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- DELETION AT BEGINNING

void begdelete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from the begining ...");
}
}

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- DELETION AT THE END

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- DELETION AT THE END

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- DELETION AT THE END

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- DELETION AT END
void end_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...");
}

else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
} Swati Jain, Assistant Professor, VSIT,
VIPS - TC
ptr1->next = NULL;
SINGLY LINKED LIST- DELETION AT A SPECIFIC NODE

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- DELETION AT A SPECIFIC NODE

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST- DELETION AT A SPECIFIC NODE
void delete_specified()
{
struct node *ptr, *ptr1;
int loc,i;
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;

if(ptr == NULL)
{
printf("\nThere are less than %d elements in the list..\n",loc);
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted %d node ",loc);
}
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
TRAVERSING IN SINGLY LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
TRAVERSING IN SINGLY LINKED LIST

void traverse()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Empty list..");
}
else
{
printf("printing values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SEARCHING IN SINGLY LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SEARCHING IN SINGLY LINKED LIST

void search()
{
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
REVERSE A LINKED LIST

Follow the steps below to solve the problem:

Initialize three pointers prev as NULL, curr as


head, and next as NULL.
Iterate through the linked list.
In a loop, do the following:
Before changing the next of curr, store the next
node
next = curr -> next
Now update the next pointer of curr to the prev
curr -> next = prev
Update prev as curr and curr as next
prev = curr
curr = next

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
REVERSING A SINGLY LINKED LIST

// Function to reverse the linked list


static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {
// Store next
next = current->next;

// Reverse current node's pointer


current->next = prev;

// Move pointers one position ahead.


prev = current;
current = next;
}
*head_ref = prev;
}
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
SORT A SINGLY LINKED LIST
Sort Linked List using Bubble Sort

• Get the Linked List to be sorted


• Apply Bubble Sort to this linked list, in which, while comparing the two adjacent nodes,
actual nodes are swapped instead of just swapping the data.
• Print the sorted list

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SORT A SINGLY LINKED LIST
/*Function to swap the nodes */
struct Node* swap(struct Node* ptr1, struct Node* ptr2)
{
struct Node* tmp = ptr2->next;
ptr2->next = ptr1;
ptr1->next = tmp;
return ptr2;
}

/* Function to sort the list */


int bubbleSort(struct Node** head, int count)
{
struct Node** h;
int i, j, swapped;

for (i = 0; i <= count; i++) {

h = head;
swapped = 0;

for (j = 0; j < count - i - 1; j++) {


Swati Jain, Assistant Professor, VSIT,
VIPS - TC
LINKED LIST IN C: MENU DRIVEN PROGRAM

Singly Linked List Menu Driven Program Link

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
SINGLY LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DOUBLY LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DOUBLY LINKED LIST
/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;

/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));

/* Assign data values */


one->data = 1;
two->data = 2;
three->data = 3;

/* Connect nodes */
one->next = two;
one->prev = NULL;

two->next = three; Swati Jain, Assistant Professor, VSIT,


two->prev = one; VIPS - TC
DOUBLY LINKED LIST: INSERTION AT THE BEGINNING

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DOUBLY LINKED LIST: INSERTION AT THE BEGINNING

Let's add a node with value 6 at the beginning of the doubly linked list we made before.
1. Create a new node
•allocate memory for newNode
•assign the data to newNode.

2. Set prev and next pointers of new node


•point next of newNode to the first node of the doubly linked list
•point prev to null

3. Make new node as head node


•Point prev of the first node to newNode (now the previous head is the second node)
•Point head to newNode

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DOUBLY LINKED LIST: INSERTION AT THE BEGINNING
// insert node at the front
void insertFront(struct Node** head, int data) {

// allocate memory for newNode


struct Node* newNode = new Node;

// assign data to newNode


newNode->data = data;

// point next of newNode to the first node of the doubly linked list
newNode->next = (*head);

// point prev to NULL


newNode->prev = NULL;

// point previous of the first node (now first node is the second node) to newNode
if ((*head) != NULL)
(*head)->prev = newNode;

// head points to newNode


(*head) = newNode;
} Swati Jain, Assistant Professor, VSIT,
VIPS - TC
DOUBLY LINKED LIST: INSERTION IN BETWEEN NODES

Let's add a node with value 6 after node with value 1 in the doubly linked list.
1. Create a new node
•allocate memory for newNode
•assign the data to newNode.

2. Set the next pointer of new node and previous node


•assign the value of next from previous node to the next of newNode
•assign the address of newNode to the next of previous node

3. Set the prev pointer of new node and the next node
•assign the value of prev of next node to the prev of newNode
•assign the address of newNode to the prev of next node

The final doubly linked list is after this insertion is:

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
void insertAfter(struct Node* prev_node, int data) {
DOUBLY LINKED LIST: INSERTION IN BETWEEN NODES
// check if previous node is NULL
if (prev_node == NULL) {
cout << "previous node cannot be NULL";
return;
}

// allocate memory for newNode


struct Node* newNode = new Node;

// assign data to newNode


newNode->data = data;

// set next of newNode to next of prev node


newNode->next = prev_node->next;

// set next of prev node to newNode


prev_node->next = newNode;

// set prev of newNode to the previous node


newNode->prev = prev_node;

// set prev of newNode's next to newNode


if (newNode->next != NULL)
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
DOUBLY LINKED LIST: INSERTION AT THE END

Let's add a node with value 6 after node with value 1 at the end of doubly linked list.
1. Create a new node
•allocate memory for newNode
•assign the data to newNode.

2. Set the next pointer of new node and previous node


•If the linked list is empty, make the newNode as the head node.
•Otherwise, traverse to the end of the doubly linked list and insert

The final doubly linked list is after this insertion is:

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
// assign data to newNode
DOUBLY LINKED LIST: INSERTION AT THE END
newNode->data = data;

// assign NULL to next of newNode


newNode->next = NULL;

// store the head node temporarily (for later use)


struct Node* temp;
temp= head;

// if the linked list is empty, make the newNode as head node


if (head == NULL) {
newNode->prev = NULL;
head = newNode;
return;
}

// if the linked list is not empty, traverse to the end of the linked list
while (temp->next != NULL)
temp = temp->next;

// now, the last node of the linked list is temp

// point the next of the last node (temp) to newNode.


Swati Jain, Assistant Professor, VSIT,
VIPS - TC
DOUBLY LINKED LIST: DELETION AT THE START

If the node to be deleted (i.e. del_node) is at the beginning,


Reset value node after the del_node (i.e. node two)
Reset the value of Head

Finally, free the memory of del_node. And, the linked will look like this

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DOUBLY LINKED LIST: DELETION AT THE START

if (head == del_node)
head = del_node->next;

if (del_node->prev != NULL)
del_node->prev->next = del_node->next;

free(del_node);

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DOUBLY LINKED LIST: DELETION OF AN INNER NODE

If del_node is an inner node (second node), we must have to reset the value of next and prev of the
nodes before and after the del_node.
For the node before the del_node (i.e. first node)
Assign the value of next of del_node to the next of the first node.
For the node after the del_node (i.e. third node)
Assign the value of prev of del_node to the prev of the third node.

Finally, we will free the memory of del_node. And, the final doubly linked list looks like this.

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DOUBLY LINKED LIST: DELETION OF AN INNER NODE

if (del_node->next != NULL)
del_node->next->prev = del_node->prev;

if (del_node->prev != NULL)
del_node->prev->next = del_node->next;

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DOUBLY LINKED LIST: DELETION OF THE LAST NODE

In this case, we are deleting the last node with value 3 of the doubly linked list.
Here, we can simply delete the del_node and make the next of node before del_node point
to NULL.

The final doubly linked list looks like this.

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DOUBLY LINKED LIST: DELETION OF THE LAST NODE

if (del_node->prev != NULL)
del_node->prev->next = del_node->next;

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
void traverse(){
DOUBLY LINKED LIST: TRAVERSE
// List is empty
// just return
if (start == NULL) {
printf("\nList is empty\n");
return;
}
// Else print the Node's Data
struct node* temp;
temp = start;
while (temp != NULL) {
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
Swati Jain, Assistant Professor, VSIT,
VIPS - TC
DOUBLY LINKED LIST: ALL OPERATIONS

Refer to Doubly List Program file in notes

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
CIRCULAR LINKED LIST

• A circular linked list is a type of linked list in which the first and the last nodes are also
connected to each other to form a circle.
• There are basically two types of circular linked list:
• 1. Circular Singly Linked List
• Here, the address of the last node consists of the address of the first node.

2. Circular Doubly Linked List


Here, in addition to the last node storing the address of the first node, the first node will also
store the address of the last node.

Note: We will be using the singly circular linked


list to represent the working of circular linked list. Swati Jain, Assistant Professor, VSIT,
VIPS - TC
REPRESENTATION OF CIRCULAR LINKED LIST

Let's see how we can represent a circular linked list on an algorithm/code. Suppose we have a
linked list:

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
REPRESENTATION OF CIRCULAR LINKED LIST
/* Initialize
Now we willnodes */ a simple circular linked list with three items to understand how
create
structworks.
this node *last;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;

/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));

/* Assign data values */


one->data = 1;
two->data = 2;
three->data = 3;

/* Connect nodes */
one->next = two;
two->next = three;
three->next = one;

/* Save address of third node in last */ Swati Jain, Assistant Professor, VSIT,
VIPS - TC
last = three;
INSERTION ON A CIRCULAR LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
INSERTION ON A CIRCULAR LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
INSERTION ON A CIRCULAR LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DELETION ON A CIRCULAR LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DELETION ON A CIRCULAR LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
DELETION ON A CIRCULAR LINKED LIST

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
C CODE TO PERFORM CIRCULAR LINKED LIST OPERATIONS
Refer to Circular List program in notes

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
HEADER LINKED LIST

• A header node is a special node that is found at the beginning of the list. A list that contains
this type of node, is called the header-linked list. T
• his type of list is useful when information other than that found in each node is needed. For
example, suppose there is an application in which the number of items in a list is often
calculated. Usually, a list is always traversed to find the length of the list. However, if the
current length is maintained in an additional header node that information can be easily
obtained.
• Types of Header Linked List:
• Grounded Header Linked List It is a list whose last node contains the NULL pointer. In the
header linked list the start pointer always points to the header node. start -> next =
NULL indicates that the grounded header linked list is empty. The operations that are
possible on this type of linked list are Insertion, Deletion, and Traversing.
• Circular Header Linked List A list in which last node points back to the header node is called
circular linked list. The chains do not indicate first or last nodes. In this case, external
pointers provide a frame of reference because last node of a circular linked list does not
contain the NULL pointer. The possible operations on this type of linked list are Insertion,
Deletion and Traversing. Swati Jain, Assistant Professor, VSIT,
VIPS - TC
HEADER LINKED LIST

Grounded Header Linked List Circular Header Linked List

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
HEADER LINKED LIST

Applications of Header Linked List


•The header linked lists are frequently used to maintain the polynomials in memory.
The header node is used to represent the zero polynomial.

•Suppose we have F(x) = 5x5 – 3x3 + 2x2 + x1 +10x0

•From the polynomial represented by F(x) it is clear that this polynomial has two
parts, coefficient and exponent, where, x is formal parameter. Hence, we can say that a
polynomial is sum of terms, each of which consists of a coefficient and an exponent.
•The computer implementation requires implementing polynomials as a list of pair of
coefficient and exponent. Each of these pairs will constitute a structure, so a polynomial will be
represented as a list of structures.
•If one wants to represent F(x) with help of linked list then the list will contain 5 nodes. When
we link each node we get a linked list structure that represents polynomial F(x).

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
HEADER LINKED LIST - POLYNOMIAL

F(x) = 5x5 – 3x3 + 2x2 + x1 +10x0

Swati Jain, Assistant Professor, VSIT,


VIPS - TC
HEADER LINKED LIST - PROGRAM
// C program for a Header Linked List
#include <malloc.h>
#include <stdio.h>

// Structure of the list


struct node {
int info;
struct node* next;
};

// Empty List
struct node* start = NULL;

// Function to create a header linked list


struct link* create_header_list(int data)
{

// Create a new node


struct node *new_node, *n;
new_node = (struct node*)
malloc(sizeof(struct node));
new_node->info = data;
new_node->next = NULL;

// If it is the first node


if (start == NULL) {
// Initialize the start Swati Jain, Assistant Professor, VSIT,
VIPS - TC
start = (struct node*)
PRACTICE QUESTIONS

• Write a program in C to create a singly linked list of n nodes and count the number of nodes.
• Write a C program that converts a singly linked list into an array and returns it.
• Write a C program to merge two sorted singly linked lists into a single sorted linked list.
• Write a C program to delete alternate nodes of a singly linked list.

Swati Jain, Assistant Professor, VSIT,


VIPS - TC

You might also like