Académique Documents
Professionnel Documents
Culture Documents
SRN: R17CS403
SET: 7
1. Create a single linked list that has a priority field associated with the data stored in each
node. Perform search operation based on the priority of the node and delete that node from
the linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head = NULL;
int z,n;
while(1)
{
printf("\n Enter your choice \n");
printf("\n 1. add element \n");
printf("\n 2. delete element \n");
printf("\n 3. search element \n");
scanf("%d",&z);
if(z==1)
{
printf("\n enter elements to add\n");
scanf("%d",&n);
addLast(&head,n);
}
else if(z==2)
{
deleteNode(&head,n);
printf("Deleted . The New Linked List:\n");
printList(head);
}
else
{
int srch;
printf(" \n enter search element \n");
scanf("%d",&srch);
if(searchNode(head,srch) == 1)
printf("Search Found\n");
else
printf("Search Not Found\n");
}
}
return 0;
}
OUTPUT
2. Write a C program to demonstrate min heap and max heap construction.
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
int main()
{
int arr[] = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9};
int n = sizeof(arr)/sizeof(arr[0]);
convertMaxHeap(arr, n);
return 0;
}
OUTPUT
3. Implement a singly linked list in C programming language that works like a stack.
struct Node {
int data;
struct Node* link;
};
struct Node* top;
void pop()
{
struct Node* temp;
// main function
int main(void)
{
// push the elements of stack
push(11);
push(22);
push(33);
push(44);
OUTPUT
4. Implement the following functions on Singly Linked List with Header Node.
a. searchKey(int key);
b. deleteKey(int key);
c. insertAfterKey(int elem, int key);
d. mergeTwoLists(NODE *, NODE *);
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head = NULL;
int ch,srch;
while(1)
{
printf("\n enter your choice \n");
printf(" 1.search \n 2.delete \n 3.insert \n 4.merge \n");
scanf("%d",&ch);
if(ch==1)
{
printf("\n enter value to search \n");
scanf("%d",&srch);
if(searchNode(head,srch) == 1)
printf("Search Found\n");
else
printf("Search Not Found\n");
}
else if(ch==2)
{
int del;
printf("\n enter value to delete \n");
scanf("%d",&del);
deleteNode(&head,del);
printList(head);
}
else if(ch==3)
{
int insert;
printf("\n enter value to insert \n");
scanf("%d",&insert);
addLast(&head,insert);
}
else if(ch==4)
{
printf(" inserting in the first list...(1,3,5) \n");
node *head1 = newNode(1);
head1->next = newNode(3);
head1->next->next = newNode(5);
printList(head1);
// 1->3->5 LinkedList created
printf(" inserting in the second list...(0,2,4) \n");
node *head2 = newNode(0);
head2->next = newNode(2);
head2->next->next = newNode(4);
printList(head2);
// 0->2->4 LinkedList created
node *mergedhead = merge(head1, head2);
printf("\n THE FINAL MERGE LIST IS : \n ");
printList(mergedhead);
}
else
{
printf("\n wrong choice \n");
}
}
return 0;
}
OUTPUT
5. Write a menu driven C program to demonstrate functions insertion, deletion on Circular
Doubly Linked List.
*head_ref = ptr1;
}
printf("\n");
}
prev = curr;
curr = curr -> next;
}
OUTPUT