DS LAb Manual
DS LAb Manual
#include <stdio.h>
int i, j, temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
int main()
{
int arr[100], i, n, step, temp;
scanf("%d", &n);
scanf("%d", &arr[i]);
bubbleSort(arr, n);
return 0;
4. Write a program to insert the elements {61,16,8,27} into singly linked list and delete 8,61,27 from
the list. Display your list after each insertion and deletion.
include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
int data;
}*start=NULL,*q,*t;
int main()
{
int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_pos();
while(1)
printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
scanf("%d",&ch);
switch(ch)
case 1:
scanf("%d",&ch);
switch(ch)
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: exit(0);
break;
case 2: display();
break;
scanf("%d",&ch);
switch(ch)
case 1: delete_beg();
break;
case 2: delete_end();
break;
case 3: delete_pos();
break;
case 4: exit(0);
break;
case 4: exit(0);
}
return 0;
void insert_beg()
int num;
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;
start=t;
else
t->next=start;
start=t;
void insert_end()
int num;
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;
if(start==NULL) //If list is empty
start=t;
else
q=start;
while(q->next!=NULL)
q=q->next;
q->next=t;
int insert_pos()
int pos,i,num;
if(start==NULL)
printf("List is empty!!");
return 0;
printf("Enter data:");
scanf("%d",&num);
scanf("%d",&pos);
t->data=num;
q=start;
for(i=1;i<pos-1;i++)
if(q->next==NULL)
return 0;
q=q->next;
t->next=q->next;
q->next=t;
return 0;
void display()
if(start==NULL)
printf("List is empty!!");
else
q=start;
while(q!=NULL)
printf("%d->",q->data);
q=q->next;
}
}
void delete_beg()
if(start==NULL)
else
q=start;
start=start->next;
free(q);
void delete_end()
if(start==NULL)
else
q=start;
while(q->next->next!=NULL)
q=q->next;
t=q->next;
q->next=NULL;
free(t);
int delete_pos()
int pos,i;
if(start==NULL)
printf("List is empty!!");
return 0;
scanf("%d",&pos);
q=start;
for(i=1;i<pos-1;i++)
if(q->next==NULL)
return 0;
q=q->next;
t=q->next;
q->next=t->next;
free(t);
return 0;
5 . Write a program to insert the elements {61,16,8,27} into linear queue and delete three elements
from the list. Display your list after each insertion and deletion.
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
int choice;
while (1)
printf("4.Quit n");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
void insert()
int item;
if(rear == MAX - 1)
else
if(front== - 1)
front = 0;
scanf("%d", &item);
rear = rear + 1;
queue_array[rear] = item;
}
void delete()
return;
else
front = front + 1;
void display()
int i;
if(front == - 1)
else
printf("Queue is : n");
printf("n");
6 . Write a program to insert the elements {61,16,8,27} into circular queue and delete 4 elements
from the list. Display your list after each insertion and deletion.
#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 ;
}
printf("Element deleted from queue is : %dn",cqueue_arr[front]);
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("%d ",cqueue_arr[front_pos]);
front_pos++;
else
{
while(front_pos <= MAX-1)
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;
*/
#include<stdio.h>
struct poly
int coeff;
int expo;
};
/* function prototypes */
int addPoly(struct poly [],struct poly [],int ,int ,struct poly []);
int main()
int t1,t2,t3;
t1=readPoly(p1);
displayPoly(p1,t1);
t2=readPoly(p2);
displayPoly(p2,t2);
t3=addPoly(p1,p2,t1,t2,p3);
displayPoly(p3,t3);
printf("\n");
return 0;
}
int readPoly(struct poly p[10])
int t1,i;
scanf("%d",&t1);
for(i=0;i<t1;i++)
scanf("%d",&p[i].coeff);
return(t1);
int addPoly(struct poly p1[10],struct poly p2[10],int t1,int t2,struct poly p3[10])
int i,j,k;
i=0;
j=0;
k=0;
if(p1[i].expo==p2[j].expo)
{
p3[k].coeff=p1[i].coeff + p2[j].coeff;
p3[k].expo=p1[i].expo;
i++;
j++;
k++;
else if(p1[i].expo>p2[j].expo)
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
else
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
while(i<t1)
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
/* for rest over terms of polynomial 2 */
while(j<t2)
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
int k;
for(k=0;k<term-1;k++)
printf("%d(x^%d)+",p[k].coeff,p[k].expo);
printf("%d(x^%d)",p[term-1].coeff,p[term-1].expo);
9. Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack, also display the
popped numbers.
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
int top=-1,stack[MAX];
void push();
void pop();
void display();
void main()
int ch;
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
scanf("%d",&ch);
switch(ch)
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
void push()
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
else
scanf("%d",&val);
top=top+1;
stack[top]=val;
void pop()
if(top==-1)
printf("\nStack is empty!!");
else
top=top-1;
void display()
int i;
if(top==-1)
printf("\nStack is empty!!");
}
else
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
#include <stdio.h>
int main() {
return 0;
if (n2 != 0)
else
return n1;
12. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix expression
#include<stdio.h>
char stack[SIZE];
printf("\nStack Overflow.");
else
top = top+1;
stack[top] = item;
char pop()
char item ;
if(top <0)
{
getchar();
exit(1);
else
item = stack[top];
top = top-1;
return(item);
/* define function that is used to determine whether any symbol is operator or not
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
return 1;
else
return 0;
}
/* define fucntion that is used to assign precendence to operator.
return(3);
return(2);
return(1);
else
return(0);
int i, j;
char item;
char x;
push('('); /* push '(' onto stack */
i=0;
j=0;
if(item == '(')
push(item);
j++;
x=pop();
j++;
push(x);
postfix_exp[j] = x;
j++;
x = pop();
else
{ /* if current symbol is neither operand not '(' nor ')' and nor
operator */
getchar();
exit(1);
i++;
if(top>0)
getchar();
exit(1);
}
if(top>0)
getchar();
exit(1);
int main()
* in parentheses ( )
* in algorithm
* */
printf("ASSUMPTION: The infix expression contains single letter variables and single digit
constants only.\n");
gets(infix);
return 0;
#include<stdio.h>
int stack[20];
void push(int x)
stack[++top] = x;
int pop()
return stack[top--];
int main()
char exp[20];
char *e;
int n1,n2,n3,num;
scanf("%s",exp);
e = exp;
while(*e != '\0')
if(isdigit(*e))
num = *e - 48;
push(num);
else
n1 = pop();
n2 = pop();
switch(*e)
case '+':
n3 = n1 + n2;
break;
case '-':
n3 = n2 - n1;
break;
case '*':
n3 = n1 * n2;
break;
case '/':
n3 = n2 / n1;
break;
push(n3);
e++;
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
};
// Inorder traversal
inorderTraversal(root->left);
inorderTraversal(root->right);
// preorderTraversal traversal
preorderTraversal(root->left);
preorderTraversal(root->right);
// postorderTraversal traversal
postorderTraversal(root->left);
postorderTraversal(root->right);
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
root->left = createNode(value);
return root->left;
root->right = createNode(value);
return root->right;
int main() {
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
inorderTraversal(root);
preorderTraversal(root);
postorderTraversal(root);
16 . Write a program to Sort the following elements using heap sort {9.16,32,8,4,1,5,8,0}
// Heap Sort in C
#include <stdio.h>
*b = temp;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
heapify(arr, n, i);
// Heap sort
heapify(arr, i, 0);
// Print an array
printf("\n");
// Driver code
int main() {
heapSort(arr, n);
printArray(arr, n);