0% found this document useful (0 votes)
9 views43 pages

U18csi6211-Dsa Lab - Manual

The document outlines the certification format for students at Kumaraguru College of Technology, detailing the Program Educational Objectives (PEOs), Program Outcomes (POs), and Program Specific Outcomes (PSOs) for engineering graduates. It includes a list of experiments related to data structures, specifically stack and linked list implementations, along with their algorithms and sample programs. The document concludes with results and inferences drawn from the experiments conducted during the academic year 2024-25.

Uploaded by

sukesh p
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)
9 views43 pages

U18csi6211-Dsa Lab - Manual

The document outlines the certification format for students at Kumaraguru College of Technology, detailing the Program Educational Objectives (PEOs), Program Outcomes (POs), and Program Specific Outcomes (PSOs) for engineering graduates. It includes a list of experiments related to data structures, specifically stack and linked list implementations, along with their algorithms and sample programs. The document concludes with results and inferences drawn from the experiments conducted during the academic year 2024-25.

Uploaded by

sukesh p
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/ 43

Kumaraguru College of Technology

Coimbatore - 641 049

Certificate

Certified that this is the bonafide record work done by

Mr / Ms _____________________ , (Reg No: ___________ ) of ____Year,

_______ Semester of the Department of __________________________

during the Academic Year 2024 – 25 (Even Semester) in the

___________________________________________________laboratory.

Faculty In charge

Submitted for the Semester End Practical ExaminationConducted on

________________

Examiner I Examiner II
PROGRAM EDUCATIONAL OBJECTIVES (PEOs):

Our graduates will be able to


PEO1: Pursue a diverse range of careers in engineering, consultancy, and entrepreneurship.
PEO2: Contribute to continuous professional development through higher studies and life-long
learning.
PEO3: Demonstrate their technical proficiency with ethical values and social responsibility.
PEO4: Innovate and provide solutions for ever-changing global environments with familiarity
in computational platforms in electrical engineering.

PROGRAM OUTCOMES (POs):

Engineering Graduates will be able to:


PO1: Apply the knowledge of mathematics, science, engineering fundamentals, and an
engineering specialization to the solution of complex engineering problems.
PO2: Identify, formulate, review research literature, and analyse complex engineering problems
reaching substantiated conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
PO3: Design solutions for complex engineering problems and design system components or
processes that meet the specified needs with appropriate consideration for the public health and
safety, and the cultural, societal, and environmental considerations.
PO4: Use research-based knowledge and research methods including design of experiments, analysis
and interpretation of data, and synthesis of the information to provide valid conclusions.
PO5: Create, select, and apply appropriate techniques, resources, and modern engineering and IT
tools including prediction and modelling to complex engineering activities with an
understanding of the limitations.
PO6: Apply reasoning informed by the contextual knowledge to assess societal, health, safety, legal
and cultural issues and the consequent responsibilities relevant to the professional engineering
practice.
PO7: Understand the impact of the professional engineering solutions in societal and environmental
contexts, and demonstrate the knowledge of, and need for sustainable development.
PO8: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
PO9: Function effectively as an individual, and as a member or leader in diverse teams, and in
multidisciplinary settings.
PO10: Communicate effectively on complex engineering activities with the engineering community
and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
PO11: Demonstrate knowledge and understanding of the engineering and management principles and
apply these to one’s own work, as a member and leader in a team, to manage projects and in
multidisciplinary environments.
PO12: Recognize the need for and have the preparation and ability to engage in independent and life-
long learning in the broadest context of technological change.

PROGRAM SPECIFIC OUTCOMES (PSOs):

PSO1: Apply the knowledge acquired in Electrical and Electronics Engineering to technological
advancements.
PSO2: Identify suitable solutions for design and control of electrical and electronic systems.

2
INDEX

Page Mark Faculty


S. No Date Title of the Experiment
No (30) Signature

1
Array implementation of stack

2
Linked implementation stack

3
Implementation of singly linked List

4
Implementation of doubly linked List

5
Implementation of circular Queue

Implement the application for checking ‘Balanced


