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

Share 'Unit-III Ds - Docx'

The document summarizes key concepts related to linked lists: 1. It defines a linked list as a linear data structure where each element contains a data field and pointer to the next node. Operations on single linked lists include insertion, deletion, searching, and traversal. 2. Sample code is provided to create a linked list by dynamically allocating nodes, and to display all elements by traversing from the start node. 3. Insertion operations into different positions of a linked list are described, including at the beginning, end, and after a specified node. The process involves allocating a new node and adjusting pointer fields.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Share 'Unit-III Ds - Docx'

The document summarizes key concepts related to linked lists: 1. It defines a linked list as a linear data structure where each element contains a data field and pointer to the next node. Operations on single linked lists include insertion, deletion, searching, and traversal. 2. Sample code is provided to create a linked list by dynamically allocating nodes, and to display all elements by traversing from the start node. 3. Insertion operations into different positions of a linked list are described, including at the beginning, end, and after a specified node. The process involves allocating a new node and adjusting pointer fields.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Subject: Data Structures

------------------------------------------------------------------------------------------------------------------------------------------

UNIT-III
LIST: Introduction, single linked list, representation of a linked list in memory, Operations-insertion,
deletion, display, search, Circular linked list, Double linked list, applications Advantages and
disadvantages of single linked list, arrays, Implementation of stack, queue using linked list.

1. What is linked list? Explain representation of linked list in memory.


Ans:

LINKED LIST

linked list is a linear data structure. In this the elements can be placed anywhere in the heap
memory unlike array which uses contiguous locations.
Linked list, is a linear collection of data items.The linear order is given by means of POINTERS.
These types of lists are often referred to as linear linked list.
* Each item in the list is called a node.
* Each node of the list has two fields:
1.Data - contains the element being stored in the list.
2.Next address- contains the address of the next node in the list.
* The last node in the list contains NULL pointer to indicate that it is the end of the list.

Representation of Linked List in Memory


In order to form a linked list, we need a structure called node, which has two fields, data and
next. Data will store the information part and next will store the address of the next node in
sequence.
Consider the fig: below which shows how a linked list is maintained in memory.

1
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

In fig: the variable START is used to store the address of the first node. In this example
START=1, so the first data is stored at address 1,which is H. The corresponding next stores the
address of next node ,which is 4. So, we look at address 4 to fetch the next data which is E.
We repeat this procedure until we reach a position where the next entry contains -1 i.e;
NULL.
When we traverse data and next in this manner, we finally see that the list in the above
example stores characters that form HELLO. The empty space in the above fig: contains data for
other applications.
The list appears as:

2. What is single linked list? Explain operations of single linked list.

Ans:

SINGLE LINKED LIST

A singly linked list, or simply a linked list, is a linear collection of data items. The linear order is
given by means of POINTERS. These types of lists are often referred to as linear linked list.
* Each item in the list is called a node.
* Each node of the list has two fields:
1. Data - contains the element being stored in the list.
2. Next address- contains the address of the next node in the list.
* The last node in the list contains NULL pointer to indicate that it is the end of the list.
Conceptual view of Singly Linked List

Operations on single linked list

The operations on the list are as follows:

1. Creating the linked list


2. Displaying or Traversing the linked list
3. Inserting a node in linked list
a). Inserting a node at the beginning
b). Inserting a node at the end
c). Inserting a node after a given node
d). Inserting a node before a given node
2
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

4. Deleting a node from linked list


a). Deleting the first node
b). Deleting the last node
c). Deleting the node after a given node
d). Deleting the node before a given node
5. Searching for a node in the linked list

1. Creating the linked list:


Linked list allows us to insert or delete nodes whenever necessary. First we have to create the
node.This is possible by using self referential structure, pointers and dynamic memory allocation
functions like malloc.
First we have to declare a structure and create memory by using malloc function.
struct node
{
int data;
struct node *next;
};
struct node *newnode,*start=NULL;
newnode=(struct node *) malloc(sizeof(struct node));
The above statement will allocate a block of memory whose size is equal to size of node of type
structure and its address is assigned to pointer variable newnode. This pointer indicates the
beginning of linked list.

Creation of a linked list involves the following steps:


1. Allocate memory for new node
2. Assign data part of newnode to element and next field to NULL
3. Assign START pointer to newnode
4. Take pointer variable ptr which points to START
5. Move ptr to which points to the next field of ptr =NULL
6. Set next field of ptr points to newnode
7. Repeat the above steps for the required number of node in the list

3
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/* Function for Creating a linked list */


void create()
{
struct node *ptr,*newnode;
int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
while(ele!=-1)
{ newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
newnode->next=NULL;
if(start==NULL)
start=newnode;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
}
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
}
}

2. Displaying the linked list


We can display all the elements in a linked list.

Displaying the linked list involves the following steps


1. Take pointer ptr which points to START
2. Move ptr which points to NULL
3. Print data part of ptr.

4
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for Displaying the element of linked list*/


void display()
{
struct node *ptr;
ptr=start;
if(ptr==NULL)
printf("\n list is empty");
else
{
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
}

3. Inserting a node in Linked list:


A new node can be inserted into linked list which requires resetting of pointers.
Inserting a new node into the list has 4 situations
a). Inserting a node at the beginning
b). Inserting a node at the end
c). Inserting a node after a given node
d). Inserting a node before a given node
The process of insertion involves search for the place of insertion .The search involves in
locating a node after which the new node is to be inserted.

a). Inserting a node at the beginning of a linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element
3. Set next field of new node to START
4. Change START pointer point to the new node

5
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for Inserting a node at beginning */


void insert_beg()
{
struct node *newnode;
int ele;
printf("Enter element");
scanf("%d",&ele);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
newnode->next=start;
start=newnode;
}
b). Inserting a node at the end of a linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element and next field to NULL
3. Take pointer variable ptr which points to START
4. Move ptr which points to last node
5. Set next part of ptr to address of newnode

6
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/* Function for inserting a node at end*/


void insert_end()
{
struct node *ptr,*new_node;
int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=ele;
new_node->next=NULL;
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=new_node;
}
c).Inserting a node after a given node involves following steps:
1. Allocate memory for new node
2. Assign data part of new node to element
3. Search for the given key element by using search function and assign key element
address to keyptr
4. Set next field of new node point to next field of keyptr
5. Set next field of keyptr node point to newnode

7
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for Inserting a node after a given node*/


void insert_after()
{
struct node *keyptr,*newnode;
int ele,key;
printf("\nEnter nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
newnode->next=keyptr->next;
keyptr->next=newnode;

8
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

}
}

d). Inserting a node before a given node involves following steps:


1. Allocate memory for new node
2. Assign data part of newnode to element
3. Search for the given key element by using search function and assign key element
address to keyptr
4. Take pointer variable ptr which points to START
5. Move ptr which points to before node of keyptr
6. Set next field of ptr point to newnode
7. Set next field of newnode point to keyptr

/*Function for Inserting a node before a given node*/


void insert_before()
{
struct node *keyptr,*newnode,*ptr;
int ele,key;
printf("\nEnter element");

9
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
ptr=start;
while(ptr->next!=keyptr)
ptr=ptr->next;
ptr->next=newnode;
newnode->next=keyptr;
}
}

4. Deleting a node from linked list


Deleting a node from the list is easier than insertion as only one pointer needs to be changed.
Here again we have four different situations
a). Deleting the first node
b). Deleting the last node

c). Deleting the node after a given node


d). Deleting the node before a given node
The memory space of deleted node may be released for reuse .The process of deletion also
involves search for the item to be deleted.

a). Deleting the first node involves the following steps


1. Take pointer variable ptr which points to START
2. START pointer is altered to point to the next field of START node in the list
3. Deallocate memory for first node from list.

10
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for Delete first node*/


void delete_first()
{
struct node *ptr;
ptr=start;
start=start->next;
free(ptr);
}
b). Deleting the last node involves the following steps
1. Take pointer variables preptr and ptr which points to START
2. Move ptr such that next field of ptr = NULL and preptr points to ptr(address of before
node of ptr)
3. Set next field of preptr points to NULL
4. Deallocate memory for ptr node from list.

11
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for Deleting last node*/


void delete_last()
{
struct node *ptr,*preptr;
ptr=start;
while(ptr->next!=NULL)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=NULL;
free(ptr);
}

c). Deleting the node after the given node involves the following steps
1. Search for the given key element by using search function and assign key element address to
keyptr
2. Take pointer variable Aptr which points to next field of keyptr
3. Set next field of keyptr to next field of Aptr
4. Deallocate memory for Aptr node from list

12
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for deleting a node after given node*/


void delete_after()
{
struct node *keyptr,*Aptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
Aptr=keyptr->next;
keyptr->next=Aptr->next;
free(Aptr);
}
}

13
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

d). Deleting the node before the given node involves the following steps
1. Search for the given key element by using search function and assign key element
address to keyptr
2. Take pointer variable ptr which points to START
3. Move ptr such that next field of ptr = keyptr and preptr points to ptr(address of before
node of ptr)
4. Set next field of keyptr to next field of ptr
5. Deallocate memory for ptr node from list

/*Function for deleting a node before given node*/


void delete_before()
{
struct node *ptr,*keyptr,*preptr;
int key;
printf("\nEnter the key element");

14
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{ ptr=start;
while(ptr->next!=keyptr)
{ preptr=ptr;
ptr=ptr->next;
}
preptr->next=ptr->next;
free(ptr);
}
}
}

5. Searching a node in linked list:-


The search function attempts to locate the requested element in the linear list. If the node in the
list matches with key, the search returns true if no key matches, it returns false.
The function given below searches through the linked list and returns a pointer the first
occurrence of the search key or returns NULL pointer if the search key is not in the list

Searching a node in linked list involves the following steps


1. Take pointer variable ptr which points to START
2. Move ptr which points to NULL
3. If data part of ptr =key then return ptr otherwise return NULL

/* Function for Search of a element in linked list*/


struct node* search(int key)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)

15
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

{
if(ptr->data==key)
return ptr;
ptr=ptr->next;
}
return NULL;
}

3. Write a program for single linked list operations.

/* Program for single linked list */


#include<stdio.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void create();
void insert_after();
void delete_after();
void display();
struct node *search(int);
void main()
{
struct node *ptr;
int ch,key;
while(1)
{
printf("\nMAIN MENU\n");
printf("1.create\n 2.Insert After\n3.Delete After\n4.print \n5.search\n 6.exit");
printf("Enter Choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2 : insert_after();
break;
case 3: delete_after(); break;
case 4: display();break;
case 5: printf("Enter Key element");
scanf("%d",&key);
ptr=search(key);
if(ptr==NULL)
printf("key is not found");

16
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

else
printf("key is found");
case 6: exit(0);
default: printf("invalid option");
}
}
}

void create()
{
struct node *ptr,*new_node;
int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
while(ele!=-1)
{ new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=ele;
new_node->next=NULL;

if(start==NULL)
start=new_node;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=new_node;
}
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);

}
}
void insert_after()
{
struct node *keyptr,*newnode;
int ele,key;
printf("\nEnter nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");

17
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

else
{
newnode->next=keyptr->next;
keyptr->next=newnode;
}
}
struct node* search(int key)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->data==key)
return ptr;
ptr=ptr->next;
}
return NULL;
}
void display()
{
struct node *ptr;
ptr=start;
if(ptr==NULL)
printf("\n list is empty");
else
{
while(ptr!=NULL)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
}
}
void delete_after()
{
struct node *keyptr,*Aptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
Aptr=keyptr->next;
keyptr->next=Aptr->next;

18
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

free(Aptr);
}
}

4. List advantages and disadvantages of single linked list.

Ans:

ADVANTAGES OF SINGLE LINKED LIST

1. Linked list is Dynamic data structure- It is not necessary to know in advance the number
of elements to be stored in the list . At run time we can allocate as much memory as we
can. Though we can allocate any number of nodes in linked list.
2. Linked list can grow and shrink during run time.
3. Insertion and deletion operations are easier.-We can insert any node at any place easily
and similarly we can remove it easily. In insertion operation we have to just update next
link of node.
4. Efficient memory utilization i.e; no need to pre allocate memory.-Memory is allocated at
run time as per requirement, so that linked list data structure provides us strong command
on memory utilization
5. Faster access time, can be expanded in constant time without memory over head.
6. Linear data structures such as stack, queue can be easily implemented using linked list.
DISADVANTAGES OF LINKED LIST
1. Wastage of memory- Pointer requires extra memory for storage
2. No Random Access- In array we can access nth element easily just by using a[n].
In linked list no random access is given to user, we have to access each node sequentially.
3. Time Complexity-Individual nodes are not stored in the contiguous memory locations.
Access time for individual element is O(n) where as array O(1).
4. Reverse Traversing is difficult- It is very difficult to traverse linked list from end.
5. Heap space restriction- If there is insufficient space in heap then it won’t create any
memory.
5. What are the applications of linked list? Explain in detail.

Ans:

APPLICATIONS OF LINKED LIST:

Linked list find applications in many areas. Some of the applications are

1. Polynomial Manipulation
2. Sparse Matrix representation

1. Polynomial Manipulation
We can represent the polynomial with the help of Linked List and the structure is as follows:

19
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

struct node
{ int coeff,exp;
struct node * next;
} *start =NULL;
And the graphical representation is as follows:

Polynomial addition:
We can add the Polynomial with the help of following logic

1. Firstly we have to create a linked list ,first is for first polynomial and second is for
second polynomial
2. Then we have to check the exponent of each node to the rest of the linked list and if the
exponent is match then we have to add the coefficient and so on

Polynomial Multiplication
We can multiply the polynomial with the help of following logic

1. Firstly we have to create two linked lists, first is for first polynomial and second is for
second polynomial
2. Then we multiply each element of the first linked list to each element of the second
linked list
3. Then if the exponents are same then we have to call the addition of polynomial function

Representing a polynomial using a Linked List


A polynomial can be represented in an array or in a linked list by simply storing the coefficient
and exponent of each term
However for any polynomial operation such as addition or multiplication of polynomials ,we
find that the linked list representation is more easier to deal with.
Note that in a polynomial all the terms may not be present especially if it is a very high order
polynomial.
Consider 5x12+ 2x9 +4x7+6x5+x2+12x
Now in this 12th order polynomial does not have all the 13 terms.

20
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

It would be very easy to represent the polynomial using a linked list structure, where each node
can hold information pertaining to a single term of the polynomial. Each node will need to store
the variable x, the exponent and the coefficient for each term.
It often does not matter whether the polynomial is in x or y. This information may not be very
crucial for the intended operations on the polynomial. Thus we need to define a node structure
to hold two integers ie exp and coff.
For the same polynomial if we use array representation we have to keep a slot for each exponent
of x , thus if we have a polynomial of order 50 but containing just 6 terms then a large number of
entries will be zero in the array.

Addition of two polynomials:


Consider the addition of the following polynomials
5x12 +2x9+4x7+6x6+x3
7x8+2x7+8x6+6x4+2x2+3x+40
The resulting polynomial is going to be
5x12 +2x9+7x8+6x7+14x6+6x4+x3+2x2+3x+40
Now notice how the addition was carried out. Let us say the result of addition is going to be
stored in a third list
We started with the highest power in any polynomial .If there was no item having same exponent
we simply appended the term to the new list and continue with the process.
Wherever we found that the exponents were matching we simply added the coefficients and then
stored the term in the new list.
If one list gets exhausted earlier and the other list still contains some lower order terms then
simply append the remaining terms to the new list.

Multiplication of two polynomials:


Consider the multiplication of the following polynomials
2x – 3 (first polynomial)
2x2 + 3x – 2 (second polynomial)
The resulting polynomial is going to be
(2x – 3)(2x2 + 3x – 2)
= 2x(2x2 + 3x – 2) – 3(2x2 + 3x – 2)
= 4x3 + 6x2 – 4x – 6x2 – 9x+ 6
= 4x3 – 13x+ 6
Now notice how the multiplication was carried out. Let us say the result of addition is going to
be stored in a third list
Start with first term in first polynomial. Multiply it with all the terms in second polynomial,
continue this process till end of first polynomial and then stored in new list
If the list contains same exponent terms then do addition of polynomials, and store in new list,
and also append the remaining terms to the new list.

21
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

2. Sparse matrix Representation

A sparse matrix is a matrix with zeros as the dominating elements.

A matrix containing maximum number of zeros as elements is called a sparse matrix.

Eg:

A matrix consumes a lot of space in memory, when the matrix is sparse to store a handful of non-
zero elements in memory, to save storage space we use triple or three tuple representation to
represent each non zero element of sparse matrix.
3 Tuple Representation:

In triple representation (it contains 3 columns)

1. Elements in first row represent the number of rows, columns and non-zero values in
sparse matrix.
2. Elements in the other rows give formation about the location (row and column) and value
of nonzero elements.

Linked list representation:

In this representation each non zero element of the matrix is represented using nod estructure

22
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

 Here ROW, COLUMN, DATA fields contains row number column number and value of
non zero elements in the matrix.
 RIGHT link points to the node holding the next non zero value in the same row of the
matrix
 DOWN link points to the node holding the next non zero value in the same column of the
matrix.
 Linked list representation ignores representing zeros in the matrix.

Steps to represent sparse matrix:


 Represent Head node, indicated by start, contains dimension sof sparse matrix in its
ROW and COL fields.
 Through RIGHT link, it forms a single linked list with head nodes of columns.
 The head nodes of columns lists have their ROW field to be zero.
 Through DOWN link it forms a single linked list with head nodes of rows.
 The head nodes of row lists have their COL fields to be zero.

23
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

6. What is circular linked list? Explain operations of circular linked list.

Ans:

CIRCULAR LINKED LIST

A circularly linked list, or simply circular list, is a linked list in which the last node always
points to the first node. This type of list can be build just by replacing the NULL pointer at the
end of the list with a pointer which points to the first node. There is no first or last node in the
circular list.

Advantages:

Any node can be traversed starting from any other node in the list.
There is no need of NULL pointer to signal the end of the list and hence, all pointers
contain valid addresses.
 In contrast to singly linked list, deletion operation in circular list is simplified as the
search for the previous node of an element to be deleted can be started from that item
itself.
Conceptual view of circular linked list

OPERATIONS ON CIRCULAR LINKED LIST

The operations on a circular linked list are as follows:

1. Creating the circular linked list


2. Displaying or Traversing the linked list
3. Inserting a node in circular linked list
a). Inserting a node at the beginning
b). Inserting a node at the end
c). Inserting a node after a given node
d). Inserting a node before a given node
4. Deleting a node from circular linked list
a). Deleting the first node
b). Deleting the last node
c). Deleting the node after a given node
d). Deleting the node before a given node
5. Searching for a node in circular linked list

24
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

1. Creating the circular linked list:

Linked list provides to insert or delete nodes whenever necessary. First we have to create the
nodes. This is possible by using self referential structure, pointers and dynamic memory
allocation functions like malloc. First we have to declare structure and create memory by using
malloc function.
struct node
{
int data;
struct node *next;
};
struct node *newnode,*start=NULL;
newnode=(struct node *) malloc(sizeof(struct node));
The above statement will allocate a block of memory whose size is equal to size of node of type
structure and its address is assigned to pointer variable newnode. This pointer indicates the
beginning of linked list

Creation of a circular linked list involves the following steps:


1. Allocate memory for new node
2. Assign data part of newnode to element
3. Assign START point to newnode and next field of newnode to START
4. Take pointer variable ptr which points to START
5. Move ptr to which points to the next field of ptr! =START
6. Set next field of ptr points to newnode and next field of newnode to START
7. Repeat the above steps for the required number of nodes in the list

25
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/* Function for Creating a circular linked list */


void create()
{
struct node *ptr,*newnode;
int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
while(ele!=-1)
{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
if(start==NULL)
{
start=newnode;
newnode->next=start;
}
else
{
ptr=start;
while (ptr->next!=start)
ptr =ptr->next;
ptr->next=newnode;
newnode->next=start;
}
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);

}
}

2. Displaying the circular linked list


We can display the all the elements in a linked list.

Displaying the circular linked list involves the following steps


4. Take pointer ptr which points to START
5. Move ptr to which points to the next field of ptr! =START
6. Print data part of ptr.

/*Function for Displaying the element of circular linked list*/

26
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

void display()
{
struct node *ptr;
ptr=start;
if(ptr==NULL)
printf("\n list is empty");
else
{
while(ptr->next!=start)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
printf("%5d",ptr->data);
}
}

3. Inserting a node in circular Linked list:


A new node can be inserted into linked list which requires resetting of pointers.
Inserting a new node into the list has 4 situations
a). Inserting a node at beginning

b). Inserting a node at end


c). Inserting a node after a given node
d). Inserting a node before a given node
The process of insertion involves search for the place of insertion .The search involves in
locating a node after which the new node is to be inserted.

a). Inserting a node at the beginning of a circular linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element
3. Set next field of new node to START
4. Take pointer ptr which points to START
5. Move ptr to which points to the next field of ptr! =START
6. Change START pointer point to the new node
7. Set next field of ptr to START

27
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for Inserting a node at beginning */


void insert_beg()
{
struct node *newnode,*ptr;
int ele;
printf("Enter element");
scanf("%d",&ele);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
newnode->next=start;
ptr=start;
while(ptr->next!=start)
ptr=ptr->next;
start=newnode;
ptr->next= start;

}
b). Inserting a node at the end of a circular linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element
3. Take pointer variable ptr which points to START
4. Move ptr to which points to next field of ptr!=START
5. Set next field of ptr to newnode
6. Set next field of newnode to START

28
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/* Function for inserting a node at end*/


void insert_end()
{
struct node *ptr,*newnode;
int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
ptr=start;
while(ptr->next!=start)
ptr=ptr->next;
ptr->next=newnode;
newnode->next=start;
}
c).Inserting a node after a given node involves following steps:
1. Allocate memory for new node
2. Assign data part of new node to element
3. Search for the given key element by using search function and assign key element
address to keyptr
4. Set next field of new node points to next field of Keyptr
5. Set next field of keyptr node points to newnode

29
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for Inserting a node after a given node*/


void insert_after()
{
struct node *keyptr,*newnode;
int ele,key;
printf("\nEnter nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
newnode->next=keyptr->next;
keyptr->next=newnode;
}
}
d). Inserting a node before a given node involves following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element

30
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

3. Search for the given key element by using search function and assign key element
address to keyptr
4. Take pointer variable ptr which points to START
5. Move ptr which points to before node of keyptr
6. Set next field of ptr point to newnode
7. Set next field of newnode point to keyptr

/*Function for Inserting a node before a given node*/


void insert_before()
{
struct node *keyptr,*newnode,*ptr;
int ele,key;
printf("\nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
ptr=start;
while(ptr->next!=keyptr)
ptr=ptr->next;

31
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

ptr->next=newnode;
newnode->next=keyptr;
}
}

4. Deleting a node from circular linked list


Deletion of a node into the list has 4 situations

a). Deleting the first node


b). Deleting the last node
c). Deleting the node after the given node
d). Deleting the node before the given node
The memory space of deleted node may be released for reuse .The process of deletion also
involves search for the item to be deleted.

a). Deleting the first node involves the following steps


1 Take pointer variable ptr which points to START
2 Move ptr to which points to next field of ptr!=START
3 Set next field of ptr points to the next field of START
4 Deallocate memory for first node from list.
5 Set START which points to the next field of ptr

/*Function for Delete first node*/


void delete_first()
32
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

{
struct node *ptr,*temp;
ptr=start;
while(ptr->next!=start)
ptr=ptr->next;
ptr->next=start->next;
free(start);
start=ptr->next;
}
b). Deleting the last node involves the following steps
1. Take pointer variables preptr and ptr which points to START
2. Move ptr such that next field of ptr !=START and preptr points to ptr(address of before
node of ptr)
3. Set next field of preptr points to START
4. Deallocate memory for ptr node from list.

/*Function for Deleting last node*/


void delete_last()
{
struct node *ptr,*preptr;
ptr=start;
while(ptr->next!=start)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=start;
free(ptr);
}

33
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

c). Deleting the node after the given node involves the following steps
1. Search for the given key element by using search function and assign key element address to
keyptr
2. Take pointer variable Aptr which points to next field of keyptr
3. Set next field of keyptr to next field of Aptr
4. Deallocate memory for Aptr node from list

/*Function for deleting a node after given node*/


void delete_after()
{
struct node *keyptr,*Aptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
Aptr=keyptr->next;
keyptr->next=Aptr->next;
free(Aptr);
}
}

34
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

d). Deleting the node before the given node involves the following steps
1. Search for the given key element by using search function and assign key element
address to keyptr
2. Take pointer variable ptr which points to START
3. Move ptr such that next field of ptr = keyptr and preptr points to ptr(address of before
node of ptr)
4. Set next field of keyptr to next field of ptr
5. Deallocate memory for ptr node from list

/*Function for deleting a node before given node*/


void delete_before()
{
struct node *ptr,*keyptr,*preptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{ ptr=start;
while(ptr->next!=keyptr)
{ preptr=ptr;
ptr=ptr->next;

35
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

}
preptr->next=ptr->next;
free(ptr);
}
}
}

5. Searching a node in circular linked list:-


The search function attempts to locate the requested element in the linear list. If the node in the
list matches with key, the search returns true if no key matches, it returns false.
The function given below searches through the linked list and returns a pointer the first
occurrence of the search key or returns NULL pointer if the search key is not in the list

Searching a node in circular linked list involves the following steps

1. Take pointer variable ptr which points to START


2. If data part of ptr=key then return ptr and ptr points to next filed of ptr
3. Move ptr which is not equals to START
4. If data part of ptr =key then return ptr otherwise return NULL

/* Function for Search of a element in linked list*/


struct node* search(int key)
{
struct node *ptr;
ptr=start;
if(ptr->data==key)
return ptr;
ptr=ptr->next;
while(ptr!=start)
{
if(ptr->data==key)

36
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

return ptr;
ptr=ptr->next;
}
return NULL;
}

7. Write a program for circular linked list operations.


/* Program for circular linked list */
#include<stdio.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void create();
void insert_after();
void delete_after();
void display();
struct node *search(int);
void main()
{
struct node *ptr;
int ch,key;
while(1)
{
printf("\nMAIN MENU\n");
printf("1.create\n 2.Insert After\n3.Delete After\n4.print \n5.search\n 6.exit");
printf("Enter Choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2 : insert_after();
break;
case 3: delete_after(); break;
case 4: display();break;
case 5: printf("Enter Key element");
scanf("%d",&key);
ptr=search(key);
if(ptr==NULL)
printf("key is not found");
else
printf("key is found");

37
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

case 6: exit(0);
default: printf("invalid option");
}
}
}

void create()
{
struct node *ptr,*newnode;
int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
while(ele!=-1)
{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
if(start==NULL)
{
start=newnode;
newnode->next=start;
}
else
{
ptr=start;
while(ptr->next!=start)
ptr=ptr->next;
ptr->next=newnode;
newnode->next=start;
}
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);

}
}
void insert_after()
{
struct node *keyptr,*newnode;
int ele,key;
printf("\nEnter nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)

38
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

printf("\nKey is not found");


else
{
newnode->next=keyptr->next;
keyptr->next=newnode;
}
}
struct node* search(int key)
{
struct node *ptr;
ptr=start;
if(ptr->data==key)
return ptr;
ptr=ptr->next;
while(ptr!=start)
{
if(ptr->data==key)
return ptr;
ptr=ptr->next;
}
return NULL;
}
void display()
{
struct node *ptr;
ptr=start;
if(ptr==NULL)
printf("\n list is empty");
else
{
while(ptr->next!=start)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
printf("%5d",ptr->data);
}
}
void delete_after()
{
struct node *keyptr,*Aptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)

39
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

printf("\nKey is not found");


else
{
Aptr=keyptr->next;
keyptr->next=Aptr->next;
free(Aptr);
}
}

8. What is double linked list? Explain operations of double linked list.


Ans:

DOUBLE LINKED LIST

In a singly linked list, each element contains a pointer to the next element. We have seen this
before. In single linked list, traversing is possible only in one direction. Sometimes, we have to
traverse the list in both directions to improve performance of algorithms. To enable this, we
require links in both the directions, that is, the element should have pointers to the right element
as well as to its left element. This type of list is called doubly linked list.

Conceptual view of doubly linked list

Doubly linked list is defined as a collection of elements, each element consisting of three fields:

• pointer to left element,

• data field, and

• pointer to right element.

Left link of the leftmost element is set to NULL which means that there is no left element to that.
And, right link of the rightmost element is set to NULL which means that there is no right
element to that.

OPERATIONS ON DOUBLE LINKED LIST

The operations on double linked list are as follows:

1. Creating the doubly linked list


2. Displaying or Traversing the doubly linked list
40
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

3. Inserting a node in doubly linked list


a). Inserting a node at beginning
b). Inserting a node at end
c). Inserting a node after a given node
d). Inserting a node before a given node
4. Deleting a node from doubly linked list
a). Deleting the first node
b). Deleting the last node
c). Deleting the node after the given node
d). Deleting the node before the given node
5. Searching for a node in doubly linked list

