0% found this document useful (0 votes)
156 views77 pages

Data Structure Lab Manual

The document describes experiments conducted on linked lists, including creating a singly linked list with 3 nodes, performing various operations like traversing, inserting, and deleting elements from the front, end, and a given position of a singly linked list, and providing the code for a menu driven C program to implement these linked list operations.

Uploaded by

kunal
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)
156 views77 pages

Data Structure Lab Manual

The document describes experiments conducted on linked lists, including creating a singly linked list with 3 nodes, performing various operations like traversing, inserting, and deleting elements from the front, end, and a given position of a singly linked list, and providing the code for a menu driven C program to implement these linked list operations.

Uploaded by

kunal
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/ 77

Lab Manual

For

Computer programming
Laboratory
2020-2021

Submitted by

RUSHIKESH MANURKAR
2020UGEC017R

B.Tech 1st Year

Submitted to

DR. DHANANJOY BHAKTA


Assistant Professor
Department of Computer Science & Engineering

Indian Institute of Information Technology, Ranchi


Index
Experiment Number page number
1.) experiment 1(Design ,develop menu) 3 to 10
2.) experiment 2(sll by 3 nodes) 11to 15
3.) Experiment 3(all operation in sll) 16 to 24
4.) Experiment 4(main menu drive in sll) 25 to 37
5.) Experiment 5(main menu drive in dll) 38 to 50
6.) Experiment 6(all operation in cll) 51 to 57
7.) Experiment 7(Stack using linked list) 58 to 61
8.) Experiment 8(operations in stak using ll) 62 to 66
9.) Experiment 9(queue using linked list) 67 to 70
10.) Experiment 10(operations in queue using ll) 71 to 77
Experiment 1
Aim :- Design, Develop and Implement a menu driven
Program in C for the following Array operations
1. Creating an Array of N Integer Elements
2. Display of Array Elements with Suitable Headings
3. Inserting an Element (ELEM) at a given valid
Position (POS)
4. Deleting an Element at a given valid Position(POS)
5. Exit.
Support the program with functions for each of the
above operations.

Theory:-
Traverse(): To see the contents of the linked list, it is necessary to
traverse the given linked list. The given traverse() function traverses and prints
the content of the linked list.
• insertAtFront(): This function simply inserts an element at the
front/beginning of the linked list.

• insertAtEnd(): This function inserts an element at the end of the


linked list.

• insertAtPosition(): This function inserts an element at a specified


position in the linked list.

• deleteFirst(): This function simply deletes an element from the


front/beginning of the linked list.

• deleteEnd(): This function simply deletes an element from the end of


the linked list.

• deletePosition(): This function deletes an element from a specified


position in the linked list.
Code:-
#include<stdio.h>
#include<stdlib.h>
int a[10], pos, elem;
int n = 0;
void create();
void display();
void insert();
void del();
void main()

