Ds Lab
Ds Lab
#include<stdio.h>
#define Max 5
int Push(int[],int);
int Pop(int[],int);
void Show(int[],int);
int main(){
int ch,stack[Max],top=-1;
for(;;)
{
printf("1.Push\n2.Pop\n3.Show\n4.Exit\nEnter your
choice");
scanf("%d",&ch);
switch(ch)
{
case 1: top=Push(stack,top);
break;
case 2: top=Pop(stack,top);
break;
case 3: Show(stack,top);
break;
case 4: return 0;
default:printf("Invalid choice\nPlease enter valid
choice\n");
}//enf of switch
}//end of for
}//end of main
//Push operation
int Push(int s[Max], int t)
{
int element;
if(t==Max-1)
printf("Stack is full....!\n");
else
{
printf("Enter the elements");
scanf("%d",&element);
t+=1;
s[t]=element;
}
return t;
}
//Pop Operation
int Pop(int s[Max],int Top)
{
int element;
if(Top==-1)
printf("Stack is empty");
else
{
element=s[Top];
printf("%d is popped",element);
Top-=1;
}
return(Top);
}
//Display
#include <stdio.h>
#define max 10
top = push(stack, top, result); // Push the result onto the stack
}
i++;
}
return pop(stack, &top); // Final result
}
int main() {
char postfix[25];
printf("Enter the postfix expression: ");
scanf("%s", postfix);
int result = evaluatePostfix(postfix);
printf("The result of postfix expression %s is: %d\n", postfix, result);
return 0;
}
//output:Enter the postfix expression: 23+5*
//The result of postfix expression 23+5* is: 25
//------------------
//(program exited with code: 0)
//Press return to continue
3.Write a C Program to convert infix to postfix expression.
#include <stdio.h>
#include <string.h>
infixToPostfix(infix, postfix);
return 0;
}
4. Write a C Program to demonstrate Queue operations using arrays.
#include <stdio.h>
int main() {
int queue[Max];
int choice;
int front = 0, rear = -1;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Insert\n2. Delete\n3. Show\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
rear = Insert(queue, rear);
break;
case 2:
front = Delete(queue, front, rear);
break;
case 3:
Show(queue, front, rear);
break;
case 4: // Add exit option
printf("Exiting...\n");
return 0;
default:
printf("Invalid queue choice!\n");
break;
}
}
return 0;
}
5. Write a C Program to demonstrate different operations on singly
linked list.
#include<stdio.h>
#include<stdlib.h>
struct Node{
int info;
struct Node* link;
};
typedef struct Node* NODE;
//Function declarations
NODE Insert_Front(int,NODE);
NODE Insert_End(int,NODE);
NODE Delete_Front(NODE);
NODE Delete_End(NODE);
void display(NODE);
NODE Next=First,prev=NULL;
while(Next->link!=NULL){
prev=Next;
Next=Next->link;
}
prev->link=NULL;
printf("%d deleted element\n", Next->info);
free(Next);
return First;
}
printf("\n");
printf("Linked List: \n");
while (temp != NULL) {
printf("%d\t", temp->info);
temp = temp->link;
}
printf("\n");
// Main function
int main() {
NODE First = NULL;
int choice, ele;
while (1) {
printf("1. Insert at Front\n2. Insert at End\n3. Delete from Front\n4.
Delete from End\n5. Display List\n6. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &ele);
First = Insert_Front(ele, First);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &ele);
First = Insert_End(ele, First);
break;
case 3:
First = Delete_Front(First);
break;
case 4:
First = Delete_End(First);
break;
case 5:
display(First);
break;
case 6:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
6. Write a C Program to demonstrate different operations on circular
doubly linked list.
#include <stdio.h>
#include <stdlib.h>
// Function declarations
NODE Insert_front_doubly_circular_list(int e, NODE First);
NODE Insert_end_doubly_circular_list(int e, NODE First);
NODE Delete_front_doubly_circular_list(NODE First);
NODE Delete_rear_doubly_circular_list(NODE First);
void Display_doubly_circular_list(NODE First);
// Function to insert a node at the front of the doubly circular linked list
NODE Insert_front_doubly_circular_list(int e, NODE First) {
NODE New;
New = (NODE)malloc(sizeof(struct Node)); // Allocate memory for
new node
New->info = e;
New->prev = NULL;
New->next = NULL;
// Function to insert a node at the end of the doubly circular linked list
NODE Insert_end_doubly_circular_list(int e, NODE First) {
NODE New = (NODE)malloc(sizeof(struct Node)); // Allocate
memory for the new node
New->info = e;
New->prev = NULL;
New->next = NULL;
// Function to delete a node from the front of the doubly circular linked
list
NODE Delete_front_doubly_circular_list(NODE First) {
if (First == NULL) {
printf("List is empty, nothing to delete from the front.\n");
return NULL;
}
return Second; // The second node becomes the new first node
}
// Function to delete a node from the rear of the doubly circular linked list
NODE Delete_rear_doubly_circular_list(NODE First) {
if (First == NULL) {
printf("List is empty, nothing to delete from the rear.\n");
return NULL;
}
while (1) {
printf("\nDoubly Circular Linked List Operations:\n");
printf("1. Insert at Front\n");
printf("2. Insert at End\n");
printf("3. Delete from Front\n");
printf("4. Delete from Rear\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to insert at the front: ");
scanf("%d", &element);
First = Insert_front_doubly_circular_list(element, First);
break;
case 2:
printf("Enter the element to insert at the end: ");
scanf("%d", &element);
First = Insert_end_doubly_circular_list(element, First);
break;
case 3:
First = Delete_front_doubly_circular_list(First);
break;
case 4:
First = Delete_rear_doubly_circular_list(First);
break;
case 5:
Display_doubly_circular_list(First);
break;
case 6:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
7.Write a C program to implement the following operation on binary
treeusing array:
i. Insert
ii. Delete
iii. Tree traversal
#include <stdio.h>
#include <stdlib.h>
int i;
for (i = 0; i < size; i++) {
if (tree[i] == value) {
break;
}
}
if (i == size) {
printf("Element not found in the tree\n");
return;
}
// Replace the element to be deleted with the last element
tree[i] = tree[size - 1];
size--;
}
int main() {
int choice, value;
while (1) {
printf("\nBinary Tree Operations\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. In-order Traversal\n");
printf("4. Pre-order Traversal\n");
printf("5. Post-order Traversal\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
printf("Enter the value to delete: ");
scanf("%d", &value);
delete(value);
break;
case 3:
printf("In-order Traversal: ");
inorder(0);
printf("\n");
break;
case 4:
printf("Pre-order Traversal: ");
preorder(0);
printf("\n");
break;
case 5:
printf("Post-order Traversal: ");
postorder(0);
printf("\n");
break;
case 6:
exit(0);
break;
default:
printf("Invalid choice\n");
}
}
return 0;
}