1. Creating the double linked list:


Double Linked list provides to insert or delete nodes whenever necessary. First we have to create
the nodes. This is possible by using self referential structure, pointers and dynamic memory
allocation functions like malloc. First we have to declare structure and create memory by using
malloc function.
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *newnode,*start=NULL;

newnode=(struct node *) malloc(sizeof(struct node));


The above statement will allocate a block of memory whose size is equal to size of node of type
structure and its address is assigned to pointer variable newnode. This pointer indicates the
beginning of linked list

Creation of a linked list involves the following steps:


1. Allocate memory for new node
2. Assign data part of newnode to element, prev and next field to NULL
3. Assign START point to newnode
4. Take pointer variable ptr which points to START
5. Move ptr to which points to the next field of ptr =NULL
6. Set next field of ptr points to newnode
7. Set prev field of newnode to ptr
8. Repeat the above steps for the required number of nodes in the list

41
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/* Function for Creating a double linked list */


void create()
{
struct node *ptr,*newnode;
int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
while(ele!=-1)
{ newnode=(struct node *)malloc(sizeof(struct node));
newnode->prev=NULL;
newnode->data=ele;
newnode->next=NULL;
if(start==NULL)
start=newnode;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
newnode->prev=ptr;
}
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);

}
}

2. Displaying the elements of doubly linked list

42
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

We can display the all the elements in a doubly linked list.

Displaying the double linked list involves the following steps


1. Take pointer ptr which points to START
2. Move ptr which points to NULL
3. Print data part of ptr.

/*Function for Displaying the element of double linked list*/


void display()
{
struct node *ptr;
ptr=start;
if(ptr==NULL)
printf("\n list is empty");
else
{
while(ptr!=NULL)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
}
}

3. Inserting a node in Doubly Linked list:


A new node can be inserted into doubly linked list which requires resetting of pointers.
Inserting a new node into the list has 4 situations
a). Inserting a node at beginning
b). Inserting a node at end
c). Inserting a node after a given node
d). Inserting a node before a given node

43
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

The process of insertion involves search for the place of insertion .The search involves in
locating a node after which the new node is to be inserted.

a). Inserting a node at the beginning of a doubly linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element
3. Set prev field of newnode to NULL
4. Set next field of new node to START
5. Set prev field of START to newnode
6. Change START pointer point to the new node

/*Function for Inserting a node at beginning */


void insert_beg()
{
struct node *newnode;
int ele;
printf("Enter element");
scanf("%d",&ele);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
newnode->prev=NULL;
newnode->next=start;
start->prev=newnode;
start=newnode;
}

44
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

b). Inserting a node at the end of a linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element and next field to NULL
3. Take pointer variable ptr which points to START
4. Move ptr which points to last node
5. Set next field of ptr to address of newnode
6. Set prev field of newnode to ptr

/* Function for inserting a node at end*/


void insert_end()
{
struct node *ptr,*new_node;
int ele;
printf("Enter the element");
scanf("%d",&ele);
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=ele;
new_node->next=NULL;
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=new_node;
newnode->prev=ptr;
}
c).Inserting a node after a given node involves following steps:
1. Allocate memory for new node
2. Assign data part of new node to element

45
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

3. Search for the given key element by using search function and assign key element
address to keyptr
4. Set prev field of new node points to keyptr
5. Set next field of new node points to next field of Keyptr
6. Set next field of keyptr of prev field to newnode
7. Set next field of keyptr node points to newnode

/*Function for Inserting a node after a given node*/


void insert_after()
{
struct node *keyptr,*newnode;
int ele,key;
printf("\nEnter nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);

46
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

if(keyptr==NULL)
printf("\nKey is not found");
else
{
newnode->prev=keyptr;
newnode->next=keyptr->next;
keyptr->next->prev=newnode;
keyptr->next=newnode;
}
}

d). Inserting a node before a given node involves following steps:


1. Allocate memory for new node
2. Assign data part of newnode to element
3. Search for the given key element by using search function and assign key element address to
keyptr
4. Set prev field of newnode to prev field of keyptr
5. Set next field of newnode points to keyptr
6. Set next field of keyptr of prev field points to newnode
7. Set prev field of keyptr to newnode

47
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for Inserting a node before a given node*/


void insert_before()
{
struct node *keyptr,*newnode;
int ele,key;
printf("\nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
newnode->prev=keyptr->prev;
newnode->next=keyptr;
keyptr->prev->next=newnode;
keyptr->prev=newnode;
}
}

4. Deleting a node from double linked list


Deleting a node from the list is easier than insertion as only one pointer needs to be changed.
Here again we have four different situations
a). Deleting the first node
b). Deleting the last node

48
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

c). Deleting the node after the given node


d). Deleting the node before the given node
The memory space of deleted node may be released for reuse .The process of deletion also
involves search for the item to be deleted.

a). Deleting the first node involves the following steps


1. Take pointer variable ptr which points to START
2. START pointer is altered to point to the next field of START node in the list
3. Set prev field of start to NULL
4. Deallocate memory for first node from list.

/*Function for Delete first node*/


void delete_first()
{
struct node *ptr;
ptr=start;
start=start->next;
start->prev=NULL;
free(ptr);
}
b). Deleting the last node involves the following steps
1. Take pointer variables ptr which points to START
2. Move ptr such that next field of ptr = NULL
3. Set previous field of ptr of next field points to NULL

49
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

4. Deallocate memory for ptr node from list.

/*Function for Deleting last node*/


void delete_last()
{
struct node *ptr;
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->prev->next=NULL;
free(ptr);
}
c). Deleting the node after the given node involves the following steps
1. Search for the given key element by using search function and assign key element
address to keyptr
2. Take pointer variable Aptr which points to next field of keyptr
3. Set next field of keyptr to next field of Aptr
4. Set next field of Aptr of prev field points to keyptr
5. Deallocate memory for Aptr node from list

50
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

/*Function for deleting a node after given node*/


void delete_after()
{
struct node *keyptr,*Aptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
Aptr=keyptr->next;
keyptr->next=Aptr->next;
Aptr->next->prev=keyptr;
free(Aptr);
}
}
d). Deleting the node before the given node involves the following steps
1. Search for the given key element by using search function and assign key element address to
keyptr

51
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

2. Take pointer variable preptr which points to prev field of keyptr


3. Set prev field of keyptr to prev field of preptr
4. Set prev field of preptr of next field to keyptr
5. Deallocate memory for ptr node from list

/*Function for deleting a node before given node*/


void delete_before()
{
struct node *preptr,*keyptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
preptr=keyptr->prev;

52
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

keyptr->prev=preptr->prev;
preptr->prev->next=keyptr;
free(preptr);
}
}
5.Searching a node in double linked list:-
The search function attempts to locate the requested element in the list. If the node in the list
matches with key, the search returns true if no key matches, it returns false.
The function given below searches through the linked list and returns a pointer the first
occurrence of the search key or returns NULL pointer if the search key is not in the list

Searching a node in double linked list involves the following steps


1. Take pointer variable ptr which points to START
2. Move ptr which points to NULL
3. If data part of ptr =key then return ptr otherwise return NULL

/* Function for Searching of a element in double linked list*/


struct node* search(int key)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->data==key)
return ptr;
ptr=ptr->next;
}
return NULL;
}
53
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

9. Write program for double linked list operations.


/* Program for Double linked list */
#include<stdio.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void create();
void insert_after();
void delete_after();
void display();
struct node *search(int);
void main()
{
struct node *ptr;
int ch,key;
while(1)
{
printf("\nMAIN MENU\n");
printf("1.create\n 2.Insert After\n3.Delete After\n4.print \n5.search\n 6.exit");
printf("Enter Choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2 : insert_after();
break;
case 3: delete_after(); break;
case 4: display();break;
case 5: printf("Enter Key element");
scanf("%d",&key);
ptr=search(key);
if(ptr==NULL)
printf("key is not found");
else
printf("key is found");
case 6: exit(0);
default: printf("invalid option");
}
}
}
void create()
{

54
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

struct node *ptr,*newnode;


int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
while(ele!=-1)
{ newnode=(struct node *)malloc(sizeof(struct node));
newnode->prev=NULL;
newnode->data=ele;
newnode->next=NULL;

if(start==NULL)
start=newnode;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
newnode->prev=ptr;
}
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
}
}
void insert_after()
{
struct node *keyptr,*newnode,*aptr;
int ele,key;
printf("\nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
newnode->prev=keyptr;
newnode->next=keyptr->next;
keyptr->next->prev=newnode;
keyptr->next=newnode;
}
}
struct node* search(int key)

55
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->data==key)
return ptr;
ptr=ptr->next;
}
return NULL;
}
void display()
{
struct node *ptr;
ptr=start;
if(ptr==NULL)
printf("\n list is empty");
else
{
while(ptr!=NULL)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
}
}
void delete_after()
{
struct node *keyptr,*Aptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
Aptr=keyptr->next;
keyptr->next=Aptr->next;
Aptr->next->prev=keyptr;
free(Aptr);
}
}

56
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

10. Explain about stack using linked list. Write a program for stack using linked list.
(GRIET/June/2015)
Ans:
STACK USING LINKED LIST:

To implement a stack using linked list we need to define a node which in turn consists of data
and a pointer to the next node. The advantage of representing stacks using linked lists is that
we can decide which end should be the top of the stack.
In our example we will select front end as the top of the stack in which we can add and remove
data.

The working principle of stack is LIFO. Using a linked list is one way to implement a stack so
that it can handle essentially any number of elements. The operations performed on a stack
are push ( ) and pop ( ).When implementing a stack using linked lists, pushing elements on to
the stack and popping elements from the stack is performed at the same end i.e. TOP.

Pushing elements to the stack :


1. Allocate memory for the new node.
2. Assign the value to the data field of the new node.
3. If stack is empty then Set next field of the new node to NULL and set TOP pointer points to
the new node .
5. Otherwise Set the next field of the newnode points to TOP and set TOP pointer points to
the new node

Popping elements from the stack :

57
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

1. If stack is empty, then display message “stack is empty – no deletion possible” and exit.
2. Otherwise , Take pointer variable ptr which points to TOP
3. set TOP points to the next field of TOP
4. Deallocate memory for ptr node from list

/*Program for stack using linked list */


#include<stdio.h>
void push(int ele);
void pop();
void display();
struct node
{
int data;
struct node *next;
}*top=NULL;
void main()
{
int ch,ele;
while(1)
{
printf("\nMAIN MENU");
printf("\n1. push \n 2.pop \n3.display\n4.exit");
printf("\nEnter Choice");
scanf("%d",&ch);

switch(ch)
{
case 1: printf("\nenter the element");
scanf("%d",&ele);
push(ele);
break;
case 2 : pop();
break;
case 3:display();
break;
case 4:exit(0);
}
}
}
void push(int ele)