{
int choice;
while(1)
{
printf("\n\n~~~~MENU~~~~");
printf("\n=>1. Create an array of N integers");
printf("\n=>2. Display of array elements");
printf("\n=>3. Insert ELEM at a given POS");
printf("\n=>4. Delete an element at a given POS");
printf("\n=>5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)

{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4:del();
break;
case 5:exit(1);
break;
default:printf("\nPlease enter a valid choice:");
}
}
}
void create()
{
int i;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: ");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
}
void display()
{
int i;
if(n == 0)
{
printf("\nNo elements to display");
return;
}
printf("\nArray elements are: ");
for(i=0; i<n;i++)
printf("%d\t ", a[i]);
}
void insert()
{
int i;
if(n == 5)
{
printf("\nArray is full. Insertion is not possible");
return;
}
do
{
printf("\nEnter a valid position where element to be
inserted: ");
scanf("%d", &pos);
}
while(pos > n);
printf("\nEnter the value to be inserted: ");
scanf("%d", &elem);
for(i=n-1; i>=pos ; i--)
{
a[i+1] = a[i];
}
a[pos] = elem;
n = n+1;
display();
}
void del()
{
int i;
if(n == 0)
{
printf("\nArray is empty and no elements to
delete");
return;
}
do
{
printf("\nEnter a valid position from where
element to be deleted: ");
scanf("%d", &pos);
}
while(pos>=n);
elem = a[pos];
printf("\nDeleted element is : %d \n", elem);
for( i = pos; i< n-1; i++)
{
a[i] = a[i+1];
}
n = n-1;
display();
}
Output:-
EXPERIMENT 2
Aim:- Create Singly Linked List with three nodes
and traverse it.

• Theroy:- Traversal - access each element of the linked list


• Insertion - adds a new element
• Deletion - removes the existing elements.
Code:-
#include <stdio.h>
#include <stdlib.h>

/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;

/*
* Functions to create and display list
*/
void createList(int n);
void traverseList();

int main()
{
int n;
printf("Enter the total number of nodes: ");
scanf("%d", &n);

createList(n);

printf("\nData in the list \n");


traverseList();

return 0;
}

/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;

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

// Terminate if memory not allocated


if(head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
// Input data of node from the user
printf("Enter the data of node 1: ");
scanf("%d", &data);

head->data = data; // Link data field with data


head->next = NULL; // Link address field to NULL

// Create n - 1 nodes and add to list


temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}

printf("Enter the data of node %d: ", i);


scanf("%d", &data);

newNode->data = data; // Link data field of newNode


newNode->next = NULL; // Make sure new node points to NULL
temp->next = newNode; // Link previous node with newNode
temp = temp->next; // Make current node as previous node
}
}

/*
* Display entire list
*/
void traverseList()
{
struct node *temp;

// Return if list is empty


if(head == NULL)
{
printf("List is empty.");
return;
}

temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
}
Output:-
Experiment 3
Aim:- Write a menu driven C program for Singly
Linked List (SLL) for given operations:

1.) Traverse

2.) Insert at front

3.) Insert at end

4.) Insert at given position

5.) Delete at front

6.) Delete at end

7.) Delete at given position

Theory:- Traverse(): To see the contents of the linked list, it is necessary to


traverse the given linked list. The given traverse() function traverses and prints
the content of the linked list.
• insertAtFront(): This function simply inserts an element at the
front/beginning of the linked list.

• insertAtEnd(): This function inserts an element at the end of the


linked list.

• insertAtPosition(): This function inserts an element at a specified


position in the linked list.

• deleteFirst(): This function simply deletes an element from the


front/beginning of the linked list.
• deleteEnd(): This function simply deletes an element from the end of
the linked list.

• deletePosition(): This function deletes an element from a specified


position in the linked list.

Code:-
#include <stdio.h>
#include <stdlib.h>

