2nd sem ds lab
2nd sem ds lab
#include<stdio.h>
int main() {
int i = 0;
gets(str);
pt = str;
i++;
pt++;
return 0;
Output:
Length of String : 18
2. Write a Program to Calculate the length of the string using a pointer.
#include <stdio.h>
int main() {
char str[100];
gets(str);
int length = 0;
length++;
ptr++;
return 0;
Output:
#include<stdio.h>
int a, b;
swap(&a, &b);
return 0;
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Output:
30
20
#include<stdio.h>
#include<string.h>
//Declaring generatePermutation()
int main()
int n =strlen(str);
generatePermutation(str,0,n);
char temp;
int i,j;
temp = str[i];
str[i] = str[j];
str[j] = temp;
temp = str[i];
str[i] = str[j];
str[j] = temp;
printf("%s\n",str);
Output:
ABC
ACB
BAC
BCA
CBA
CAB
5. Write a Program to store n students information using structure.
#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s;
int main() {
printf("Enter information:\n");
scanf("%d", &s.roll);
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
return 0;
Output:
Enter information:
Enter name: Jack
Displaying Information:
Name: Jack
Roll number: 23
Marks: 34.5
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
void push();
void pop();
void show();
int main()
int choice;
while (1)
scanf("%d", &choice);
switch (choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice!!");
void push()
int x;
if (top == SIZE - 1)
printf("\nOverflow!!");
}
else
scanf("%d", &x);
top = top + 1;
inp_array[top] = x;
void pop()
if (top == -1)
printf("\nUnderflow!!");
else
top = top - 1;
void show()
if (top == -1)
printf("\nUnderflow!!");
}
else
printf("%d\n", inp_array[i]);
Output:
Top element is : 20
...........................................................
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
return 0;
}
int main()
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
push(*e);
else
printf("%c ",pop());
push(*e);
}
e++;
while(top != -1)
printf("%c ",pop());
}return 0;
Output:
abc*+
ab+c*da-+
48+65-32-22+/
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<stdlib.h>
#define MAX 100
char stack[MAX];
int isFull ()
int isEmpty ()
if (isFull ())
return;
top++;
stack[top] = item;
int pop ()
if (isEmpty ())
return INT_MIN;
// decrements top and returns what has been popped
return stack[top--];
int peek ()
if (isEmpty ())
return INT_MIN;
return stack[top];
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
switch (ch)
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
return -1;
int i, j;
if (checkIfOperand (expression[i]))
expression[++j] = expression[i];
push (expression[i]);
else
pop (stack);
}
else // if an opertor
push (expression[i]);
expression[++j] = '\0';
int j = size, i = 0;
char temp[size];
temp[j--] = '\0';
temp[j] = exp[i];
j--;
i++;
int i = 0;
if (exp[i] == '(')
exp[i] = ')';
exp[i] = '(';
i++;
// reverse string
reverse (exp);
//change brackets
brackets (exp);
//get postfix
getPostfix (exp);
// reverse string again
reverse (exp);
int main ()
InfixtoPrefix (expression);
return 0;
Output:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define BLANK ' '
#define MAX 50
char *pop();
char prefix[MAX];
char stack[MAX][MAX];
int isempty();
void prefix_to_postfix();
int top;
int main()
top = -1;
gets(prefix);
prefix_to_postfix();
}/*End of main()*/
void prefix_to_postfix()
int i;
char operand1[MAX], operand2[MAX];
char symbol;
char temp[2];
char strin[MAX];
for(i=strlen(prefix)-1;i>=0;i--)
symbol=prefix[i];
temp[0]=symbol;
temp[1]='\0';
if(!white_space(symbol))
switch(symbol)
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
strcpy(operand1,pop());
strcpy(operand2,pop());
strcpy(strin,operand1);
strcat(strin,operand2);
strcat(strin,temp);
push(strin);
break;
push(temp);
puts(stack[0]);
}/*End of prefix_to_postfix()*/
printf("\nStack overflow\n");
exit(1);
else
top=top+1;
}/*End of push()*/
char *pop()
if(top == -1 )
exit(2);
else
return (stack[top--]);
}/*End of pop()*/
int isempty()
if(top==-1)
return 1;
else
return 0;
return 1;
else
return 0;
}/*End of white_space()*/
Output:
Process returned 0
Postfix Expression :: 2
Process returned 0
10. Write Program to perform the operation Insert, Delete and Display on Queue.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void insertion()
printf(“\nQueue Overflow\n”);
}
else if (rear==0)
front = rear = 1;
else if(rear==n)
rear=1;
else
rear=rear+1;
scanf(“%d”,&item);
q[rear] = item;
printf(“%d is inserted\n\n”,item);
void deletion()
if(front==0)
printf(“\nQueue Underflow\n\n”);
item=q[front];
if(front==rear)
front=0;
rear=0;
else if (front=n)
front=1;
else
front=front+1;
printf(“\n%d is deleted\n\n”,item);
void show()
for(int i=0;i<=rear;i++)
printf(“%d\t”,q[i]);
}
int main()
int op;
scanf(“%d”,&n);
do
printf(“\n1 : Insert”);
printf(“\n2 : Delete”);
printf(“\n3 : Print”);
printf(“\n4 : Exit”);
scanf(“%d”,&op);
switch(op)
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
show();
break;
//default:
}while(op!=4);
printf(“\n—THE END—\n”);
Output:
11. Write Program to implement Circular queue.
#include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
return;
if(front == -1)
front = 0;
rear = 0;
else
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
cqueue_arr[rear] = item ;
void deletion()
if(front == -1)
{
printf("Queue Underflown");
return ;
if(front == rear)
front = -1;
rear=-1;
else
if(front == MAX-1)
front = 0;
else
front = front+1;
void display()
if(front == -1)
printf("Queue is emptyn");
return;
}
printf("Queue elements :n");
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
else
printf("%d ",cqueue_arr[front_pos])
front_pos++;
front_pos = 0;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
printf("n");
int main()
{
int choice,item;
do
printf("1.Insertn");
printf("2.Deleten");
printf("3.Displayn");
printf("4.Quitn");
scanf("%d",&choice);
switch(choice)
case 1 :
scanf("%d", &item);
insert(item);
break;
case 2 :
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choicen");
}while(choice!=4);
return 0;
Output:
12. Write Program to implement Double ended queue.
#include <stdio.h>
#include <stdlib.h>
int deque[MAX];
int isFull() {
int isEmpty() {
if (isFull()) {
return;
front = 0;
rear = 0;
} else if (front == 0) {
} else {
front = front - 1;
deque[front] = key;
if (isFull()) {
return;
front = 0;
rear = 0;
} else {
rear = rear + 1;
deque[rear] = key;
void deleteFront() {
if (isEmpty()) {
return;
front = -1;
rear = -1;
} else {
front = front + 1;
void deleteRear() {
if (isEmpty()) {
return;
front = -1;
rear = -1;
} else if (rear == 0) {
} else {
rear = rear - 1;
}
printf("Deleted %d from the rear.\n", removed);
void displayDeque() {
if (isEmpty()) {
printf("Deque is empty.\n");
return;
int i = front;
while (1) {
if (i == rear)
break;
i = (i + 1) % MAX;
printf("\n");
int main() {
insertRear(5);
displayDeque();
insertFront(15);
displayDeque();
insertRear(25);
displayDeque();
deleteFront();
displayDeque();
deleteRear();
displayDeque();
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int heap[MAX];
int size;
};
pq->size = 0;
*a = *b;
*b = temp;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
if (largest != i) {
swap(&pq->heap[i], &pq->heap[largest]);
heapify(pq, largest);
if (pq->size == MAX) {
return;
pq->heap[pq->size] = value;
pq->size++;
current = (current - 1) / 2;
if (pq->size == 0) {
return -1;
pq->size--;
heapify(pq, 0);
return highest;
if (pq->size == 0) {
return;
printf("\n");
int main() {
init(&pq);
insert(&pq, 10);
insert(&pq, 20);
insert(&pq, 15);
insert(&pq, 30);
insert(&pq, 25);
display(&pq);
display(&pq);
display(&pq);
insert(&pq, 40);
insert(&pq, 5);
display(&pq);
return 0;
Output:
Priority Queue: 30 25 15 10 20
Priority Queue: 25 20 15 10
Priority Queue: 20 10 15
Priority Queue: 40 20 15 10 5
if (arr[i] == target) {
int main() {
int target = 8;
if (result != -1)
else
return 0;
}
Output:
15. Write a Program to sort given Array using Insertion sort technique.
#include <stdio.h>
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
printf("\n");
int main() {
display(arr, size);
insertionSort(arr, size);
display(arr, size);
return 0;
Output:
Original Array: 12 11 13 5 6
Sorted Array: 5 6 11 12 13
16. Write a Program to sort given Array using Bubble sort technique.
#include <stdio.h>
arr[j + 1] = temp;
}
void display(int arr[], int size) {
printf("\n");
int main() {
display(arr, size);
bubbleSort(arr, size);
display(arr, size);
return 0;
Output:
Original Array: 64 34 25 12 22 11 90
Sorted Array: 11 12 22 25 34 64 90
17. Write a Program to sort given Array using Quick sort technique.
#include <stdio.h>
*b = temp;
i++;
swap(&arr[i], &arr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
}
}
printf("\n");
int main() {
display(arr, size);
display(arr, size);
return 0;
Output:
Original Array: 10 7 8 9 1 5
Sorted Array: 1 5 7 8 9 10
18. Write a Program to sort given Array using selection sort technique.
#include <stdio.h>
int minIdx = i;
minIdx = j;
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
printf("\n");
int main() {
display(arr, size);
selectionSort(arr, size);
display(arr, size);
return 0;
Output:
Original Array: 64 25 12 22 11
Sorted Array: 11 12 22 25 64
#include <stdio.h>
#include <stdlib.h>
// Define a node of the linked list
struct Node {
int data;
};
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
temp = temp->next;
printf("NULL\n");
int main() {
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
display(head);
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
};
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
printf("NULL\n");
int main() {
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
display(head);
return 0;
Output: