0% found this document useful (0 votes)
29 views31 pages

Unit II Linked List 2

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)
29 views31 pages

Unit II Linked List 2

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/ 31

UNIT-II: LINKED LIST

Define Linked list.


Linked list is a linear data structure where elements (nodes) are not stored in
contiguous memory locations; instead, each node holds data and a reference (pointer)
to the next node in the sequence. This allows for dynamic resizing and insertion/deletion
operations at any point in the list.
The elements in a linked list are linked using pointers as shown in the below image:

Each structure of the list is called a node and consists of two fields, one containing the
data and the other containing the address of the next item in the list.
A linked list is therefore a collection of structures not ordered by their physical
placements in the memory (like an array) but by logical links that are stored as a part of
the data in the structure itself.

Applications of linked list


 Implementing dynamic data structures
 Memory management
 Graphs and trees
 Undo/redo functionality
 Polynomial representation

Types of Linked List


There are four types of linked list. They are:
1. Singly Linked List: This is the most basic type of linked list. Each node has one
pointer referencing the next node in the list. Efficient for insertions/deletions at
the beginning but requires traversing the list for operations at the end. Traversal
of items can be done in the forward direction only due to the linking of every node
to its next node.

head data next data next data next null

Node Node Node

2. Doubly Linked List: Each node has two pointers - one to the next node and
another to the previous node. It allows for traversal in both directions (forward
and backward). It enables efficient insertion/deletion at any position as you can
navigate from either side.

head null
prev data next prev data next prev data next
null tail

Node Node Node


3. Circular Linked List: In this linked list the last node's pointer points back to the
first node, creating a circular chain. It is useful for representing cyclic structures or
implementing algorithms like circular buffers. In circular linked list there is no
NULL at the end.

data next data next data next tail

Node Node Node

4. Circular Doubly Linked List: It combines the features of doubly linked list and
circular linked list. Each node has two pointers: one to the next and one to the
previous node, forming a closed loop. It offers efficient bi-directional traversal and
insertion/deletion at any point within the circular structure.

head prev data next prev data next prev data next

Node Node Node

What is the type of linked list called if the last node points to the first node?
If the last node of a linked list points back to the first node, it is called a "circular
linked list".

In what condition the list converted to circular link list. Give example.
A list can be converted to a circular linked list by making the last node of the list point to
the first node, forming a loop. To convert a regular linked list into a circular linked list,
the next pointer of the last node is modified to point back to the first node.
Consider the following regular linked list:
1 -> 2 -> 3 -> 4 -> null
To convert it into a circular linked list, we make the last node (4) point back to the first
node (1):
1 -> 2 -> 3 -> 4 -> (points back to 1)
Now, the last node (4) has its next pointer pointing to the first node (1), creating a loop
or circular structure. With this modification, we have successfully converted the regular
linked list into a circular linked list.

What are the different parts of a node in a doubly linked list? Draw a schematic
diagram of a doubly linked list with 5 nodes.
A doubly linked list is a type of linked list where each node contains three separate
components:
 Data: This part holds the actual data value associated with the node.
 Previous Pointer (prev): It points to the previous node in the list. prev data next
 Next Pointer (next): It points to the next node in the list.
Node
Schematic representation of a doubly linked list with five nodes:

head null
1 2 3 4 5
null tail
What do you mean by a circular doubly linked list? Draw a diagram of a circular
doubly linked list with five nodes.
A circular doubly linked list combines the features of doubly linked list and circular linked
list. Each node has two pointers: one to the next and one to the previous node, forming
a closed loop.

head 1 2 3 4 5

Mention one advantage of doubly linked list over singly linked list.
One advantage of a doubly linked list over a singly linked list is its ability to support
bidirectional traversal. In a doubly linked list, each node has both a next and a prev
pointer. This allows for efficient navigation in both forward and backward directions.
Unlike singly linked lists, which only allow forward traversal, doubly linked lists provide
greater flexibility for certain operations.

Mention one disadvantage of doubly linked list over singly linked list.
One disadvantage of a doubly linked list compared to a singly linked list is that it
requires more memory due to the additional storage needed for the previous
pointers (prev). In a singly linked list, each node only contains a next pointer, which
makes it more memory-efficient. However, in a doubly linked list, each node has both a
next and a prev pointer, resulting in increased memory overhead.

Describe the advantages and disadvantages of doubly linked list over single
linked list.
Advantages of Doubly Linked List:
 Bidirectional Traversal: In a doubly linked list, each node has both a next and a
prev pointer. This allows for forward and backward traversal, making it easier to
navigate in both directions.
 Insertions and Deletions at Both Ends: Doubly linked lists allow efficient
insertions and deletions at both the beginning and the end of the list. For example:
 Tail Pointer: A doubly linked list maintains a tail pointer, which points to the last