struct node{
int info;
struct node *next
};
struct node *head=NULL;
void insertatfront()
{
int data;
struct node *temp;
temp=malloc(sizeof(struct node));
printf("Enter The NO. to inserted ...!");
scanf("%d",&data);
temp->info=data;

temp->next=head; //pointer of temp will be assigned


head=temp;
}
void insertatend()
{
int data;
struct node *temp,*start;
temp=malloc(sizeof(struct node));
printf("Enter The number to insert");
scanf("%d",&data);

temp->next=0;
temp->info=data; //change links of nodes
start=head;
while(head->next!=NULL);
{
head=head->next;
}
head->next=temp;
}
void insertat_position()
{
struct node *temp , *newnode;
int pos,data=1;
int i=1;
newnode=malloc(sizeof(struct node));
printf("Enter position & data");
scanf("%d%d",&pos,&data);

temp=head;
newnode->info=data;
newnode->next=0;
while(i < pos-1)
{
temp = temp -> next;
i++;
}
newnode->next=temp->next;
temp->next=newnode;
}
void delectatfront()
{
struct node *temp;
if(head==NULL)
{
printf("List is EMPTY \n");
}
else
{
temp=head;
head=head->next;
free(temp);
}

void delectatend()
{
struct node *temp,*prevnode;
if(head==NULL)
{
printf("List not EXIST\n");
}
temp=head;
while(temp->next!=0)
{
prevnode=temp;
temp=temp->next;
}
free(temp);
prevnode->next=0;

}
void delectatpostion()
{
struct node *temp,*position;
int i=1,pos;
if(head==NULL)
{
printf("The list is EXIST");
}
else
{
printf("Enter the Position");
scanf("%d",&pos);
position=malloc(sizeof(struct node));
temp=head;

while(i < pos-1) //Traverse


{
temp=temp->next;
i++;
}
position=temp->next;
temp->next=position->next;
free(position);
}

}
void traverse()
{
struct node *temp;
if(head==NULL)
{
printf("List is Empty");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d",temp->info);
temp=temp->next;
}
}
}
int main()
{
int choice;
while(1)
{
printf("\n1.Insert the node at beginning");
printf("\n2.Insert the node at end");
printf("\n3.Insert the node at any position");
printf("\n4.Delecte your node at beginning");
printf("\n5.Delecte your node at end");
printf("\n6.Delecte your node from any position");
printf("\n7.view your linked list");
printf("\n8.exist");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{

case 1:
insertatfront();
break;
case 2:
insertatend();
break;
case 3:
insertat_position();
break;
case 4:
delectatfront();
break;
case 5:
delectatend();
break;
case 6:
delectatpostion();
break;
case 7:
traverse();
break;
case 8:
exit(0);
break;
default:
printf("Invalid input\n ");
}
}
return 0;
}
Output:-
Experiment 4
Aim:- Write a menu driven C program for Singly
Linked List (SLL) for given operations:
1.) Traverse
2.) Maximum
3.) Mean
4.) Sort
5.) Reversal
Theroy:-
• Traverse(): To see the contents of the linked list, it is necessary to
traverse the given linked list. The given traverse() function traverses
and prints the content of the linked list.
• InsertAtFront(): This function simply inserts an element at the
front/beginning of the linked list.
• InsertAtEnd(): This function inserts an element at the end of the
linked list.
• InsertAtPosition(): This function inserts an element at a specified
position in the linked list.
• DeleteFirst(): This function simply deletes an element from the
front/beginning of the linked list.
• DeleteEnd(): This function simply deletes an element from the end of
the linked list.
• DeletePosition(): This function deletes an element from a specified
position in the linked list.
• Maximum(): This function finds the maximum element in a linked list.
• Mean(): This function finds the mean of the elements in a linked list.
• Sort(): This function sort the given linked list in ascending order.
• ReverseLL(): This function reverses the given linked list.
Code:-
// C program for the all operations in
// the Singly Linked List
#include <stdio.h>
#include<stdlib.h>

// Linked List Node


struct node {
int info;
struct node* link;
};
struct node* start = NULL;

// Function to traverse the linked list


void traverse()
{
struct node* temp;

// List is empty
if (start == NULL)
printf("\nList is empty\n");

// Else print the LL


else {
temp = start;
while (temp != NULL) {
printf("Data = %d\n",
temp->info);
temp = temp->link;
}
}
}

// Function to insert at the front


// of the linked list
void insertAtFront()
{
int data;
struct node* temp;
temp = malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
temp->info = data;

// Pointer of temp will be


// assigned to start
temp->link = start;
start = temp;
}

// Function to insert at the end of


// the linked list
void insertAtEnd()
{
int data;
struct node *temp, *head;
temp = malloc(sizeof(struct node));

// Enter the number


printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);

// Changes links
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL) {
head = head->link;
}
head->link = temp;
}
// Function to insert at any specified
// position in the linked list
void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = malloc(sizeof(struct node));

// Enter the position and data


printf("\nEnter position and data :");
scanf("%d %d", &pos, &data);

// Change Links
temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1) {
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}

// Function to delete from the front


// of the linked list
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->link;
free(temp);
}
}
// Function to delete from the end
// of the linked list
void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\nList is Empty\n");
else {
temp = start;
while (temp->link != 0) {
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}

// Function to delete from any specified


// position from the linked list
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;

// If LL is empty
if (start == NULL)
printf("\nList is empty\n");

// Otherwise
else {
printf("\nEnter index : ");

// Position to be deleted
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;

// Traverse till position


while (i < pos - 1) {
temp = temp->link;
i++;
}

// Change Links
position = temp->link;
temp->link = position->link;

// Free memory
free(position);
}
}

// Function to find the maximum element


// in the linked list
void maximum()
{
int a[10];
int i;
struct node* temp;

// If LL is empty
if (start == NULL)
printf("\nList is empty\n");

// Otherwise
else {
temp = start;
int max = temp->info;

// Traverse LL and update the


// maximum element
while (temp != NULL) {
// Update the maximum
// element
if (max < temp->info)
max = temp->info;
temp = temp->link;
}
printf("\nMaximum number "
"is : %d ",
max);
}
}

// Function to find the mean of the


// elements in the linked list
void mean()
{
int a[10];
int i;
struct node* temp;

// If LL is empty
if (start == NULL)
printf("\nList is empty\n");

// Otherwise
else {
temp = start;

// Stores the sum and count of


// element in the LL
int sum = 0, count = 0;
float m;

// Traverse the LL
while (temp != NULL) {
// Update the sum
sum = sum + temp->info;
temp = temp->link;
count++;
}

// Find the mean


m = sum / count;

// Print the mean value


printf("\nMean is %f ", m);
}
}

// Function to sort the linked list


// in ascending order
void sort()
{
struct node* current = start;
struct node* index = NULL;
int temp;

// If LL is empty
if (start == NULL) {
return;
}

// Else
else {

// Traverse the LL
while (current != NULL) {
index = current->link;

// Traverse the LL nestedly


// and find the minimum
// element
while (index != NULL) {

// Swap with it the value


// at current
if (current->info > index->info) {
temp = current->info;
current->info = index->info;
index->info = temp;
}
index = index->link;
}

// Update the current


current = current->link;
}
}
}

// Function to reverse the linked list


void reverseLL()
{
struct node *t1, *t2, *temp;
t1 = t2 = NULL;

// If LL is empty
if (start == NULL)
printf("List is empty\n");

// Else
else {

// Traverse the LL
while (start != NULL) {

// reversing of points
t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
}
start = t1;

// New head Node


temp = start;

printf("Reversed linked "


"list is : ");

// Print the LL
while (temp != NULL) {
printf("%d ", temp->info);
temp = temp->link;
}
}
}

// Driver Code
int main()
{
int choice;
while (1) {

printf("\n\t1 To see list\n");


printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To find maximum among"
" the elements\n");
printf("\t9 To find mean of "
"the elements\n");
printf("\t10 To sort element\n");
printf("\t11 To reverse the "
"linked list\n");
printf("\t12 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
maximum();
break;
case 9:
mean();
break;
case 10:
sort();
break;
case 11:
reverseLL();
break;
case 12:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}
Output:-
Experiment 5
Aim:- Write a menu driven C program for Doubly
Linked List (DLL) for given operations:

1.) Traverse

2.) Insert at front

3.) Insert at end

4.) Insert at given position

5.) Delete at front

6.) Delete at end

7.) Delete at given position

Theory:-

traverse(): To see the contents of the linked list, it is necessary to


traverse the given doubly linked list. The given traverse() function traverses and
prints the content of the doubly linked list.
• insertAtFront(): This function simply inserts an element at the
front/beginning of the doubly linked list.
• insertAtEnd(): This function inserts an element at the end of the
doubly linked list.
• insertAtPosition(): This function inserts an element at a specified
position in the doubly linked list.
• deleteFirst(): This function simply deletes an element from the
front/beginning of the doubly linked list.
• deleteEnd(): This function simply deletes an element from the end of
the doubly linked list.
• deletePosition(): This function deletes an element from a specified
position in the doubly linked list.
Code:-
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

//definition of the basic element of the linked list, node


struct node
{
int data;
struct node * prev;
struct node * next;
}*head, *end;

//Prototype definitions of the functions


void createList(int n);
void displayList();
void revdisplaylist();
void insertAtBeginning(int data);
void insertAtEnd(int data);
void insertAtBN(int data, int position);
void DlListDeleteFirstNode();
void DlListDeleteendNode();
void DlListDeleteAnyNode(int pos);

int main()
{

int n, data, choice=1, a,insPlc;


head = NULL;
end = NULL;
printf("\n CREATE A DOUBLY LINKED LIST ");
printf("\n-----------------------------------------\n");
printf("\n Enter the total number of nodes: ");
scanf("%d",&n);
createList(n);
displayList();
revdisplaylist();

while(choice != 7)
{
printf("\nDOUBLY LINKED LIST PROGRAM\n");

printf("1. Insert node at beginning\n");


printf("2. Insert node at end\n");
printf("3. Insert node at a particular position\n");
printf("4. Delete the first node\n");
printf("5. Delete the end node\n");
printf("6. Delete the node at a particular position \n");
printf("7. Exit\n");

printf(" Enter your choice : ");


scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\n Enter data of first node : ");
scanf("%d",&data);

insertAtBeginning(data);
displayList();
revdisplaylist();
break;
case 2:
printf("\n Enter data of end node : ");
scanf("%d",&data);

insertAtEnd(data);
displayList();
revdisplaylist();
break;
case 3:
printf("\n Enter the position(0 to Totalnode+1) where you
want to insert new node: ");
scanf("%d",&n);
printf("\n Enter inserting data value: ");
scanf("%d",&data);

insertAtBN(data,n);
displayList();
revdisplaylist();
break;
case 4:
DlListDeleteFirstNode();
displayList();
revdisplaylist();
break;

case 5:
DlListDeleteendNode();
displayList();
revdisplaylist();
break;

case 6:
printf(" Enter the position(1 to Totalnode) to delete a node :
");
scanf("%d",&insPlc);

DlListDeleteAnyNode(insPlc);
displayList();
revdisplaylist();
break;

case 7:
printf("Stop program.Exit ");
break;
default:
printf("\n Error! Invalid choice...Try again. ");
}
}
return 0;
}

void createList(int n)
{

int i, data;
struct node *newNode;

//Creating the list


if(n >= 1)
{
head = (struct node *)malloc(sizeof(struct node));

printf(" Enter data of first node: ");


scanf("%d",&data);

head->data = data;
head->prev = NULL;
head->next = NULL;

end = head;

for(i=2; i<=n; i++)


{

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

printf(" Enter data of next node: ");


scanf("%d", &data);
newNode->data = data;
newNode->prev = end;
newNode->next = NULL;

end->next = newNode;
end = newNode;
}

}
}

void displayList()
{

struct node * temp;


int n = 1;

//If the linked list is empty


if(head == NULL)
{
printf("List is empty.\n");
}
else
{

temp = head;
printf("\n Data in the list:\n");

while(temp != NULL)
{
printf("Data of %d node = %d\n", n, temp->data);

n++;
//Move the current pointer to next node
temp = temp->next;
}
}
}

//Function of reverse the display list


void revdisplaylist()
{
struct node *list = end;
printf("\n\nThe list printed from back to front is :\n");

while(list!=head)
{
printf("%4d",list->data);
list=list->prev;
}
printf("%4d\n",list->data);
}

//function to insert a node at the begining of the list


void insertAtBeginning(int data)
{
//declaring a newe node
struct node * newNode;

//if the list is empty


if(head == NULL)
{
printf("\n Error! List is Empty!\n");
}
else
{
//allocating memory fo the node
newNode = (struct node *)malloc(sizeof(struct node));

newNode->data = data;
newNode->next = head; // Point to next node which is currently
head
newNode->prev = NULL; // Previous node of first node is NULL

// Link previous address field of head with newnode


head->prev = newNode;

// Make the new node as head node


head = newNode;
}
}

//function to add a node at the end of the list


void insertAtEnd(int data)
{
//declaratrion opf a new node
struct node * newNode;

//if the node is empty


if(end == NULL)
{
printf("\n Error! List is empty!\n");
}
else
{
//allocating memoty for the new node
newNode = (struct node *)malloc(sizeof(struct node));

//inserting the node


newNode->data = data;
newNode->next = NULL;
newNode->prev = end;

end->next = newNode;
end = newNode;
}
}
//function to insert a node at nth position of a doubly linked list
void insertAtBN(int data, int position)
{
//declaration of variables
int i;
struct node * newNode, *temp;

//if the list is empty


if(head == NULL)
{
printf("\n Error! List is empty!\n");
}
else
{

temp = head;
i=1;

while(i<position-1 && temp!=NULL)


{
temp = temp->next;
i++;
}

//when the entered postion is 1


if(position == 0 || position == 1)
{
//calling the dfunction to insert at the begining
insertAtBeginning(data);
}
//the the position is nth
else if(temp == end)
{
//calling the function tp insert at the end
insertAtEnd(data);
}
else if(temp!=NULL)
{
//allocating the memory for the new node
newNode = (struct node *)malloc(sizeof(struct node));

newNode->data = data;
newNode->next = temp->next; // Connect new node with
n+1th node
newNode->prev = temp; // Connect new node with n-1th
node

if(temp->next != NULL)
{
//Connect n+1th node with new node
temp->next->prev = newNode;
}
// Connect n-1th node with new node
temp->next = newNode;
}
else
{
//If the positon is invalid
printf("\n Error! Invalid position...Try again.\n");
}
}
}

//defining the function to delete the first node of the list


void DlListDeleteFirstNode()
{
//creating a new node
struct node * NodeToDel;

//if the list is empty


if(head == NULL)
{
printf("\n Delete is not possible.List is Empty.\n");
}
else
{
NodeToDel = head;
head = head->next; // move the next address of starting node
to 2 node
head->prev = NULL; // set previous address of staring node
is NULL
free(NodeToDel); // delete the first node from memory
}
}

void DlListDeleteendNode()
{
struct node * NodeToDel;

if(end == NULL)
{
printf(" Delete is not possible.List is Empty.\n");
}
else
{
NodeToDel = end;
end = end->prev; // move the previous address of the end
node to 2nd end node
end->next = NULL; // set the next address of end node to
NULL
free(NodeToDel); // delete the end node
}
}

//Function to delete a node at a particular position of the list


void DlListDeleteAnyNode(int pos)
{
//declaring a new node-type pointer
struct node *curNode;
int i;
curNode = head;

//deleting the node


for(i=1; i<pos && curNode!=NULL; i++)
{
curNode = curNode->next;
}

if(pos == 1)
{
DlListDeleteFirstNode();
}
else if(curNode == end)
{
DlListDeleteendNode();
}
else if(curNode != NULL)
{
curNode->prev->next= curNode->next;
curNode->next->prev = curNode->prev;

free(curNode); //Delete the n node


}
else
{
printf(" Invalid position!...Try again.\n");
}
}
Output:-
Experiment 6
Aim:- Write a menu driven C program for Circularly
Linked List (CLL) for given operations:

1.) Traverse

2.) Insert at front

3.) Insert at end

4.) Insert at given position

5.) Delete at front

6.) Delete at end

7.) Delete at given position

Theory:-

Circular Linked List is a variation of Linked list in which the first


element points to the last element and the last element points to
the first element. Both Singly Linked List and Doubly Linked List
can be made into a circular linked list.
Code:-
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node* next;
};
struct node* last = NULL;
void insertAtFront()
{
int data;
struct node * temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data to be inserted: \n");
scanf("%d", &data);
if (last == NULL)
{
temp->info = data;
temp->next = temp;
last = temp;
} else
{
temp->info = data;
temp->next = last->next;
last->next = temp;
}
}
void traverse()
{
if (last == NULL)
printf("\nList is empty\n");
else
{
struct node* temp;
temp = last->next;
do
{
printf("\nData = %d", temp->info);
temp = temp->next;
}
while (temp != last->next);
}}
void insertAtEnd()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data to be inserted : \n");
scanf("%d", &data);
if (last == NULL)
{
temp->info = data;
temp->next = temp;
last = temp;
}
else
{
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
} void insertafter()
{
int data, value;
struct node *temp, *n;
printf("\nEnter number after which you want to enter number: \n");
scanf("%d", &value);
temp = last->next;
do
{
if (temp->info == value)
{
n = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data to be inserted : \n");
scanf("%d", &data);
n->info = data;
n->next = temp->next;
temp->next = n;
if (temp == last)
last = n;
break;
}
else temp = temp->next;
}
while (temp != last->next);
}
void deletefirst()
{
struct node* temp;
if (last == NULL)
printf("\nList is empty.\n");
else
{
temp = last->next;
last->next = temp->next;
free(temp);
}
}
void deletelast()
{
struct node* temp;
if (last == NULL)
printf("\nList is empty.\n"); temp = last->next;
while (temp->next != last)
temp = temp->next;
temp->next = last->next;
last = temp;
}
void deleteAtIndex()
{
int pos, i = 1;
struct node *temp, *position;
temp = last->next;
if (last == NULL)
printf("\nList is empty.\n");
else
{
printf("\nEnter index : ");
scanf("%d", &pos);
while (i <= pos - 1)
{
temp = temp->next;
i++;
} position = temp->next;
temp->next = position->next;
free(position);
}
}
int main()
{
int ch;
while(1)
{
printf("\n1. Insert a Node at beginning\n");
printf("2. Insert a Node at end\n");
printf("3. Insert a Node at any position\n");
printf("4. Delete your Node from beginning\n");
printf("5. Delete your Node from end\n");
printf("6. Delete your Node from any position\n");
printf("7. View your link list\n");
printf("8. Exit\n");
printf("Enter a choice\n");
scanf("%d",&ch);
switch(ch) {
case 1:
insertAtFront();
break;
case 2:
insertAtEnd();
break;
case 3:
insertafter();
break;
case 4:
deletefirst();
break;
case 5:
deletelast();
break;
case 6:
deleteAtIndex();
break;
case 7:
traverse();
break;
case 8: exit(0);
break;
default:
printf("Invalid Choice\n");
}
}
return(0);
}
Output:-
Experiment 7
Aim:- Write a C program to implement stack
using linked list.

Theory :- Stack is like first come last out.


Push Operation : We will dynamically allocate memory for a struct node
variable(let's say temp). Then we will attach new node in front of linked list by
setting temp- >next = top. Finally set top pointer to temp.(top = temp;)
Pop Operation : Remove head node(pointed by top pointer) of the linked
list. Store the top pointer in a temp variable. Now, move top pointer to next
node(top = top->next;). Deallocate memory of temp node using free.
Peek Operation : Returns the value of head node of linked list without
removing it.(return top->data;)
Is Empty Check : Check if top pointer is NULL or not. If top pointer is null
then stack is empty otherwise non-empty.
Code:-
#include <stdio.h>
#include <stdlib.h>

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

void initialize() {
top = NULL;
}

int isEmpty() {
if (top == NULL)
return 1;
else
return 0;
}
int peek() {
return top->data;
}

int getStackSize(struct node *head){

if (head == NULL) {
printf("Error : Invalid stack pointer !!!\n");
return;
}

int length = 0;
while(head != NULL){
head = head->next;
length++;
}
return length;
}

void push(int num) {


struct node *temp;
temp =(struct node *)malloc(1*sizeof(struct node));
temp->data = num;

if (top == NULL) {
top = temp;
top->next = NULL;
} else {
temp->next = top;
top = temp;
}
}

void pop() {
struct node *temp;
if (isEmpty(top)) {
printf("\nStack is Empty\n");
return;
} else {
temp = top;
top = top->next;
printf("Removed Element : %d\n", temp->data);
free(temp);
}
}

void printStack(struct node *nodePtr) {


while (nodePtr != NULL) {
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
printf("\n");
}

void main()
{
initialize();
push(10);
push(12);
push(31);
push(41);
printf("Stack Size : %d\n", getStackSize(top));
printf("\nTop Element : %d\n", peek());
printf("Stack as linked List\n");
printStack(top);
pop();
pop();
pop();
pop();
pop();
printStack(top);
return;
}

Output:-
Experiment 8
Aim :- Write a C program to perform following
operations on stack using linked list:
1.) Traverse the stack.
2.) Insert a data on stack.
3.) Delete the data from stack.
4.) Access the top most element from the stack.