6
Parenthesis’ using array implementation of Stack
ADT.

Implement the application for ‘Evaluating Postfix


7
Expressions’ using linked list implementations of
Stack ADT.

8 Implement Search Tree - Binary Search Tree

9
Implement Heap Sort

10
Implement Quick Sort

Average Marks: ------------ Faculty Signature:

3
Experiment No: Date:

Title of the Experiment: ARRAY IMPLEMENTATION OF STACK

AIM:

To implement stack using array and to perform the basic operations PUSH () and POP ().

ALGORITHM
PUSH OPERATION
Step 1: Start
Step 2: Declare Stack [MAX]; //Maximum size of Stack

Step 3: Check if the stack is full or not by comparing top with (MAX-1).If the stack is full, Then
print "Stack Overflow" i.e, stack is full and cannot be pushed with another element

Step 4: Else, the stack is not full. Increment top by 1 and Set, a[top] = x which pushes the element x
into the address pointed by top // The element x is stored in a[top]

Step 5: Stop
POP OPERATION
Step 1: Start
Step 2: Declare Stack[MAX]
Step 3: Push the elements into the stack
Step 4: Check if the stack is empty or not by comparing top with base of array i.e 0. If top is less
than 0, then stack is empty, print "Stack Underflow”.

Step 5: Else, If top is greater than zero the stack is not empty, then store the value pointed by top in
a variable x=a[top] and decrement top by 1. The popped element is x

DISPLAY OPERATION
Step 1: Start
Step 2: Declare Stack [MAX]
Step 3: Push the elements into the stack.
Step 4: Print the value stored in the stack pointed by top.
Step 6: Stop

PROGRAM:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
4
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
5
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}

INFERENCE:

In an array-based implementation, the push operation is implemented by incrementing the


index of the top element and storing the new element at that index. The pop operation is
implemented by decrementing the index of the top element and returning the value stored at
that index.

RESULT:
Thus, the array implementation of stack is performed and the output is verified

6
Experiment No: Date:

Title of the Experiment: LINKED IMPLEMENTATION OF STACK

AIM:

To implement stack using linked list.


ALGORITHM:
PUSH OPERATION
Step 1 - Create a newNode with given value.
Step 2 - Check whether stack is Empty (top == NULL)
Step 3 - If it is Empty, then set newNode → next = NULL.
Step 4 - If it is Not Empty, then set newNode → next = top.
Step 5 - Finally, set top = newNode.
POP OPERATION
Step 1 - Check whether stack is Empty (top == NULL).
Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate
the function
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
Step 4 - Then set 'top = top → next'.
Step 5 - Finally, delete 'temp'. (free(temp)).
DISPLAY OPERATION
Step 1 - Check whether stack is Empty (top == NULL).
Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize with top.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same
until temp reaches to the first node in the stack. (temp → next != NULL).
Step 5 - Finally! Display 'temp → data ---> NULL

PROGRAM:
#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()
7
{
int no, ch, e;

printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");

create();

while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
8
}
}
}

/* Create empty stack */


void create()
{
top = NULL;
}

/* Count stack elements */


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++;
}

/* Display stack elements */


void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}

while (top1 != NULL)


{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}

/* Pop Operation on stack */


void pop()
9
{
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--;
}

/* Return top element */


int topelement()
{
return(top->info);
}

/* Check if stack is empty or not */


void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}

/* Destroy entire stack */


void destroy()
{
top1 = top;

while (top1 != NULL)


{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;

printf("\n All stack elements destroyed");


count = 0;
}

10
INFERENCE:
Using an array will put a restriction on the maximum capacity of the array which can lead to stack
overflow. Using a linked list over arrays is that it is possible to implement a stack that can shrink or
grow as much as needed.
RESULT:
Thus, the implementation of stack using Linked List is performed and the output is verified

11
Experiment No: Date:

Title of the Experiment: IMPLEMENTATION OF SINGLY LINKED LIST

AIM:

To implement singly linked list using C.


ALGORITHM
INSERT A NODE
Step 1 - Check if the given node exists or not.
Step 2 -If it do not exists, terminate the process.
Step 3 - If the given node exists, Make the element to be inserted as a new node. Change the next
pointer of given node to the new node. Shift the original next pointer of given node to the next
pointer of new node.
Insert at the beginning
Step 1 - Allocate memory for new node
Step 2 - Store data
Step 3 - Change next of new node to point to head
Step 4 - Change head to point to recently created node
Insert at the End
Step 1 - Allocate memory for new node
Step 2 - Store data
Step 3 - Traverse to last node
Step 4 - Change next of last node to recently created node
UPDATE A NODE
Step 1 - Find the first element of the second half of the linked list, i.e., find the middle node of the
linked list.
Step 2 - Push all elements starting from the node found above till the last of the linked list into a
stack.
Step 3 – Use a temporary pointer, temp iterate the linked list starting from the head until the stack
is not empty and modify temp→data by doing temp→data = (s.top() – temp→data) and then
pop the element from the stack.
Step 4 - If the traversal is over, finalize the modified list
DELETE A NODE
Step 1- Find the previous node of the node to be deleted.
Step 2- Change the next of the previous node.
Step 3- Free memory for the node to be deleted.
SEARCH A NODE
Step 1 -Iterate the linked list using a loop.
Step 2 - If any node has the given key value, return 1.
Step 3 -I f the program execution comes out of the loop (the given key is not present in the linked
list), return -1.

PROGRAM:
#include<stdio.h>
#include<malloc.h>

//type declaration of a node


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

//global head pointer


struct node* head = NULL;

//prototyping of the functions


struct node* create_node(int);
void insert_at_beginning(int);
void insert_at_end(int);
void insert_at_position(int, int);
void delete_at_beginning();
void delete_at_end();
void delete_at_position(int);
void print_from_beginning();
void print_from_end(struct node*);
void search_data(int);
void update_node_data(int, int);
void empty_message(void);
int size_of_list();
int getData();
int getPosition();

int main()
{

char user_active = 'Y';


int user_choice;
int data, position;

while(user_active == 'Y' || user_active == 'y')


{

printf("\n\n------ Singly Linked List -------\n");


printf("\n1. Insert a node at beginning");
printf("\n2. Insert a node at end");
printf("\n3. Insert a node at given position");
printf("\n\n4. Delete a node from beginning");
printf("\n5. Delete a node from end");
printf("\n6. Delete a node from given position");
printf("\n\n7. Print list from beginning");
printf("\n8. Print list from end");
printf("\n9. Search a node data");
printf("\n10. Update a node data");
printf("\n11. Exit");
printf("\n\n------------------------------\n");

printf("\nEnter your choice: ");


scanf("%d", &user_choice);

printf("\n------------------------------\n");
switch(user_choice)
{
13
case 1:
printf("\nInserting a node at beginning");
data = getData();
insert_at_beginning(data);
break;

case 2:
printf("\nInserting a node at end");
data = getData();
insert_at_end(data);
break;

case 3:
printf("\nInserting a node at the given position");
data = getData();
position = getPosition();
insert_at_position(data, position);
break;

case 4:
printf("\nDeleting a node from beginning\n");
delete_at_beginning();
break;

case 5:
printf("\nDeleting a node from end\n");
delete_at_end();
break;

case 6:
printf("\nDelete a node from given position\n");
position = getPosition();
delete_at_position(position);
break;

case 7:
printf("\nPrinting the list from beginning\n\n");
print_from_beginning();
break;

case 8:
printf("\nPrinting the list from end\n\n");
print_from_end(head);
break;

case 9:
printf("\nSearching the node data");
data = getData();
search_data(data);
break;

case 10:
printf("\nUpdating the node data");
data = getData();
position = getPosition();
14
update_node_data(data, position);
break;

case 11:
printf("\nProgram was terminated\n\n");
return 0;

default:
printf("\n\tInvalid Choice\n");
}

printf("\n...............................\n");
printf("\nDo you want to continue? (Y/N) : ");
fflush(stdin);
scanf(" %c", &user_active);
}

return 0;
}

/*
* Function will show an empty list message
*/
void empty_message()
{
printf("\n\tList is Empty!\n");
}

/*
* Function is used to show the memory allocation failure
*/
void memory_message()
{
printf("\nMemory can't be allocated\n");
}

/*
* Creates a new node and returns the address of that node
*/
struct node* create_node(int data)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));

if(new_node == NULL)
{
memory_message();
return NULL;
}

new_node->data = data;
new_node->next = NULL;
return new_node;
}

/*
15
* Insert the new node at the beginning of the list
*/
void insert_at_beginning(int data)
{
struct node* new_node = NULL;
new_node = create_node(data);

if(new_node != NULL)
{
new_node->next = head;
head = new_node;
printf("\n* Node with data %d was Inserted\n", data);
}
}

/*
* Insert the new node at the end of the list
*/
void insert_at_end(int data)
{
struct node* new_node = NULL;
new_node = create_node(data);

if(new_node != NULL)
{
//if list is empty
if(head == NULL)
{
head = new_node;
}
else
{
struct node* last = head;

//getting the last node


while(last->next != NULL)
{
last = last->next;
}

//link the last node next pointer to the new node


last->next = new_node;
}
printf("\n* Node with data %d was Inserted\n", data);
}
}

/*
* Insert the new node at the given position
*/
void insert_at_position(int data, int pos)
{
//calculate the size of the list
int list_size = 0;
16
list_size = size_of_list();

//if the list is empty and the position is greater than the 1
if(head == NULL && (pos <= 0 || pos > 1))
{
printf("\nInvalid position to insert a node\n");
return;
}

// if the list is not empty and the position is out of range


if(head != NULL && (pos <= 0 || pos > list_size))
{
printf("\nInvalid position to insert a node\n");
return;
}

struct node* new_node = NULL;


new_node = create_node(data);

if(new_node != NULL)
{
struct node* temp = head;

//getting the position-1 node


int count = 1;
while(count < pos-1)
{
temp = temp -> next;
count += 1;
}

//if the position is 1 then insertion at the beginning


if(pos == 1)
{
new_node->next = head;
head = new_node;
}
else
{
new_node->next = temp->next;
temp->next = new_node;
}
printf("\n* Node with data %d was Inserted\n", data);
}
}

/*
* Delete the node from the beginning of the list
*/
void delete_at_beginning()
{
if(head == NULL)
{
empty_message();
17
return;
}

struct node* temp = head;


int data = head->data;

//move head pointer to the next node to the head


head = head->next;
free(temp);

printf("\n* Node with data %d was Deleted\n", data);


}

/*
* Delete the node from the ending of the list
*/
void delete_at_end()
{
if(head == NULL)
{
empty_message();
return;
}

struct node* temp = head;


struct node* prev = NULL;
int data;

//reaching the last node


while(temp->next != NULL)
{
prev = temp;
temp = temp->next;
}

data = temp->data;

//if there is only one node


if(temp == head)
{
free(temp);
head = NULL;
}

else
{
free(temp);
prev->next = NULL;
}
printf("\n* Node with data %d was Deleted\n", data);
}

/*
18
* Deleting the node from the given position
*/
void delete_at_position(int pos)
{
//calculate the size of the list
int list_size = 0;
list_size = size_of_list();

// if the position is out of range


if(pos <= 0 || pos > list_size)
{
printf("\nInvalid position to delete a node\n");
return;
}

struct node* temp = head;


struct node* prev = NULL;
int count = 1;

while(count < pos)


{
prev = temp;
temp = temp->next;
count += 1;
}

int data = temp->data;

if(temp == head)
{
head = head->next;
free(temp);
}

else
{
prev->next = temp->next;
free(temp);
}
printf("\n* Node with data %d was Deleted\n", data);

/*
* Search the node with given data in the list
*/
void search_data(int data)
{
int position = 0;
int flag = 0;

struct node* temp = head;

while(temp != NULL)
{
19
position += 1;
if(temp->data == data)
{
flag = 1;
break;
}
temp = temp->next;
}

if(flag == 0)
{
printf("\nNode with data %d was not found!\n", data);
}
else
{
printf("\nFound data at %d position\n", position);
}
}

/*
* Update the node with the given new data
*/
void update_node_data(int new_data, int pos)
{
//calculate the size of the list
int list_size = 0;
list_size = size_of_list();

// if the position is out of range


if(pos <= 0 || pos > list_size)
{
printf("\nInvalid position to update a node\n");
return;
}

struct node* temp = head;


int count = 1;

while(count < pos)


{
temp = temp->next;
count += 1;
}

temp->data = new_data;
printf("\nUpdated node data is %d\n", new_data);
}

/*
* Prints the data from the start of the list
*/
void print_from_beginning()
{
if(head == NULL)
{
20
empty_message();
return;
}

struct node* temp = head;

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

/*
* Prints the list from the end of the list
*/
void print_from_end(struct node* head)
{
if(head == NULL)
{
return;
}
print_from_end( head->next );
printf("%d ", head->data);
}

/*
* Returns the size of the list
*/
int size_of_list()
{
struct node* temp = head;
int count = 0;

while(temp != NULL)
{
count += 1;
temp = temp->next;
}
return count;
}

/*
* Getting node data from the user
*/
int getData()
{
int data;
printf("\n\nEnter Data: ");
scanf("%d", &data);

return data;
}

/*
21
* Getting the position of the node from the user
*/
int getPosition()
{
int pos;

printf("\nEnter Position: ");


scanf("%d", &pos);

return pos;
}

INFERENCE:
Singly linked lists are fundamental data structures that strike a balance between efficiency and
flexibility. While they excel in dynamic size management and efficient insertions/deletions, they do
come with limitations, such as a lack of backward traversal and relatively inefficient access times.
RESULT:
Thus, the implementation of singly linked list using C is performed and the output is verified.

22
Experiment No: Date:

Title of the Experiment: IMPLEMENTATION OF DOUBLY LINKED LIST

AIM:

To implement doubly linked list using C


ALGORITHM:
INSERT A NODE AT THE BEGINNING
Step 1 - START
Step 2 - Create a new node with three variables: prev, data, next.
Step 3 - Store the new data in the data variable
Step 4 - If the list is empty, make the new node as head.
Step 5 - Otherwise, link the address of the existing first node to the next variable of the new node,
and assign null to the prev variable.
Step 6 - Point the head to the new node.
Step 7 - END
INSERT AT THE END
Step 1 - START
Step 2 - If the list is empty, add the node to the list and point the head to it.
Step 3 - If the list is not empty, find the last node of the list.
Step 4 - Create a link between the last node in the list and the new node
Step 5 - The new node will point to NULL as it is the new last node.
DELETE A NODE
Step 1 - If the node/element to be deleted is the head/beginning node, then make the next node as
the head node.
Step 2 - If the node/element to be deleted is not the last node, then adjust the prev of del->next.
Step 3 - If the node to be deleted is not the first node, then adjust the next of del->prev.
Step 4 - Free the memory occupied by del.
SEARCH A NODE
Step 1 - Initialize a variable, say pos, to store the position of the node containing data value X in
the doubly linked list.
Step 2 - Initialize a pointer, say temp, to store the head node of the doubly linked list.
Step 3 - Iterate over the linked list and for every node, check if data value of that node is equal
to X or not. If found to be true, then print pos. Otherwise, print -1.

PROGRAM 1:
/* C Program to Implement Doubly Linked List using Singly Linked List */
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};

void create(struct node **);


void move (struct node *);
void release(struct node **);

23
void display(struct node *);

int main()
{
struct node *p = NULL, *q = NULL;
int result, count;

printf("Enter data into the list\n");


create(&p);
printf("Displaying list:\n");
display(p);
move(p);
release (&p);

return 0;
}

void move(struct node *head)


{
struct node *p, *q;
int ch;

p = q = head;
printf("\nPointer at %d\n", head->num);
do
{
printf("Select option:\n1. Move front\n2. Move back\n3. Exit\nYour choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: if(q->next != NULL)
{
q = q->next;
printf("\nPointer at %d\n", q->num);
}
else
{
printf("\nPointer at last node %d. Cannot move ahead.\n", q->num);
}
break;
case 2: while (p->next != q)
{
p = p->next;
}
if (p == q)
{
printf("\nPointer at first node %d. Cannot move behind.\n", q->num);
}
else
{
q = p;
p = head;
printf("\nPointer at %d\n", q->num);
}
break;
24
case 3: return;
default: printf("\nInvalid choice entered. Try again\n");
}
} while (1);
}

void create(struct node **head)


{
int c, ch;
struct node *temp, *rear;

do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}

void display(struct node *head)


{
while (head != NULL)
{
printf("%d\t", head->num);
head = head->next;
}
printf("\n");
}

void release(struct node **head)


{
struct node *temp;
while ((*head) != NULL)
{
temp = *head;
(*head) = (*head)->next;
free(temp);
}
}

PROGRAM 2:
25
/ * C program to Implement the Doubly Linked List and its Operations */

#include<stdio.h>
#include<stdlib.h>

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

struct node* head = NULL;

/* functions prototyping */
void insert_at_beginning(int);
void insert_at_end(int);
void insert_at_position(int, int);
void delete_from_beginning();
void delete_from_position(int);
void delete_from_end();
void print_from_beginning();
void print_from_end(struct node*);
void search_data(int);
void update_node_data(int, int);
void list_sort();

/* helper functions */
struct node* create_node(int);
int size_of_list();
int getData();
int getPosition();
void empty_list_message();
void memory_error_message();
void invalid_position_message();

int main()
{
char user_active = 'Y';
int user_choice;
int data, position;

while (user_active == 'Y' || user_active == 'y')


{

printf("\n\n------ Doubly Linked List -------\n");


printf("\n1. Insert a node at the beginning");
printf("\n2. Insert a node at the end");
printf("\n3. Insert a node at the given position");
printf("\n\n4. Delete a node from the beginning");
printf("\n5. Delete a node from the end");
printf("\n6. Delete a node from the given position");
printf("\n\n7. Print list from the beginning");
printf("\n8. Print list from the end");
26
printf("\n9. Search a node data");
printf("\n10. Update a node data");
printf("\n11. Sort the list");
printf("\n12. Exit");
printf("\n\n------------------------------\n");

printf("\nEnter your choice: ");


scanf("%d", &user_choice);

printf("\n------------------------------\n");
switch(user_choice)
{
case 1:
printf("\nInserting a node at beginning");
data = getData();
insert_at_beginning(data);
break;

case 2:
printf("\nInserting a node at end");
data = getData();
insert_at_end(data);
break;

case 3:
printf("\nInserting a node at the given position");
data = getData();
position = getPosition();
insert_at_position(data, position);
break;

case 4:
printf("\nDeleting a node from beginning\n");
delete_from_beginning();
break;

case 5:
printf("\nDeleting a node from end\n");
delete_from_end();
break;

case 6:
printf("\nDelete a node from given position\n");
position = getPosition();
delete_from_position(position);
break;

case 7:
printf("\nPrinting the list from beginning\n\n");
print_from_beginning();
break;

case 8:
printf("\nPrinting the list from end\n\n");
print_from_end(head);
27
printf("NULL\n");
break;

case 9:
printf("\nSearching the node data");
data = getData();
search_data(data);
break;

case 10:
printf("\nUpdating the node data");
data = getData();
position = getPosition();
update_node_data(data, position);
break;

case 11:
printf("\nSorting the list\n");
list_sort();
break;

case 12:
printf("\nProgram was terminated\n\n");
return 0;

default:
printf("\n\tInvalid Choice\n");
}

printf("\n...............................\n");
printf("\nDo you want to continue? (Y/N) : ");
fflush(stdin);
scanf(" %c", &user_active);
}

return 0;
}

/* prints the message when the memory was not allocated */


void memory_error_message()
{
printf("\nMemory was not allocated!\n");
}

/* prints the message when the position is not valid for operation*/
void invalid_position_message()
{
printf("\nInvalid position!\n");
}

/* prints the message when the linked list is empty */


void empty_list_message()
{
printf("\nList is Empty!\n");
}
28
/* creates a new node dynamically */
struct node* create_node(int data)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));

if (new_node == NULL)
{
return NULL;
}
else
{
new_node->prev = NULL;
new_node->data = data;
new_node->next = NULL;
}
}

/* inserts a new node at beginning of the list */


void insert_at_beginning(int data)
{
struct node* new_node = create_node(data);

if (new_node == NULL)
{
memory_error_message();
return;
}
else if(head == NULL)
{
head = new_node;
}
else
{
head->prev = new_node;
new_node->next = head;
head = new_node;
}
printf("\n* Node with data %d was inserted \n", data);
}

/* inserts a new node at the end of the list */


void insert_at_end(int data)
{
struct node* new_node = create_node(data);

if (new_node == NULL)
{
memory_error_message();
return;
}
else if (head == NULL)
{
head = new_node;
}
29
else
{
struct node* temp = head;
//traverse to the last node
while (temp->next != NULL)
{
temp = temp = temp->next;
}
temp->next = new_node;
new_node->prev = temp;
}
printf("\n* Node with data %d was inserted \n", data);
}

/* inserts a new node at the given position */


void insert_at_position(int data, int pos)
{
struct node* new_node = create_node(data);
int size = size_of_list();

if (new_node == NULL)
{
memory_error_message();
return;
}
else if (head != NULL && (pos < 1 || pos > size))
{
invalid_position_message();
return;
}
else if (head == NULL && pos == 1)
{
head = new_node;
}
else if (head != NULL && pos == 1)
{
new_node->next = head;
head->prev = new_node;
head = new_node;
}
else
{

struct node* temp = head;


int count = 1;

//traverse to the before given position


while (++count < pos)
{
temp = temp->next;
}

temp->next->prev = new_node;
new_node->next = temp->next;

30
temp->next = new_node;
new_node->prev = temp;
}
printf("\n* Node with data %d was inserted \n", data);
}

/* deletes a node from the beginning of the list */


void delete_from_beginning()
{
if (head == NULL)
{
empty_list_message();
return;
}
struct node* temp = head;
head = head->next;
int data = temp->data;

//free the memory from the heap


free(temp);

printf("\n* Node with data %d was deleted \n", data);


}

/* deletes a node from the end of the list */


void delete_from_end()
{

if (head == NULL)
{
empty_list_message();
return;
}
struct node* temp = head;
int data = 0;

while (temp->next != NULL)


{
temp = temp->next;
}

if (temp->prev == NULL)
{
head = NULL;
}
else
{
temp->prev->next = temp->next;
}
data = temp->data;

free(temp);
printf("\n* Node with data %d was deleted \n", data);
}

31
/* deletes a node from the given position */
void delete_from_position(int pos)
{

if (head == NULL)
{
empty_list_message();
return;
}
int size = size_of_list();
struct node* temp = head;
int data = 0;

if (pos < 1 || pos > size)


{
invalid_position_message();
return;
}
else if (pos == 1)
{
head = head->next;
data = head->data;
free(temp);
printf("\n* Node with data %d was deleted \n", data);
}
else
{
int count = 0;

while (++count < pos)


{
temp = temp->next;
}

//update previous node


temp->prev->next = temp->next;

// if deleting the last node then just update the previous node
if (pos != size)
{
//update next node
temp->next->prev = temp->prev;
}
data = temp->data;

//free memory
free(temp);

printf("\n* Node with data %d was deleted \n", data);


}
}

/* prints the data of nodes from the beginning of the list */


void print_from_beginning()
{
32
struct node* temp = head;

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

/* prints the data of nodes from the end of the list */


void print_from_end(struct node* head)
{
if (head == NULL)
{
return;
}

print_from_end(head->next);
printf("%d ", head->data);
}

/* search a data node with the given value */


void search_data(int data)
{
struct node* temp = head;
int position = 0;
int flag = 0;

while (temp != NULL)


{
position += 1;
if (temp->data == data)
{
flag = 1;
break;
}
temp = temp->next;
}

if (flag == 0)
{
printf("\nNode with data %d was not found\n", data);
}
else
{
printf("\nNode found at %d position\n", position);
}
}

/* updates a node from the given position */


void update_node_data(int data, int pos)
{
if (head == NULL)
{
33
empty_list_message();
return;
}

int size = size_of_list();

if (pos < 1 || pos > size)


{
invalid_position_message();
return;
}

struct node* temp = head;


int count = 1;

while (++count < pos)


{
temp = temp->next;
}

temp->data = data;

printf("\nNode Number %d was Updated!\n", pos);


}

/* sort the linked list data using insertion sort */


void list_sort()
{
if (head == NULL)
{
empty_list_message();
return;
}
struct node* temp1 = head;
struct node* temp2 = head;
int key = 0;

while (temp1 != NULL)


{
key = temp1->data;
temp2 = temp1;

while (temp2->prev != NULL && temp2->prev->data > key)


{
temp2->data = temp2->prev->data;
temp2 = temp2->prev;
}
temp2->data = key;
temp1 = temp1->next;
}

printf("\nList was sorted!\n");


}

/* getting node data from the user */


34
int getData()
{
int data;
printf("\n\nEnter Data: ");
scanf("%d", &data);

return data;
}

/* getting node position from the user */


int getPosition()
{
int position;
printf("\nEnter Position: ");
scanf("%d", &position);

return position;
}

/* finding the size of the list */


int size_of_list()
{
struct node* temp = head;
int count = 0;

while (temp != NULL)


{
count += 1;
temp = temp->next;
}

return count;
}
INFERENCE:
A Doubly linked list is a bidirectional linked list. Traversal the list from head to tail node or tail to head
node. Unlike singly linked lists, its node has an extra pointer that points at the last node.
RESULT:
Thus, the implementation of singly linked list using C is performed and the output is verified.

35
Experiment No: Date:

Title of the Experiment: IMPLEMENTATION OF CIRCULAR QUEUE

AIM:

To implement circular queue using C and to perform enqueue and dequeue operations.
ALGORITHM
ENQUEUE OPERATION
Step 1 - Check if the queue is full
Step 2 - Set value of FRONT to 0, for the first element
Step 3 - Circularly increase the REAR index by 1 (i.e. if the rear reaches the end, next it would be
at the start of the queue)
Step 4 - Add the new element in the position pointed to by REAR
DEQUEUE OPERATION
Step 1 - Check if the queue is empty
Step 2 - Return the value pointed by FRONT
Step 3 - Circularly increase the FRONT index by 1
Step 4 - For the last element, reset the values of FRONT and REAR to -1

PROGRAM:
#include <stdio.h>
# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
36
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x; // variables declaration

while(choice<4 && choice!=0) // while loop


{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice");
scanf("%d", &choice);

switch(choice)
{

case 1:

printf("Enter the element which is to be inserted");


scanf("%d", &x);
enqueue(x);
break;
case 2:
37
dequeue();
break;
case 3:
display();

}}
return 0;
}

INFERENCE:
The circular queue is a linear data structure whose end is connected to the start and is used in the traffic
system, memory management, and CPU scheduling.

RESULT:
Thus, the circular queue is implemented using C and the output is verified.

38
Experiment No: Date:

Title of the Experiment:

AIM:

ALGORITHM
PROGRAM:

INFERENCE:
RESULT:

39
Experiment No: Date:

Title of the Experiment:

AIM:

ALGORITHM:
PROGRAM:

INFERENCE:
RESULT:

40
Experiment No: Date:

Title of the Experiment:

AIM:

ALGORITHM:
PROGRAM:

INFERENCE:
RESULT:

41
Experiment No: Date:

Title of the Experiment:

AIM:

ALGORITHM:
PROGRAM:

INFERENCE:
RESULT:

42
Experiment No: Date:

Title of the Experiment: v

AIM:

ALGORITHM:
PROGRAM:
;

INFERENCE:
RESULT:

43

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy