0% found this document useful (0 votes)
16 views74 pages

DS (1 To 20) - Compressed

Uploaded by

sjain87654321
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)
16 views74 pages

DS (1 To 20) - Compressed

Uploaded by

sjain87654321
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/ 74

SHAURYA JAIN

0801ME231095
IT SECOND YEAR (B-3)

DATA STRUCTURES ASSIGNMENT

Q1)write a program to print the largest number among the given number in an array
using dynamic array.

#include<stdio.h>
#include<stdlib.h>
int main() {
int n;
int *arr;
printf("Enter the size of array: ");
scanf("%d", &n);
arr = (int*)malloc(n * sizeof(int));
printf("Enter the elements:\n");
for(int i= 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int large = arr[0];
for(int i= 1; i < n; i++)
{
if(arr[i] > large) {
large = arr[i];}
}
printf("The largest number is: %d\n", large);
free(arr);
printf("\nSHAURYA JAIN\n0801ME231095\n");
return 0; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q2)write a program to print the smallest number among the given number in an array
using dynamic array.

#include<stdio.h>
#include<stdlib.h>
int main() {
int n;
int *arr;
printf("Enter the size of array: ");
scanf("%d", &n);
arr = (int*)malloc(n * sizeof(int));
printf("Enter the elements:\n");

for(int i= 0; i < n; i++) {


scanf("%d", &arr[i]);
}
int smallest = arr[0];
for(int i= 1; i < n; i++) {
if(arr[i] < smallest) {
smallest = arr[i]; }
}
printf("The smallest number is: %d\n", smallest);
free(arr);
printf("\nSHAURYA JAIN\n0801ME231095\n");
return 0;}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q3)Write a program to print the elements of an array in reverse order. (show the use
of malloc(),realloc() and free() function in your program).

#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
int *arr;

printf("Enter the number of elements: ");


scanf("%d", &n);
arr = (int*)malloc(n * sizeof(int));

printf("Enter the elements:\n");

for (int i= 0; i < n; i++) {


scanf("%d", &arr[i]);
}
int m;
printf("Enter the number of elements you want to add :\n");
scanf("%d", &m);

arr = (int*)realloc(arr, (n + m) * sizeof(int));

printf("enter the elements you want to add: ");

for (int i= n; i < n + m; i++) {


scanf("%d", &arr[i]);
}
n =n+m;
printf("Elements in reverse order:\n");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

printf("\n");
free(arr);
printf("\nSHAURYA JAIN\n0801ME231095\n");

return 0;
}

Q4)Write a program to find the occurrence of duplicate element in array.

#include <stdio.h>

void duplicate(int arr[], int n)


{
int count;
printf("Duplicate elements in the array are:\n");

for (int i = 0; i <n; i++)


{
count = 1;
for (int j = i + 1; j <n; j++)
{
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

if (arr[i] == arr[j]&& arr[j]!=12345) {


count++;
arr[j] = 12345;
}
}
if (count > 1) {
printf("%d occurs %d times\n", arr[i], count);
}
}
}

int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i <n; i++) {
scanf("%d", &arr[i]);
}
duplicate(arr,n);
return 0; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q5. Write a menu driven program to implement following operations on the singly linked list.
(a) Insert a node at the front of the linked list. (b) Insert a node at the end of the linked list. (c)
Insert a node such that linked list is in ascending order.(according to info. Field) (d) Delete a
rst node of the linked list. (e) Delete a node before speci ed position. (f) Delete a node after
speci ed position.

#include <stdio.h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

} *start = NULL;

int count=0;

void display() //display function

if(start==NULL){

printf("empty");

return;

struct node *ptr;

ptr=start;

printf("\nthe linked list created is\n");

while(ptr!=NULL){

printf("%d ,",ptr->data);

ptr=ptr->next;}

}
fi
fi
fi
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void insert1() // beginning insertion

struct node *newnode ;

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

printf("\nenter data for new node:");

scanf("%d",&newnode->data);

if(start==NULL){

start=newnode;

count++;

else{

newnode->next=start;

start = newnode;

count++;

printf("\nthe LL after updation is:");

void insert2() { //last insertion

struct node *newnode, *last,*ptr ;

ptr = start;

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


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

printf("\nenter data for new node:");

scanf("%d",&newnode->data);

if(start==NULL){

start=newnode;

count++;

else{

while(ptr->next!=NULL){ // traverse purpose

ptr= ptr->next;

ptr->next=newnode; // new node ko insert kra

last=newnode;

count++;

void insert3()

int n;

struct node *newnode, *temp = start;

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


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

printf("Enter position at which u want to insert: ");

scanf("%d", &n);

if(n>0 && n<=count+1){

if (n == 1) {

insert1(); }

else {

printf("\nEnter data: ");

scanf("%d", &newnode->data);

int i = 1;

while (i < n-1) {

temp = temp->next;

i++; }

newnode->next = temp->next;

temp->next = newnode;

count++;

else{

printf("enter correct position");

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void delete1()

{ // deletion from beginning

struct node *ptr ;

if(start==NULL){

printf("empty");

else {

ptr = start;

start = start->next; //start ko khiska diya

free(ptr);

count--;

void delete2() // delete from end

struct node *temp , *ptr = start;

if(start == NULL){

printf("Linked list is Empty");

return;

while(ptr->next!= NULL)

temp=ptr;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

ptr=ptr->next;

count--;

free(ptr);

temp->next = NULL;

void delete3()

int n;

struct node *ptr ,*temp = start;

if(start == NULL){

printf("Linked list is Empty!!");

return ;}

printf("Enter position of node u want to delete: ");

scanf("%d", &n);

if(n>0 && n<=count){

if (n == 1) {

delete1();

else {

int i = 1;

while (i < n ) {
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

ptr = temp;

temp = temp->next;

i++; }

ptr->next = temp->next;

free(temp);

count--;

else{

printf("enter correct position");

void create() // funtion to create nodes

{ int k ,m ;

struct node *newnode ,*last;

do{

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

printf("\nenter the data:");

scanf("%d",&newnode->data);

if(start==NULL){

start =newnode ;

last = newnode;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

count++;

else {

last->next = newnode;

last= newnode;

count++;

printf("\npress any key number to continue and 0 to exit:");

scanf("%d",&k); // termination condition

}while(k!=0);

int main() { int m , k;

do

{ printf("\n\nMENU\n CREATE : 0\n INSERT : 1>beginning 2>end 3>at specific positon \n


DELETE : 4>beginning 5>end 6>at specific positon\n EXIT : 7\n");

scanf("%d", &m);

switch(m){

case 0 : create(); break;

case 1: insert1(); display();break;

case 2: insert2(); display();break;

case 3: insert3(); display();break;

case 4: delete1(); display();break;

case 5: delete2(); display();break;

case 6: delete3(); display();break;

case 7: k=7;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

}while(k!= 7);

return 0; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q6. Write a menu driven program to implement following operations on the doubly linked
list. (a) Insert a node at the front of the linked list. (b) Insert a node at the end of the linked
list. (c) Insert a node such that linked list is in ascending order.(according to info. Field) (d)
Delete a first node of the linked list. (e) Delete a node before specified position. (f) Delete a
node after specified position.

#include <stdio.h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

struct node *prev;

} *start = NULL;

int count=0;

void display() //display function

struct node *ptr;

ptr=start;

if(start==NULL){

printf("empty");

printf("\nthe linked list created is\n");

while(ptr!=NULL){

printf("%d ,",ptr->data);

ptr=ptr->next;}

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void insert1() // beginning insertion

struct node *newnode ;

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

printf("\nenter data for new node:");

scanf("%d",&newnode->data);

newnode->next= NULL;

if(start==NULL){

start=newnode;

newnode->prev= NULL;

count++;

else{

newnode->next=start;

start->prev= newnode;

newnode->prev= NULL;

start = newnode;

count++;

void insert2() { //last insertion

struct node *newnode, *last,*ptr ;

ptr = start;

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

printf("\nenter data for new node:");


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

scanf("%d",&newnode->data);

newnode->next= NULL;

if(start==NULL){

start=newnode;

newnode->prev= NULL;

count++;

else{

while(ptr->next!=NULL){ // traverse purpose

ptr= ptr->next;

ptr->next=newnode; // new node ko insert kra

newnode->prev= ptr;

last=newnode;

count++;

void insert3()

int n;

struct node *newnode, *temp = start;

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

newnode->next=NULL;

printf("Enter position at which u want to insert: ");

scanf("%d", &n);
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

if(n>0 && n<=count+1){

if (n == 1) { insert1(); }

else if(n==count+1){ insert2(); }

else {

printf("\nEnter data: ");

scanf("%d", &newnode->data);

int i = 1;

while (i < n-1) {

temp = temp->next;

i++; }

newnode->next = temp->next;

temp->next = newnode;

newnode->prev= temp; // MOST IMP PART

newnode->next->prev = newnode;

count++;

else{ printf("enter correct position”); }

void delete1()

struct node *ptr ;

if(start==NULL){

printf("empty");
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

else {

ptr = start;

start = start->next; //start ko khiska diya

start->prev=NULL;

free(ptr);

count--;

void delete2() // delete from end

struct node *ptr = start;

if(start == NULL){

printf("Linked list is Empty");

return;

if(start->next==NULL){

free(start);

start = NULL;

count--;

return;

while(ptr->next!= NULL)

ptr=ptr->next;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

ptr->prev->next= NULL;

free(ptr);

count--;

void delete3()

int n;

struct node *ptr ,*temp = start;

if(start == NULL){

printf("Linked list is Empty!!");

return ;}

printf("Enter position of node u want to delete: ");

scanf("%d", &n);

if(n>0 && n<=count){

if (n == 1) {

delete1();

else if (n == count) { delete2(); }

else {

int i = 1;

while (i < n ) {

ptr = temp;

temp = temp->next;

i++; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

ptr->next = temp->next;

temp->next->prev= ptr; // most imp part

free(temp);

count--;

else{ printf("enter correct position"); }

void create() // funtion to create nodes

int k ;

struct node *newnode ,*last;

do{

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

printf("\nenter the data:");

scanf("%d",&newnode->data);

newnode->next= NULL;

if(start==NULL){

newnode->prev=NULL;

start =newnode ;

last = newnode;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

else {

newnode->prev = last;

last->next = newnode;

last = newnode;

printf("\npress any key number to continue and 0 to exit:");

scanf("%d",&k); // termination condition

}while(k!=0);

int main() {

int m , k;

do

{ printf("\n\nMENU\n CREATE : 0\n INSERT : 1>beginning 2>end 3>at specific positon \n


DELETE : 4>beginning 5>end 6>at specific positon\n EXIT : 7\n");

scanf("%d", &m);

switch(m){

case 0 : create(); break;

case 1: insert1(); display();break;

case 2: insert2(); display();break;

case 3: insert3(); display();break;

case 4: delete1(); display();break;

case 5: delete2(); display();break;

case 6: delete3(); display();break;

case 7: k=7;

}while(k!= 7);
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

return 0; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q7. Write a menu driven program to implement following operations on the singly circular
linked list. (a) Insert a node at the front of the linked list. (b) Insert a node at the end of the
linked list. (c) Insert a node such that linked list is in ascending order.(according to info. Field)
(d) Delete a rst node of the linked list. (e) Delete a node before speci ed position. (f) Delete a
node after speci ed position.

#include <stdio.h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

} *start = NULL;

int count=0;

void display() //display function

if(start==NULL){

printf("empty");

return;

struct node *ptr;

ptr=start;

printf("\nthe linked list created is\n");

while(ptr->next!=start)

printf("%d ,",ptr->data);

ptr=ptr->next; }
fi
fi
fi
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

printf("%d ,",ptr->data);

void insert1() // beginning insertion

struct node *newnode ;

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

printf("\nenter data for new node:");

scanf("%d",&newnode->data);

newnode->next=NULL;

if(start==NULL){

start =newnode ;

start->next=start;

count++;

else {

struct node *temp;

temp=start;

while(temp->next!=start){

temp=temp->next;

temp->next=newnode;

newnode->next= start;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

start= newnode;

count++;

void insert2() { //last insertion

struct node *newnode ;

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

printf("\nenter data for new node:");

scanf("%d",&newnode->data);

if(start==NULL){

start =newnode ;

start->next=start;

count++;

else {

struct node *temp;

temp=start;

while(temp->next!=start){

temp=temp->next;

temp->next=newnode;

newnode->next= start;

count++;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void insert3()

int n;

struct node *newnode, *temp = start;

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

printf("Enter position at which u want to insert: ");

scanf("%d", &n);

if(n>0 && n<=count+1){

if (n == 1) { insert1(); }

else {

printf("\nEnter data: ");

scanf("%d", &newnode->data);

int i = 1;

while (i < n-1) {

temp = temp->next;

i++; }

newnode->next = temp->next;

temp->next = newnode;

count++;

else{ printf("enter correct position”); }

void delete1()

{ struct node *ptr , *temp ;


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

temp= start;

if(start==NULL){

printf("empty");

else{

while(temp->next!=start){

temp=temp->next;

ptr=start;

temp->next= start->next;

start=start->next;

ptr->next=NULL;

free(ptr);

count--;

void delete2() // delete from end

{ struct node *ptr , *temp ;

temp= start;

if(start==NULL){

printf("empty");

else{

while(temp->next!=start){

ptr=temp;

temp=temp->next;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

ptr->next=start;

temp->next=NULL;

free(temp);

count--;

void delete3()

int n;

struct node *ptr ,*temp = start;

if(start == NULL){ printf("Linked list is Empty!!");

return ;}

printf("Enter position of node u want to delete: ");

scanf("%d", &n);

if(n>0 && n<=count){

if (n == 1) {

delete1();

else {

int i = 1;

while (i < n ) {

ptr = temp;

temp = temp->next;

i++; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

ptr->next = temp->next;

free(temp);

count--;

else{

printf("enter correct position");

void create() // funtion to create nodes

{ int k ,m ;

struct node *newnode ,*last;

do{

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

printf("\nenter the data:");

scanf("%d",&newnode->data);

if(start==NULL){

start =newnode ;

last = newnode;

count++;

else {
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

last->next = newnode;

last= newnode;

count++; }

printf("\npress any key number to continue and 0 to exit:");

scanf("%d",&k); // termination condition

}while(k!=0);

Int main() { int m , k;

do

{ printf("\n\nMENU\n CREATE : 0\n INSERT : 1>beginning 2>end 3>at specific positon \n


DELETE : 4>beginning 5>end 6>at specific positon\n EXIT : 7\n");

scanf("%d", &m);

switch(m){

case 0 : create(); break;

case 1: insert1(); display();break;

case 2: insert2(); display();break;

case 3: insert3(); display();break;

case 4: delete1(); display();break;

case 5: delete2(); display();break;

case 6: delete3(); display();break;

case 7: k=7;

}while(k!= 7);

return 0; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q 7 output:
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q8. Write a menu driven program to implement following operations on the doubly circular
linked list. (a) Insert a node at the front of the linked list. (b) Insert a node at the end of the
linked list. (c) Insert a node such that linked list is in ascending order.(according to info. Field)
(d) Delete a first node of the linked list. (e) Delete a node before specified position. (f) Delete a
node after specified position.

#include <stdio.h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

struct node *prev;

} *start = NULL;

int count=0;

void display() //display function

struct node *ptr;

ptr=start;

if(start==NULL){

printf("empty");

printf("\nthe linked list created is\n");

while(ptr->next!=start){

printf("%d ,",ptr->data);

ptr=ptr->next;

printf("%d ,",ptr->data);

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void insert1() // beginning insertion

struct node *newnode ;

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

printf("\nenter data for new node:");

scanf("%d",&newnode->data);

newnode->next= NULL;

newnode->prev= NULL;

if(start==NULL){

start=newnode;

start->prev=start ;

start->next=start;

count++;

else{

start->prev->next=newnode;

newnode->prev=start->prev;

newnode->next=start;

start->prev=newnode;

start=newnode;

count++;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void insert2() // end insertion

struct node *newnode ;

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

printf("\nenter data for new node:");

scanf("%d",&newnode->data);

newnode->next= NULL;

newnode->prev= NULL;

if(start==NULL){

start=newnode;

start->prev=start ;

start->next=start;

count++;

else{

start->prev->next=newnode;

newnode->prev=start->prev;

newnode->next=start;

start->prev=newnode;

count++;

} }

void insert3(){
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

int n;

struct node *newnode, *temp;

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

newnode->next=NULL;

printf("Enter position at which u want to insert: ");

scanf("%d", &n);

if(n>0 && n<=count+1){

if (n == 1) {

insert1();}

else if(n==count+1){

insert2();

else { temp=start;

printf("\nEnter data: ");

scanf("%d", &newnode->data);

int i = 1;

while (i < n-1) {

temp = temp->next;

i++; }

newnode->next=temp->next;

temp->next->prev=newnode;

newnode->prev=temp;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

temp->next= newnode;

count++;

else{

printf("enter correct position");

void delete1()

{ // deletion from beginning

struct node *ptr ;

if(start==NULL){

printf("empty");

return;

else {

ptr = start;

start->prev->next= start->next;

start->next->prev=start->prev;

start=start->next;

free(ptr);

count--;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void delete2() // delete from end

struct node *temp;

if(start == NULL){

printf("Linked list is Empty");

return;

else{

temp= start->prev;

start->prev=temp->prev;

temp->prev->next= start;

free(temp);

count--;

void delete3()

int n;

struct node *temp ;

if(start == NULL){

printf("Linked list is Empty!!");

return ;}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

printf("Enter position of node u want to delete: ");

scanf("%d", &n);

if(n>0 && n<=count){

if (n == 1) {

delete1();

else if (n == count) {

delete2(); }

else {

temp = start;

int i = 1;

while (i < n ) {

temp = temp->next;

i++; }

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

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

free(temp);

count--;

else{

printf("enter correct position");


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void create() // funtion to create nodes

{ int k ;

struct node *newnode ,*last;

do{

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

printf("\nenter the data:");

scanf("%d",&newnode->data);

newnode->next= NULL;

newnode->prev=NULL;

if(start==NULL){

start =newnode ;

last = newnode;

start->prev=last;

last->next=start;

count++;

else {

newnode->next = last->next;

last->next=newnode;

start->prev=newnode;

newnode->prev=last;

last=newnode;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

count++;

printf("\npress any key number to continue and 0 to exit:");

scanf("%d",&k); // termination condition

}while(k!=0);

int main() { int m , k;

do

{ printf("\n\nMENU\n CREATE : 0\n INSERT : 1>beginning 2>end 3>at specific positon \n


DELETE : 4>beginning 5>end 6>at specific positon\n EXIT : 7\n");

scanf("%d", &m);

switch(m){

case 0 : create(); display(); break;

case 1: insert1(); display(); break;

case 2: insert2(); display();break;

case 3: insert3(); display();break;

case 4: delete1();

display();break;

case 5: delete2();

display();break;

case 6: delete3();

display();break;

case 7: k=7;

}while(k!= 7);
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

return 0; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q9. Write a program to create two different singly link list and merge them Into a síngle link
list.

#include <stdio.h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

};

struct node *start1 = NULL;

struct node *start2 = NULL;

struct node *last1 = NULL;

void display(struct node *headofLL){

if(headofLL == NULL){

printf("empty");

return;}

struct node *ptr;

ptr=headofLL;

printf("\n linked list created is : ");

while(ptr!=NULL){

printf("%d ",ptr->data);

ptr=ptr->next;}

void mergeLLs(){
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

if(start1==NULL && start2==NULL){printf("both list empty");}

else if(start1 ==NULL){printf("1st list is empty");}

else if(start2 ==NULL){printf("2nd list is empty");}

else{

last1->next=start2;

void createLL1() // funtion to create nodes

{ int k ;

struct node *newnode ;

do{

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

printf("\nenter the data for 1st LL:");

scanf("%d",&newnode->data);

if(start1==NULL){

start1 =newnode ;

last1 = newnode;

else {

last1->next = newnode;

last1= newnode;

printf("\npress any key number to continue and 0 to exit:");

scanf("%d",&k); // termination condition


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

}while(k!=0);

void createLL2() // funtion to create nodes

{ int k ;

struct node *newnode ,*last2;

do{

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

printf("\nenter the data for 2nd LL:");

scanf("%d",&newnode->data);

if(start2==NULL){

start2 =newnode ;

last2 = newnode;

else {

last2->next = newnode;

last2= newnode;

last2->next=NULL;

printf("\npress any key number to continue and 0 to exit:");

scanf("%d",&k); // termination condition

}while(k!=0);

int main() {

int m,k;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

do{

printf("\n\nMENU\n 1>create LL1. 2>create LL2 \n 3>merge both LL 4>EXIT\n ");

scanf("%d", &m);

switch(m){

case 1: createLL1(); display(start1); break;

case 2: createLL2(); display(start2);break;

case 3: mergeLLs(); display(start1);break;

case 4: k=4; }

}while(k!= 4);

return 0;}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q10 .Write a program to nd the mid element of singly link list.

#include <stdio.h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

} *start = NULL;

void display() //display function

if(start==NULL){ printf("empty");

return;

struct node *ptr;

ptr=start;

printf("\nthe linked list created is\n");

while(ptr!=NULL){

printf("%d ,",ptr->data);

ptr=ptr->next;}

void midelem(){

if(start==NULL){

printf("empty");

return;

struct node *ptr=start;


fi
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

struct node *temp=start;

while(temp!=NULL && temp->next!=NULL){

temp= temp->next->next;

ptr= ptr->next;

printf("\n The Mid Element is : %d", ptr->data); }

void create() // funtion to create nodes

{ int k ;

struct node *newnode ,*last;

do{

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

printf("\nenter the data:");

scanf("%d",&newnode->data);

if(start==NULL){

start =newnode ;

last = newnode;

else {

last->next = newnode;

last= newnode;

printf("\npress any key number to continue and 0 to exit:");

scanf("%d",&k); // termination condition

}while(k!=0);

int main() { int m,k;


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

do

{ printf("\n\nMENU\n CREATE : 1\n MID ELEMENT : 2\n EXIT : 3\n");

scanf("%d", &m);

switch(m){

case 1 : create(); display(); break;

case 2 :midelem(); break;

case 3 : k=3; }

}while(k!= 3);

return 0;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q11 . Write a program to print data of singly link list in reverse order.

#include <stdio.h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

} *start = NULL;

void display() //display function

if(start==NULL){

printf("empty");

return;

struct node *ptr;

ptr=start;

printf("\nthe linked list created is\n");

while(ptr!=NULL){

printf("%d ,",ptr->data);

ptr=ptr->next;}

void reverse(){

if(start==NULL){

printf("empty");
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

return;

struct node *prev=NULL;

struct node *temp=start;

struct node *front=start->next;

while(temp!=NULL){

temp->next= prev;

prev= temp;

temp= front;

if(temp!=NULL){

front=temp->next;}

start = prev;

void create() // funtion to create nodes

{ int k ;

struct node *newnode ,*last;

do{

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

printf("\nenter the data:");

scanf("%d",&newnode->data);

if(start==NULL){
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

start =newnode ;

last = newnode;

else {

last->next = newnode;

last= newnode;

printf("\npress any key number to continue and 0 to exit:");

scanf("%d",&k); // termination condition

}while(k!=0);

int main() { int m,k;

do

printf("\n\nMENU\n CREATE : 1\n REVERSE : 2\n EXIT : 3\n");

scanf("%d", &m);

switch(m){

case 1 : create();

display();

break;

case 2 : reverse();

display();

break;

case 3 : k=3;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

}while(k!= 3);

return 0;}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q12. Write a program for stack that performs following operations using
array. (a) PUSIH (b) POP (C) PEEP(d) CHANGE (C) DISPLAY
#include<stdio.h>

#include<stdlib.h>

#de ne SIZE 100

struct stack {

int A[SIZE];

int top;

} s;

void push()

{ int x ;

printf("enter value u want to push\n");

scanf("%d",&x);

if(s.top== SIZE-1){

printf("OVERFLOW\n");

else. {

s.top++;

s.A[s.top] =x; }

void pop()

if(s.top==-1){

printf("under ow\n");

else{ s.top—; }
fi
fl
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void peep(){

int position;// remember position starts from 1 and top

printf("enter the position from top”);

scanf("%d",&position);

if(position<1 || position> s.top+1){

printf("invalid\n");

else{

printf("%d", s.A[s.top+1-position]);

void change()

int position , value ;// remember position starts from 1 and top

printf("enter the position value and v from top");

scanf("%d",&position);

scanf("%d",&value);

if(position<1 || position> s.top+1){

printf("invalid\n");

else{

s.A[s.top+1-position]= value;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void display()

if(s.top==-1){

printf("under ow\n");

else{

for(int i= s.top ; i>-1 ; i--){

printf("%d ,", s.A[i]);

int main()

{ int n;

s.top= -1;

do{

printf("\nMENU \n 1>push & 2>pop \n 3>peep & 4>change \n 5>display & 6>exit\n enter
what u want");

scanf("%d",&n);

switch(n){

case 1 : push(); display(); break;

case 2 : pop(); display(); break;

case 3 : peep(); break;

case 4 : change(); display(); break;

case 5 : display(); display(); break;

case 6 : ; }
fl
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

}while(n!=6);

return 0; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q13 . Write a program to implement stack using linked list.(LIFO)

#include <stdio.h>

#include <stdlib.h>

struct node{

int data;

struct node *next;

} *top= NULL;

void push(){

struct node *newnode ;

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

printf("enter value u wnat to push\n");

scanf("%d",&newnode->data);

if(top==NULL) { top= newnode; }

else{

newnode->next= top;

top = newnode; }

void pop(){

if(top==NULL){ printf(“empty"); }

else{

struct node *temp = top;

top= top->next;

free(temp); }

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

void display(){

struct node *temp = top;

if(top==NULL){ printf("empty");

return; }

while(temp!=NULL) {

printf("%d ,",temp->data);

temp=temp->next; }

int main() { int n;

do{

printf("\nMENU \n 1>push & 2>pop \n 3>display & 4>exit\n enter what u want");

scanf(“%d",&n);

switch(n){

case 1 : push(); display(); break;

case 2 : pop(); display(); break;

case 3: display(); display(); break;

case 4 : ; }

}while(n!=4);

return 0;}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q16 . Write a programn to implement OUEUE using array to perfoms following


operations(a) INSERT (b) DELETE (c) DISPLAY

#include <stdio.h>

#de ne SIZE 20

int queue[SIZE];

int front =-1;

int rear=-1;

void enqueue() {

int value;

printf("\nenter the value u want to store : ");

scanf("%d",&value);

if(rear==SIZE-1){ printf(" OVERFLOW");}

else if(rear==-1 && front==-1){

front=0;

rear=0;

queue[rear]= value;

else{

rear++;

queue[rear]= value;

void dequeue(){

if(front==-1 && rear==-1){

return; }

else if(front == rear){

printf("the dequeued element is : %d",queue[front]);


fi
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

rear =-1;

front=-1;

else{

printf("the dequeued element is : %d",queue[front]);

front= (front+1); }

void display(){

if(front==-1 && rear==-1){

printf("UNDERFLOW");

else{ printf("\n the queue is : ");

for(int i = front ; i<=rear; I++) { printf("%d “,queue[i]); }

int main() {

int n;

do{

printf("\nMENU \n 1>enqueue & 2>dequeue \n 3>display & 4>exit\n enter what u want :");

scanf(“%d",&n);

switch(n){

case 1 : enqueue(); display(); break;

case 2 : dequeue(); display(); break;

case 3: display(); display(); break;

case 4 : ; }
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

}while(n!=4);

return 0;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q17 .Write a programn to implement OUEUE using link list.(FIFO)

#include <stdio.h>

#include<stdlib.h>

struct queue {

int data;

struct node *next;

};

struct queue *front = NULL;

struct queue *rear = NULL;

void enqueue()

struct queue *newnode;

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

printf("enter value u want to insert : ");

scanf("%d",&newnode->data);

if(rear==NULL){

rear = front = newnode;

rear->next= NULL;

else{

rear->next=newnode;

rear= newnode; }

void dequeue() {

if(front==NULL){ printf("empty queue”);

return;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

else {

struct queue *temp;

temp=front;

front = front->next;

free(temp);

void display()

if(front==NULL){ printf("empty queue");

return; }

else{

struct queue *ptr;

ptr=front;

while(ptr!=NULL){

printf("%d ",ptr->data);

ptr=ptr->next;

int main() { int k;

do {

printf("\nMENU :\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d",&k);
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

switch (k) {

case 1:enqueue(); display(); break;

case 2:dequeue(); display();break;

case 3: display(); break;

case 4: ;

} while (k != 4);

return 0;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q18 . Write a programn to implement CIRCULAR OUEUE using array to perfoms


following operations(a) INSERT (b) DELETE (c) DISPLAY

#include <stdio.h>

#de ne SIZE 20

int queue[SIZE];

int front =-1;

int rear=-1;

void enqueue() {

int value;

printf("\nenter the value u want to store : ");

scanf("%d",&value);

if ((rear + 1) % SIZE == front) { printf("OVERFLOW\n"); }

else if (rear == -1 && front == -1) {

front = rear = 0;

queue[rear] = value;

else {

rear = (rear + 1) % SIZE;

queue[rear] = value; }

void dequeue(){

if(front==-1 && rear==-1){ return; }

else if(front == rear){

printf("the dequeued element is : %d",queue[front]);

rear =-1;

front=-1;

}
fi
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

else{

printf("the dequeued element is : %d",queue[front]);

front= (front+1)%SIZE; }

void display(){

if(front==-1 && rear==-1){

printf("UNDERFLOW");

else{ printf("\n the queue is : ");

int i= front;

while(i!=rear){

printf("%d ",queue[i]);

i= (i+1)%SIZE;

printf("%d \n", queue[rear]);

int main() {

int n;

do{

printf("\nMENU \n 1>enqueue & 2>dequeue \n 3>display & 4>exit\n enter what u want :");

scanf("%d",&n);

switch(n){

case 1 : enqueue(); display(); break;

case 2 : dequeue(); display(); break;


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

case 3: display(); display(); break;

case 4 : ; }

}while(n!=4);

return 0;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q 19. Write a programn to implement CIRCULAR OUEUE using linked list.

#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

};

struct node *rear=NULL;

struct node *front=NULL;

void enqueue(){

printf("enter the value:");

struct node *newnode;

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

scanf("%d",&newnode->data);

newnode->next=NULL;

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

front=rear=newnode;

rear->next= front; }

else{

newnode->next=front;

rear->next=newnode;

rear=newnode; }

void dequeue(){

if(front==NULL && rear==NULL){ printf("empty queue");

return;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

struct node *temp =front;

if(rear==front){

printf("\ndequed element is: %d\n", temp->data);

rear = front = NULL;

free(temp); }

else{

front= front->next;

rear->next= front;

free(temp); }

void display(){

if(front==NULL && rear==NULL){ printf("empty queue\n”);

return; }

struct node *temp =front;

while(temp->next !=front)

printf("%d ",temp->data);

temp=temp->next;

printf("%d ",temp->data);

int main() { int n;

do{

printf("\nMENU \n 1>enqueue & 2>dequeue \n 3>display & 4>exit\n enter what u want :");

scanf("%d",&n);

switch(n){ case 1 : enqueue(); display(); break;


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

case 2 : dequeue(); display(); break;

case 3: display(); display(); break;

case 4 : ; }

}while(n!=4);

return 0;

}
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

Q20.Write a program to imnplement priority queue using link list.

#include <stdio.h>

#include <stdlib.h>

struct pqueue{

int data;

int priority;

struct pqueue *next;

};

struct pqueue *front = NULL;

void enqueue()

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

printf("enter the value and priority : ");

scanf("%d",&newnode->data);

scanf(“%d",&newnode->priority);

newnode->next=NULL;

if(front==NULL || newnode->priority < front->priority ){

newnode->next= front;

front = newnode;

return; }

else{

struct pqueue *temp = front;

while(temp->next!=NULL && temp->next->priority <= newnode->priority){

temp= temp->next;

newnode->next = temp->next;
SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

temp->next= newnode;

void dequeue (){

if(front==NULL){ printf("\nqueue is empty\n”);

return; }

struct pqueue *temp = front;

printf("dequed element is: %d , and it prioirity is: %d\n", front->data, front->priority );

front=front->next;

free(temp);

void display(){

if(front==NULL){ printf("\nqueue is empty");

return;}

struct pqueue *temp = front;

printf("\nelement -> priority\n");

while(temp!=NULL) {

printf("%d -> %d \n",temp->data , temp->priority);

temp=temp->next; }

int main() { int n;

do{

printf("\nMENU \n 1>enqueue & 2>dequeue \n 3>display & 4>exit\n enter what u want :");

scanf("%d",&n);

switch(n){

case 1 : enqueue(); display(); break;


SHAURYA JAIN
0801ME231095
IT SECOND YEAR (B-3)

case 2 : dequeue(); display(); break;

case 3: display(); display(); break;

case 4 : ; }

}while(n!=4);

return 0; }

You might also like