Theory :-
1.) Push Operation : We will dynamically allocate memory for a struct node
variable(let's say temp). Then we will attach new node in front of linked
list by setting temp- >next = top. Finally set top pointer to temp.(top =
temp;)
2.) Pop Operation : Remove head node(pointed by top pointer) of the
linked list. Store the top pointer in a temp variable. Now, move top
pointer to next node(top = top->next;). Deallocate memory of temp node
using free.
3.) Peek Operation : Returns the value of head node of linked list without
removing it.(return top->data;).
Code:-
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1.Dipslay");
printf("\n 2.Push");
printf("\n 3.Pop");
printf("\n 4.Top");
printf("\n 5.Exit");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
display();
break;
case 2:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 3:
pop();
break;
case 4:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 5:
exit(0);
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
top = NULL;
}
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;

int topelement()
{
return(top->info);
}
Output:-
Experiment 9
Aim :- Write a C program to implement queue using linked list.

Theory :- The queue is a linear data structure used to represent a linear list. It
allows insertion of an element to be done at one end and deletion of an element to
be performed at the other end.

Code:-

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
} *front, *back;
void initialize() {
front = back = NULL;
}
int getQueueSize()
{
struct node *temp = front;
int count = 0;
if(front == NULL && back == NULL)
return 0;
while(temp != back){
count++;
temp = temp->next;
}
if(temp == back)
count++;

return count;
}
int getFrontElement() {
return front->data;
}

int getBackElement() {
return back->data;
}
void isEmpty() {
if (front == NULL && back == NULL)
printf("Empty Queue\n");
else
printf("Queue is not Empty\n");
}
void enqueue(int num) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
temp->next = NULL;

if (back == NULL) {
front = back = temp;
} else {
back->next = temp;
back = temp;
}
}

void dequeue() {
struct node *temp;
if (front == NULL) {
printf("\nQueue is Empty \n");
return;
} else {
temp = front;
front = front->next;
if(front == NULL){
back = NULL;
}
printf("Removed Element : %d\n", temp->data);
free(temp);
}
}
void printQueue() {
struct node *temp = front;

if ((front == NULL) && (back == NULL)) {


printf("Queue is Empty\n");
return;
}
while (temp != NULL) {
printf("%d", temp->data);
temp = temp->next;
if(temp != NULL)
printf("-->");
}
}
int main()
{
initialize();
enqueue(11);
enqueue(32);
enqueue(74);
enqueue(52);
enqueue(100);
printQueue();
printf("\nSize of Queue : %d\n", getQueueSize());
printf("Front Element : %d\n", getFrontElement());
printf("Rear Element : %d\n", getBackElement());
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
return 0;
}

Output:-
Experiment 10
Aim:- Write a C program to perform following
operations on Queue using linked list:
1.) Traverse the queue.
2.) Insert a data value.
3.) Delete the data value.
4.) Access the front item from the queue.
5.) Access the last item from the queue.
Theory :- Queue is type of data structure in which first come first out rule
held.

Code :-
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;

int frontelement();
void enq(int data);
void deq();
void empty();
void display(); //traverse
void create();
void queuesize();

int count = 0;
void main()
{
int no, ch, e;
printf("\n 1.Enque");
printf("\n 2.Deque");
printf("\n 3.Front element");
printf("\n 4.Empty");
printf("\n 5.Exit");
printf("\n 6.Display");
printf("\n 7.Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}

void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("\n Queue size : %d", count);
}
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}

void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
Output:-

You might also like