Data Structure Term Work
Data Structure Term Work
Data Structure Term Work
Write a C Program to store N elements to an array and then send all positive elements
of the array to the end without altering the original sequence.
Code:
#include<stdio.h>
int sort(int a[],int n)
{
int x=0,z=0;
int b[10],c[10];
for(int i=0;i<n;i++)
{
if(a[i]<0)
{
b[x]=a[i];
x++;
}
else
{
c[z]=a[i];
z++;
}
}
for (int i=0;i<x;i++)
{
a[i]=b[i];
}
int temp=0;
for (int i=x;i<x+z;i++)
{
a[i]=c[temp];
temp++;
}
return *a;
}
void main()
{
int n;
printf("enter the number");
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
printf("enter %d element",i+1);
scanf("%d",&a[i]);
}
int *p=a;
*p=sort(a,n);
for(int i=0;i<n;i++)
{
printf("%d ",*(p+i));
}
}
Output:
Q2. Write a C program to Insert and Delete elements form a Queue using link list ,each
node should have the following information about a product
1. Product_Id(char)
2. Product_Name(string)
3. Total_sale(integer)
4. Product_Grade(Char)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Output:
Q3. Write a C program to find union (of two linked lists) based on their information field that implements
singly linked list (with information field Emp_Id and Name of employee for each node ).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char Emp_Id;
char Emp_Name[20];
} nodetype;
nodetype *p = NULL;
char X, Y[30];
p = (nodetype *)malloc(sizeof(nodetype));
if (p != NULL)
scanf("%s", &Y);
p->Emp_Id = X;
strcpy(p->Emp_Name, Y);
p->next = L;
L = p;
return L;
{
if (L1->Emp_Id == L2->Emp_Id)
return 0;
L1 = L1->next;
return 1;
nodetype *t = L1;
while (t != NULL)
t = t->next;
if (isPresent(L1, L2))
L2 = L2->next;
void main()
int ch;
do
scanf("%d", &ch);
switch (ch)
{
case 1:
L1 = insert(L1);
break;
case 2:
L2 = insert(L2);
break;
case 3:
else
unionNodes(L1, L2);
break;
Output:
Q4. Write a C program to evaluate any given postfix expression. For example ( 3,4,5,*,+) :Answer = 23
Code:
#include<stdio.h>
int main()
{ int stack[20];
char exp[20];
char *e;
int n1,n2,n3,num;
scanf("%s",exp);
e = exp;
void push(int x)
stack[++top] = x;
int pop()
return stack[top--];
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;
Output:
Q5. Write a C program to create a linked list P, then write a ‘C’ function named split to create two linked
lists Q & R from P So that Q contains all elements in odd positions of P and R contains the remaining
elements. Finally print both linked lists i.e. Q and R.
Code:
#include <stdio.h>
#include <stdlib.h>
int info;
} nodetype;
nodetype *p = NULL;
int X;
p = (nodetype *)malloc(sizeof(nodetype));
if (p != NULL)
scanf("%d", &X);
p->info = X;
p->next = L;
L = p;
return L;
while (L != NULL)
t->info = L->info;
if (t->info < 0)
t->next = (*P);
(*P) = t;
t->next = (*N);
(*N) = t;
L = L->next;
while (P != NULL)
printf("%d\t", P->info);
P = P->next;
while (N != NULL)
printf("%d\t", N->info);
N = N->next;
void main()
int ch;
do
scanf("%d", &ch);
switch (ch)
case 1:
L = insert(L);
break;
case 2:
if (L == NULL)
printf("empty");
else
break;
case 3:
printf("empty");
else
display(positive, negative);
break;
Output:
Q6. Write a C program to create a linked list P, then write a ‘C’ function named split to create two linked
lists Q & R from P So that Q contains all elements in odd positions of P and R contains the remaining
elements. Finally print both linked lists i.e. Q and R.
Code:
#include <stdio.h>
#include <stdlib.h>
int info;
} nodetype;
nodetype *t = NULL;
int X;
t = (nodetype *)malloc(sizeof(nodetype));
if (t != NULL)
scanf("%d", &X);
t->info = X;
t->next = P;
P = t;
return P;
while (P != NULL)
t->info = P->info;
if (t->info % 2 != 0)
t->next = (*Q);
(*Q) = t;
else if (t->info % 2 == 0)
t->next = (*R);
(*R) = t;
P = P->next;
while (Q != NULL)
printf("%d\t", Q->info);
Q = Q->next;
while (R != NULL)
printf("%d\t", R->info);
R = R->next;
void main()
int ch;
do
scanf("%d", &ch);
switch (ch)
{
case 1:
P = insert(P);
break;
case 2:
if (P == NULL)
printf("empty");
else
break;
case 3:
printf("empty");
else
display(Q, R);
break;
OUTPUT
Q7. Write a C program to insert a node in a circular doubly link list, from left to right so that nodes
of that circular doubly link list will be in ascending order.
Code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int ch;
nodetype *L = NULL, *R = NULL;
do
{
printf("\nEnter 1 to insert\nEnter 2 to display\nEnter 3 for exit\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert(&L, &R);
break;
case 2:
if (L == NULL)
printf("Empty");
else
display(L);
break;
}
} while (ch <= 2);
}
Output:
Q8. W.A.P. to create a binary search tree and perform following operations:
1) Search a particular key.
2) Delete a node from the tree.
3) Find total number of leaf nodes
4) Find height of a binary search tree
5) Count total numbers of nodes from right hand side of root node
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct BST
{
struct BST *Left;
struct BST *Right;
int data;
} treetype;
treetype *insert(treetype *R, int X)
{
treetype *p = NULL;
if (R == NULL)
{
p = (treetype *)malloc(sizeof(treetype));
p->data = X;
p->Left = p->Right = NULL;
R = p;
return R;
}
else if (R->data > X)
R->Left = insert(R->Left, X);
else
R->Right = insert(R->Right, X);
return R;
}
int search_key(treetype *R, int X)
{
if (R != NULL)
{
if (R->data == X)
return 1;
search_key(R->Left, X);
search_key(R->Right, X);
}
return 0;
}
treetype *inOrderPredecessor(treetype *R)
{
R = R->Left;
while (R->Right != NULL)
R = R->Right;
return R;
}
treetype *delete_Node(treetype *R, int X)
{
treetype *iPre = NULL;
if (R == NULL)
{
printf("Node not found!!");
return NULL;
}
if (R->Left == NULL && R->Right == NULL && R->data == X)
{
printf("Node is deleted");
free(R);
return NULL;
}
if (X < R->data)
R->Left = delete_Node(R->Left, X);
else if (X > R->data)
R->Right = delete_Node(R->Right, X);
else
{
iPre = inOrderPredecessor(R);
R->data = iPre->data;
R->Left = delete_Node(R->Left, iPre->data);
}
return R;
}
void total_leaf_node(treetype *R, int *c)
{
if (R != NULL)
{
if (R->Left == NULL && R->Right == NULL)
(*c)++;
total_leaf_node(R->Left, c);
total_leaf_node(R->Right, c);
}
}
int height_tree(treetype *R)
{
if (R == NULL)
return 0;
else
{
int left_Height = height_tree(R->Left);
int right_Height = height_tree(R->Right);
if (left_Height > right_Height)
return left_Height + 1;
else
return right_Height + 1;
}
}
void total_Right_Node(treetype *R, int *c)
{
if (R != NULL)
{
(*c)++;
total_Right_Node(R->Left, c);
total_Right_Node(R->Right, c);
}
}
void main()
{
treetype *Root = NULL;
int X, ch;
int c = 0, d = 0;
do
{
printf("\nEnter 1 to insert\nEnter 2 to find a key\nEnter 3 to delete a node\nEnter 4 to find total
leaf Node\nEnter 5 to find height of tree\nEnter 6 to find total nodes at right side\nEnter 7 for
exit\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter Number: ");
scanf("%d", &X);
Root = insert(Root, X);
break;
case 2:
printf("Enter Key: ");
scanf("%d", &X);
if (search_key(Root, X))
printf("Find the key\n");
else
printf("Didn't find the key\n");
break;
case 3:
printf("Enter Node to delete: ");
scanf("%d", &X);
Root = delete_Node(Root, X);
break;
case 4:
total_leaf_node(Root, &c);
printf("Total Leaf Nodes: %d\n", c);
break;
case 5:
printf("Height of tree: %d", height_tree(Root));
break;
case 6:
total_Right_Node(Root->Right, &d);
printf("Total Nodes at right side: %d\n", d);
break;
}
} while (ch <= 6);
}
Output:
Q9.Write a program to add of two polynomials of degree n, using linked list
p1 = first polynomial
p2 = second polynomial
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int degree;
int exp1;
int exp2;
} nodetype;
nodetype *p = NULL;
while (N >= 0)
p = (nodetype *)malloc(sizeof(nodetype));
p->degree = N;
p->exp1 = e1;
p->exp2 = e2;
p->next = L;
L = p;
N--;
}
return L;
int total = 0;
P1 = P1->next;
P2 = P2->next;
int main()
do
scanf("%d", &ch);
switch (ch)
{
case 1:
scanf("%d", &N);
scanf("%d", &e1);
scanf("%d", &e2);
break;
case 2:
scanf("%d", &N);
scanf("%d", &e1);
scanf("%d", &e2);
break;
case 3:
add(P1, P2);
break;
return 0;
}
Output:
Q10. Write a C program to sort a sequence of characters given by user in an array, using Quick sort
technique.
Code:
#include <stdio.h>
#include <stdlib.h>
void quickSort(char A[], int LB, int UB)
{
int i = LB, j = UB;
char key = A[LB], t;
if (LB >= UB)
return;
while (i < j)
{
while (key >= A[i] && i < j)
i++;
while (key < A[j])
j--;
if (i < j)
{
t = A[i];
A[i] = A[j];
A[j] = t;
}
}
A[LB] = A[j];
A[j] = key;
quickSort(A, LB, j - 1);
quickSort(A, j + 1, UB);
}
int main()
{
char A[] = {'c', 'r', 'e', 'a', 'y', 'b', '\0'};
int N = 6;
printf("Before Sorting\n");
for (int i = 0; i < N; i++)
printf("%c\t", A[i]);
printf("\n");
printf("After Sorting\n");
quickSort(A, 0, N - 1);
for (int i = 0; i < N; i++)
printf("%c\t", A[i]);
return 0;
}
Output:
Q11. W.A.P. to Merge two different single linked lists of different size and sorted values, into a single
list.
Code 11:
#include <stdio.h>
#include <stdlib.h>
void main()
{
nodetype *L1 = NULL, *L2 = NULL, *L3 = NULL;
L1 = insert(L1, 5);
L1 = insert(L1, 10);
L1 = insert(L1, 15);
L2 = insert(L2, 2);
L2 = insert(L2, 4);
L2 = insert(L2, 8);
L2 = insert(L2, 12);
printf("Before Merge\n");
display(L1);
printf("\n");
display(L2);
printf("\nAfter Merge\n");
L3 = merge(L1, L2, L3);
display(L3);
}
Output: