Dslab1 41
Dslab1 41
Dslab1 41
Roll No. 41 C3
PracticalNo.1
Aim:Writeamenudrivenprogramtoimplementarrayaddress
calculations using RMO and CMO
for1D,2Dand3Darrays.
Code:#include<stdio.h>
voidaddress1D(){
intbaseaddress,index,size;
printf("Enterbaseaddress:");
scanf("%d", &baseaddress);
printf("Enter index: ");
scanf("%d",&index);
printf("Entersize:");
scanf("%d", &size);
intaddress=baseaddress+index*size;
printf("Addressof1Delementatindex%d:%d\n",index,address);
}
voidaddress2D(){
intbaseaddress,i,j,size,lbr,lbc,M,N;
printf("Enterbaseaddress:");
scanf("%d", &baseaddress);
printf("Enter i (row index): ");
scanf("%d", &i);
printf("Enterj(columnindex):");
scanf("%d", &j);
printf("Entersize:");
scanf("%d", &size);
printf("Enterlbr(rowlowerbound):");
scanf("%d", &lbr);
printf("Enterlbc(columnlowerbound):"); scanf("%d",
&lbc);
printf("EnterM(numberofrows):");
scanf("%d", &M);
printf("EnterN(numberofcolumns):");
scanf("%d", &N);
printf("RMOAddressof2Delementat(%d,%d):%d\n",i,j, addRMO);
printf("CMOAddressof2Delementat(%d,%d):%d\n",i,j, addCMO);
}
voidaddress3D(){
intbaseaddress,i,j,k,size,lbr,lbc,lbd,M,N;
printf("Enterbaseaddress:");
scanf("%d", &baseaddress);
printf("Enter i (first index): ");
scanf("%d", &i);
printf("Enterj(secondindex):");
scanf("%d", &j);
printf("Enterk(thirdindex):");
scanf("%d", &k);
printf("Entersize:");
scanf("%d", &size);
printf("Enterlbr(firstdimensionlowerbound):"); scanf("%d",
&lbr);
printf("Enterlbc(seconddimensionlowerbound):");
scanf("%d", &lbc);
printf("Enterlbd(thirddimensionlowerbound):");
scanf("%d", &lbd);
printf("EnterM(numberofrowsin2nddimension):");
scanf("%d",&M);
printf("EnterN(numberofrowsin3rddimension):");
scanf("%d", &N);
printf("RMOAddressof3Delementat(%d,%d,%d):%d\n",i,j,k,
addRMO);
printf("CMOAddressof3Delementat(%d,%d,%d):%d\n",i,j,k,
addCMO);
}
int main() {
intchoice;
do{
printf("\nArrayAddressCalculationMenu");
printf("\n1. 1D Array");
printf("\n2.2DArray(RMOandCMO)");
printf("\n3.3DArray(RMOandCMO)");
printf("\n4. Exit");
printf("\nEnteryourchoice:");
scanf("%d",&choice);
switch(choice){
case1:
address1D();
break;
case 2:
address2D();
break;
case 3:
address3D();
break;
case 4:
printf("Exitingprogram.\n");
break;
default:
printf("Invalidchoice.Pleasetryagain.\n");
}
}while(choice!=4);
return0;
}
Output:
Test1:1D:Givenanarrayintage[]={23,25,12,15,16,17,18,19,20},
calculate the address of
age[6]ifthebaseaddress=800.
Test2:2D:Considerazero-indexedarrayAwithdimensions5x6x7 that
stores 4-byte
integersislocatedatanaddress6400.CalculatetheaddressofA(4, 5, 6)
using row-major order and column-major order.
Test3:3D:Givenanarray[1..8,1..5,1..7]ofintegersandmemory size of 2.
Calculate
addressofelementA[5,3,6],byusingrowsandcolumnsmethods,if
BA=900.
Name: Pranav Pandit
RollNo.41 C3
Practical No 2
Aim:WriteamenudrivenprogramtoimplementStackanditsallowable
operationssuchaspush(),
pop()andpeek()
Code:#include<stdio.h>
#defineMAX10
int
stack[MAX];int
top=-1;
voidpush(intst[],intval){
if(top== MAX-1){
printf("\nSTACKOVERFLOW\n");
}else{
top++;
st[top]=val;
printf("\nPushed%dontothestack\n",val);
}
}
intpop(intst[]){
int val;
if(top== -1) {
printf("\nSTACKUNDERFLOW\n");
return-1;
}else{
val =
st[top];top--
;
printf("\nPopped%dfromthestack\n",val);
returnval;
}
}
intpeek(intst[]){
if(top== -1) {
printf("\nSTACKISEMPTY\n");
return-1;
}else{
returnst[top];
}
}
voiddisplay(intst[]){
if(top== -1) {
printf("\nSTACKISEMPTY\n");
}else{
printf("\nStackelementsare:\n");
for (int i=top;i>=0;i--){
printf("%d\n",st[i]);
}
}
}
intmain(){
intchoice,val;
do{
printf("\nSTACK
OPERATIONS\n");printf("1.Push\n
");
printf("2.Pop\n");
printf("3.
Peek\n");printf("4.Di
splay\n");printf("5.Ex
it\n");
printf("Enteryourchoice:");scanf("
%d",&choice);
switch(choice){
case1:
printf("\nEnterthevaluetopush:");scanf("%d",&
val);
push(stack,val);
break;
case2:
val=pop(stack);
if(val!=-1){
printf("\nPoppedvalue:%d\n",val);
}
break;
case3:
val=peek(stack);
if(val!=-1){
printf("\nTopelement:%d\n",val);
}
break;
case4:
display(stack);
break;
case5:
printf("\nExiting...\n");
break;
default:
printf("\nInvalidchoice,pleasetryagain.\n");
}
}while (choice!=5);
return0;
}
Output:
Test1:Push:InsertanelementinaStack.
a. SuccessfulInsertion.
b. StackOverflow.Noinsertion.
Test2:Pop:DeleteanelementinaStack.
a. SuccessfulDeletion.
b. StackUnderflow.NoDeletion.
Test3:Peek:Visit andprintthetopmostelementofthestack.
a. SuccessfulOperation.Printthetopmostelement.
b. StackUnderflow.Print“StackisEmpty”.
Name:Pranav Pandit
Roll No. 41 C3
PracticalNo.3
Aim:WriteamenudrivenprogramtoimplementLinearqueue& Circular
Queue and its allowable
operationssuchasenqueue(),dequeue()anddisplay().
Code:#include<stdio.h>
#define MAX 5
intqueue[MAX];
int front = -1;
intrear=-1;
intcqueue[MAX];
int cfront = -1;
intcrear=-1;
voidlinear_enqueue(){
int num;
printf("\nEnterthenumbertobeinsertedinthelinearqueue: ");
scanf("%d", &num);
if(rear==MAX-1){
printf("\nLinearQueueOVERFLOW\n");
}elseif(front==-1&&rear==-1){ front =
rear = 0;
queue[rear]=num;
}else{
rear++;
queue[rear]=num;
}
}
intlinear_dequeue(){ int
val;
if(front==-1||front>rear){
printf("\nLinearQueueUNDERFLOW\n");
return -1;
}else{
val=queue[front]; front++;
if(front>rear){
front=rear=-1;
}
printf("%disbeendequeue\n",val);
return val;
}
}
intlinear_peek(){
if(front==-1||front>rear){
printf("\nLinearQueueisEMPTY\n"); return
-1;
}else{
returnqueue[front];
}
}
voidlinear_display(){
if(front==-1||front>rear){
printf("\nLinearQueueisEMPTY\n");
}else{
printf("\nLinearQueueelementsare:");
for(int i = front; i <= rear; i++) {
printf("%d",queue[i]);
}
printf("\n");
}
}
voidcircular_enqueue(){
intnum;
printf("\nEnterthenumbertobeinsertedinthecircularqueue: ");
scanf("%d", &num);
if((cfront==0&&crear==MAX-1)||(crear==cfront-1)){
printf("\nCircular Queue OVERFLOW\n");
}elseif(cfront==-1&&crear==-1){ cfront
= crear = 0;
cqueue[crear]=num;
}elseif(crear==MAX-1&&cfront!=0){ crear =
0;
cqueue[crear]=num;
}else{
crear++;
cqueue[crear]=num;
}
}
intcircular_dequeue(){
int val;
if(cfront==-1&&crear==-1){
printf("\nCircularQueueUNDERFLOW\n");
return -1;
}else{
val=cqueue[cfront];
if(cfront == crear) {
cfront=crear=-1;
}elseif(cfront==MAX-1){ cfront =
0;
}else{
cfront++;
}
returnval;
}
}
intcircular_peek(){
if(cfront==-1&&crear==-1){
printf("\nCircularQueueisEMPTY\n");
return -1;
}else{
returncqueue[cfront];
}
}
voidcircular_display(){
if(cfront==-1&&crear==-1){
printf("\nCircularQueueisEMPTY\n");
}else{
printf("\nCircularQueueelementsare:"); int
i = cfront;
while(1){
printf("%d",cqueue[i]);
if(i == crear) {
break;
}
i=(i+1)% MAX;
}
printf("\n");
}
}
intmain(){
intchoice,type;
while(1){
printf("\nQUEUEOPERATIONSMENU\n");
printf("1. Linear Queue Operations\n");
printf("2.CircularQueueOperations\n");
printf("3. Exit\n");
printf("Enteryourchoice:");
scanf("%d", &type);
if(type==1){
printf("\nLINEARQUEUEMENU\n");
printf("1. Enqueue\n");
printf("2.Dequeue\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5.BacktoMainMenu\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice){
case1:linear_enqueue();break;
case2:linear_dequeue();break;
case3:printf("\nPeekelement:%d\n",linear_peek());break;
case 4: linear_display(); break;
case5:break;
default:printf("\nInvalidchoice.Tryagain.\n");
}
}elseif(type==2){
printf("\nCIRCULARQUEUEMENU\n");
printf("1. Enqueue\n");
printf("2.Dequeue\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5.BacktoMainMenu\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice){
case1:circular_enqueue();break;
case2:circular_dequeue();break;
case3:printf("\nPeekelement:%d\n",circular_peek());
break;
case4:circular_display();break; case
5: break;
default:printf("\nInvalidchoice.Tryagain.\n");
}
}elseif(type==3){
printf("\nExiting...\n");
break;
}else{
printf("\nInvalidchoice.Tryagain.\n");
}
}
return0;
}
Output:ForLinearqueue
Test1:Insert
Test2:Delete
Test3:Display/Traverse.
Test4:OverflowandUnderflow.
Output:ForCircularqueue Test
1:Insert
Test2:Delete
Test3:Display\Traverse
Test4:UnderflowandOverflow
Name: Pranav Pandit Roll
No. 41 C3
Practical No.4
Aim:Writeamenudrivenprogramtoimplementthe
allowable operations of priority queues.
Code:#include<stdio.h>
#defineMAX10
intpriorityQueue[MAX];
int front = -1, rear = -1;
voidinsert(intvalue); int
delete();
voiddisplay();
intmain(){
intchoice,value;
while(1){
printf("\nPRIORITYQUEUEOPERATIONS MENU\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3.Display\n");
printf("4. Exit\n");
printf("Enteryourchoice:"); scanf("%d",
&choice);
switch(choice){
case1:
printf("Enterthevaluetobeinserted:");
scanf("%d", &value);
insert(value);
break;
case 2:
value=delete();
if(value!=-1){
printf("Deletedelement:%d\n",value);
}
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
return0;
default:
printf("Invalidchoice.Tryagain.\n");
}
}
return0;
}
voidinsert(intvalue){
if (rear == MAX - 1) {
printf("QueueOverflow\n");
return;
}
if(front==-1){
front=rear =0;
priorityQueue[rear]=value;
}else{
int i;
for(i=rear;i>=front&&priorityQueue[i]>value;
i--){
priorityQueue[i+1]=priorityQueue[i];
}
priorityQueue[i+1]=value; rear++;
}
}
intdelete(){
if(front==-1){
printf("QueueUnderflow\n");
return-1;
}
intvalue=priorityQueue[front];
if (front == rear) {
front=rear=-1;
}else{
front++;
}
returnvalue;
}
voiddisplay(){
if(front==-1){
printf("QueueisEmpty\n");
return;
}
printf("PriorityQueueElements:");
for (int i = front; i <= rear; i++) {
printf("%d",priorityQueue[i]);
}
printf("\n");
}
OUTPUT:1)INSERT
2) DELETE
3) DISPLAY
4) UNDERFLOWANDOVERFLOW
Name- PranavPandit
Roll no - C3 41
Code-
#include
<stdio.h>#include<
stdlib.h>
structnode
{
intdata;
structnode*next;
};
structnode*createll(structnode*head)
{
struct node *new_node = (struct node *)malloc(sizeof(struct
node));printf("\nEnterdata:");
scanf("%d", &new_node-
>data);new_node->next=NULL;
head =
new_node;returnh
ead;
}
structnode*insertatbeg(structnode*head)
{
struct node *new_node = (struct node *)malloc(sizeof(struct
node));printf("\nEnterdata:");
scanf("%d", &new_node-
>data);new_node->next= head;
head=new_node;
returnhead;
}
structnode*insertatend(structnode*head)
{
structnode*new_node=(structnode*)malloc(sizeof(structnode));stru
ctnode*ptr;
ptr =
head;printf("\nEnterdata
:");
scanf("%d", &new_node-
>data);while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr-
>next=new_node;new_no
de->next =
NULL;returnhead;
}
structnode*insertafter(structnode*head,intdata)
{
structnode*new_node=(structnode*)malloc(sizeof(structnode));stru
ctnode*ptr=head;
printf("\nEnterdata:");scanf("%d
", &new_node->data);while (ptr-
>data!=data)
{
ptr=ptr->next;
}
new_node->next =ptr->next;
ptr->next =new_node;
returnhead;
}
structnode*insertbefore(structnode*head,intdata)
{
structnode*new_node=(structnode*)malloc(sizeof(structnode));stru
ctnode*ptr=head;
printf("\nEnterdata:");scanf("%d
", &new_node->data);while (ptr-
>next->data!= data)
{
ptr=ptr->next;
}
new_node->next = ptr-
>next;ptr->next=new_node;
returnhead;
}
structnode*deleteatbeg(struct node*head)
{
structnode*ptr=head;hea
d= ptr->next;free(ptr);
returnhead;
}
structnode*deleteatend(structnode*head)
{
struct node
*ptr;struct node
*ptr2;ptr= head;
ptr2=head->next;
while(ptr2->next!=NULL)
{
ptr= ptr2;
ptr2=ptr2->next;
}
ptr->next =
NULL;free(ptr2);
returnhead;
}
structnode*deleteafter(structnode*head,intdata)
{
struct node
*ptr;struct node
*ptr2;ptr= head;
ptr2 =head-
>next;while(ptr-
>data!=data)
{
ptr= ptr2;
ptr2=ptr2->next;
}
ptr->next = ptr2-
>next;free(ptr2);
returnhead;
}
structnode*deletebefore(structnode*head,intdata)
{
struct node
*ptr;struct node
*ptr2;ptr= head;
ptr2=head->next;
while(ptr2->next->data!=data)
{
ptr= ptr2;
ptr2=ptr2->next;
}
ptr->next = ptr2-
>next;free(ptr2);
returnhead;
}
intsearch(structnode*head,intdata)
{
structnode*ptr=head;
while(ptr!=NULL)
{
if(ptr->data==data)
{
return1;
}
ptr=ptr->next;
}
return0;
}
structnode*reverselist(structnode*head)
{
struct node *prev =
NULL;structnode*present=h
ead;structnode*next=NULL;
while(present!=NULL)
{
next = present-
>next;present->next
= prev;prev =
present;present=
next;
}
head=prev;retur
nhead;
}
voidtraverse(structnode*head)
{
struct node
*new_node;new_node
=head;
while(new_node!=NULL)
{
printf("\t%d", new_node-
>data);new_node=new_node-
>next;
}
}
intmain()
{
structnode*head=NULL;inta,
b,c;
while(1)
{
printf("\n1.Create a node.\n2.Insert at the beginning.\n3.Insert at the
end.\n4.InsertAfter.\n5.Insert Before.\n6.Delete at the beginning.\n7.Delete at the
end.\n8.DeleteAfter.\n9.DeleteBefore.\n10.Searchanelement.\n11.Reverse the
list.\n12.Exit.");
printf("\nEnter the
choice:");scanf("%d",&a);
switch(a)
{
case1:
head=createll(head);t
raverse(head);break;
case2:
printf("\nInserting at
beginning:");head =
insertatbeg(head);traverse(head);
break;
case3:
printf("\nInsertingattheend:");h
ead =
insertatend(head);traverse(head
);
break;
case4:
printf("Entervalueafterwhichtobeinserted:");scanf
("%d",&b);
printf("\nInsertingattheafter:");h
ead
=insertafter(head,b);traverse(hea
d);
break;
case5:
printf("Enter value before which to be
inserted:");scanf("%d",&b);
printf("\nInsertingatthebefore:");h
ead
=insertbefore(head,b);traverse(hea
d);
break;
case6:
printf("\ndeletingatbeginning:");h
ead=
deleteatbeg(head);traverse(head);
break;
case7:
printf("\ndeletingatend:");h
ead =
deleteatend(head);traverse(
head);
break;
case8:
printf("Entervalueafterwhichtobedeleted:");sca
nf("%d",&b);
printf("\ndeleting
after:");head =
deleteafter(head,
b);traverse(head);
break;
case9:
printf("Entervaluebeforewhichtobedeleted:");sca
nf("%d",&b);
printf("\ndeleting
before:");head =
deletebefore(head,
b);traverse(head);
break;
case10:
printf("Entervaluetobesearched:");sc
anf("%d",&c);
intval=search(head,c);if(
val==1)
{
printf("Elementfound.");
}
else
{
printf("Elementnotfound.");
}
break;
case11:
head =
reverselist(head);printf("\
nReversedlist:");
traverse(head);
break;
case12:
printf("\nExiting.");re
turn0;
}
}
}
Testcases-
1) 2)
3) 4)
5) 6)
8)
7)
9)
Name-OmChoudhari
Name - Pranav Pandit
Roll no - C3 41
1.ShellSort-
#include<stdio.h>
for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
printf("Originalarray:\n");
printArray(arr,n);
shellSort(arr,n);
printf("Sorted array:\n");
printArray(arr,n);
return0;
}
Output-
2.MergeSort-
#include<stdio.h>
voidmerge(intarr[],intleft,intmid,intright){ int
i,j,k,n1=mid-left+1,n2=right-mid;
int
L[n1],R[n2];for(i=0;i<n1;i++)L[i
]=arr[left+i];
for(j=0;j<n2;j++)R[j]=arr[mid+1+j];
i=0;j=0;k=left;
while(i<n1&&j<n2) {
if(L[i]<=R[j])arr[k++]=L[i++];
else arr[k++]=R[j++];
}
while(i<n1)arr[k++]=L[i++];
while(j<n2)arr[k++]=R[j++];
}
voidmergeSort(intarr[],intleft,intright){
if(left<right) {
int mid=left+(right-left)/2;
mergeSort(arr,left,mid);
mergeSort(arr,mid+1,right);
merge(arr,left,mid,right);
}
}
intmain(){ int
n;
printf("Enternumberofelements:");
scanf("%d",&n);
2.MergeSort-
for(inti=0;i<n;i++)scanf("%d",&arr[i]);
mergeSort(arr,0,n-1);
printf("Sortedarray:");
for(inti=0;i<n;i++)printf("%d",arr[i]);
printf("\n");
return0;
}
Output-
3.MergeSort-
#include<stdio.h>
voidquickSort(intarr[],intlow,inthigh){ if
(low < high) {
intpivot=arr[high], i=low-1; for
(int j= low; j< high; j++) {
if(arr[j]<pivot){ i++;
inttemp=arr[i];
arr[i] = arr[j];
arr[j]=temp;
}
}
int temp = arr[i+ 1];
arr[i+1]=arr[high];
arr[high] = temp;
int pi = i + 1;quickSort(arr,
low, pi - 1);
quickSort(arr,pi+1,high);
}
}
voidprintArray(intarr[],intn){ for
(int i = 0; i < n; i++) {
printf("%d",arr[i]);
}
printf("\n");
}
intmain(){ int
n;
printf("Enterthenumberofelements:");
scanf("%d", &n);
intarr[n];
printf("Enter%delements:",n); for
(int i = 0; i < n; i++) {
scanf("%d",&arr[i]);
}
quickSort(arr, 0, n - 1);
printf("Sortedarray:\n");
printArray(arr,n);
return 0;
}
Output-
Name:Pranav Pandit Roll
No. : C3-41
Code:
#include <stdio.h>
#include<stdlib.h>
structnode{
structnode*prev;
int data;
structnode*next;
};
structnode*Createdll(){
structnode*nn=(structnode*)malloc(sizeof(structnode)); nn-
>prev = NULL;
printf("Enterthedataforthefirstnode:");
scanf("%d", &nn->data);
nn->next=NULL;
return nn;
}
structnode*insert_beg(structnode*start){
structnode*nn=(structnode*)malloc(sizeof(structnode));
printf("\nEnter the data for the new node: ");
scanf("%d",&nn->data);
nn->prev = NULL;
nn->next =start;
if(start!=NULL){
start->prev=nn;
}
returnnn;
}
structnode*insert_end(structnode*start){
structnode*nn=(structnode*)malloc(sizeof(structnode));
printf("\nEnter the data for the new node: ");
scanf("%d",&nn->data);
nn->next = NULL;
if(start==NULL){
nn->prev=NULL;
returnnn;//Newheadifthelistisempty
}
structnode*insert_after(structnode*start){
structnode*nn=(structnode*)malloc(sizeof(structnode));
printf("\nEnter the data for the new node: ");
scanf("%d",&nn->data);
int pos;
printf("\nEnterthevalueafterwhichthedatahastobeinserted:"); scanf("%d",
&pos);
structnode*ptr=start;
while(ptr!=NULL&&ptr->data!=pos){ ptr =
ptr->next;
}
if(ptr==NULL){
printf("Valuenotfoundinthelist.\n");
free(nn);
returnstart;
}
nn->next=ptr->next;
nn->prev = ptr;
if (ptr->next != NULL) {
ptr->next->prev=nn;
}
ptr->next=nn;
return start;
}
structnode*insert_before(structnode *start){
struct node *nn;
struct node *ptr;
structnode*preptr;
int pos;
nn=(structnode*)malloc(sizeof(structnode));
printf("\n Enter the data : ");
scanf("%d",&nn->data);
printf("\nEnterthevaluebeforewhichthedatahastobeinserted:"); scanf("%d",
&pos);
ptr=start;
while(ptr->data!=pos){
preptr = ptr;
ptr=ptr->next;
}
preptr->next=nn;
nn->prev=preptr;
nn->next = ptr;
ptr->prev=nn;
return start;
}
voiddisplay(structnode*start){
struct node* ptr = start;
if (ptr == NULL) {
printf("Listisempty.\n");
return;
}
printf("Listelements:");
while (ptr != NULL) {
printf("%d",ptr->data);
ptr = ptr->next;
}
printf("\n");
}
structnode*delete_beg(structnode*start){
struct node *ptr;
ptr=start;
start = start -> next;
start->prev=NULL;
free(ptr);
returnstart;
}
structnode*delete_end(structnode*start){
struct node *ptr;
ptr=start;
while(ptr->next!=NULL) ptr =
ptr -> next;
ptr->prev->next=NULL;
free(ptr);
returnstart;
}
structnode*delete_after(structnode*start){
struct node *ptr, *temp;
intval;
printf("Enterthevalueafterwhichthenodehastodeleted:"); scanf("%d", &val);
ptr=start;
while(ptr->data!=val) ptr
= ptr -> next;
temp=ptr->next;
ptr->next=temp->next;
temp->next->prev=ptr;
free(temp);
returnstart;
}
structnode*delete_before(structnode*start){ struct
node *ptr, *temp;
intval;
printf("\nEnterthevaluebeforewhichthenodehastodeleted:"); scanf("%d",
&val);
ptr=start;
while(ptr->data!=val) ptr
= ptr -> next;
temp=ptr->prev; if(temp
== start)
start=delete_beg(start);
else{
ptr->prev=temp->prev;
temp->prev->next=ptr;
}
free(temp);
returnstart;
}
voidsearch(structnode*start){ int
val;
printf("\nEnterthevaluetobesearched:");
scanf("%d", &val);
structnode*ptr=start;
while (ptr != NULL) {
if(ptr->data==val){
printf("Element%dfoundinthelist.\n",val); return;
}
ptr=ptr->next;
}
printf("Element%dnotfoundinthelist.\n",val);
}
structnode*interchange_k(structnode*start){ intk ;
printf("enterthekthpositionforthek+1thposition");
scanf("%d",&k);
structnode*ptr=start;
for(inti=1;i<k&&ptr!=NULL;i++){ ptr = ptr-
>next;
}
node_k1->prev=node_k->prev;
node_k->next=node_k1->next;
node_k1->next = node_k; node_k-
>prev = node_k1;
if(node_k->next!=NULL){
node_k->next->prev=node_k;
}
returnstart;
}
intmain(){
structnode*start=NULL; int
choice;
while(1){
printf("\nMenuForDoublyLinkedList\n");
printf("1. Create a Node\n");
printf("2. Insert at Beginning\n");
printf("3. Insert at End\n"); printf("4.
Insert After a Node\n");
printf("5.InsertbeforeaNode\n");
printf("6. Display\n");
printf("7. Delete at beg\n"); printf("8.
Delete at end\n"); printf("9. Delete
after a node \n");
printf("10.Deletebeforeanode\n");
printf("11. Search\n");
printf("12. Interchange\n");
printf("13. Exit\n");
printf("Enteryourchoice:");
scanf("%d", &choice);
switch(choice){
case 1:
if (start == NULL) {
start=Createdll();
}else{
printf("Listalreadycreated.\n");
}
break;
case 2:
start=insert_beg(start);
break;
case3:
start=insert_end(start);
break;
case4:
start=insert_after(start);
break;
case5:
start=insert_before(start);
case 6:
display(start);
break;
case7:
start=delete_beg(start);
break;
case8:
start=delete_end(start);
break;
case9:
start=delete_after(start);
break;
case10:
start=delete_before(start);
break;
case 11:
search(start);
break;
case 12:
interchange_k(start);
break;
case 13:
printf("Exiting...\n");
return 0;
default:
printf("Invalidchoice.Pleasetryagain.\n");
}
}
return0;
}#include<stdio.h>
#include<stdlib.h>
structnode{
structnode*prev;
int data;
structnode*next;
};
structnode*Createdll(){
structnode*nn=(structnode*)malloc(sizeof(structnode)); nn-
>prev = NULL;
printf("Enterthedataforthefirstnode:");
scanf("%d", &nn->data);
nn->next=NULL;
return nn;
}
structnode*insert_beg(structnode*start){
structnode*nn=(structnode*)malloc(sizeof(structnode));
printf("\nEnter the data for the new node: ");
scanf("%d",&nn->data);
nn->prev = NULL;
nn->next =start;
if(start!=NULL){
start->prev=nn;
}
returnnn;
}
structnode*insert_end(structnode*start){
structnode*nn=(structnode*)malloc(sizeof(structnode));
printf("\nEnter the data for the new node: ");
scanf("%d",&nn->data);
nn->next = NULL;
structnode*insert_after(structnode*start){
structnode*nn=(structnode*)malloc(sizeof(structnode));
printf("\nEnter the data for the new node: ");
scanf("%d",&nn->data);
int pos;
printf("\nEnterthevalueafterwhichthedatahastobeinserted:"); scanf("%d",
&pos);
structnode*ptr=start;
while(ptr!=NULL&&ptr->data!=pos){ ptr =
ptr->next;
}
if(ptr==NULL){
printf("Valuenotfoundinthelist.\n");
free(nn);
returnstart;
}
nn->next=ptr->next;
nn->prev = ptr;
if (ptr->next != NULL) {
ptr->next->prev=nn;
}
ptr->next=nn;
return start;
}
structnode*insert_before(structnode*start){ struct
node *nn;
struct node *ptr;
structnode*preptr;
int pos;
nn=(structnode*)malloc(sizeof(structnode));
printf("\n Enter the data : ");
scanf("%d",&nn->data);
printf("\nEnterthevaluebeforewhichthedatahastobeinserted:"); scanf("%d",
&pos);
ptr=start;
while(ptr->data!=pos){
preptr = ptr;
ptr=ptr->next;
}
preptr->next=nn;
nn->prev=preptr;
nn->next = ptr;
ptr->prev=nn;
return start;
}
voiddisplay(structnode*start){
struct node* ptr = start;
if (ptr == NULL) {
printf("Listisempty.\n");
return;
}
printf("Listelements:");
while (ptr != NULL) {
printf("%d",ptr->data);
ptr = ptr->next;
}
printf("\n");
}
structnode*delete_beg(structnode*start){
struct node *ptr;
ptr=start;
start = start -> next;
start->prev=NULL;
free(ptr);
returnstart;
}
structnode*delete_end(structnode*start){
struct node *ptr;
ptr=start;
while(ptr->next!=NULL) ptr =
ptr -> next;
ptr->prev->next=NULL;
free(ptr);
returnstart;
}
structnode*delete_after(structnode*start){
struct node *ptr, *temp;
intval;
printf("Enterthevalueafterwhichthenodehastodeleted:");
scanf("%d",&val); ptr
= start;
while(ptr->data!=val) ptr
= ptr -> next;
temp=ptr->next;
ptr->next=temp->next;
temp->next->prev=ptr;
free(temp);
returnstart;
}
structnode*delete_before(structnode*start){ struct
node *ptr, *temp;
intval;
printf("\nEnterthevaluebeforewhichthenodehastodeleted:"); scanf("%d",
&val);
ptr=start;
while(ptr->data!=val) ptr
= ptr -> next;
temp=ptr->prev; if(temp
== start)
start=delete_beg(start);
else{
ptr->prev=temp->prev;
temp->prev->next=ptr;
}
free(temp);
returnstart;
}
voidsearch(structnode*start){ int
val;
printf("\nEnterthevaluetobesearched:");
scanf("%d", &val);
structnode*ptr=start;
while (ptr != NULL) {
if(ptr->data==val){
printf("Element%dfoundinthelist.\n",val); return;
}
ptr=ptr->next;
}
printf("Element%dnotfoundinthelist.\n",val);
}
structnode*interchange_k(structnode*start){ intk ;
printf("enterthekthpositionforthek+1thposition");
scanf("%d",&k);
structnode*ptr=start;
for(inti=1;i<k&&ptr!=NULL;i++){ ptr = ptr-
>next;
}
node_k1->prev=node_k->prev;
node_k->next=node_k1->next;
node_k1->next = node_k; node_k-
>prev = node_k1;
returnstart;
}
intmain(){
structnode*start=NULL; int
choice;
while(1){
printf("\nMenuForDoublyLinkedList\n");
printf("1. Create a Node\n");
printf("2. Insert at Beginning\n");
printf("3. Insert at End\n"); printf("4.
Insert After a Node\n");
printf("5.InsertbeforeaNode\n");
printf("6. Display\n");
printf("7. Delete at beg\n"); printf("8.
Delete at end\n"); printf("9. Delete
after a node \n");
printf("10.Deletebeforeanode\n");
printf("11. Search\n");
printf("12. Interchange\n");
printf("13. Exit\n");
printf("Enteryourchoice:");
scanf("%d", &choice);
switch(choice){
case 1:
if (start == NULL) {
start=Createdll();
}else{
printf("Listalreadycreated.\n");
}
break;
case 2:
start=insert_beg(start);
break;
case3:
start=insert_end(start);
break;
case4:
start=insert_after(start);
break;
case5:
start=insert_before(start);
case 6:
display(start);
break;
case7:
start=delete_beg(start);
break;
case8:
start=delete_end(start);
break;
case9:
start=delete_after(start);
break;
case10:
start=delete_before(start);
break;
case 11:
search(start);
break;
case 12:
interchange_k(start);
break;
case 13:
printf("Exiting...\n");
return 0;
default:
printf("Invalidchoice.Pleasetryagain.\n");
}
}
return0;
}
Output:
1)
2)
3)
4)
5)
6)
7)
8)
9)
Name-Pranav Pandit
Roll No- C3 41
PracticalNo.-8
Code-
#include<stdio.h>#inclu
de<stdlib.h>structNode{
intdata;
structNode*left;
structNode*right;
};
structNode*createNode(intdata){
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));newNode->data=data;
newNode->left = newNode->right =
NULL;returnnewNode;
}
struct Node* insert(struct Node* root, int data)
{if(root==NULL)returncreateNode(data);
if (data < root->data) root->left = insert(root->left,
data);elseroot->right =insert(root->right, data);
returnroot;
}
structNode*search(structNode*root,intdata){
if(root==NULL||root->data==data)returnroot;
if (data < root->data) return search(root->left,
data);returnsearch(root->right, data);
}
void preorder(struct Node* root)
{if(root !=NULL){
printf("%d ", root-
>data);preorder(root-
>left);preorder(root-
>right);
}
}
void inorder(struct Node* root)
{if(root !=NULL){
inorder(root-
>left);printf("%d ", root-
>data);inorder(root-
>right);
}
}
void postorder(struct Node* root)
{if(root != NULL){
postorder(root-
>left);postorder(root-
>right);
printf("%d",root->data);
}
}
intmain(){
struct Node* root =
NULL;root = insert(root,
45);insert(root,38);
insert(root,54);
insert(root,21);
insert(root,41);
insert(root,49);
insert(root,60);
insert(root,10);
insert(root,29);
Output-