Dsa Ela Da 2
Dsa Ela Da 2
INPUT/OUTPUT:
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
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
struct Node {
int data;
int priority;
struct Node* next;
};
}
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
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Deque {
struct Node* front;
struct Node* rear;
};
deque->rear = newNode;
} else {
newNode->prev = deque->rear;
deque->rear->next = newNode;
deque->rear = newNode;
}
}
int main() {
struct Deque *deque = createDeque();
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2
INPUT/OUTPUT:
DATA STRUCTURES ALGORITHMS AND ANALYSIS ELA DIGITAL ASSIGNMENT 2