Vous êtes sur la page 1sur 18

DSA ASSIGNMENT-II

NAME : SNEHASISH BANIK


SEM: III (G)

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

void addLast(struct node **head, int val)


{
//create a new node
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;

//if head is NULL, it is an empty list


if(*head == NULL)
*head = newNode;
//Otherwise, find the last node and add the newNode
else
{
struct node *lastNode = *head;

//last node's next address will be NULL.


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

//add the newNode at the end of the linked list


lastNode->next = newNode;
}

void deleteNode(struct node **head, int key)


{
//temp is used to freeing the memory
struct node *temp;

//key found on the head node.


//move to head node to the next and free the head.
if((*head)->data == key)
{
temp = *head; //backup to free the memory
*head = (*head)->next;
free(temp);
}
else
{
struct node *current = *head;
while(current->next != NULL)
{
//if yes, we need to delete the current->next node
if(current->next->data == key)
{
temp = current->next;
//node will be disconnected from the linked list.
current->next = current->next->next;
free(temp);
break;
}
//Otherwise, move the current node and proceed
else
current = current->next;
}
}
}

void printList(struct node *head)


{
struct node *temp = head;

//iterate the entire linked list and print the data


while(temp != NULL)
{
printf("%d ->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int searchNode(struct node *head,int key)
{
struct node *temp = head;

//iterate the entire linked list and print the data


while(temp != NULL)
{
//key found return 1.
if(temp->data == key)
return 1;
temp = temp->next;
}
//key not found
return -1;
}

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;

void MaxHeapify(int arr[], int i, int n)


{
int l = 2*i + 1;
int r = 2*i + 2;
int largest = i;
if (l < n && arr[l] > arr[i])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
swap(arr[i], arr[largest]);
MaxHeapify(arr, largest, n);
}
}
void convertMaxHeap(int arr[], int n)
{
for (int i = (n-2)/2; i >= 0; --i)
MaxHeapify(arr, i, n);
}

void printArray(int* arr, int size)


{
for (int i = 0; i < size; ++i)
printf("%d ", arr[i]);
}

int main()
{
int arr[] = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9};
int n = sizeof(arr)/sizeof(arr[0]);

printf("Min Heap array : ");


printArray(arr, n);

convertMaxHeap(arr, n);

printf("\nMax Heap array : ");


printArray(arr, n);

return 0;
}

OUTPUT

3. Implement a singly linked list in C programming language that works like a stack.

// C program to Implement a stack


//using singly linked list
#include <stdio.h>
#include <stdlib.h>

// Declare linked list node

struct Node {
int data;
struct Node* link;
};
struct Node* top;

// Utility function to add an element data in the stack


// insert at the beginning
void push(int data)
{
// create new node temp and allocate memory
struct Node* temp;
temp = (struct Node*)malloc(sizeof(struct Node));

// check if stack (heap) is full. Then inserting an element would


// lead to stack overflow
if (!temp) {
printf("\nHeap Overflow");
exit(1);
}

// initialize data into temp data field


temp->data = data;

// put top pointer reference into temp link


temp->link = top;

// make temp as top of Stack


top = temp;
}

// Utility function to check if the stack is empty or not


int isEmpty(struct Node* top)
{
return top == NULL;
}

// Utility function to return top element in a stack


int peek()
{
// check for empty stack
if (!isEmpty(top))
return top->data;
else
exit(EXIT_FAILURE);
}

// Utility function to pop top element from the stack

void pop()
{
struct Node* temp;

// check for stack underflow


if (top == NULL) {
printf("\nStack Underflow");
exit(1);
}
else {
// top assign into temp
temp = top;

// assign second node to top


top = top->link;

// destroy connection between first and second


temp->link = NULL;

// release memory of top node


free(temp);
}
}

void display() // remove at the beginning


{
struct Node* temp;

// check for stack underflow


if (top == NULL) {
printf("\nStack Underflow");
exit(1);
}
else {
temp = top;
while (temp != NULL) {

// print node data


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

// assign temp link to temp


temp = temp->link;
}
}
}

// main function

int main(void)
{
// push the elements of stack
push(11);
push(22);
push(33);
push(44);

// display stack elements


display();

// print top elementof stack


printf("\nTop element is %d\n", peek());

// delete top elements of stack


pop();
pop();

// display stack elements


display();

// print top elementof stack


printf("\nTop element is %d\n", peek());
return 0;
}

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

void addLast(struct node **head, int val)


{
//create a new node
struct node *newNode =(struct node *)malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;

//if head is NULL, it is an empty list


if(*head == NULL)
*head = newNode;
//Otherwise, find the last node and add the newNode
else
{
struct node *lastNode = *head;

//last node's next address will be NULL.


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

//add the newNode at the end of the linked list


lastNode->next = newNode;
}

int searchNode(struct node *head,int key)


{
struct node *temp = head;

//iterate the entire linked list and print the data


while(temp != NULL)
{
//key found return 1.
if(temp->data == key)
return 1;
temp = temp->next;
}
//key not found
return -1;
}
void deleteNode(struct node **head, int key)
{
//temp is used to freeing the memory
struct node *temp;

//key found on the head node.


//move to head node to the next and free the head.
if((*head)->data == key)
{
temp = *head; //backup to free the memory
*head = (*head)->next;
free(temp);
}
else
{
struct node *current = *head;
while(current->next != NULL)
{
//if yes, we need to delete the current->next node
if(current->next->data == key)
{
temp = current->next;
//node will be disconnected from the linked list.
current->next = current->next->next;
free(temp);
break;
}
//Otherwise, move the current node and proceed
else
current = current->next;
}
}
}
void printList(struct node *head)
{
struct node *temp = head;

//iterate the entire linked list and print the data


while(temp != NULL)
{
printf("%d ->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
node *newNode(int key)
{
struct node *temp = new node;
temp->data = key;
temp->next = NULL;
return temp;
}
node *merge(node *h1, node *h2)
{
if (!h1)
return h2;
if (!h2)
return h1;

if (h1->data < h2->data)


{
h1->next = merge(h1->next, h2);
return h1;
}
else
{
h2->next = merge(h1, h2->next);
return h2;
}
}

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.

// C program to delete a given key from


// linked list.
#include<stdio.h>
#include<stdlib.h>

/* structure for a node */


struct Node
{
int data;
struct Node *next;
};

/* Function to insert a node at the beginning of


a Circular linked list */
void push(struct Node **head_ref, int data)
{
// Create a new node and make head as next
// of it.
struct Node *ptr1 =
(struct Node *)malloc(sizeof(struct Node));
ptr1->data = data;
ptr1->next = *head_ref;

/* If linked list is not NULL then set the


next of last node */
if (*head_ref != NULL)
{
// Find the node before head and update
// next of it.
struct Node *temp = *head_ref;
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1; /*For the first node */

*head_ref = ptr1;
}

/* Function to print nodes in a given


circular linked list */
void printList(struct Node *head)
{
struct Node *temp = head;
if (head != NULL)
{
do
{
printf("%d ", temp->data);
temp = temp->next;
}
while (temp != head);
}

printf("\n");
}

/* Function to delete a given node from the list */


void deleteNode(struct Node *head, int key)
{
if (head == NULL)
return;

// Find the required node


struct Node *curr = head, *prev;
while (curr->data != key)
{
if (curr->next == head)
{
printf("\nGiven node is not found"
" in the list!!!");
break;
}

prev = curr;
curr = curr -> next;
}

// Check if node is only node


if (curr->next == head)
{
head = NULL;
free(curr);
return;
}

// If more than one node, check if


// it is first node
if (curr == head)
{
prev = head;
while (prev -> next != head)
prev = prev -> next;
head = curr->next;
prev->next = head;
free(curr);
}

// check if node is last node


else if (curr -> next == head)
{
prev->next = head;
free(curr);
}
else
{
prev->next = curr->next;
free(curr);
}
}

/* Driver program to test above functions */


int main()
{
/* Initialize lists as empty */
struct Node *head = NULL;
int z;
while(1)
{
printf("\n enter your choice \n 1. insert \n 2. delete \n");
scanf("%d",&z);
if(z==1)
{
int x;
printf("\n enter the value to insert \n");
scanf("%d",&x);
push(&head,x);
}
else
{
printf("List Before Deletion: ");
printList(head);
int f;
printf("\n enter the value to delete \n");
scanf("%d",&f);
deleteNode(head,f);
printf("List After Deletion: ");
printList(head);
}
}
return 0;
}

OUTPUT

Vous aimerez peut-être aussi