0% found this document useful (0 votes)
9 views89 pages

Dsa file[1]

The document contains lab exercises for a Data Structures course, focusing on C programming with arrays and linked lists. It includes tasks such as creating arrays, performing operations on singly and doubly linked lists, and implementing circular linked lists. Each section provides code snippets and expected outputs for various operations like insertion and deletion.

Uploaded by

lokeshusar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views89 pages

Dsa file[1]

The document contains lab exercises for a Data Structures course, focusing on C programming with arrays and linked lists. It includes tasks such as creating arrays, performing operations on singly and doubly linked lists, and implementing circular linked lists. Each section provides code snippets and expected outputs for various operations like insertion and deletion.

Uploaded by

lokeshusar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

LAB FILE

Data Structures Lab(ARD - 255)


B.Tech - Artificial Intelligence & Machine
Learning

Submitted By Submitted To:


Name: Lokesh Yadav Dr. Anshul Bhatia
Batch: AIML B1
Roll No.: 04619051623
1. Write C programs by using Array data structure for the following problem
domains:
a. Create an array of integer with size n. Return the difference between the largest and
the smallest value inside that array.

void main() {
int n;
printf("enter number of elements in array ");
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++){
printf("enter element %d ",i+1);
scanf("%d",&a[i]);
}
int min=a[0],max=a[0];
for( int i=0;i<n;i++){
if (a[i]>max){
max=a[i];
}
if (a[i]<min){
min=a[i];
}
}
printf("max=%d \nmin=%d \ndifference=%d",max,min,max-min);
}
codes:
b. Initializes an array with ten random integers and then prints four lines of output,
containing: Every element at an even index, Every odd element, All elements in reverse
order, Only the first and last element

void main(){
int a[10];
for(int i=0;i<10;i++){
printf("enter element %d ",i+1);
scanf("%d",&a[i]);
}
printf("elements at even index are ");
for(int i=0;i<10;i+=2){
printf("%d ",a[i]);
}
printf("%c",'\n');
printf("odd elements are ");
for(int i=0;i<10;i++){
if(a[i]%2!=0){
printf("%d ",a[i]);
}
}
printf("%c",'\n');
printf("all elemts in reverse order");
for(int i=9;i>=0;i--){
printf("%d ",a[i]);
}
printf("%c",'\n');
printf("first element= %d\tlast element=%d",a[0],a[9]);
}
codes:

output:
c. Consider an integer array of size 5 and display the following: Sum of all the elements,
Sum of alternate elements in the array, and second highest element in the array

void main(){
int a[5];
for(int i=0;i<5;i++){
printf("enter elemt %d ",i+1);
scanf("%d",&a[i]);
}
int max=a[0],smax=a[0];
int sum=0,altsum=0;
for(int i=0;i<5;i++){
sum+=a[i];
if(i%2==0){
altsum+=a[i];
}
}
printf("sum of all elements =%d",sum);
printf("%c",'\n');
printf("sum of alternative elements =%d",altsum);
for(int i=1;i<5;i++){
if(a[i]>max){
max=a[i];
}
}
for(int i=1;i<5;i++){
if(a[i]>smax &&a[i]<max){
smax=a[i];
}
}

printf("%c",'\n');
printf("second highest number =%d",smax);
}
code:

output:
2. Write a program to create a singly linked list of n nodes and perform:

a. Insertion at the beginning


b. Insertion at the end
c. Insertion at a specific location
d. Deletion at the beginning
e. Deletion at the end
f. Deletion At a specific location

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

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

