0% found this document useful (0 votes)
57 views19 pages

Dsa Ela Da 2

The document describes an assignment submission for a Data Structures, Algorithms, and Analysis course. It includes the student's name, registration details, and faculty. It then provides source code for implementing various data structures - including linked lists, stacks, and priority queues - along with descriptions of functions for operations like insertion, deletion, traversal and more. Sample inputs and outputs are also presented.

Uploaded by

Varshan Manish
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)
57 views19 pages

Dsa Ela Da 2

The document describes an assignment submission for a Data Structures, Algorithms, and Analysis course. It includes the student's name, registration details, and faculty. It then provides source code for implementing various data structures - including linked lists, stacks, and priority queues - along with descriptions of functions for operations like insertion, deletion, traversal and more. Sample inputs and outputs are also presented.

Uploaded by

Varshan Manish
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
You are on page 1/ 19

DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

NAME: VARSHAN MANISH


REGISTRATION: 22MIC0101
PROGRAM: MTECH INTEGRATED CSE
COURSE: DATA STRUCTURE ALGORITHM AND ANALYSIS (CSI2002)
FACULTY: DR. ARUP GHOSH
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

SOURCE CODE (CODE IS TEXT BASED NOT PICTURE; COPY POSSIBLE):


#include <iostream>
using namespace std;
struct node
{
int data;
node *link;
};
node n1, n2, n3, n4;
int main()
{
cout<<"\nEnter Data N1: ";
cin>>n1.data;
cout<<"\nEnter Data N2: ";
cin>>n2.data;
cout<<"\nEnter Data N3: ";
cin>>n3.data;
cout<<"\nEnter Data N4: ";
cin>>n4.data;
n1.link=&n2;
n2.link=&n3;
n3.link=&n4;
cout<<"\n Data of N1, N2, N3, N4 are: ";
cout<<n1.data<<" "<<n2.data<<" ";
cout<<n3.data<<" "<<n4.data;
cout<<"\nLink of n1, n2, n3, n4 are: ";
cout<<n1.link<<" "<<n2.link<<" ";
cout<<n3.link<<" "<<n4.link;
return 0;
}

INPUT/OUTPUT:
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

SOURCE CODE (CODE IS TEXT BASED NOT PICTURE; COPY POSSIBLE):


#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
printf("***Doubly Linked List***");
while(choice != 9)
{
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random
location\n4.Delete from Beginning\n5.Delete from last\n6.Delete the node after the
given data\n7.Search\n8.Show\n9.Exit\n");
printf("\nEnter your choice: ");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

exit(0);
break;
default:
printf("Invalid Choice");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value: ");
scanf("%d",&item);

if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}

}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value: ");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}

}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location: ");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value: ");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted: ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search: ");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}

INPUT/OUTPUT:
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

SOURCE CODE (CODE IS TEXT BASED NOT PICTURE; COPY POSSIBLE):


#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}*top = NULL;

void push(int);
void pop();
void display();

void main()
{
int choice, value;
printf("***Stack using Linked List***\n");
while(1){
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nInvalid Choice\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;

else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = top->next;
free(temp);
}
}
void display() {
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

if (top == NULL)
printf("\nStack is Empty\n");
else {
struct Node *temp = top;
while (temp != NULL) {
printf("%d--->", temp->data);
temp = temp->next;
}
}
}

INPUT/OUTPUT:
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

SOURCE CODE (CODE IS TEXT BASED NOT PICTURE; COPY POSSIBLE):


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

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

struct Node* newNode(int data, int priority) {


struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->priority = priority;
temp->next = NULL;
return temp;
}

void enqueue(struct Node** front, int data, int priority) {


struct Node* newElement = newNode(data, priority);
if (*front == NULL || priority < (*front)->priority) {
newElement->next = *front;
*front = newElement;
} else {
struct Node* current = *front;
while (current->next != NULL && current->next->priority <= priority) {
current = current->next;
}
newElement->next = current->next;
current->next = newElement;
}
}

int dequeue(struct Node** front) {


if (*front == NULL) {
printf("Queue is empty.\n");
return -1; // You can choose a different error code if needed
}
struct Node* temp = *front;
int data = temp->data;
*front = (*front)->next;
free(temp);
return data;
}

int isEmpty(struct Node* front) {


return (front == NULL);
}

void display(struct Node* front) {


if (front == NULL) {
printf("Queue is empty.\n");
return;
}
printf("Queue (data, priority):\n");
struct Node* current = front;
while (current != NULL) {
printf("(%d, %d) ", current->data, current->priority);
current = current->next;
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

}
printf("\n");
}

int main() {
struct Node* front = NULL;
int choice, data, priority;
printf("***PriorityQueue Using Linked List***\n");
while (1) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data and priority: ");
scanf("%d %d", &data, &priority);
enqueue(&front, data, priority);
break;
case 2:
data = dequeue(&front);
if (data != -1) {
printf("Dequeued element: %d\n", data);
}
break;
case 3:
display(front);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

INPUT/OUTPUT:
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

SOURCE CODE (CODE IS TEXT BASED NOT PICTURE; COPY POSSIBLE):


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

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

struct Deque {
struct Node* front;
struct Node* rear;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}

struct Deque* createDeque() {


struct Deque* deque = (struct Deque*)malloc(sizeof(struct Deque));
if (deque == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
deque->front = NULL;
deque->rear = NULL;
return deque;
}

int isEmpty(struct Deque* deque) {


return (deque->front == NULL);
}

void insertFront(struct Deque* deque, int data) {


struct Node* newNode = createNode(data);
if (isEmpty(deque)) {
deque->front = newNode;
deque->rear = newNode;
} else {
newNode->next = deque->front;
deque->front->prev = newNode;
deque->front = newNode;
}
}

void insertRear(struct Deque* deque, int data) {


struct Node* newNode = createNode(data);
if (isEmpty(deque)) {
deque->front = newNode;
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

deque->rear = newNode;
} else {
newNode->prev = deque->rear;
deque->rear->next = newNode;
deque->rear = newNode;
}
}

int removeFront(struct Deque* deque) {


if (isEmpty(deque)) {
fprintf(stderr, "Deque is empty\n");
exit(EXIT_FAILURE);
}
struct Node* temp = deque->front;
int data = temp->data;
deque->front = deque->front->next;
if (deque->front == NULL) {
deque->rear = NULL;
} else {
deque->front->prev = NULL;
}
free(temp);
return data;
}

int removeRear(struct Deque* deque) {


if (isEmpty(deque)) {
fprintf(stderr, "Deque is empty\n");
exit(EXIT_FAILURE);
}
struct Node* temp = deque->rear;
int data = temp->data;
deque->rear = deque->rear->prev;
if (deque->rear == NULL) {
deque->front = NULL;
} else {
deque->rear->next = NULL;
}
free(temp);
return data;
}

void display(struct Deque* deque) {


if (isEmpty(deque)) {
printf("Deque is empty\n");
return;
}
struct Node* current = deque->front;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

void destroyDeque(struct Deque* deque) {


while (!isEmpty(deque)) {
removeFront(deque);
}
free(deque);
}

int main() {
struct Deque *deque = createDeque();
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

int choice, data;


printf("***Deque Using Linked List\n");

INPUT/OUTPUT:
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2

You might also like