node. This makes appending elements to the end of the list more efficient.
Disadvantages of Doubly Linked List:
 Increased Memory Overhead: Each node in a doubly linked list requires additional
memory for the prev pointer. In contrast, a singly linked list only needs a next
pointer. Therefore, doubly linked lists consume more memory.
 Complexity: Managing two pointers (next and prev) for each node increases the
complexity of operations. Insertions and deletions require updating both pointers,
which can lead to more error-prone code.
 Slower Traversal: Although doubly linked lists allow bidirectional traversal,
accessing nodes in the middle of the list is slower than in a singly linked list. This is
because we need to traverse through both next and prev pointers.

What is the condition for a doubly linked linear list, specified by head and tail
as the pointers respectively to the first and last nodes, to be empty?
A doubly linked linear list is considered empty when both the head and tail pointers are
set to null or when the head pointer is null and the tail pointer is not pointing to any
valid node.
Advantages of linked list:
1. Linked list can grow or shrink in size during the execution of a program
2. A linked list can be made just as long as required
3. Linked list does not waste memory space; it uses the memory which is needed for
the list at any point of time.
4. Linked list can provide flexibility in allowing the items to be rearranged efficiently.
5. It is easier to insert or delete items at any place, only by rearranging the links.

Disadvantages of linked list:


1. Searching of items takes more time compared to arrays.
2. For fixed length list, it is better to use an array rather that a linked list because linked
list will use more storage (memory) than an array with same number of items.

Distinguish between array and linked list


Linked List Array

1. It can grow or shrink in size during the 1. It cannot grow or shrink in size once it
execution of a program. is declared.

2. It does not waste memory space. 2. It wastes memory space if the length
is not known at time of declaration.

3. It is easier to insert or delete items at 3. It is difficult to insert or delete items


any place, only by rearranging the links. at any place because shifting is required.

4. Traversing is costly because we 4. We can access any item in an array


cannot access to an item directly. directly by its index.

5. For fixed length list, it takes more 5. For fixed length list, it takes less
memory than an array. memory than a linked list.

“Linked list is not always better than array” – do you agree with the
statement? Justify your answer.
I agree with the statement "Linked list is not always better than array." Both arrays and
linked lists are fundamental data structures, but they have different strengths and
weaknesses that make them suitable for different use cases.
The best data structure choice depends on our specific needs.
Use arrays when:
 Random access is frequent.
 Data size is known beforehand and unlikely to change significantly.
 Memory efficiency for primitive data types is a priority.
 Simplicity and ease of implementation are crucial.
Use linked lists when:
 The data size is dynamic or can vary significantly.
 Frequent insertions/deletions are required, especially in the middle of the list.
 Memory efficiency for complex data structures is important.
What is self-referential structure?
A structure which contains a member field that points to the same structure type is
called self-referential structure. Structure of a node in a linked list is an example of self-
referential structure.

How a linked list can be implemented in C?


A linked list is a collection of nodes arranged together by links. To represent a node in C
we will take a self-referential structure. The general form of a node of a linked list is:
typedef struct node
{
type1 member1;
type2 member2;
...
...
struct node *next;
}node;

Following are some examples of nodes:


typedef struct node
{
int data;
struct node *next;
}node;

typedef struct node


{
char name[20];
int code;
float salary;
struct node *next;
}node;
The beginning of the list is marked by a special pointer named „head‟ or „start‟. This
pointer holds the address of the first node of the list. The link (next) part of each node
points to the next node in the list, but the link (next) part of the last node has no next
node to point to, so it is made to NULL. If the list is empty the „head‟ contains NULL
value. So our first job is to declare the pointer start and initialized it to NULL. This can
be done as-
node *head=NULL;
To create the first node, we dynamically allocate the space for the node using the
malloc() function and store the data in the first node as follows:
head = (node*)malloc(sizeof(node));
head->data = 10; // The number 10 is stored in head
To create the second node,
head->next = (node*)malloc(sizeof(node));
head->next->data = 20; // The number 20 is stored in 2nd node
To create the third node,
head->next->next = (node*)malloc(sizeof(node));
head->next->next->data = 30; // The number 30 is stored in 3rd node
And so on, we can store all the data in linked list nodes. At the last node we place NULL
in the next pointer as,
head->next->next->next = NULL;
The above code will create a linked list with 3 nodes with numbers 10, 20 and 30.
To display the numbers stored in the linked list, we can use a loop as follows,
for(temp = head; temp != NULL; temp = temp->next)
printf(“%d\t”,temp->data);

Write a program to create and display a singly linked list.