struct Node* createNode(int data) {


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

void insertAtFirst(struct Node** head, int data) {


struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

void insertAtEnd(struct Node** head, int data) {


struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

void insertAtPosition(struct Node** head, int data, int position) {


struct Node* newNode = createNode(data);
if (position == 0) {
insertAtFirst(head,data);
return;
}
struct Node* temp = *head;
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}

void deleteFromFirst(struct Node** head) {


if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = temp->next;
free(temp);
}

void deleteFromEnd(struct Node** head) {


if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (temp->next == NULL) {
free(temp);
*head = NULL;
return;
}
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}

void deleteAtPosition(struct Node** head, int position) {


if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (position == 0) {
deleteFromFirst(head);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("Position out of range\n");
return;
}
struct Node* next = temp->next->next;
free(temp->next);
temp->next = next;
}

void print(struct Node* head) {


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

void main() {
struct Node* head = NULL;

insertAtFirst(&head, 10);
printf("Linked list after inserting the node:10 at the beginning \n");
print(head);

printf("Linked list after inserting the node:70 at the beginning \n");


insertAtFirst(&head, 70);
print(head);

printf("Linked list after inserting the node:90 at the beggining \n");


insertAtFirst(&head, 90);
print(head);
printf("Linked list after inserting the node:15 at position 2 \n");
insertAtPosition(&head,15,2);
print(head);

printf("Linked list after inserting the node:77 at position 3 \n");


insertAtPosition(&head, 77, 3);
print(head);

printf("Linked list after inserting the node:100 at the end \n");


insertAtEnd(&head, 100);
print(head);

printf("Linked list after deleting the first node: \n");


deleteFromFirst(&head);
print(head);

printf("Linked list after deleting the last node: \n");


deleteFromEnd(&head);
print(head);

printf("Linked list after deleting the node at position 1: \n");


deleteAtPosition(&head, 1);
print(head);
}
codes:
output:
3. Write a program to create a doubly linked list of n nodes and perform:
a. Insertion at the beginning
b. Insertion at the end
c. Insertion at a specific location
d. Deletion at the beginning
e. Deletion at the end
f. Deletion At a specific location

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

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

struct node* createnode(int data) {


struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = data;
newnode->next = NULL;
newnode->prev = NULL;
}

void ins_beg(struct node **head, int data) {


struct node* newnode = createnode(data);
if (*head == NULL) {
*head = newnode;
} else {
newnode->next = *head;
(*head)->prev = newnode;
*head = newnode;
}
}

void ins_end(struct node **head,int data){


struct node *newnode=createnode(data);
struct node *temp=*head;
while(temp->next!=NULL){
temp=temp->next;
}
newnode->prev=temp;
temp->next=newnode;
}

void ins_at_pos(struct node **head, int data,int pos){


struct node *newnode =createnode(data);
struct node* temp= *head;
if (pos==1){
ins_beg(head,data);
}
for(int i=1; i<pos-1;i++){
temp=temp->next;
}
newnode->next=temp->next;
temp->next->prev=newnode;
newnode->prev=temp;
temp->next=newnode;
}

void del_beg(struct node **head){


struct node *temp=*head;
*head=(*head)->next;
free(temp);
(temp)=NULL;
}
void del_end(struct node **head){
struct node *temp=*head;
while(temp->next->next!=NULL){
temp=temp->next;
}
free(temp->next);
temp->next=NULL;
}

void del_at_pos(struct node **head,int pos){


struct node *temp=*head;
for(int i=1;i<pos-1;i++){
temp=temp->next;
}
struct node *delnode=temp->next;
temp->next=temp->next->next;
free(delnode);
}

void trav(struct node **head) {


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

void main() {
struct node *head = NULL;
printf("Linked list after inserting the node:5 at the beggining \n");
ins_beg(&head, 5);
trav(&head);
printf("Linked list after inserting the node:10 at the beggining \n");
ins_beg(&head, 10);
trav(&head);

printf("Linked list after inserting the node:11 at the end \n");


ins_end(&head, 11);
trav(&head);

printf("Linked list after inserting the node:12 at the end \n");


ins_end(&head, 12);
trav(&head);

printf("Linked list after inserting the node:15 at the end \n");


ins_end(&head, 15);
trav(&head);

printf("Linked list after inserting the node:9 at position 2 \n");


ins_at_pos(&head,9,2);
trav(&head);

printf("Linked list after deleting the node at the beggining \n");


del_beg(&head);
trav(&head);

printf("Linked list after deleting the node at the end \n");


del_end(&head);
trav(&head);

printf("Linked list after deleting the node at position 2 \n");


del_at_pos(&head,2);
trav(&head);
}
code:

output:
4. Write a program to create a singly circular and doubly linked list of n nodes and
perform:
a. Insertion at the beginning
b. Insertion at the end
c. Insertion at a specific
d. location
e. Deletion at the beginning
f. Deletion at the end
g. Deletion At a specific location

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
newNode->next = *head;
temp->next = newNode;
*head = newNode;
}
}
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}
}
void insertAtPosition(struct Node** head, int data, int position) {
struct Node* newNode = createNode(data);
if (position < 1) {
printf("Position should be >= 1\n");
} else if (position == 1) {
insertAtBeginning(head, data);
} else {
struct Node* temp = *head;
for (int i = 1; i < position - 1 && temp->next != *head; i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
void deleteAtBeginning(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
struct Node* toDelete = *head;
if (temp->next == *head) {
*head = NULL;
free(temp);
} else {
while (temp->next != *head) {
temp = temp->next;
}
*head = (*head)->next;
temp->next = *head;
free(toDelete);
}
}
void deleteAtEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
struct Node* toDelete;
if (temp->next == *head) {
*head = NULL;
free(temp);
} else {
while (temp->next->next != *head) {
temp = temp->next;
}
toDelete = temp->next;
temp->next = *head;
free(toDelete);
}
}
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (position == 1) {
deleteAtBeginning(head);
return;
}
struct Node* toDelete;
for (int i = 1; i < position - 1 && temp->next != *head; i++) {
temp = temp->next;
}
toDelete = temp->next;
temp->next = temp->next->next;
free(toDelete);
}

void displayList(struct Node* head) {


if (head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = head;
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("HEAD\n");
}
void main() {
struct Node* head = NULL;
insertAtBeginning(&head, 10);
insertAtBeginning(&head, 20);
insertAtBeginning(&head, 30);
insertAtBeginning(&head, 40);
printf("List after inserting at the beggening:\n");
displayList(head);
printf("\n");

insertAtBeginning(&head, 5);
printf("List after inserting at the beginning:\n");
displayList(head);

insertAtEnd(&head, 200);
printf("List after inserting at the end:\n");
displayList(head);
insertAtPosition(&head, 25, 3);
printf("List after inserting at position 3:\n");
displayList(head);

deleteAtBeginning(&head);
printf("List after deleting at the beginning:\n");
displayList(head);

deleteAtEnd(&head);
printf("List after deleting at the end:\n");
displayList(head);

deleteAtPosition(&head, 2);
printf("List after deleting at position 2:\n");
displayList(head);}
codes:
output:
DOUBLE CIRCULAR
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = newNode;
newNode->prev = newNode;
return newNode;
}
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* last = (*head)->prev;
newNode->next = *head;
newNode->prev = last;
last->next = newNode;
(*head)->prev = newNode;

*head = newNode;
}
}
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* last = (*head)->prev;
newNode->next = *head;
newNode->prev = last;
last->next = newNode;
(*head)->prev = newNode;
}
}
void insertAtLocation(struct Node** head, int data, int position) {
if (position == 0) {
insertAtBeginning(head, data);
return;
}
struct Node* newNode = createNode(data);
struct Node* temp = *head;

for (int i = 0; i < position - 1; i++) {


temp = temp->next;
if (temp == *head) {
printf("Position out of range.\n");
return;
}
}
newNode->next = temp->next;
newNode->prev = temp;
temp->next->prev = newNode;
temp->next = newNode;
}

