0% found this document useful (0 votes)
353 views38 pages

BCS351 Data Structures Lab Solutions

Uploaded by

SACHIN VERMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
353 views38 pages

BCS351 Data Structures Lab Solutions

Uploaded by

SACHIN VERMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &

Research,Bareilly

DATA STRUCTURE
LAB SOLUTION
BCS351
Semester: III Year: SECOND
Session: 2024-25
Department of Computer Science and Engineering

1
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

List of Experiments (Indicative & not limited to)

1. Implementing Sorting Techniques: Bubble Sort, Insertion Sort, Selection Sort, Shell, Sort, Radix Sort, Quick
sort

2. Implementing Searching and Hashing Techniques: Linear search, Binary search, Methods for Hashing:

Modulo Division, Digit Extraction, Fold shift, Fold Boundary, Linear Probe for Collision Resolution.

Direct and Subtraction hashing

3. Implementing Stacks: Array implementation, Linked List implementation, Evaluation of postfix expression and
balancing of parenthesis, Conversion of infix notation to postfix notation

4. Implementing Queue: Linked List implementation of ordinary queue, Array implementation of circular queue,
Linked List implementation of priority queue, Double ended queue

5. Implementing Linked List: Singly Linked Lists, Circular Linked List, Doubly Linked Lists: Insert, Display,
Delete, Search, Count, Reverse (SLL), Polynomial, Addition, Comparative study of arrays and linked list

6. Implementing Trees: Binary search tree: Create, Recursive traversal: preorder, post order, in order,

Search Largest, Node, Smallest Node, Count Number of nodes, Heap: Min Heap, Max Heap: reheap up, reheap
Down, Delete, Expression Tree, Heap sort

7. Implementing Graphs: Represent a graph using the Adjacency Matrix, BFS, Find the minimum spanning tree
(using any method Kruskal’s Algorithm or Prim’s Algorithm) Self Learning Topics :Shortest Path Algorithm

2
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

1. Implementing Sorting Techniques: Bubble Sort, Insertion Sort, Selection Sort, Shell, Sort, Radix Sort, Quick
sort

Bubble Sort:

#include<stdio.h>
#include<conio.h>
void main()
{
int array[100],size,i,j,temp;
printf("Enter the size of the array:");
scanf("%d",&size);
printf("\nEnter the elements:");
for(i=0;i<size;i++)
{
scanf("\n%d",&array[i]);
}
for(i=0;i<size;i++)
{
for(j=0;j<size-i-1;j++)
{
if(array[j+1]<array[j])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
printf("\nThe sorted array is:");
for(i=0;i<size;i++)
{
printf("\n%d",array[i]);
}
}

3
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

Insertion Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100],size,i,j,temp;
printf("Enter the size of the array:");
scanf("%d",&size);
printf("\nEnter the elements:");
for(i=0;i<size;i++)
{
scanf("\n%d",&array[i]);
}
for(i=1;i<size;i++)
{
temp=array[i];
j=i-1;
while(j>=0 && temp<=array[j])
{
array[j+1]=array[j];
j--;
}
array[j+1]=temp;
}
printf("\nThe sorted array is:");
for(i=0;i<size;i++)
{
printf("\n%d",array[i]);
}
getch();
}

4
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

Selection Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100],size,i,j,temp,pos;
printf("Enter the size of the array:");
scanf("%d",&size);
printf("\nEnter the elements:");
for(i=0;i<size;i++)
{
scanf("\n%d",&array[i]);
}
for(i=0;i<size;i++)
{
pos=i;
temp=array[i];
for(j=i+1;j<size;j++)
{
if(array[j]<temp)
{
temp=array[j];
pos=j;
}
temp=array[i];
array[i]=array[pos];
array[pos]=temp;
}
}
printf("\nThe sorted array is:");
for(i=0;i<size;i++)
{
printf("\n%d",array[i]);
}
getch();
}

5
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

Shell Sort
#include<stdio.h>
#include<conio.h>
void ShellSort(int a[], int n)
{
int i, j, increment, tmp;
for(increment = n/2; increment > 0; increment /= 2)
{
for(i = increment; i < n; i++)
{
tmp = a[i];
for(j = i; j >= increment; j -= increment)
{
if(tmp < a[j-increment])
a[j] = a[j-increment];
else
break;
}
a[j] = tmp;
}
}
}
void main()
{
int i, n, a[10];
printf("Enter the number of elements :: ");
scanf("%d",&n);
printf("Enter the elements :: ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
ShellSort(a,n);
printf("The sorted elements are :: ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);

6
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
printf("\n");
getch();
}

Radix Sort
#include<stdio.h>
#include <conio.h>
#include <math.h>
void main()
{ int a[100] [100],r=0,c=0,i,sz,b[50],temp;

clrscr();

printf("Size of array: ");

scanf("%d", &sz);

printf("\n");
for(r=0;r<100;r++)

{
for(c=0;c<100;c++)

a[r][c]=1000;

}
for(i=0;i<sz;i++)

{ printf("Enter element %d: ",i+1);

scanf("%d", &b[i]);

r=b[i]/100;

c=b[i]%100;

a[r][c]=b[i];

}
7
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
for(r=0;r<100;r++)

{ for(c=0;c<100;c++)

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

{ if(a[r][c]==b[i])

{ printf("\n\t");

printf("%d", a[r][c]);

}
getch();

Quick sort
#include<stdio.h>
#include<conio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
8
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
void main(){
int i, count, number[25];
clrscr();
printf("Enter the element:");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
getch();
}

Implementing Searching and Hashing Techniques: Linear search, Binary search, Methods for Hashing:
Modulo Division, Digit Extraction, Fold shift, Fold Boundary, Linear Probe for Collision Resolution.
Direct and Subtraction hashing

Linear search:
#include <stdio.h>
#include<conio.h>
int main()

9
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
{
int a[10], i, item,n;
printf("\nEnter number of elements of an array:\n");
scanf("%d",&n);
printf("\nEnter elements: \n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
printf("\nItem found at location %d", i+1);
break;
}
if (i > 9)
printf("\nItem does not exist.");
getch();
}

Output:
Enter number of elements of an array:
8
Enter elements:
23578641

Enter item to search: 1

Item found at location 8

Binary search
#include <stdio.h>
#include<conio.h>
void main()
{
int i, f, l, mid, num, tar, arr[100];
clrscr();
printf("How many numbers you want to enter in the array: ");
scanf("%d",&num);
printf("Please Enter Elements in Ascending order\n");
for (i = 0; i < num; i++)
scanf("%d",&arr[i]);
printf("Enter the number which position you want to know: ");

10
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
scanf("%d", &tar);
f = 0;
l = num - 1;
mid = (f+l)/2;
while (f <= l) {
if (arr[mid] < tar)
f = mid + 1;
else if (arr[mid] == tar) {
printf("%d is at %d position\n", tar, mid+1);
break;
}
else
l = mid - 1;
mid = (f + l)/2;
}
if (f> l)
printf("This element is not in the Array");
getch();
}

Implementing Stacks: Array implementation, Linked List implementation, Evaluation of postfix


expression and balancing of parenthesis, Conversion of infix notation to postfix notation

Array implementation
#include<stdio.h>
#include<conio.h>
#define MAX 10
int st[MAX], top=-1;
void push(int st[], int val);
int pop (int st[]);
int peek(int st[]);
void display(int st[]);
int main()
{
int val, option;
clrscr();
do
11
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
{
printf("\n Main menu");
printf("\n1. push");
printf("\n2. pop");
printf("\n3. peek");
printf("\n4. display");
printf("\n5. exit");
printf("\n enter your option");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\n enter your number to be pushed on stack");
scanf("%d",&val);
push(st,val);
break;
case 2:
val=pop(st);
if(val!=-1)
printf("\n the value deleted from stack is %d", val);
break;
case 3:
val=peek(st);
if(val!=-1)
printf("\n the value stored at top of stack is %d",val);
break;
case 4:
display(st);
break;
}
}
while(option!=5);
getch();
return 0;
}
void push(int st[],int val)
{
if (top==MAX-1)
{
printf("\n stack overflow");
}
else
{
top++;
st[top]=val;
}

12
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
}
int pop(int st[])
{
int val;
if (top==-1)
{
printf("\n stack underflow");
return -1;
}
else
{
val=st[top];
top--;
return val;
}
}
void display(int st[])
{
int i;
if (top==-1)
printf("\n stack is empty");
else
{
for (i=top; i>=0; i--)
printf("\n%d",st[i]);
}
}
int peek(int st[])
{
if (top==-1)
{
printf("\n stack is empty");
return-1;
}
else
return (st[top]);
}

13
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

Linked List implementation


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct stack
{
int data;
struct stack *next;
};
struct stack*top =NULL;
struct stack *push(struct stack *, int);
struct stack *display(struct stack *);
struct stack *pop(struct stack *);
int main()
{
int val, option;
clrscr();
do
{
printf("\n Main menu");
printf("\n1. push");
printf("\n2. pop");
printf("\n3. peek");
printf("\n4. display");
printf("\n5. exit");
printf("\n enter your option");
scanf("%d",&option);

14
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
switch(option)
{
case 1:
printf("\n enter your number to be pushed on stack");
scanf("%d",&val);
top=push(top, val);
break;
case 2:
top=pop(top);
break;
case 3:
val=peek(top);
if (val!=-1)
printf("\n the value at the top of stack is %d", val);
else
printf("\n stack is empty");
break;
case 4:
top=display(top);
break;
}
}
while(option!=5);
getch();
return 0;
}
struct stack *push(struct stack *top, int val)
{
struct stack *ptr;
ptr=(struct stack*)malloc(sizeof(struct stack));
ptr->data=val;
if (top==NULL)
{
ptr->next=NULL;
top=ptr;
}
else
{
ptr->next=top;
top=ptr;
}
return top;
}
struct stack *display(struct stack *top)
{
struct stack *ptr;
ptr=top;

15
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
if(top==NULL)
printf("\n stack is empty");
else
{
while(ptr!=NULL)
{
printf("\n%d", ptr->data);
ptr=ptr->next;
}
}
return top;

}
struct stack *pop(struct stack *top)
{
struct stack *ptr;
ptr=top;
if (top== NULL)
printf("\n stack underflow");
else
{
top=top->next;
printf("\n the value being deleted is %d", ptr->data);
free(ptr);
}
return top;
}
int peek(struct stack *top)
{
if(top==NULL)
return-1;
else
return top->data;
}

16
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

4. Implementing Queue: Linked List implementation of ordinary queue, Array implementation of circular
queue, Linked List implementation of priority queue, Double ended queue

Linked List implementation of ordinary queue


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));

17
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
int main()
{
int i=0;
front=NULL;
printf(" \n1. insert to Queue");
printf(" \n2. delete from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;

18
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
printf("\nEnter a valueber to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}

19
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

Array implementation of circular queue

#include<conio.h>
#include<stdio.h>
#define max 3
int q[10],front=0,rear=-1;
void main()
{
int ch;
void insert();
void delet();
void display();
clrscr();
printf("\nCircular Queue operations\n");
printf("[Link]\[Link]\[Link]\[Link]\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
20
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
case 3:display();
break;
case 4:exit();
default:printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit();
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];

21
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
exit();
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}
getch();

22
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

Linked List implementation of priority queue

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

typedef struct node {


int data;

int priority;

struct node* next;

} Node;

Node* newNode(int d, int p)


{
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = d;
temp->priority = p;
temp->next = NULL;

return temp;
}
23
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

int peek(Node** head)


{
return (*head)->data;
}

void pop(Node** head)


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

void push(Node** head, int d, int p)


{
Node* start = (*head);

Node* temp = newNode(d, p);

if ((*head)->priority > p) {

temp->next = *head;
(*head) = temp;
}
else {

while (start->next != NULL &&


start->next->priority < p) {
start = start->next;
}

temp->next = start->next;
start->next = temp;
}
}

int isEmpty(Node** head)


{
return (*head) == NULL;
}

void main()
{

Node* pq = newNode(40, 1);


push(&pq, 50, 2);
push(&pq, 60, 3);

24
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
push(&pq, 70, 0);

while (!isEmpty(&pq)) {
printf("%d ", peek(&pq));
pop(&pq);
}

getch();
}

Double ended queue


#include <stdio.h>
#include<conio.h>
#define MAX 5

int deque_arr[MAX];
int left = -1;
int right = -1;

/*Begin of insert_right*/
void insert_right() {
int added_item;
if ((left == 0 && right == MAX - 1) || (left == right + 1)) {
printf("Queue Overflow\n");
return;
}
if (left == -1) /* if queue is initially empty */
{
left = 0;
right = 0;
} else if (right == MAX - 1) /*right is at last position of queue */
right = 0;
else
right = right + 1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[right] = added_item;
}
/*End of insert_right*/

/*Begin of insert_left*/
void insert_left() {
int added_item;
if ((left == 0 && right == MAX - 1) || (left == right + 1)) {
printf("Queue Overflow \n");
25
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
return;
}
if (left == -1) /*If queue is initially empty*/
{
left = 0;
right = 0;
} else if (left == 0)
left = MAX - 1;
else
left = left - 1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[left] = added_item;
}
/*End of insert_left*/

/*Begin of delete_left*/
void delete_left() {
if (left == -1) {
printf("Queue Underflow\n");
return;
}
printf("Element deleted from queue is : %d\n", deque_arr[left]);
if (left == right) /*Queue has only one element */
{
left = -1;
right = -1;
} else if (left == MAX - 1)
left = 0;
else
left = left + 1;
}
/*End of delete_left*/

/*Begin of delete_right*/
void delete_right() {
if (left == -1) {
printf("Queue Underflow\n");
return;
}
printf("Element deleted from queue is : %d\n", deque_arr[right]);
if (left == right) /*queue has only one element*/
{
left = -1;
right = -1;
} else if (right == 0)
right = MAX - 1;

26
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
else
right = right - 1;
}
/*End of delete_right*/
/*Begin of input_que*/
void display_queue() {
int front_pos = left, rear_pos = right;
if (left == -1) {
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if (front_pos <= rear_pos) {
while (front_pos <= rear_pos) {
printf("%d ", deque_arr[front_pos]);
front_pos++;
}
} else {
while (front_pos <= MAX - 1) {
printf("%d ", deque_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos) {
printf("%d ", deque_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}
/*End of display_queue*/
/*Begin of input_que*/
void input_que() {
int choice;
do {
printf("[Link] at right\n");
printf("[Link] from left\n");
printf("[Link] from right\n");
printf("[Link]\n");
printf("[Link]\n");

printf("Enter your choice : ");


scanf("%d", &choice);
switch (choice) {
case 1:
insert_right();
break;

27
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
case 2:
delete_left();
break;
case 3:
delete_right();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
} while (choice != 5);
}
/*End of input_que*/

/*Begin of output_que*/
void output_que() {
int choice;
do {
printf("[Link] at right\n");
printf("[Link] at left\n");
printf("[Link] from left\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_left();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}

28
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
} while (choice != 5);
}
/*End of output_que*/

/*Begin of main*/
main() {
int choice;
printf("[Link] restricted dequeue\n");
printf("[Link] restricted dequeue\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
input_que();
break;
case 2:
output_que();
break;
default:
printf("Wrong choice\n");
}
}
/*End of main*/

29
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
5. Implementing Linked List: Singly Linked Lists, Circular Linked List, Doubly Linked Lists: Insert,
Display, Delete, Search, Count, Reverse (SLL), Polynomial, Addition, Comparative study of arrays and
linked list

Singly Linked Lists

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
struct node *start= NULL;
void create();
void addatbeg();
void addafter();
void delatbeg();
void delatlast();
void delatspecified();
void display();
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n*************SINGLY LINKED LIST**************");
printf("\n\n1. create and insert node \n");
printf("2. insert a node at beginning\n");
printf("3. add after specified position\n");
printf("4. delete a node from beginning\n");
printf("5. delete a node from last\n");
printf("6. delete at specified\n");
printf("7. display\n");
printf("8. exit\n");
printf("************************************************");
printf("\n enter your choice");
scanf("%d",&choice);
switch (choice)
{
case 1: create();
break;
case 2: addatbeg();
break;
case 3: addafter();
break;

30
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
case 4: delatbeg();
break;
case 5: delatlast();
break;
case 6: delatspecified();
break;
case 7: display();
break;
case 8: exit();
default: printf("\n\t\tYou entered wrong choice,\n\t\tPlease enter choice from 1 - 8\n\n");
}
}
}
void create()
{
struct node *temp, *r;
int num;
if (start== NULL)
{
printf("linked list is empty\n");
printf("enter first data in a linked list");
scanf("%d",&num);
temp=malloc(sizeof(struct node));
temp->data= num;
temp->link=NULL;
start=temp;
}
else
{
printf("enter next data");
scanf("%d",&num);
temp= start;
while(temp->link!=NULL)
{
temp=temp->link;
}
r=malloc(sizeof(struct node));
r->data= num;
r->link=NULL;
temp->link=r;
}
printf("\n");
}
void display()
{
struct node *temp;
printf("%u\n", start);
temp= start;
printf("\n");
31
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
printf("\n");
}

void addatbeg()
{
struct node *temp;
int num;
printf("enter data");
scanf("%d",&num);
temp=malloc(sizeof(struct node));
temp->data = num;
temp->link =start;
start= temp;
}

void addafter()
{
struct node *temp, *r;
int loc, num, i;
temp=start;
printf("\n enter the location");
scanf("%d",&loc);
printf("\n enter the data");
scanf("%d", &num);
for(i=0; i<loc-1;i++)
{
temp=temp->link;
if(temp==NULL)
{
printf("there are less number");
return;
}
}
r= malloc(sizeof(struct node));
r->data= num;
r->link=temp->link;
temp->link=r;
}

void delatbeg()
{
struct node *temp;
if (start==NULL)
{
32
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
printf("\n\tyour linked list is empty, no node is available for deletion.");
printf("\n\t please insert node by choice 1 only\n");
return;
}
temp=start;
start=temp->link;
free(temp);
printf("\n first node is deleted\n");
}

void delatlast()
{
struct node *temp, *old;
if (start== NULL)
{
printf("\n\tyour linked list is empty, no node is available for deletion.");
printf("\n\t please insert node by choice 1 only\n");
return;
}
temp=start;
while(temp->link!=NULL)
{
old =temp;
temp=temp-> link;
}
old->link=NULL;
free(temp);
printf("\n last node is deletion\n");
}

void delatspecified()
{
int loc,i;
struct node *temp, *old;

printf("enter the loc");


scanf("%d",&loc);
temp=start;
for(i=0;i<loc-1;i++)
{
old=temp;
temp=temp->link;
if(temp==NULL)
{
printf("\n\tyour linked list is empty, no node is available for deletion.");
printf("\n\t please insert node by choice 1 only\n");
return;
}
}
33
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
old->link=temp->link;
free(temp);
printf("specified node is delete");
}

Doubly Linked Lists


#include<conio.h>
#include<stdio.h>

struct doubly
{
struct doubly *front;
struct doubly *back;
int i;
};

void main()
{
struct doubly *head=0;
struct doubly *last=0;
struct doubly *p=0;
struct doubly *temp=0;

34
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
int ch,intch,user,cnt=0,t,add;
clrscr();
printf("\n\t 1. CREATE.");
printf("\n\t 2. INSERT.");
printf("\n\t 3. DELETE.");
printf("\n\t 4. DISPLAY.");
printf("\n\t [Link].");
scanf("%d",&ch);

while(ch!=5)
{
if(ch==1)
{
cnt=0;
head=(struct doubly *)malloc(sizeof(struct doubly));
head->back=0;
head->front=0;
printf("\n\t ENTER DAT[Link] ");
scanf("%d",&head->i);
cnt++;
last=head;
}
if(ch==2)
{

printf("\n\t 1. INSERTION AT FRONT.");


printf("\n\t 2. INSERTION AT MIDDLE.");
printf("\n\t 3. INSERTION AT END.");
scanf("%d",&intch);
temp=(struct doubly *)malloc(sizeof(struct doubly));
printf("\n\t ENTER DAT[Link]");
scanf("%d",&temp->i);

if(intch==1)
{
temp->front=0;
temp->back=head;
head=temp;
cnt++;
}
if(intch==3)
{
p=head;
while(p->back!=0)
{
p=p->back;
}
temp->back=0;
temp->front=p;
35
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
p->back=temp;
cnt++;
}
if(intch==2)
{
printf("\n\tNTER VALUE BETVN 1--%d",cnt);
scanf("%d",&add);
t=1;
p=head;
while(t<add)
{
p=p->back;
t++;
}
temp->back=p->back;
p->back->front=temp;
p->back=temp;
temp->front=p;
cnt++;
}
}
if(ch==4)
{
p=head;
while(p!=0)
{
printf("\t-->%d",p->i);
p=p->back;
}
printf("\n\t total nodes %d",cnt);
}
if(ch==3)
{
printf("\n\t1. DELETE FRONT");
printf("\n\t2. DELETE MIDDLE");
printf("\n\t3. DELETE END");
scanf("%d",&intch);
if(intch==1)
{

head->back=t;
free(head);
head=t;
head->front=0;
cnt--;
}
if(intch==3)
{
p=head;
36
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
while(p->back!=0)
{
p=p->back;
}
temp->front=p;
p->back=temp;
temp->back=0;
cnt--;
}
if(intch==2)
{
printf("\n\tNTER VALUE BETVN 1--%d",cnt);
scanf("%d",&add);
t=1;
p=head;
while(t<(add-1))
{
p=p->back;
t++;
}
temp=p->back;
p->back=temp->back;
temp->back->front=p;
free(temp);

}
printf("\n\t 2. INSERT.");
printf("\n\t 3. DELETE.");
printf("\n\t 4. DISPLAY.");
printf("\n\t [Link].");
scanf("%d",&ch);
}
getch();
}

37
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

38

Common questions

Powered by AI

Adjacency matrices and adjacency lists offer distinct trade-offs for graph representation. An adjacency matrix provides O(1) time complexity for edge existence queries but requires O(V^2) space, where V is the number of vertices, making it inefficient for sparse graphs. The adjacency list takes O(V + E) space, more suited for sparse graphs, where E is the number of edges, with edge existence checks at O(V) in the worst case. Thus, adjacency matrices are advantageous for dense graphs with frequent edge checks, while adjacency lists optimize for space in graphs with fewer edges .

Linear search involves checking each element of the list until the desired element is found or the list ends. Its time complexity is O(n), making it inefficient for large datasets. Binary search requires a sorted array and uses divide-and-conquer by cutting the search interval in half. It compares the target with the middle element of the array and recursively searches in the half where the target could be. Binary search has a time complexity of O(log n), making it more efficient than linear search for large datasets .

Inserting an element into a linked list-based priority queue involves creating a new node with the element's data and priority. You start at the head of the list, searching for the appropriate position where the node's priority fits in respect to the existing nodes. If the new node's priority is higher than the head's, it is inserted at the front. Otherwise, you proceed node by node until finding the position where the next node has a lower priority. The new node is inserted by correctly updating the pointers of the adjacent nodes to include it in the chain .

Traversal methods in binary search trees (BST) include in-order, pre-order, and post-order. In-order traversal visits nodes in ascending order for sorted data, directly supporting binary search operations by allowing efficient range queries and element examination. The time complexity of these traversal operations is O(n), where n is the number of nodes, as each node is visited once. These traversals are fundamental for BST operations where sorted sequences are critical, like deletion and finding minimum/maximum elements .

The Bubble Sort algorithm involves repeatedly stepping through the list to be sorted, comparing adjacent elements and swapping them if they are in the wrong order. The task is continued until no swaps are required, which indicates that the list is sorted. Key steps include using nested loops: the outer loop iterates through each element, and the inner loop performs comparisons and swaps as needed. The optimization insight that the largest unsorted element is placed at the correct position after each inner loop execution helps reduce the sorting range in subsequent iterations .

Hash functions are critical in mapping data to fixed-size values within hash tables. A good hash function minimizes collisions, which occur when two keys hash to the same index. Collision resolution methods, like linear probing, help address collisions by placing the new entry in the next available space, scanning sequentially. The efficiency of hashing depends heavily on the choice of hash function and collision resolution strategy, as these influence both the speed of the data retrieval and the distribution of entries across the table .

In a singly linked list, each node contains data and a pointer to the next node, facilitating traversal in one direction only. Doubly linked lists have nodes with two pointers: one to the next and one to the previous node, enabling bidirectional traversal. This difference in structure requires more memory for doubly linked lists due to the additional pointer, and although they allow more complex operations like reverse traversal and easier node deletion, they involve complex pointer updates when nodes are inserted or removed .

A circular queue using an array reuses the space of dequeued elements by connecting the end of the array to the start, effectively handling overflow issues inherent in a standard linear queue. This implementation ensures efficient use of space and memory, avoiding unnecessary shifts upon dequeue operations. Challenges include correctly managing the wrap-around effect using modulo arithmetic and addressing edge cases where the queue might appear full (rear is just behind front due to wrap) when it actually has space, which requires careful front and rear management .

Using a linked list to implement a stack has the advantage of dynamic size, allowing it to grow and shrink as needed, unlike an array where the size is fixed. Linked list stacks provide constant time operations for push and pop, while array implementations might require resizing. However, linked lists require additional memory for storing pointers and can be slower due to memory allocation overhead. On the other hand, arrays allow faster access to elements due to contiguous memory but risk wasting space or causing overflows if undersized .

Reheapifying in heaps involves adjusting the nodes to restore the heap property after an insertion or deletion disrupts it. For a min-heap, reheapifying (also called heapify) occurs by swapping an out-of-place node downwards (reheap down) or upwards (reheap up) until the correct position is established. Reheaping ensures that in a min-heap, the smallest element is always at the root, and each parent node is less than its children. This process involves comparing and potentially swapping nodes and maintains a structured tree to enable efficient operations like extract-min with O(log n) complexity .

You might also like