#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *create()
{
node *head,*i;
int x;
printf("Enter the first number: ");
scanf("%d",&x);
head=(node*)malloc(sizeof(node)); // Allocates space for head node
head->data=x; // Store the first number in head node
i=head; // set loop variable i with address of head node
do
{
printf("Enter next number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
{
i->next=NULL; // set i as the last node
break;
}
i->next=(node*)malloc(sizeof(node));//Allocate space for next node
i->next->data=x; // store the number in next node
i=i->next; // move i to set to point to the next node
}while(x!=-999);
return head;
}
void display(node *head)
{
node *temp;
printf("Elements of linked list: ");
for(temp=head;temp!=NULL;temp=temp->next)
printf("\t%d", temp->data);
printf("\n");
}
void main()
{
node *head;
head=create();
display(head);
}
Write a program to create and display a circular linked list.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *create()
{
node *head,*temp;
int x;
printf("Enter the first number: ");
scanf("%d",&x);
head=(node*)malloc(sizeof(node));
head->data=x;
temp=head;
do
{
printf("Enter next number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
break;
temp->next=(node*)malloc(sizeof(node));
temp->next->data=x;
temp=temp->next;
}while(x!=-999);
temp->next = head;
return temp; // Address of last node
}
void display(node *tail)
{
node *temp;
printf("Elements of Circular linked list: ");
if (tail==NULL)
return;
temp=tail->next;
do {
printf("\t%d", temp->data);
temp=temp->next;
} while (temp!=tail->next);
printf("\n");
}
void main()
{
node *tail=NULL;
tail=create();
display(tail);
}

Write a program to create and display a doubly linked list.


#include<stdio.h>
#include<stdlib.h>
typedef struct node {
struct node *prev;
int data;
struct node *next;
}node;
void create(node **addr_head,node **addr_tail) {
node *temp;
int x;
printf("Enter first number: ");
scanf("%d",&x);
*addr_head = (node*)malloc(sizeof(node));
(*addr_head)->prev=NULL;
(*addr_head)->data = x;
temp=*(addr_head);
do {
printf("Enter next number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
break;
temp->next = (node*)malloc(sizeof(node));
temp->next->data = x;
temp->next->prev = temp;
temp = temp->next;
} while(x!=-999);
temp->next = NULL;
*addr_tail=temp;
}
void display(node *head) {
node *temp;
printf("Content of Doubly linked list: ");
for(temp=head;temp!=NULL;temp=temp->next)
printf("%d ",temp->data);
printf("\n");
}
void display_reverse(node *tail) {
node *temp;
printf("Content of Doubly linked list in reverse: ");
for(temp=tail;temp!=NULL;temp=temp->prev)
printf("%d ",temp->data);
printf("\n");
}
void main() {
node *head=NULL,*tail=NULL;
create(&head,&tail);
display(head);
display_reverse(tail);
}

Write a program to create and display a doubly circular linked list.


#include<stdio.h>
#include<stdlib.h>
typedef struct node {
struct node *prev;
int data;
struct node *next;
}node;
node *create() {
node *head,*temp;
int x;
printf("Enter first number: ");
scanf("%d",&x);
head = (node*)malloc(sizeof(node));
head->data = x;
temp=head;
do {
printf("Enter next number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
break;
temp->next = (node*)malloc(sizeof(node));
temp->next->data = x;
temp->next->prev = temp;
temp = temp->next;
} while(x!=-999);
temp->next = head;
head->prev=temp;
return head;
}
void display(node *head) {
node *temp;
printf("Content of linked list: ");
temp=head;
do {
printf("%d ",temp->data);
temp=temp->next;
}while(temp!=head);
printf("\n");
}
void display_reverse(node *tail)
{
node *temp;
printf("Content of linked list in reverse: ");
temp=tail;
do {
printf("%d ",temp->data);
temp=temp->prev;
}while(temp!=tail);
printf("\n");
}
void main() {
node *head=NULL;
head=create();
display(head);
display_reverse(head->prev);
}

Write the algorithm for insertion and deletion of elements in Linked


list.
The general algorithm for insertion:
If the list is empty or the new node comes before the head node then
insert the new node as the first node
else if the new node comes after the last node then
insert the node as the end node
else
insert the new node in the body of the list.

The general algorithm for deletion:


If the list is empty, then
node cannot be deleted
else if the node to be deleted is the first node then
make the head points to the second node
else
delete the node from the body of the list.

How can you insert nodes in a linked list? Explain with example.
Inserting a node in a linked list
There can be four cases while inserting a node in a linked list.
 Insertion in an empty list.
 Insertion at the beginning.
 Insertion at the end.
 Insertion in between the list nodes.
To insert a node, initially we will dynamically allocate space for that node using malloc().
Suppose newnode is a pointer that points to this dynamically allocated node. If memory
is not available the newnode will have NULL, otherwise in the data part of the node we
will put the value to insert.
newnode = (node*)malloc(sizeof(node));
if (newnode == NULL) {
printf("Unable to allocate memory for the new node.\n");
return;
}
newnode->data=x;
The „next‟ part of the newnode contains garbage value. We will assign address to it
separately in the different cases.

Insertion in an empty list:


(i) Next of newnode should points to NULL.
newnode->next = NULL;
(ii) We make newnode as the first node.
*addr_head = newnode;

Insertion at the beginning:


(i) Next of newnode should contain the address of first node.
newnode->next = *addr_head;
(ii) We make newnode as the first node.
*addr_head = newnode;

Insertion at the end:


(i) Next of newnode is NULL, as it is the last node.
newnode->next = NULL;
(ii) If the list is empty, We make newnode as the first node.
*addr_head = newnode;
(iii) Otherwise, Find the address of the last node of the list. Suppose the last node of
the list is „temp‟. Next of „temp‟ will contain the address of newnode.
temp=*addr_head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newnode;

Insertion in between the list nodes:


There are three cases of insertion in between the nodes.
 Insertion after a node
 Insertion before a node
 Insertion at a given position

Insertion after a node:


(i) Find the address of the node after which we want to insert. Suppose the node is
„temp‟.
temp=*addr_head;
while(temp!=NULL && temp->data != key) /* key is the node data after
which we want to insert */
temp = temp->next;
(ii) If „temp‟ is NULL, the key is “Not present”. Otherwise, insert the newnode after
„temp‟.
if (temp==NULL)
printf(“Key Not Present\n”);
else
{
newnode->next = temp->next; /* Next of newnode will contain the
address of the node next to „temp‟ */

temp->next = newnode; /* Next of „temp‟ will contains the


address of newnode.*/
}

Insertion before a node:


(i) If the node to be inserted is before the head node, then insert the node at the
beginning.
if ((*addr_head)->data==key)
{
newnode->next = *addr_head;
*addr_head = newnode;
}
(ii) If not before head, find the address of the node after which we want to insert.
Suppose the node is „temp‟.
temp=*addr_head;
while(temp->next!=NULL && temp->next->data != key) /*key is item
before which we want to insert*/
temp = temp->next;
(iii) If „temp->next‟ is NULL, the key is “Not present”. Otherwise, insert the newnode
after „temp‟.
if (temp->next==NULL)
printf(“Key Not Present\n”);
else
{
newnode->next = temp->next; /* Next of newnode will contain the
address of the node next to „temp‟ */

temp->next = newnode; /* Next of „temp‟ will contains the


address of newnode.*/
}

Insertion at a given position (say „pos‟);


(i) If „pos‟ is 1, insert at the beginning.
if (pos==1)
{
newnode->next = *addr_head;
*addr_head = newnode;
}
(ii) if „pos‟ is not 1 find the node at (pos – 1). Suppose the node is „temp‟.
temp=*addr_head;
i=1;
while (temp!=NULL && i!=(pos-1))
{
temp = temp->next;
i++;
}
(iii) If „temp‟ is NULL, the given position is “Invalid”. Otherwise, insert the newnode
after „temp‟.
if (temp==NULL)
printf(“Invalid position”);
else
{
newnode->next=temp->next;
temp->next=newnode;
}

Write a function in C to insert a node at the beginning of an empty


singly linked list.
void insert_into_empty_list(node** addr_head, int x)
{
node* newnode = (node*)malloc(sizeof(node));
newnode->data = x;
newnode->next = NULL;
*addr_head = newnode;
}

Write a function to insert a node at the beginning of a singly linked


list.
void insert_at_beginning(node** addr_head, int x)
{
node* newnode = (node*)malloc(sizeof(node));
newnode->data = x;
newnode->next = *addr_head;
*addr_head = newnode;
}

Write a function to insert a node at the end of a singly linked list.


void insert_at_end(node** addr_head, int x)
{
node *temp;
node* newnode = (node*)malloc(sizeof(node));
newnode->data = x;
newnode->next = NULL;
if (*addr_head==NULL)
*addr_head = newnode;
else
{
temp=*addr_head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
}

Write a function to insert a node after a given key (value) in a


singly linked list.
void insert_after_key(node** addr_head, int x, int key)
{
node *temp, *newnode;
newnode = (node*)malloc(sizeof(node));
newnode->data = x;
temp=*addr_head;
while(temp!=NULL && temp->data != key)
temp=temp->next;
if (temp==NULL)
printf("Key Not Present\n");
else
{
newnode->next = temp->next;
temp->next = newnode;
}
}

Write a function to insert a node before a given key (value) in a


singly linked list.
void insert_before_key(node** addr_head, int x, int key)
{
node *temp, *newnode;
newnode = (node*)malloc(sizeof(node));
newnode->data = x;
if ((*addr_head)->data==key)
{
newnode->next = *addr_head;
*addr_head = newnode;
}
else
{
temp=*addr_head;
while(temp->next!=NULL && temp->next->data != key)
temp=temp->next;
if (temp->next==NULL)
printf("Key Not Present\n");
else
{
newnode->next = temp->next;
temp->next = newnode;
}
}
}

Write a function to insert a node at the nth position in a singly


linked list.
void insert_by_position(node** addr_head, int x, int pos)
{
node *temp, *newnode;
int i;
newnode = (node*)malloc(sizeof(node));
newnode->data = x;
if (pos==1)
{
newnode->next = *addr_head;
*addr_head = newnode;
}
else
{
temp=*addr_head;
i=1;
while(temp!=NULL && i != (pos-1))
{
temp=temp->next;
i++;
}
if (temp==NULL)
printf("Invalid Position\n");
else
{
newnode->next = temp->next;
temp->next = newnode;
}
}
}

How can you delete nodes from a list? Explain with example.
Deleting a node from a linked list
There can be three cases while deleting a node from a linked list.
 Deletion at the beginning.
 Deletion at the end.
 Deletion in between the list nodes.

Deletion at the beginning:


(i) If head is NULL, the list is “Empty”, otherwise follow the remaining steps.
if (*addr_head==NULL)
printf("List is empty\n");
(ii) Store the address of the first node in a variable say delnode.
delnode=*addr_head;
(iii) Make the second node as the head.
*addr_head = (*addr_head)->next;
(iv) Free the memory space for the first node.
free(delnode);

Deletion at the end:


(i) If head is NULL, the list is “Empty”, otherwise follow the remaining steps.
(ii) If (*addr_head)->next is NULL, delete head, otherwise follow the remaining steps.
(iii) Find the address of the second last node of the list.
temp=*addr_head;
while(temp->next->next != NULL)
temp = temp->next;
(iv) Store the address of the last node in a variable say delnode.
delnode=temp->next;
(v) Set the next of second last node to NULL.
temp->next = NULL;
(vi) Free the memory space for the last node.
free(delnode);

Deletion in between the list nodes:


There are two cases of deletion in between the nodes.
 Deletion of node by value
 Deletion of node by position

Deletion by value:
(i) If head is NULL, the list is “Empty”, otherwise follow the remaining steps.
(ii) If the value present at head node, then delete the first node from the linked list.
if ((*addr_head)->data==key)
{
delnode = *addr_head;
*addr_head = (*addr_head)->next;
free(delnode);
}
(iii) Otherwise, find the address of the node after which we want to delete. Suppose
the node is „temp‟.
temp=*addr_head;
while(temp->next!=NULL && temp->next->data != key) /* key is item
which we want to delete */
temp = temp->next;
(iv) If „temp->next‟ is NULL, the key is “Not present”. Otherwise, delete the node after
„temp‟.
if (temp->next==NULL)
printf(“Item not present”);
else {
delnode=temp->next;
temp->next = temp->next->next;
free(delnode);
}

Deletion by position (say „pos‟);


(i) If head is NULL, the list is “Empty”, otherwise follow the remaining steps
(ii) If „pos‟ is 1 delete the first node.
(iii) if „pos‟ is not 1 find the node at (pos – 1). Suppose the node is „temp‟.
temp=*addr_head;
i=1;
while (temp->next!=NULL && i!=(pos-1))
{
temp = temp->next;
i++;
}
(iv) If „temp->next‟ is NULL, the given position is “Invalid”. Otherwise, delete the node
after „temp‟.
if (temp->next==NULL)
printf(“Invalid position”);
else
{
delnode=temp->next;
temp->next=temp->next->next;
free(delnode);
}

Write a function to delete the first node from a singly linked list.
void delete_from_beginning(node **addr_head)
{
node *delnode;
if (*addr_head==NULL)
printf("List is empty\n");
else
{
delnode = *addr_head;
*addr_head = (*addr_head)->next;
free(delnode);
}
}

Write a function to delete the last node from a singly linked list.
void delete_from_end(node **addr_head)
{
node *delnode,*temp;
if (*addr_head==NULL)
printf("List is empty\n");
else if ((*addr_head)->next==NULL)
{
delnode = *addr_head;
*addr_head = (*addr_head)->next;
free(delnode);
}
else
{
temp=*addr_head;
while(temp->next->next != NULL)
temp = temp->next;
delnode = temp->next;
temp->next = NULL;
free(delnode);
}
}

Write a function to delete a node by a given value from a singly


linked list.
void delete_by_value(node **addr_head,int key)
{
node *delnode,*temp;
if (*addr_head==NULL)
printf("List is empty\n");
else if ((*addr_head)->data==key)
{
delnode = *addr_head;
*addr_head = (*addr_head)->next;
free(delnode);
}
else
{
temp=*addr_head;
while(temp->next != NULL && temp->next->data !=key)
temp = temp->next;
if (temp->next == NULL)
printf("Key not Present\n");
else
{
delnode = temp->next;
temp->next = temp->next->next;
free(delnode);
}
}
}

Write a function to delete a node at a given position from a singly


linked list.
void delete_by_position(node **addr_head, int pos)
{
node *delnode,*temp;
int i;
if (*addr_head==NULL)
printf("List is empty\n");
else if (pos==1)
{
delnode = *addr_head;
*addr_head = (*addr_head)->next;
free(delnode);
}
else
{
temp=*addr_head;
i=1;
while(temp->next != NULL && i != (pos-1))
{
temp = temp->next;
i++;
}
if (temp->next == NULL)
printf("Invalid Position\n");
else
{
delnode = temp->next;
temp->next = temp->next->next;
free(delnode);
}
}
}

Write an interactive (menu driven) C program to perform insertion


of element, deletion of element, count number of elements, display
operations in a one way (single) linked list.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
void insert_by_position(node **addr_head,int x,int pos)
{
node *newnode, *temp;
int i;
newnode = (node*)malloc(sizeof(node));
newnode->data = x;
if (pos==1)
{
newnode->next=*addr_head;
*addr_head=newnode;
}
else
{
temp = *addr_head;
i=1;
while(temp!=NULL && i!=(pos-1))
{
temp = temp->next;
i++;
}
if (temp==NULL)
printf("Invalid Position\n");
else
{
newnode->next = temp->next;
temp->next = newnode;
}
}
}
void delete_by_position(node **addr_head,int pos)
{
node *delnode, *temp;
int i;
if (*addr_head==NULL)
printf("List is empty\n");
else if (pos==1)
{
delnode=*addr_head;
*addr_head = (*addr_head)->next;
free(delnode);
}
else
{
temp = *addr_head;
i=1;
while(temp->next!=NULL && i!=(pos-1))
{
temp = temp->next;
i++;
}
if (temp->next==NULL)
printf("Invalid Position\n");
else
{
delnode = temp->next;
temp->next = temp->next->next;
free(delnode);
}
}
}
int count_nodes(node *head)
{
int count=0;
node *temp;
for(temp=head;temp!=NULL;temp=temp->next)
{
count++;
}
return count;
}
void display(node *head)
{
node *temp;
printf("Content of linked list:\t");
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("%d\t",temp->data);
}
printf("\n");
}
void main()
{
node *head=NULL;
int ch,x,pos;
do
{
printf("Menu\n1.Insert 2.Delete 3.Count 4.Display 5.Exit\n");
printf("Your choice? ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number and position: ");
scanf("%d %d",&x,&pos);
insert_by_position(&head,x,pos);
break;
case 2:
printf("Enter the position: ");
scanf("%d",&pos);
delete_by_position(&head,pos);
break;
case 3:
printf("Number of nodes: %d\n",count_nodes(head));
break;
case 4:
display(head);
break;
}
}while(ch!=5);
}

Write down the function for making a copy of a list.


#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *create()
{
node *head, *temp;
int x;
printf("Enter 1st number: ");
scanf("%d",&x);
head=(node*)malloc(sizeof(node));
head->data=x;
temp=head;
do
{
printf("Enter next number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
{
temp->next=NULL;
break;
}
temp->next=(node*)malloc(sizeof(node));
temp->next->data=x;
temp=temp->next;
}while(x!=-999);
return head;
}
node *copy_list(node *head)
{
node *temp,*copy;
if (head==NULL)
copy=NULL;
else
{
copy=(node*)malloc(sizeof(node));
copy->data = head->data;
temp=copy;
while(head->next!=NULL)
{
temp->next = (node*)malloc(sizeof(node));
temp->next->data = head->next->data;
head = head->next;
temp = temp->next;
}
temp->next=NULL;
}
return copy;
}
void display(node *head)
{
node *temp;
printf("Content of linked list:\t");
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("%d\t",temp->data);
}
printf("\n");
}
void main()
{
node *head,*copy;
head=create();
display(head);
copy=copy_list(head);
display(copy);
}

Write a function to reverse a singly linked list.


#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *create()
{
node *head, *temp;
int x;
printf("Enter 1st number: ");
scanf("%d",&x);
head=(node*)malloc(sizeof(node));
head->data=x;
temp=head;
do
{
printf("Enter next number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
break;
temp->next=(node*)malloc(sizeof(node));
temp->next->data=x;
temp=temp->next;
}while(x!=-999);
temp->next=NULL;
return head;
}
node *reverse(node *head)
{
node *newhead=NULL,*temp;
while(head!=NULL)
{
temp=head->next;

head->next=newhead;
newhead=head;

head=temp;
}
return newhead;
}
void display(node *head)
{
node *temp;
printf("Content of linked list:\t");
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("%d\t",temp->data);
}
printf("\n");
}
void main()
{
node *head;
head=create();
display(head);
head=reverse(head);
display(head);
}

Write a program to create and display a sorted linked list.


#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
void insert_proper(node **addr_head, int x)
{
node *newnode,*temp;
newnode = (node*)malloc(sizeof(node));
newnode->data = x;
if (*addr_head==NULL || (*addr_head)->data > x)
{
newnode->next = *addr_head;
*addr_head =newnode;
}
else
{
temp=*addr_head;
while(temp->next!=NULL && temp->next->data < x)
temp = temp->next;
newnode->next = temp->next;
temp->next = newnode;
}
}
node *create_sorted()
{
node *head=NULL;
int x;
do
{
printf("Enter any number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
break;
insert_proper(&head,x);
}while(x!=-999);
return head;
}

void display(node *head)


{
printf("Content of linked list: ");
while (head!=NULL)
{
printf("%d\t",head->data);
head = head->next;
}
}
void main()
{
node *head;
head=create_sorted();
display(head);
}

Write a program in C with necessary functions to merge two sorted


linked lists.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
void insert_proper(node **addr_head,int x)
{
node *newnode,*temp;
newnode = (node*)malloc(sizeof(node));
newnode->data = x;
if (*addr_head==NULL || (*addr_head)->data > x)
{
newnode->next = *addr_head;
*addr_head =newnode;
}
else
{
temp=*addr_head;
while(temp->next!=NULL && temp->next->data < x)
temp = temp->next;
newnode->next = temp->next;
temp->next = newnode;
}
}
node *create_sorted()
{
node *head=NULL;
int x;
do
{
printf("Enter any number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
break;
insert_proper(&head,x);
}while(x!=-999);
return head;
}

void display(node *head)


{
printf("Content of linked list: ");
while (head!=NULL)
{
printf("%d\t",head->data);
head = head->next;
}
printf("\n");
}
node *merge_list(node *head1, node *head2)
{
node *head3,*temp;
if (head2==NULL)
return head1;
else if (head1==NULL)
return head2;
else
{
if (head1->data < head2->data)
{
head3 = head1;
head1 = head1->next;
}
else
{
head3 = head2;
head2 = head2->next;
}
temp=head3;
while(head1!=NULL && head2!=NULL)
{
if (head1->data < head2->data)
{
temp->next = head1;
head1 = head1->next;
}
else
{
temp->next = head2;
head2 = head2->next;
}
temp=temp->next;
}
if (head1==NULL)
temp->next = head2;
else
temp->next = head1;
}
return head3;
}
void main()
{
node *head1,*head2,*head3;
printf("Elements for 1st List:\n");
head1=create_sorted();
display(head1);
printf("Elements for 2nd list:\n");
head2=create_sorted();
display(head2);
head3 = merge_list(head1,head2);
printf("Merged List:\n");
display(head3);
}
Write a program to implement Stack using linked list.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
void push(node **addr_top,int x)
{
node *newnode=(node*)malloc(sizeof(node));
if (newnode==NULL)
printf("Stack Overflow\n");
else
{
newnode->data=x;
newnode->next=*addr_top;
*addr_top=newnode;
}
}
int pop(node **addr_top)
{
node *delnode;
int x=-999;
if (*addr_top==NULL)
printf("Stack Underflow\n");
else
{
delnode = *addr_top;
x=delnode->data;
*addr_top = (*addr_top)->next;
free(delnode);
}
return x;
}
void display(node *top)
{
printf("Content of stack: ");
while(top!=NULL)
{
printf("%d\t",top->data);
top=top->next;
}
printf("\n");
}
void main()
{
int ch,x;
node *top=NULL;
do
{
printf("1.Push 2.Pop 3.Display 4.Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number to be pushed: ");
scanf("%d",&x);
push(&top,x);
break;
case 2:
x=pop(&top);
if (x!=-999)
printf("Item poped: %d\n",x);
break;
case 3:
display(top);
break;
}
}while(ch!=4);
}

Write a function to append a node and display the elements in


reverse order from the link list.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *create()
{
node *head, *temp;
int x;
printf("Enter 1st number: ");
scanf("%d",&x);
head=(node*)malloc(sizeof(node));
head->data=x;
temp=head;
do
{
printf("Enter next number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
{
temp->next=NULL;
break;
}
temp->next=(node*)malloc(sizeof(node));
temp->next->data=x;
temp=temp->next;
}while(x!=-999);
return head;
}
void append_node(node **addr_head, int x)
{
node *temp;
node* newnode = (node*)malloc(sizeof(node));
newnode->data = x;
newnode->next = NULL;
if (*addr_head==NULL)
*addr_head = newnode;
else
{
temp=*addr_head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
}
void display(node *head)
{
if (head!=NULL)
{
printf("%d\t",head->data);
display(head->next);
}
}
void display_reverse(node *head)
{
if (head!=NULL)
{
display_reverse(head->next);
printf("%d\t",head->data);
}
}
void main()
{
node *head;
int x;
head=create();
printf("Original List: ");
display(head);
printf("\nEnter number to append: ");
scanf("%d",&x);
append_node(&head,x);
printf("List after append: ");
display(head);
printf("\nList in Reverse order: ");
display_reverse(head);
}

Write a function in C to delete a node containing a specific data


element from a doubly linked list.
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
struct node *prev;
int data;
struct node *next;
}node;
void create(node **addr_head,node **addr_tail) {
node *temp;
int x;
printf("Enter first number: ");
scanf("%d",&x);
*addr_head = (node*)malloc(sizeof(node));
(*addr_head)->prev=NULL;
(*addr_head)->data = x;
temp=*(addr_head);
do {
printf("Enter next number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
break;
temp->next = (node*)malloc(sizeof(node));
temp->next->data = x;
temp->next->prev = temp;
temp = temp->next;
} while(x!=-999);
temp->next = NULL;
*addr_tail=temp;
}
void display(node *head) {
node *temp;
printf("Content of Doubly linked list: ");
for(temp=head;temp!=NULL;temp=temp->next)
printf("%d ",temp->data);
printf("\n");
}
void display_reverse(node *tail)
{
node *temp;
printf("Content of Doubly linked list in reverse: ");
for(temp=tail;temp!=NULL;temp=temp->prev)
printf("%d ",temp->data);
printf("\n");
}
void delete_by_value(node **addr_head, node **addr_tail, int key)
{
node *delnode,*temp;
if (*addr_head==NULL)
printf("List is empty\n");
else if ((*addr_head)->data==key)
{
delnode = *addr_head;
*addr_head = (*addr_head)->next;
if (*addr_head==NULL)
*addr_tail = NULL;
else
(*addr_head)->prev = NULL;
free(delnode);
}
else if ((*addr_tail)->data==key)
{
delnode=*addr_tail;
*addr_tail = (*addr_tail)->prev;
if (*addr_tail==NULL)
*addr_head=NULL;
else
(*addr_tail)->next = NULL;
free(delnode);
}
else
{
temp=*addr_head;
while(temp->next != *addr_tail && temp->next->data !=key)
temp = temp->next;
if (temp->next == *addr_tail)
printf("Key not Present\n");
else
{
delnode = temp->next;
temp->next = temp->next->next;
temp->next->prev = temp;
free(delnode);
}
}
}

void main() {
node *head=NULL,*tail=NULL;
int x;
create(&head,&tail);
display(head);
display_reverse(tail);
printf("Enter data to be deleted: ");
scanf("%d",&x);
delete_by_value(&head,&tail,x);
display(head);
display_reverse(tail);
}

Write the code for insertion in double linked list at sorted position.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct mode *prev;
int data;
struct node *next;
}node;
void insert_proper(node **addr_head, node **addr_tail, int x)
{
node *newnode,*temp;
newnode = (node*)malloc(sizeof(node));
newnode->data = x;
if (*addr_head==NULL)
{
newnode->next = newnode->prev = NULL;
*addr_head = *addr_tail = newnode;
}
else if ((*addr_head)->data > x)
{
newnode->next = *addr_head;
newnode->prev=NULL;
(*addr_head)->prev=newnode;
*addr_head =newnode;
}
else if ((*addr_tail)->data < x)
{
newnode->prev = *addr_tail;
newnode->next = NULL;
(*addr_tail)->next = newnode;
*addr_tail = newnode;
}
else
{
temp=*addr_head;
while(temp->next!=*addr_tail && temp->next->data < x)
temp = temp->next;
newnode->next = temp->next;
newnode->prev = temp;
temp->next->prev = newnode;
temp->next = newnode;
}
}
void create_sorted(node **addr_head,node **addr_tail)
{
int x;
do
{
printf("Enter any number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
break;
insert_proper(addr_head,addr_tail,x);
}while(x!=-999);
}

void display(node *head)


{
printf("Content of linked list: ");
while (head!=NULL)
{
printf("%d\t",head->data);
head = head->next;
}
printf("\n");
}
void display_reverse(node *tail)
{
node *temp;
printf("Content of Doubly linked list in reverse: ");
for(temp=tail;temp!=NULL;temp=temp->prev)
printf("%d\t",temp->data);
printf("\n");
}
void main()
{
node *head=NULL,*tail=NULL;
create_sorted(&head,&tail);
display(head);
display_reverse(tail);
}

You might also like