void deleteFromBeginning(struct Node** head) {


if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = *head;
struct Node* last = (*head)->prev;
if (*head == (*head)->next) {
free(temp);
*head = NULL;
} else {
last->next = temp->next;
temp->next->prev = last;
*head = temp->next;
free(temp);
}
}

void deleteFromEnd(struct Node** head) {


if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* last = (*head)->prev;
if (*head == last) {
free(last);
*head = NULL;
} else {
struct Node* secondLast = last->prev;
secondLast->next = *head;
(*head)->prev = secondLast;
free(last);
}
}
void deleteFromLocation(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
if (position == 0) {
deleteFromBeginning(head);
return;
}

struct Node* temp = *head;


for (int i = 0; i < position; i++) {
temp = temp->next;
if (temp == *head) {
printf("Position out of range.\n");
return;
}
}

temp->prev->next = temp->next;
temp->next->prev = temp->prev;

if (temp == *head) {
*head = temp->next;
}

free(temp);
}
void printList(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


do {
printf("%d<-> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("head");
printf("\n");
}
void main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 12);
insertAtBeginning(&head, 13);
insertAtBeginning(&head, 14);
printf("List after insertions: ");
printList(head);
printf("\n");
insertAtBeginning(&head, 19);
printList(head);
insertAtEnd(&head, 2);
printList(head);
insertAtLocation(&head, 4, 1);
printList(head);

deleteFromBeginning(&head);
printList(head);
deleteFromEnd(&head);
printList(head);
deleteFromLocation(&head, 1);
printList(head);

}
codes:
output:
5. Write a program to implement stack using arrays and linked lists.

Using Array

#include <stdio.h>
#include <stdlib.h>
#define size 10

int stack[size];
int TOS = -1;

void push(int data);


int pop();
int peek();
void display();

int main() {
int conti = 1;

while(conti) {
int choice;
printf("1: Push\n");
printf("2: Pop\n");
printf("3: Peek\n");
printf("4: Display\n");
printf("0: Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &choice);

int data;
switch (choice) {
case 1:
printf("Enter the value you want to add : ");
scanf("%d", &data);
push(data);
break;
case 2:
data = pop();
printf("Popped element is : %d \n", data);
break;
case 3:
data = peek();
printf("Element at the top is : %d\n", data);
break;
case 4:
display();
break;
case 0:
printf("Thank you for using stack operator menu");
exit(0);

break;
default:
continue;
}
}
}

void push(int data) {


if(TOS == size - 1) {
printf("Stack Overflow\n");
exit(0);
}
TOS++;
stack[TOS] = data;
}
int pop() {
if(TOS == -1) {
printf("Stack Underflow\n");
exit(0);
}
int data = stack[TOS];
TOS--;
return data;
}

int peek() {
if(TOS == -1) {
printf("Stack Underflow\n");
exit(0);
}
return stack[TOS];
}

void display() {
if(TOS == -1) {
printf("Stack Underflow\n");
exit(0);
}
for(int i = TOS; i >= 0; i--) {
printf("%d, ", stack[i]);
}
printf("\n");
}
codes:

output:
Using Linked List
#include <stdio.h>
#include <stdlib.h>

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

struct Node* top = NULL;

void push(int data);


int pop();
int peek();
void display();

int main() {
int conti = 1;

while(conti) {
int choice;
printf("1: Push\n");
printf("2: Pop\n");
printf("3: Peek\n");
printf("4: Display\n");
printf("0: Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &choice);

int data;
switch (choice) {
case 1:
printf("Enter the value you want to add : ");
scanf("%d", &data);
push(data);
break;
case 2:
data = pop();
if(data != -1)
printf("Popped element is : %d \n", data);
break;
case 3:
data = peek();
if(data != -1)
printf("Element at the top is : %d\n", data);
break;
case 4:
display();
break;
case 0:
exit(0);
break;
default:
continue;
}
}
}

void push(int data) {


struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = top;
top = newNode;
}

int pop() {
if(top == NULL) {
printf("Stack Underflow\n");
return -1;
}
struct Node* temp = top;
int data = top->data;
top = top->next;
free(temp);
return data;
}

int peek() {
if(top == NULL) {
printf("Stack Underflow\n");
return -1;
}
return top->data;
}

void display() {
if(top == NULL) {
printf("Stack Underflow\n");
return;
}
struct Node* temp = top;
while(temp != NULL) {
printf("%d, ", temp->data);
temp = temp->next;
}
printf("\n");
}
codes:

output:
6.Write a program to reverse a sentence/string using stack.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100

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

struct Node* top = NULL;

void push(char data);


char pop();
int isEmpty();

int main() {
char str[MAX];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';

for (int i = 0; i < strlen(str); i++) {


push(str[i]);
}

printf("Reversed string: ");


while (!isEmpty()) {
printf("%c", pop());
}
printf("\n");
return 0;
}

void push(char data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = data;
newNode->next = top;
top = newNode;
}

char pop() {
if (isEmpty()) {
printf("Stack Underflow\n");
return '\0';
}
struct Node* temp = top;
char data = top->data;
top = top->next;
free(temp);
return data;
}

int isEmpty() {
return top == NULL;
}
codes:

output:
7. Write a program to check for balanced parenthesis in a given expression.

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

#define MAX 100

// Stack structure
typedef struct {
char arr[MAX];
int top;
} Stack;

// Stack operations
void push(Stack* stack, char element) {
if (stack->top == MAX - 1) {
printf("Stack overflow\n");
exit(1);
}
stack->arr[++stack->top] = element;
}

char pop(Stack* stack) {


if (stack->top == -1) {
printf("Stack underflow\n");
exit(1);
}
return stack->arr[stack->top--];
}

char peek(Stack* stack) {


return stack->arr[stack->top];
}

int isEmpty(Stack* stack) {


return stack->top == -1;
}

// Function to check if a pair of brackets is matching


int isMatchingPair(char open, char close) {
return (open == '(' && close == ')') ||
(open == '{' && close == '}') ||
(open == '[' && close == ']');
}

// Function to check for balanced parentheses


int isBalanced(char* expression) {
Stack stack;
stack.top = -1;

for (int i = 0; expression[i] != '\0'; i++) {


char ch = expression[i];

// Push opening brackets onto the stack


if (ch == '(' || ch == '{' || ch == '[') {
push(&stack, ch);
}
// Pop and check for matching closing brackets
else if (ch == ')' || ch == '}' || ch == ']') {
if (isEmpty(&stack) || !isMatchingPair(pop(&stack), ch)) {
return 0; // Not balanced
}
}
}

// If stack is empty, the parentheses are balanced


return isEmpty(&stack);
}

int main() {
char expression[MAX];

printf("Enter an expression: ");


scanf("%s", expression);

if (isBalanced(expression)) {
printf("The parentheses are balanced.\n");
} else {
printf("The parentheses are not balanced.\n");
}

return 0;
}
codes:

output:
8. Write a program to convert infix expression to prefix and postfix
expression.

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

#define MAX 100

// Stack structure for operators and operands


typedef struct {
char arr[MAX];
int top;
} Stack;

// Function prototypes
void push(Stack* stack, char element);
char pop(Stack* stack);
char peek(Stack* stack);
int isEmpty(Stack* stack);
int precedence(char operator);
int isOperator(char ch);
void reverseString(char* str);
void infixToPostfix(char* infix, char* postfix);
void infixToPrefix(char* infix, char* prefix);

int main() {
char infix[MAX], postfix[MAX], prefix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);

infixToPostfix(infix, postfix);
printf("Postfix Expression: %s\n", postfix);

infixToPrefix(infix, prefix);
printf("Prefix Expression: %s\n", prefix);

return 0;
}

// Push an element onto the stack


void push(Stack* stack, char element) {
if (stack->top == MAX - 1) {
printf("Stack overflow\n");
exit(1);
}
stack->arr[++stack->top] = element;
}

// Pop an element from the stack


char pop(Stack* stack) {
if (stack->top == -1) {
printf("Stack underflow\n");
exit(1);
}
return stack->arr[stack->top--];
}

// Peek the top element of the stack


char peek(Stack* stack) {
return stack->arr[stack->top];
}

// Check if the stack is empty


int isEmpty(Stack* stack) {
return stack->top == -1;
}

// Get precedence of operators


int precedence(char operator) {
if (operator == '+' || operator == '-') return 1;
if (operator == '*' || operator == '/') return 2;
if (operator == '^') return 3;
return 0;
}

// Check if a character is an operator


int isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}

// Reverse a string
void reverseString(char* str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}

// Convert infix to postfix


void infixToPostfix(char* infix, char* postfix) {
Stack stack;
stack.top = -1;
int i = 0, j = 0;

while (infix[i]) {
if (isalnum(infix[i])) {
postfix[j++] = infix[i];
} else if (infix[i] == '(') {
push(&stack, infix[i]);
} else if (infix[i] == ')') {
while (!isEmpty(&stack) && peek(&stack) != '(') {
postfix[j++] = pop(&stack);
}
pop(&stack); // Remove '('
} else if (isOperator(infix[i])) {
while (!isEmpty(&stack) && precedence(peek(&stack)) >=
precedence(infix[i])) {
postfix[j++] = pop(&stack);
}
push(&stack, infix[i]);
}
i++;
}

while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
}

postfix[j] = '\0';
}

// Convert infix to prefix


void infixToPrefix(char* infix, char* prefix) {
char reversedInfix[MAX], reversedPrefix[MAX];
int len = strlen(infix);

// Reverse infix expression and replace '(' with ')' and vice versa
for (int i = 0; i < len; i++) {
if (infix[len - i - 1] == '(') {
reversedInfix[i] = ')';
} else if (infix[len - i - 1] == ')') {
reversedInfix[i] = '(';
} else {
reversedInfix[i] = infix[len - i - 1];
}
}
reversedInfix[len] = '\0';

// Convert the reversed infix to postfix


infixToPostfix(reversedInfix, reversedPrefix);
// Reverse the postfix to get the prefix
reverseString(reversedPrefix);
strcpy(prefix, reversedPrefix);
}
codes:
output:
9. Write a program to implement Linear Queue using Array and Linked Lists.
Using Array
#include <stdio.h>
#include <stdlib.h>

