0% found this document useful (0 votes)
32 views

Data Structure Term Work

Uploaded by

Yashasvi rana
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)
32 views

Data Structure Term Work

Uploaded by

Yashasvi rana
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/ 32

Q1.

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>

typedef struct Que


{
char Product_id;
char Product_Name[30];
int Total_Sale;
char Product_Grade;
struct Que *next;
} Que;

Que *Enq(Que *R)


{
Que *p = NULL;
int W;
char X, Y;
char Z[30];
p = (Que *)malloc(sizeof(Que));
if (p == NULL)
printf("Not enough memory");
else
{
printf("Enter Product Id:");
scanf(" %c", &X);
fflush(stdin);
printf("Enter Product Name: ");
scanf("%s", &Z);
fflush(stdin);
printf("Enter Total Sale: ");
scanf("%d", &W);
fflush(stdin);
printf("Enter Product Grade: ");
scanf(" %c", &Y);
p->Product_id = X;
strcpy(p->Product_Name, Z);
p->Total_Sale = W;
p->Product_Grade = Y;
if (R == NULL)
R = p;
else
{
R->next = p;
R = p;
}
R->next = NULL;
}
return R;
}
Que *Deq(Que *F)
{
Que *p = NULL;
p = F;
printf("\nProduct Id: %c\n", F->Product_id);
printf("Product Name: %s\n", F->Product_Name);
printf("Total Sale: %d\n", F->Total_Sale);
printf("Product Grade: %c\n", F->Product_Grade);
F = F->next;
free(p);
return F;
}
void display(Que *F)
{
while (F != NULL)
{
printf("\nProduct Id: %c\n", F->Product_id);
printf("Product Name: %s\n", F->Product_Name);
printf("Total Sale: %d\n", F->Total_Sale);
printf("Product Grade: %c\n", F->Product_Grade);
F = F->next;
}
}
void main()
{
Que *F = NULL, *R = NULL;
int ch;
do
{
printf("\nEnter 1 to insert\nEnter 2 to delete\nEnter 3 for display\nEnter 4 for exit\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
R = Enq(R);
if (F == NULL)
F = R;
break;
case 2:
if (F == NULL)
printf("Empty Que");
else
{
F = Deq(F);
if (F == NULL)
R = NULL;
}
break;
case 3:
if (F == NULL)
printf("Empty Que");
else
display(F);
break;
}
} while (ch <= 3);
}

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>

typedef struct list

char Emp_Id;

char Emp_Name[20];

struct list *next;

} nodetype;

nodetype *insert(nodetype *L)

nodetype *p = NULL;

char X, Y[30];

p = (nodetype *)malloc(sizeof(nodetype));

if (p != NULL)

printf("Enter employee Id: ");

scanf(" %c", &X);

printf("Enter emplyee name: ");

scanf("%s", &Y);

p->Emp_Id = X;

strcpy(p->Emp_Name, Y);

p->next = L;

L = p;

return L;

int isPresent(nodetype *L1, nodetype *L2)

while (L1 != NULL)

{
if (L1->Emp_Id == L2->Emp_Id)

return 0;

L1 = L1->next;

return 1;

void unionNodes(nodetype *L1, nodetype *L2)

nodetype *t = L1;

while (t != NULL)

printf("Employee Id: %c\n", t->Emp_Id);

printf("Employee Name: %s\n\n", t->Emp_Name);

t = t->next;

while (L2 != NULL)

if (isPresent(L1, L2))

printf("Employee Id: %c\n", L2->Emp_Id);

printf("Employee Name: %s\n\n", L2->Emp_Name);

L2 = L2->next;

void main()

nodetype *L1 = NULL, *L2 = NULL;

int ch;

do

printf("\nEnter 1 to insert in first list\nEnter 2 to insert in second list\nEnter 3 to display union\nEnter 4


for exit\n");

scanf("%d", &ch);

switch (ch)

{
case 1:

L1 = insert(L1);

break;

case 2:

L2 = insert(L2);

break;

case 3:

if (L1 == NULL || L2 == NULL)

printf("One list is empty!!");

else

unionNodes(L1, L2);

break;

} while (ch <= 3);

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];

int top = -1;

char exp[20];

char *e;

int n1,n2,n3,num;

printf("Enter the expression :: ");

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++;

printf("\nThe result of expression %s = %d\n\n",exp,pop());

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>

typedef struct list

int info;

struct list *next;

} nodetype;

nodetype *insert(nodetype *L)

nodetype *p = NULL;

int X;

p = (nodetype *)malloc(sizeof(nodetype));

if (p != NULL)

printf("Enter Number: ");

scanf("%d", &X);

p->info = X;

p->next = L;

L = p;

return L;

void split(nodetype *L, nodetype **P, nodetype **N)

while (L != NULL)

nodetype *t = (nodetype *)malloc(sizeof(nodetype));

t->info = L->info;

if (t->info < 0)

t->next = (*P);

(*P) = t;

else if (t->info >= 0)


{

t->next = (*N);

(*N) = t;

L = L->next;

printf("split the list into positive and negative linked list\n");

void display(nodetype *P, nodetype *N)

printf("Positive Linked list:\n");

while (P != NULL)

printf("%d\t", P->info);

P = P->next;

printf("\nNegative Linked list:\n");

while (N != NULL)

printf("%d\t", N->info);

N = N->next;

void main()

nodetype *L = NULL, *positive = NULL, *negative = NULL;

int ch;

do

printf("\nEnter 1 to insert\nEnter 2 to split\nEnter 3 for display\nEnter 4 for exit\n");

scanf("%d", &ch);

switch (ch)

case 1:

L = insert(L);

break;

case 2:

if (L == NULL)
printf("empty");

else

split(L, &positive, &negative);

break;

case 3:

if (positive == NULL && negative == NULL)

printf("empty");

else

display(positive, negative);

break;

} while (ch <= 3);

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>

typedef struct list

int info;

struct list *next;

} nodetype;

nodetype *insert(nodetype *P)

nodetype *t = NULL;

int X;

t = (nodetype *)malloc(sizeof(nodetype));

if (t != NULL)

printf("Enter Number: ");

scanf("%d", &X);

t->info = X;

t->next = P;

P = t;

return P;

void split(nodetype *P, nodetype **Q, nodetype **R)

while (P != NULL)

nodetype *t = (nodetype *)malloc(sizeof(nodetype));

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;

printf("split the list into odd and even linked list\n");

void display(nodetype *Q, nodetype *R)

printf("ODD Linked list:\n");

while (Q != NULL)

printf("%d\t", Q->info);

Q = Q->next;

printf("\nEven Linked list:\n");

while (R != NULL)

printf("%d\t", R->info);

R = R->next;

void main()

nodetype *P = NULL, *Q = NULL, *R = NULL;

int ch;

do

printf("\nEnter 1 to insert\nEnter 2 to split\nEnter 3 for display\nEnter 4 for exit\n");

scanf("%d", &ch);

switch (ch)

{
case 1:

P = insert(P);

break;

case 2:

if (P == NULL)

printf("empty");

else

split(P, &Q, &R);

break;

case 3:

if (Q == NULL && R == NULL)

printf("empty");

else

display(Q, R);

break;

} while (ch <= 3);

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>

typedef struct list


{
struct list *prev;
int info;
struct list *next;
} nodetype;

void insert(nodetype **L, nodetype **R)


{
int X;
nodetype *p = NULL, *t = NULL;
p = (nodetype *)malloc(sizeof(nodetype));
if (p != NULL)
{
printf("Enter number:");
scanf("%d", &X);
p->info = X;
if ((*L) == NULL && (*R) == NULL)
(*L) = (*R) = p;
else if (X < (*L)->info)
{
p->next = (*L);
(*L)->prev = p;
(*L) = p;
}
else if (X > (*R)->info)
{
p->prev = (*R);
(*R)->next = p;
(*R) = p;
}
else
{
t = (*L);
while (X > t->next->info)
t = t->next;
p->next = t->next;
p->prev = t;
t->next->prev = p;
t->next = p;
}
(*L)->prev = (*R);
(*R)->next = (*L);
}
}

void display(nodetype *L)


{
nodetype *t = L;
do
{
printf("%d\t", t->info);
t = t->next;
} while (t != L);
}

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

For example p1=anx¬n+an-1xn-1 + an-2xn-2 + …….. a0x0

P2=bnx¬n+bn-1xn-1 + bn-2xn-2 + …….. b0x0

p1 = first polynomial

p2 = second polynomial

Find out p3= p1+p2

Code:

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

typedef struct list

int degree;

int exp1;

int exp2;

struct list *next;

} nodetype;

nodetype *insert(nodetype *L, int N, int e1, int e2)

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;

void add(nodetype *P1, nodetype *P2)

int total = 0;

while (P1 != NULL)

total += pow(P1->exp1, P1->degree) * pow(P1->exp2, P1->degree);

P1 = P1->next;

while (P2 != NULL)

total += pow(P2->exp1, P2->degree) * pow(P2->exp2, P2->degree);

P2 = P2->next;

printf("P3 = %d", total);

int main()

nodetype *P1 = NULL, *P2 = NULL;

int ch, N, e1, e2;

do

printf("\nEnter 1 to insert in first polynomial\nEnter 2 to insert in second polynomial\nEnter 3


to add both polynomial\nEnter 4 for exit\n");

scanf("%d", &ch);

switch (ch)
{

case 1:

printf("Enter degree: ");

scanf("%d", &N);

printf("Enter first exponent: ");

scanf("%d", &e1);

printf("Enter second exponent: ");

scanf("%d", &e2);

P1 = insert(P1, N, e1, e2);

break;

case 2:

printf("Enter degree: ");

scanf("%d", &N);

printf("Enter first exponent: ");

scanf("%d", &e1);

printf("Enter second exponent: ");

scanf("%d", &e2);

P2 = insert(P2, N, e1, e2);

break;

case 3:

add(P1, P2);

break;

} while (ch <= 3);

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>

typedef struct list


{
int info;
struct list *next;
} nodetype;
nodetype *insert(nodetype *L, int X)
{
nodetype *p = NULL;
p = (nodetype *)malloc(sizeof(nodetype));
if (p != NULL)
{
p->info = X;
p->next = L;
L = p;
}
return L;
}
nodetype *merge(nodetype *L1, nodetype *L2, nodetype *L3)
{
while (L1 != NULL && L2 != NULL)
{
if (L1->info > L2->info)
{
L3 = insert(L3, L1->info);
L1 = L1->next;
}
else
{
L3 = insert(L3, L2->info);
L2 = L2->next;
}
}
while (L1 != NULL)
{
L3 = insert(L3, L1->info);
L1 = L1->next;
}
while (L2 != NULL)
{
L3 = insert(L3, L2->info);
L2 = L2->next;
}
return L3;
}

void display(nodetype *L)


{
while (L != NULL)
{
printf("%d\t", L->info);
L = L->next;
}
}

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:

You might also like