58
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
if(top==NULL)
{
newnode->next=NULL;
top=newnode;
}
else
{
newnode->next=top;
top=newnode;
}
}
void pop()
{
struct node *ptr;
if(top==NULL)
printf("stack is empty");
else
{
ptr=top;
top=top->next;
printf("\nThe deleted element is :%d",ptr->data);
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr=top;
if(top==NULL)
printf("stack is empty");
else
{
while(ptr!=NULL)
{
printf("%4d->",ptr->data);
ptr=ptr->next;

59
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

}
}
}

11. Explain about queue using linked list. Write a program for queue using linked list.
Ans:

QUEUE USING LINKED LIST

Like stacks when implementing a queue using linked list a node must be defined .Front end of
Queue is used to remove data and rear end is used to add data. The advantage of representing a
queue using linked list is that it allows us to select any end to add and remove data items.
So,we can select the end of the linked list as rear and beginning of the list as front.

The working principle of queue is FIFO. Using a linked list is one way to implement a queue
so that it can handle essentially any number of elements. The operations performed on a
queue are enqueue( ) and dequeue( ).When implementing a queue using linked lists, inserting
elements to the queue is done at one end and deletion of elements from the queue is done
at the other end.

Inserting a node at the Rear end:


1. Allocate memory for the new node.
2. Assign the value to the data field of the new node.
3. Set the next field of the new node to NULL.
4. If the queue is empty, then set FRONT and REAR pointer points to the new node.
5. Otherwise ,Set the next field of REAR points to new node.
60
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

6. Set REAR pointer points to the new node.

Deleting a node at the Front end:


1. If queue is empty, then display message “ Queue is empty – no deletion is possible” and exit.
2. Otherwise, Take pointer variable ptr which points to FRONT.
3. Set FRONT points to the next field of FRONT.
4. Deallocate memory for ptr node from list.

/*Program for queue using Linked List*/


#include<stdio.h>
void enqueue(int ele);
void dequeue();
void display();
struct node
{
int data;
struct node *next;
}*front=NULL,*rear=NULL;
void main()
{
int ch,ele;
clrscr();
while(1)
{
printf("\nMAIN MENU\n");
printf("1.enqueue\n2.dequeue\n3.display\n4.exit\n");
printf("enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nenter the element");
scanf("%d",&ele);
enqueue(ele);
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4:exit(0);
}
}

61
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

}
void enqueue(int ele)
{
struct node *newnode;
printf("enter number");
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
newnode->next=NULL;
if(front==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
void dequeue()
{
struct node *ptr;
if(front==NULL)
printf("queue is empty");
else
{
ptr=front;
front=front->next;
printf("\nThe deleted element is:%d",ptr->data);
free(ptr);
}
}
void display()
{
struct node *ptr;
if(front==NULL)
printf("queue is empty");
else
{
ptr=front;

62
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

while(ptr!=NULL)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
}
}

12. Explain differences between array and linked list?

Ans: DIFFERENCE BETWEEN ARRAY AND LINKED LIST:

Array Linked List


List of elements stored in contiguous memory List of elements need not stored in contiguous
location i.e all elements linked physically memory locations i.e all elements will be
linked logically

Contiguous memory required for complete list Contiguous memory required for single node
that will be large requirements of list that will be small requirements
List is static in nature i.e created at compile List is dynamic i.e. created and manipulated at
time mostly time of execution
List can’t grow and shrink List can grow and shrink dynamically
Memory allotted to single item of list can’t Memory allotted to single node of list can also
free be free
Random access of ith element is possible Sequential access of ith element i.e all previous
through a[i] i-1 node have to traverse before
Traversal easy since any elements can access Traversal is done node by node hence not as
dynamically and randomly good as in array
Searching can be linear and if sorted than in Searching operation must be linear in case of
array we can also apply binary search of sorted list also. Time complexity is
algorithm time complexity proportional to list length
Insertion and deletion is costly since require Insertion and deletion is performed by simply
shifting of many items pointer exchange.

13. Compare Single linked list with Double linked list.


(GRIET/June/2015)
Ans:

 In single list Each node contains at least two parts:

a) info
b) link
In doubly linked list Each node contains at least three parts:
a) info
63
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

b) link to next node c) link to previous node

 Each element in the singly linked list contains a reference to the next element in the list,
while each element in the doubly linked list contains references to the next element as
well as the previous element in the list. Doubly linked lists require more space for each
element in the list and elementary operations such as insertion and deletion is more
complex since they have to deal with two references. But doubly link lists allow easier
manipulation since it allows traversing the list in forward and backward directions.
 Singly linked list uses less memory per node (one pointer) , Doubly linked list uses More
memory per node than Singly Linked list (two pointers)
 In Singly linked list, Complexity of Insertion and Deletion at known position is O (n). In
Doubly linked list Complexity of Insertion and Deletion at known position is O (1).
 If we know in advance that element to be searched is found near the end of the list(for
example name 'Yogesh' in a telephone directory), even then singly linked list is traversed
sequentially from beginning. In doubly linked list If we know in advance that element to
be searched is found near the end of the list(for example name 'Yogesh' in a telephone
directory), then the list can traversed from the end thereby saving time

14. Give the Representation of a node in a Double linked list using structures.
(GRIET/June/2015)
Ans:

DOUBLE LINKED LIST

In a singly linked list, each element contains a pointer to the next element. We have seen this
before. In single linked list, traversing is possible only in one direction. Sometimes, we have to
traverse the list in both directions to improve performance of algorithms. To enable this, we
require links in both the directions, that is, the element should have pointers to the right element
as well as to its left element. This type of list is called doubly linked list.

Conceptual view of doubly linked list

Doubly linked list is defined as a collection of elements, each element consisting of three fields:

• pointer to left element,

• data field, and

• pointer to right element.

64
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------

Left link of the leftmost element is set to NULL which means that there is no left element to that.
And, right link of the rightmost element is set to NULL which means that there is no right
element to that.

Double Linked list provides to insert or delete nodes whenever necessary. First we have to create
the nodes. This is possible by using self referential structure, pointers and dynamic memory
allocation functions like malloc. First we have to declare structure and create memory by using
malloc function.
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *newnode,*start=NULL;

15. What are the disadvantages of Arrays.


(GRIET/June/2015)
Ans:
1.We must know in advance that how many elements are to be stored in array.
2. Array is static structure. It means that array is of fixed size. The memory which is allocated to
array cannot be increased or reduced.
3. Since array is of fixed size, if we allocate more memory than requirement then the memory
space will be wasted. And if we allocate less memory than requirement, then it will create
problem.
4. The elements of array are stored in consecutive memory locations. So insertions and deletions
are very difficult and time consuming.

65

You might also like