#define MAX 100

struct Queue {
int arr[MAX];
int front;
int rear;
};

void initQueue(struct Queue* queue) {


queue->front = -1;
queue->rear = -1;
}

int isFull(struct Queue* queue) {


return (queue->rear + 1) % MAX == queue->front;
}

int isEmpty(struct Queue* queue) {


return queue->front == -1;
}

void enqueue(struct Queue* queue, int data) {


if (isFull(queue)) {
printf("Queue Overflow\n");
return;
}
if (isEmpty(queue)) {
queue->front = 0;
}
queue->rear = (queue->rear + 1) % MAX;
queue->arr[queue->rear] = data;
}

int dequeue(struct Queue* queue) {


if (isEmpty(queue)) {
printf("Queue Underflow\n");
return -1;
}
int data = queue->arr[queue->front];
if (queue->front == queue->rear) {
queue->front = queue->rear = -1; // Queue is now empty
} else {
queue->front = (queue->front + 1) % MAX;
}
return data;
}

int front(struct Queue* queue) {


if (isEmpty(queue)) {
printf("Queue is empty\n");
return -1;
}
return queue->arr[queue->front];
}

void display(struct Queue* queue) {


if (isEmpty(queue)) {
printf("Queue is empty\n");
return;
}
printf("Queue elements: ");
int i = queue->front;
while (1) {
printf("%d ", queue->arr[i]);
if (i == queue->rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}

int main() {
struct Queue queue;
initQueue(&queue);

int n, data;
printf("Enter the number of elements to enqueue: ");
scanf("%d", &n);

for (int i = 0; i < n; i++) {


printf("Enter element %d: ", i + 1);
scanf("%d", &data);
enqueue(&queue, data);
}

display(&queue);
printf("Front element: %d\n", front(&queue));
printf("Dequeued: %d\n", dequeue(&queue));
display(&queue);

return 0;
}
codes:

output:
Using Linked List
#include <stdio.h>
#include <stdlib.h>

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

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

void initQueue(struct Queue* queue) {


queue->front = NULL;
queue->rear = NULL;
}

int isEmpty(struct Queue* queue) {


return queue->front == NULL;
}

void enqueue(struct Queue* queue, int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = data;
newNode->next = NULL;

if (isEmpty(queue)) {
queue->front = queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
}

int dequeue(struct Queue* queue) {


if (isEmpty(queue)) {
printf("Queue Underflow\n");
return -1;
}
struct Node* temp = queue->front;
int data = temp->data;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL; // Queue is now empty
}
free(temp);
return data;
}

int front(struct Queue* queue) {


if (isEmpty(queue)) {
printf("Queue is empty\n");
return -1;
}
return queue->front->data;
}

void display(struct Queue* queue) {


if (isEmpty(queue)) {
printf("Queue is empty\n");
return;
}
struct Node* temp = queue->front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Queue queue;
initQueue(&queue);

int n, data;
printf("Enter the number of elements to enqueue: ");
scanf("%d", &n);

for (int i = 0; i < n; i++) {


printf("Enter element %d: ", i + 1);
scanf("%d", &data);
enqueue(&queue, data);
}

display(&queue);
printf("Front element: %d\n", front(&queue));
printf("Dequeued: %d\n", dequeue(&queue));
display(&queue);

return 0;
}
codes:

output:
10. Write a program to implement Circular Queue using Array and Linked Lists.

Circular Queue using Array

#include <stdio.h>
#include <stdlib.h>
#define MAX 100

typedef struct {
int arr[MAX];
int front, rear;
} CircularQueue;

// Initialize the queue


void initQueue(CircularQueue* cq) {
cq->front = -1;
cq->rear = -1;
}

// Check if the queue is empty


int isEmpty(CircularQueue* cq) {
return cq->front == -1;
}

// Check if the queue is full


int isFull(CircularQueue* cq) {
return (cq->rear + 1) % MAX == cq->front;
}

// Enqueue operation
void enqueue(CircularQueue* cq, int value) {
if (isFull(cq)) {
printf("Queue is full!\n");
return;
}
if (isEmpty(cq)) {
cq->front = cq->rear = 0;
} else {
cq->rear = (cq->rear + 1) % MAX;
}
cq->arr[cq->rear] = value;
printf("Inserted %d\n", value);
}

// Dequeue operation
int dequeue(CircularQueue* cq) {
if (isEmpty(cq)) {
printf("Queue is empty!\n");
return -1;
}
int value = cq->arr[cq->front];
if (cq->front == cq->rear) {
cq->front = cq->rear = -1; // Queue is now empty
} else {
cq->front = (cq->front + 1) % MAX;
}
return value;
}

// Display the queue


void display(CircularQueue* cq) {
if (isEmpty(cq)) {
printf("Queue is empty!\n");
return;
}
printf("Queue elements: ");
int i = cq->front;
while (1) {
printf("%d ", cq->arr[i]);
if (i == cq->rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}

int main() {
CircularQueue cq;
initQueue(&cq);

enqueue(&cq, 10);
enqueue(&cq, 20);
enqueue(&cq, 30);
display(&cq);

printf("Dequeued: %d\n", dequeue(&cq));


display(&cq);

enqueue(&cq, 40);
display(&cq);

return 0;
}
codes:

output:
Circular Queue using linked list

#include <stdio.h>

#include <stdlib.h>

// Node structure

typedef struct Node {

int data;

struct Node* next;

} Node;

// Circular Queue structure

typedef struct {

Node* front;

Node* rear;

} CircularQueue;

// Initialize the queue

void initQueue(CircularQueue* cq) {

cq->front = NULL;

cq->rear = NULL;

}
// Check if the queue is empty

int isEmpty(CircularQueue* cq) {

return cq->front == NULL;

// Enqueue operation

void enqueue(CircularQueue* cq, int value) {

Node* newNode = (Node*)malloc(sizeof(Node));

if (!newNode) {

printf("Memory allocation failed!\n");

return;

newNode->data = value;

if (isEmpty(cq)) {

cq->front = cq->rear = newNode;

newNode->next = newNode; // Point to itself

} else {

newNode->next = cq->front;

cq->rear->next = newNode;

cq->rear = newNode;
}

printf("Inserted %d\n", value);

// Dequeue operation

int dequeue(CircularQueue* cq) {

if (isEmpty(cq)) {

printf("Queue is empty!\n");

return -1;

Node* temp = cq->front;

int value = temp->data;

if (cq->front == cq->rear) {

cq->front = cq->rear = NULL; // Queue is now empty

} else {

cq->front = cq->front->next;

cq->rear->next = cq->front;

free(temp);
return value;

// Display the queue

void display(CircularQueue* cq) {

if (isEmpty(cq)) {

printf("Queue is empty!\n");

return;

printf("Queue elements: ");

Node* current = cq->front;

do {

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

current = current->next;

} while (current != cq->front);

printf("\n");

int main() {

CircularQueue cq;

initQueue(&cq);
enqueue(&cq, 10);

enqueue(&cq, 20);

enqueue(&cq, 30);

display(&cq);

printf("Dequeued: %d\n", dequeue(&cq));

display(&cq);

enqueue(&cq, 40);

display(&cq);

return 0;

}
codes:

output:
11.. Write a program to implement Doubly Ended Queue using Array and linked list

Array
#include <stdio.h>
#define MAX 5 // Maximum size of the deque

// Global variables for the deque


int deque[MAX];
int front = -1, rear = -1;

// Check if the deque is empty


int isEmpty() {
return front == -1;
}

// Check if the deque is full


int isFull() {
return (rear + 1) % MAX == front;
}

// Insert an element at the front


void insertFront(int value) {
if (isFull()) {
printf("Deque Overflow! Cannot insert %d at the front.\n", value);
return;
}
if (isEmpty()) {
front = rear = 0;
} else {
front = (front - 1 + MAX) % MAX;
}
deque[front] = value;
printf("%d inserted at the front.\n", value);
}

// Insert an element at the rear


void insertRear(int value) {
if (isFull()) {
printf("Deque Overflow! Cannot insert %d at the rear.\n", value);
return;
}
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX;
}
deque[rear] = value;
printf("%d inserted at the rear.\n", value);
}

// Delete an element from the front


int deleteFront() {
if (isEmpty()) {
printf("Deque Underflow! Cannot delete from the front.\n");
return -1;
}
int value = deque[front];
if (front == rear) {
front = rear = -1; // Reset the deque
} else {
front = (front + 1) % MAX;
}
return value;
}

// Delete an element from the rear


int deleteRear() {
if (isEmpty()) {
printf("Deque Underflow! Cannot delete from the rear.\n");
return -1;
}
int value = deque[rear];
if (front == rear) {
front = rear = -1; // Reset the deque
} else {
rear = (rear - 1 + MAX) % MAX;
}
return value;
}

// Peek the front element


int getFront() {
if (isEmpty()) {
printf("Deque is empty! Cannot get front.\n");
return -1;
}
return deque[front];
}

// Peek the rear element


int getRear() {
if (isEmpty()) {
printf("Deque is empty! Cannot get rear.\n");
return -1;
}
return deque[rear];
}

// Display all elements in the deque


void display() {
if (isEmpty()) {
printf("Deque is empty!\n");
return;
}
printf("Deque elements: ");
int i = front;
while (1) {
printf("%d ", deque[i]);
if (i == rear) {
break;
}
i = (i + 1) % MAX;
}
printf("\n");
}

// Main function to demonstrate deque operations


int main() {
int choice, value;

do {
printf("\n--- Double-Ended Queue Operations ---\n");
printf("1. Insert Front\n");
printf("2. Insert Rear\n");
printf("3. Delete Front\n");
printf("4. Delete Rear\n");
printf("5. Get Front\n");
printf("6. Get Rear\n");
printf("7. Display\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(value);
break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(value);
break;
case 3:
value = deleteFront();
if (value != -1) {
printf("Deleted element from front: %d\n", value);
}
break;
case 4:
value = deleteRear();
if (value != -1) {
printf("Deleted element from rear: %d\n", value);
}
break;
case 5:
value = getFront();
if (value != -1) {
printf("Front element: %d\n", value);
}
break;
case 6:
value = getRear();
if (value != -1) {
printf("Rear element: %d\n", value);
}
break;
case 7:
display();
break;
case 8:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 8);

return 0;
}

code:
output:
Linked Lists.

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

// Define a node for the deque


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

// Define the deque structure


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

// Initialize the deque


void initDeque(Deque* dq) {
dq->front = NULL;
dq->rear = NULL;
}

// Check if the deque is empty


int isEmpty(Deque* dq) {
return dq->front == NULL;
}

// Insert an element at the front


void insertFront(Deque* dq, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed!\n");
return;
}
newNode->data = value;
newNode->prev = NULL;
newNode->next = dq->front;

if (isEmpty(dq)) {
dq->front = dq->rear = newNode;
} else {
dq->front->prev = newNode;
dq->front = newNode;
}
printf("Inserted %d at the front.\n", value);
}

// Insert an element at the rear


void insertRear(Deque* dq, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed!\n");
return;
}
newNode->data = value;
newNode->next = NULL;
newNode->prev = dq->rear;

if (isEmpty(dq)) {
dq->front = dq->rear = newNode;
} else {
dq->rear->next = newNode;
dq->rear = newNode;
}
printf("Inserted %d at the rear.\n", value);
}
// Delete an element from the front
void deleteFront(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return;
}
Node* temp = dq->front;
printf("Deleted %d from the front.\n", temp->data);

dq->front = dq->front->next;
if (dq->front) {
dq->front->prev = NULL;
} else {
dq->rear = NULL;
}
free(temp);
}

// Delete an element from the rear


void deleteRear(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return;
}
Node* temp = dq->rear;
printf("Deleted %d from the rear.\n", temp->data);

dq->rear = dq->rear->prev;
if (dq->rear) {
dq->rear->next = NULL;
} else {
dq->front = NULL;
}
free(temp);
}

// Display the elements of the deque


void display(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return;
}
printf("Deque elements: ");
Node* current = dq->front;
while (current) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

// Main function with menu


int main() {
Deque dq;
initDeque(&dq);

int choice, value;

do {
printf("\nMenu:\n");
printf("1. Insert at Front\n");
printf("2. Insert at Rear\n");
printf("3. Delete from Front\n");
printf("4. Delete from Rear\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(&dq, value);
break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(&dq, value);
break;
case 3:
deleteFront(&dq);
break;
case 4:
deleteRear(&dq);
break;
case 5:
display(&dq);
break;
case 6:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 6);

return 0;
codes:
outputs:

You might also like