0% found this document useful (0 votes)
10 views102 pages

Datastructure Completed

Uploaded by

anusha bharath
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)
10 views102 pages

Datastructure Completed

Uploaded by

anusha bharath
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/ 102

DEPARTMENT OF COMPUTER SCIENCE

IVTH SEMESTER LAB QUESTIONS

TABLE OF CONTENTS

Experiments Page No
1. Sort a given list of strings - 3
2. Reverse a string using pointers - 5
3. Pattern matching - 7
4. 2-D Array search - 9
5. Append 2 arrays - 12
6. Linear search - 16
7. Binary search - 19
8. Sparse metrix&Triplet representation - 22
9. Linked list- Display - 25
10. Linked list - Deletion - 29
11. Linked list - Sort - 35
12. Linked list - Search - 40
13. Doubly linked list- bckwrd,frwrd repn - 45
14. Addition of 2 polynomials using array - 49
15. Implement Stack using array - 54
16. Implement Stack using linked list - 60
17. Evaluation of postfix expression - 65
18. Implement Queue using array - 68
19. Implement Queue using linked list - 72
20. Traverse a binary search tree in preorder - 77
21. Traverse a binary search tree in inorder - 81
22. Traverse a binary search tree in postorder - 84
23. Search an element in a binary search tree - 87
24. Implement exchange sort - 91
25. Implement selection sort - 94
26. Implement insertion sort - 97
27. Implement quick sort - 100
PAGE NO:3

1.Sort a given list of strings


ALGORITHM:
step 1: start
step 2: declare variables i,j,count of integers and character array
str[ ][ ],temp[ ]
step 3: read total number of strings as count
step 4: read array str[i]
step 5: set i=0, repeat step to until i<=count
step 6: set j=i+1, repeat step to until j<=count
step 7:
if strcmp (str[i],str[j])>0)

set strcpy(temp,str[i]);
set strcpy(str[i],str[j]);
set strcpy(str[j],temp);

step 8: j++
step 9: i++
step 10: write sorted array str
step 11:stop

C CODE:
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,count;
char str[25][25],temp[25];
clrscr();
puts("How many strings u are going to enter?: ");
scanf("%d",&count);
puts("Enter Strings one by one: ");
for(i=0;i<=count;i++) PAGE NO:4

gets(str[i]);

for(i=0;i<=count;i++)
for(j=i+1;j<=count;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}

printf("\nOrder of Sorted Strings:\n");


for(i=0;i<=count;i++)
puts(str[i]);

getch();
return 0;
}

OUTPUT
PAGE NO:5

2.Reverse a string using pointers.

ALGORITHM:
step 1:start
step 2:declare variables len,i of integer and character pointer s
step 3:read s
step 4:store string length to len
step 5: set i=len, for i>=0 repeat
print *(s+i)
step 6:i--
step 7:stop

C CODE:
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("\nENTER A STRING: ");
gets(s);
len=strlen(s);
printf("\nTHE REVERSE OF THE STRING IS:");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}
PAGE NO:6
OUTPUT:
PAGE NO:7

3.Implement Pattern Matching Algorithm

ALGORITHM:

step 1:start
step 2:declare variables m,n,i,j of integers and character array
str1,str2
step 3:read string in str1 and pattern in str2
step 4:set m=strlen(str1) and n=strlen(str2)
step 5: set i=0, repeat step 6 to 9 until i<m-n+1
step 6: set j=0 repeat step 7 to 8 until j<n
step 7: if(str1[i+j] != str2[j])
break;
if(j == n)
print pattern found at i+1
step 8: j++
step 9:i++
step 10: stop

C CODE:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int m,n,i,j;
clrscr();
printf("\nEnter text string : ");
gets(str1);

PAGE NO:8

printf("Enter pattern to search : ");


gets(str2);

m = strlen(str1);
n = strlen(str2);

for(i=0;i<m-n+1;i++)
{
for(j=0;j<n;j++)
{
if(str1[i+j] != str2[j])
break;
}
if(j == n)
{
printf("\n\n Pattern found at %d. ",i+1);
}
}
getch();
}

OUTPUT:
PAGE NO:9

4.Search an element in the 2-dimensional


array
ALGORITHM:

step 1:start
step 2: declare i,j,r,c,item,loc,loc1 and a[][] array
step 3: read number of rows,r and columns,c
step 4:set i=1, repeat step 5 to 8 until i<=r
step 5: set j=1, repeat step 6 to 7 until j<=C
step 6: read a[i][j]
step 7:j++
step 8:i++
step 9:read search element as item
step 10:set i=1, repeat step 11 to 14 until i<=r
step 11: set j=1, repeat step 12 to 13 until j<=c
step 12:
if item = a[i][j]
set loc=i;
set loc1=j;
set break;
step 13: j++
PAGE NO:10

step 14:i++
step 15:print loc & loc1
step 16:stop

C CODE:
void main()
{
int i=0,j=0,r=0,c=0,item,loc=0,loc1=0;
int a[10][10];
clrscr();
printf("\n\tThis Program is Used To search an element in
2Dimensional Array using Linear Search\n");
printf("\nEnter the number of rows&columns:\n");
scanf("\n%d\n%d",&r,&c);
printf("\n\tEnter The %dx%d Array:\n",r,c);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
scanf("%d",&a[i][j]);
}
printf("\n\tEneter The Value To Be Serched:");
scanf("%d",&item);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(item==a[i][j])
{
PAGE NO:11
loc=i;
loc1=j;
break;
}
}
}
printf("\n\tThe Item is at %d Row And %d
Coloumn.",loc,loc1);
printf("\n\n\t\tSearch Completed.");
getch();
}

OUTPUT:
PAGE NO:12

5.Append 2 arrays

ALGORITHM:
step 1: start
step 2: declare variables i,j,k,n1,n2 and arr1[],arr2[],re[] of
integer
step 3: read no.of elements as n1,n2 store elements in
arr1[],arr2[]
step 4: set i=0,j=0,k=0
step 5: repeat step 6 while i< n1 && j < n2
step 6:
if arr1[i] <= arr2[j]
set res[k] = arr1[i]
set i++;
set k++;

else
set res[k] = arr2[j]
set k++
set j++

step 7:
while i < n1
set res[k] = arr1[i]
set i++
PAGE NO:13
set k++

step 8:
while j < n2
set res[k] = arr2[j]
set k++
set j++

step 9:write merged array res


step 10: stop

C CODE:

#include<stdio.h>
int main()
{
int arr1[30], arr2[30], res[60];
int i, j, k, n1, n2;

printf("\nEnter no of elements in 1st array :");


scanf("%d", &n1);
for (i = 0; i < n1; i++)
{
scanf("%d", &arr1[i]);
}

printf("\nEnter no of elements in 2nd array :");


scanf("%d", &n2);
for (i = 0; i < n2; i++)
{
PAGE NO:14
scanf("%d", &arr2[i]);
}

i = 0;
j = 0;
k = 0;

// Merging starts
while (i < n1 && j < n2)
{
if (arr1[i] <= arr2[j])
{
res[k] = arr1[i];
i++;
k++;
}
else
{
res[k] = arr2[j];
k++;
j++;
}
}
/* Some elements in array 'arr1' are still remaining
where as the array 'arr2' is exhausted */

while (i < n1)


{
res[k] = arr1[i];
i++;
k++;
}
PAGE NO:15
/* Some elements in array 'arr2' are still remaining
where as the array 'arr1' is exhausted */

while (j < n2)


{
res[k] = arr2[j];
k++;
j++;
}

printf("\nMerged array is :");

for (i = 0; i < n1 + n2; i++)


printf("%d ", res[i]);

return (0);
}

OUT PUT:
PAGE NO:16

6.Search an element in the array using linear


search

ALGORITHM:

step 1: start
step 2: declare variables search,c,n & array of integers
step 3:read n as number of elements in array
step 4:set c=0, read array[c] until c<n
step 5:read search number as search
step 6: set c=0, repeat step 7 to step 8 until c<n
step 7:
if (array[c] == search)
write "present" search,c+1
step 8:c++
step 9:
if (c == n)
write "not present",search
step 10:stop
PAGE NO:17

C CODE:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
clrscr();
printf("Enter number of elements in array\n");
scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search) // If required element is found
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);

getch();
return 0;
}
PAGE NO:18

OUTPUT:
PAGE NO:19

7.Search an element in the array using binary


search
ALGORITHM:

step 1: start
step 2: declare variables c,first,last,middle,n,search & array[] of integer
step 3: read total elements as n and store to array[]
step 4: read search item as search
step 5: set
first = 0;
last = n - 1;
middle = (first+last)/2;

step 6: while first<=last repeat step 7


step 7:
if (array[middle] < search)
set first = middle + 1;
else if (array[middle] == search)
write "item found at location" search, middle+1
break;
else
set last = middle - 1;
set middle = (first + last)/2;

step 8:
if first>last
write "not found"
PAGE NO:20

step 9: stop

C CODE:

#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d integers\n", n);


for (c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last)


{
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search)
{
printf("\n\n%d found at location %d.\n", search,
middle+1);
PAGE NO:21
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n",
search);
getch();
return 0;
}

OUTPUT:
PAGE NO:22

8.Read A Sparse Matrix And Display Its


Triplet Representation Using Array
C CODE:

#include <stdio.h>
#define MAX 20

void read_matrix(int a[10][10], int row, int column);


void print_sparse(int b[MAX][3]);
void create_sparse(int a[10][10], int row, int column, int
b[MAX][3]);

int main()
{
int a[10][10], b[MAX][3], row, column;
clrscr();
printf("\nEnter the size of matrix (rows, columns): ");
scanf("%d%d", &row, &column);

read_matrix(a, row, column);


create_sparse(a, row, column, b);
print_sparse(b);
getch();
return 0;
}

void read_matrix(int a[10][10], int row, int column)


{
int i, j;
PAGE NO:23
printf("\nEnter elements of matrix\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
}

void create_sparse (int a[10][10], int row, int column, int


b[MAX][3] )
{
int i, j, k;
k = 1;
b[0][0] = row;
b[0][1] = column;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if (a[i][j] != 0)
{
b[k][0] = i;
b[k][1] = j;
b[k][2] = a[i][j];
k++;
}
}
b[0][2] = k - 1;
}
PAGE NO:24
}

void print_sparse(int b[MAX][3])


{
int i, column;
column = b[0][2];
printf("\nSparse form - list of 3 triples\n\n");
for (i = 0; i <= column; i++)
{
printf("%d\t%d\t%d\n", b[i][0], b[i][1], b[i][2]);
}
}

OUTPUT:
PAGE NO:25

9.Create A Singly Linked List Of N Nodes


And Display It.

C CODE:

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

struct node
{
int num; //Data of the node
struct node *nextptr; //Address of the next node
}*stnode;

void createNodeList(int n); // function to create the list


void displayList(); // function to display the list

int main()
{
int n;
clrscr();
printf("\n\n Linked List : To create and display Singly
Linked List :\n");
printf("---------------------------------------------------------\n");

printf(" Input the number of nodes : ");


scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list : \n");
displayList(); PAGE NO:26
getch();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode == NULL) //check whether the fnnode is NULL


{ //and if so no memory allocation
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard

printf(" Input data for node 1 : ");


scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL; // links the address field to
tmp = stnode; //NULL

// Creating n nodes and adding to linked list


for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
PAGE NO:27
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);

fnNode->num = num; // links the num field


//of fnNode with num
fnNode->nextptr = NULL; // links the address
//field of fnNode with NULL

tmp->nextptr = fnNode; // links previous node


//i.e. tmp to the fnNode
tmp = tmp->nextptr;
}
}
}
}

void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{ // prints the data of
PAGE NO:28
printf(" Data = %d\n", tmp->num); //current node
tmp = tmp->nextptr; // advances the position
//of current node
}
}
}

OUTPUT:
PAGE NO:29

10.Delete A Given Node From A Singly


Linked List.
C CODE:

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data; // Data
struct node *next; // Address
} * head;

void createList(int n);


void deleteFirstByKey(int key); DECLARING FUNCTIONS
void displayList();

int main()
{
int n, key;
clrscr();
// Input node count to create
printf("Enter number of node to create: ");
scanf("%d", &n);
createList(n);
// Display list
printf("\nData in list before deletion\n");
displayList();
PAGE NO:30
printf("\nEnter element to delete with key: ");
scanf("%d", &key);

// Call function to delete first element by key


deleteFirstByKey(key);

// Print final list


printf("\nData in list after deletion\n");
displayList();
getch();
return 0;
}

void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = malloc(sizeof(struct node));

if (head == NULL)
{
printf("Unable to allocate memory. Exiting from app.");
exit(0);
}

printf("Enter data of node 1: ");


scanf("%d", &data);

head->data = data; // Link data field with data


PAGE NO:31
head->next = NULL; // Link address field to NULL
temp = head;

for (i = 2; i <= n; i++)


{
newNode = malloc(sizeof(struct node));

if (newNode == NULL)
{
printf("Unable to allocate memory. Exiting from
app.");
exit(0);
}

printf("Enter data of node %d: ", i);


scanf("%d", &data);

newNode->data = data; // Link data field of newNode


newNode->next = NULL; // The newNode should
//point to nothing

temp->next = newNode; // Link previous node i.e.


//temp to the newNode
temp = temp->next;
}
}

void displayList()
{
PAGE NO:32
struct node *temp;

/*
* If the list is empty i.e. head = NULL,
* dont perform any action and return.
*/
if (head == NULL)
{
printf("List is empty.\n");
return;
}

temp = head;
while (temp != NULL)
{
printf("%d, ", temp->data);
temp = temp->next; // Move to next node
}

printf("\n");
}

void deleteFirstByKey(int key)


{
struct node *prev, *cur;

/* Check if head node contains key */


while (head != NULL && head->data == key)
PAGE NO:33
{
// Get reference of head node
prev = head;
// Adjust head node link
head = head->next;
// Delete prev since it contains reference
//to head node
free(prev);

// No need to delete further


return;
}

prev = NULL;
cur = head;

// For each node in the list


while (cur != NULL)
{
// Current node contains key
if (cur->data == key)
{
// Adjust links for previous node
if (prev != NULL)
prev->next = cur->next;

free(cur);
return;
}

prev = cur;
PAGE NO:34
cur = cur->next;
}
}

OUTPUT:
PAGE NO:35

11.Sort A Singly Linked List


C CODE:

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data; // Data
struct node *next; // Address
} * head;

void createList(int n);


void displayList(); FUNCTION DECLARATION
void sortList();

int main()
{
int n, key;
clrscr();
printf("Enter number of node to create: ");
scanf("%d", &n);

createList(n);

printf("\nData in list before deletion\n");


displayList();
sortList();
displayList();
getch();
PAGE NO:36
return 0;
}

void createList(int n)
{
struct node *newNode, *temp;
int data, i;

head = malloc(sizeof(struct node));

if (head == NULL)
{
printf("Unable to allocate memory. Exiting from
app.");
exit(0);
}

// Input head node data from user


printf("Enter data of node 1: ");
scanf("%d", &data);

head->data = data; // Link data field with data


head->next = NULL; // Link address field to NULL
temp = head;

for (i = 2; i <= n; i++)


{
PAGE NO:37
newNode = malloc(sizeof(struct node));
//If memory is not allocated for newNode
if (newNode == NULL)
{
printf("Unable to allocate memory. Exiting from
app.");
exit(0);
}

printf("Enter data of node %d: ", i);


scanf("%d", &data);

newNode->data = data; // Link data field of


//newNode
newNode->next = NULL; // The newNode
//should point to nothing

temp->next = newNode; // Link previous node


//i.e.temp to the newNode
temp = temp->next;
}

void displayList()
{
struct node *temp;
if (head == NULL) PAGE NO:38
{

printf("List is empty.\n");
return;
}

temp = head;
while (temp != NULL)
{
printf("%d, ", temp->data);
temp = temp->next; // Move to next node
}

printf("\n");
}

void sortList()
{
struct node *first,*second;
int temp_data;
first=head;
printf("THE LIST AFTER SORTING IS:");

while(first!=NULL)
{
second=first->next;
while(second!=NULL)
{
if(first->data>second->data)
{ PAGE NO:39
temp_data=first->data;

first->data=second->data;
second->data=temp_data;
}
second=second->next;
}
first=first->next;
}
}

OUTPUT:
PAGE NO:40

12.Create A Singly Linked List And Search An


Element From That List.
C CODE:

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data; // Data
struct node *next; // Address
} * head;

void createList(int n);


void displayList();
int search(int key);

int main()
{
int n, keyToSearch, index;
clrscr();
printf("Enter number of node to create: ");
scanf("%d", &n);

createList(n);

printf("\nData in list: \n");


displayList();

// Input element to search from user.


PAGE NO:41
printf("\nEnter element to search: ");
scanf("%d", &keyToSearch);

index = search(keyToSearch);

// Element found in the list


if (index >= 0)
printf("%d found in the list at position %d\n",
keyToSearch, index + 1);
else
printf("%d not found in the list.\n", keyToSearch);

getch();
return 0;
}

void createList(int n)
{
struct node *newNode, *temp;
int data, i;

head = malloc(sizeof(struct node));

if (head == NULL)
{
printf("Unable to allocate memory. Exiting from app.");
exit(0);
}

printf("Enter data of node 1: ");


PAGE NO:42
scanf("%d", &data);

head->data = data; // Link data field with data


head->next = NULL; // Link address field to NULL
temp = head;

for (i = 2; i <= n; i++)


{
newNode = malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if (newNode == NULL)
{
printf("Unable to allocate memory. Exiting from
app.");
exit(0);
}

printf("Enter data of node %d: ", i);


scanf("%d", &data);

newNode->data = data; /* Link data field of


newNode */
newNode->next = NULL; /* The newNode
should point to nothing*/

temp->next = newNode; /* Link previous node


i.e. temp to the newNode*/
temp = temp->next;
}
PAGE NO:43
}

void displayList()
{
struct node *temp;

if (head == NULL)
{
printf("List is empty.\n");
return;
}

temp = head;
while (temp != NULL)
{
printf("%d, ", temp->data);
temp = temp->next; // Move to next node
}
printf("\n");
}

/**
* Search an element with given key in linked list.
* It return a positive integer specifying index of the element
* on success, otherwise returns -1.
*/
int search(int key)
{
int index;
PAGE NO:44
struct node *curNode;
index = 0;
curNode = head;

// Iterate till last element until key is not found


while (curNode != NULL && curNode->data != key)
{
index++;
curNode = curNode->next;
}

return (curNode != NULL) ? index : -1;


}

OUTPUT:
PAGE NO:45

13.Create A Doubly Linked List Of Integers


And Display In Forward And Backward
Direction.

C CODE:

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};

struct node *head = NULL;


struct node *last = NULL;
struct node *current = NULL;

//display the list


void printList()
{
struct node *ptr = head;

printf("\nLIST:");
//start from the beginning
while(ptr != NULL)
{
PAGE NO:46
printf(" \t%d ",ptr->data);
ptr = ptr->next;
}

//display the list


void print_backward()
{
struct node *ptr = last;

printf("\nLIST IN BACKWARD:");
//start from the beginning
while(ptr != NULL)
{
printf("\t%d",ptr->data);
ptr = ptr->prev;
}

//Create Linked List


void insert(int data)
{
// Allocate memory for new node;
struct node *link = (struct node*) malloc(sizeof(struct
node));

link->data = data;
link->prev = NULL;
PAGE NO:47
link->next = NULL;

// If head is empty, create new list


if(head==NULL)
{
head = link;
return;
}

current = head;

// move to the end of the list


while(current->next!=NULL)
current = current->next;

// Insert link at the end of the list


current->next = link;
last = link;
link->prev = current;
}

int main()
{
int data,i=0,n;
clrscr();

printf("ENTER THE NUMBER OF NODES");


scanf("%d",&n);

for(i=0;i<+n;i++)
{
PAGE NO:48
scanf("%d",&data);
insert(data);
}

printList();
print_backward();

getch();
return 0;
}

OUTPUT:
PAGE NO:49

14.Addition Of 2 Polynomials Using Array.

C CODE:

#include<stdio.h>
#include<conio.h>
main()
{
int a[10], b[10], c[10],m,n,k,k1,i,j,x;
clrscr();

printf("\tPolynomial Addition\n");
printf("\n\tEnter the no. of terms of the polynomial:");
scanf("\t%d", &m);

printf("\n\tEnter the degrees and coefficients:");


for (i=0;i<2*m;i++)
scanf("\t%d", &a[i]);

printf("\n\tFirst polynomial is:");


k1=0;

if(a[k1+1]==1)
printf("x^%d", a[k1]);
else
printf("%dx^%d", a[k1+1],a[k1]);
k1+=2;

while (k1<i)
PAGE NO:50
{
printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;
}

printf("\n\tEnter the no. of terms of 2nd polynomial:");


scanf("\t%d", &n);
printf("\n\tEnter the degrees and co-efficients:");

for(j=0;j<2*n;j++)
scanf("\t%d", &b[j]);

` printf("\n\tSecond polynomial is:");


k1=0;

if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);
k1+=2;

while (k1<2*n)
{
printf("+%dx^%d", b[k1+1],b[k1]);
k1+=2;
}
i=0;
j=0;
k=0;

while (m>0 && n>0)


PAGE NO:51
{
if (a[i]==b[j])
{
c[k+1]=a[i+1]+b[j+1];
c[k]=a[i];
m--;
n--;
i+=2;
j+=2;
}
else if (a[i]>b[j])
{
c[k+1]=a[i+1];
c[k]=a[i];
m--;
i+=2;
}
else
{
c[k+1]=b[j+1];
c[k]=b[j];
n--;
j+=2;
}
k+=2;
}

while (m>0)
{
c[k+1]=a[i+1];
c[k]=a[i];
PAGE NO:52
k+=2;
i+=2;
m--;
}

while (n>0)
{
c[k+1]=b[j+1];
c[k]=b[j];
k+=2;
j+=2;
n--;
}

printf("\n\tSum of the two polynomials is:");


k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]);
else
printf("%dx^%d", c[k1+1],c[k1]);
k1+=2;

while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]);
else
printf("+%dx^%d", c[k1+1], c[k1]);
k1+=2;
}
PAGE NO:53
getch();
return 0;
}

OUTPUT:

(different degrees)

(same degrees)
PAGE NO:54

15.Implement Stack Using Array

C CODE:

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[ MAX=100 ]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY STACK
ELEMNTS \n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;

case 2:
PAGE NO:55
pop();
break;
case 3:
display();
break;
case 4:
printf("\n\t EXIT POINT ");
break;

default:
printf ("\n\t Please Enter a Valid
Choice(1/2/3/4)");

}
} while(choice!=4);

return 0;
}

void push()
{
if(top>=n-1)
printf("\n\tSTACK is over flow");
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
PAGE NO:56
}

void pop()
{
if(top<=-1)
printf("\n\t Stack is under flow");
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}

void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
PAGE NO:57

OUTPUT:
PAGE NO.58
PAGE NO. 59
PAGE NO.60

16.Implement Stack Using Linked List


C CODE:

#include<stdio.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*top = NULL;

void push(int);
void pop();
void display();

int main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");

printf("\n****** MENU ******\n");


printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
do
{

printf("\nEnter your choice: ");


PAGE NO.61
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try
again!!!\n");
}
} while(choice!=4);
return 0;
}

void push(int value)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("Insertion is Success!!!\n");
}
PAGE NO.62

void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}

void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
while(temp->next != NULL)
{
printf ("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
PAGE NO.63

OUTPUT:
PAGE NO. 64
PAGE NO. 65

17.Evaluation Of Postfix Expression


C CODE:

#include<stdio.h>
int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
clrscr();
printf("Enter The Expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
PAGE NO. 66
{

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());
getch();
PAGE NO. 67
return 0;
}

OUTPUT:
PAGE NO. 68

18.Implement Queue Using Array


C CODE:

#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
clrscr();
printf("\n\tQueue using Array");
printf("\n\t1.Insertion \n\t2.Deletion \n\t3.Display
\n\t4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
PAGE NO. 69
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is
%d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the
options");

}
}
getch();
}
PAGE NO. 70

OUTPUT:
PAGE NO. 71
PAGE NO. 72

19.Implement Queue Using Linked List

C CODE:

#include<stdio.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;

void insert(int);
void delete();
void display();

void main()
{
int choice, value;
clrscr();
printf("\n:: Queue Implementation using Linked List ::\n");

printf("\n****** MENU ******\n");


printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");

do
{

printf("\nEnter your choice: ");


PAGE NO. 73
scanf("%d",&choice);

switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try
again!!!\n");
}
} while(choice!=4);
}

void insert(int value)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else
{
rear -> next = newNode;
PAGE NO. 74
rear = newNode;
}
printf("\tInsertion is Success!!!\n");
}

void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}

void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
PAGE NO. 75
}
printf("%d--->NULL\n",temp->data);
}
}

OUTPUT:
PAGE NO. 76
PAGE NO. 77

20.Traverse A Binary Search Tree In


Preorder

C CODE:

#include<stdio.h>
#include<conio.h>

typedef struct node


{
int data;
struct node *left;
struct node *right;
} node;

node *create()
{
node *p;
int x;
printf("Enter data(-1 for no node):");
scanf("%d",&x);

if(x==-1)
return NULL;

p=(node*)malloc(sizeof(node));
p->data=x;
PAGE NO. 78
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
p->right=create();

return p;
}

void preorder(node *t)


{
if(t!=NULL)
{
printf(" %d",t->data);
preorder(t->left);
preorder(t->right);
}
}

void main()
{
node *root;
clrscr();
root=create();
printf("\nThe preorder traversal of tree is: ");
preorder(root);
getch();
}
PAGE NO. 79

OUTPUT:
PAGE NO. 80
PAGE NO.81

21.Traverse A Binary Search Tree In


Inorder
C CODE:

#include<stdio.h>
#include<conio.h>

typedef struct node


{
int data;
struct node *left;
struct node *right;
} node;

node *create()
{
node *p;
int x;
printf("Enter data(-1 for no node):");
scanf("%d",&x);

if(x==-1)
return NULL;

p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
PAGE NO. 82
p->right=create();

return p;
}

void inorder(node *t)


{
if(t!=NULL)
{
inorder(t->left);
printf(" %d",t->data);
inorder(t->right);
}
}

void main()
{
node *root;
clrscr();
root=create();
printf("\nThe Inorder traversal of tree is: ");
inorder(root);

getch();
}
PAGE NO. 83

OUTPUT:
PAGE NO. 84

22.Traverse A Binary Search Tree In


Postorder
C CODE:

#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} node;

node *create()
{
node *p;
int x;
printf("Enter data(-1 for no node):");
scanf("%d",&x);

if(x==-1)
return NULL;

p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
p->right=create();
PAGE NO. 85
return p;
}

void postorder(node *t)


{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf(" %d",t->data);
}
}

void main()
{
node *root;
clrscr();
root=create();
printf("\nThe Postorder traversal of tree is: ");
postorder(root);
getch();
}
PAGE NO. 86

OUTPUT:
PAGE NO. 87

23.Search An Element In A Binary Search


Tree
C CODE:

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

struct TreeNode
{
int data;
struct TreeNode *leftChildNode;
struct TreeNode *rightChildNode;
};

typedef struct TreeNode node;


node *rootNode = NULL;

void insertNode(int i, node **n)


{
if (*n == NULL)
{
(*n) = (node*)malloc(sizeof(node));
(*n)->leftChildNode = NULL;
(*n)->rightChildNode = NULL;
(*n)->data = i;
}
else if ((*n)->data == i)
printf("\nThis value already exists in the tree!");
PAGE NO. 88
else if (i > (*n)->data)
insertNode(i, &((*n)->rightChildNode));
else
insertNode(i, &((*n)->leftChildNode));
}

// Function to search an element in a Binary search tree

void searchNode(int i, node **n)


{
if (*n == NULL)
printf("\nValue does not exist in tree!");
else if((*n)->data == i)
printf("\nValue found!");
else if(i > (*n)->data)
searchNode(i, &((*n)->rightChildNode));
else
searchNode(i, &((*n)->leftChildNode));
}

int main()
{
int ch, num, num1;
printf("\nSelect a choice from the menu
below\n---------------------------------------------.");
printf("\n1. Insert a node.");
printf("\n2. Search for a node.");
printf("\nSelect a choice from the menu below.");
printf("\nChoice: ");
PAGE NO. 89
scanf("%d", &ch);
do
{
switch(ch)
{
case 1:
printf("\nEnter an element: ");
scanf("%d", &num);
insertNode(num, &rootNode);
break;
case 2:
printf("\nEnter the element to be searched for:
");
scanf("%d", &num);
searchNode(num, &rootNode);
break;
default:
exit(0);
}

} while(choice!=3);
return 0;
}

OUTPUT:
PAGE NO. 90
PAGE NO. 91

24.Implement Exchange Sort

ALGORITHM:

step 1: start
step 2: declare variables num,i,j,temp & array[] of integers
step 3: read total number of elements as num
step 4: set i=0, read elements as array[i]
step 5: set i=o, repeat step to until i<num
step 6: set j=i+1, repeat step to until j<num
step 7: if (array[i] > array[j])

set temp = array[i];


set array[i] = array[j];
set array[j] = temp;

step 8: set i=0, write array as array[i] until i<num


step 9: stop

C CODE:

#include<stdio.h>
#include<conio.h>
int main()
{
PAGE NO. 92
int array[100],num,i, j, temp;
clrscr();
printf( "Enter Total Number Of Elements: ");
scanf("%d",&num);

for (i = 0; i < num; i++)


{
printf("\nEnter The %d Elements:\t",(i+1));
scanf("%d",&array[i]);
}

for(i = 0; i <num; i++)


{
for (j=(i + 1); j <num ; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}

printf("\nElemets After Sorting\n");


for (i = 0; i < num; i++)
{
printf("\t%d",array[i]);
}
getch();
return 0;
PAGE NO. 93
}

OUTPUT:
PAGE NO. 94

25.Implement Selection Sort

ALGORITHM:

step 1: start
step 2: declare variable n,i,j,position,swap & array as a[] of
integers
step 3: read number of elements as n
step 4: read n number to array a[]
step 5: set i=0, repeat step 6 to step 10 until i<n-1
step 6: set position=i
step 7: set j=i+1, repeat step 7 to step 9 until j<n
step 8:
if a[position] > a[j]
set position=j;

if position != i

set swap=a[i];
set a[i]=a[position];
set a[position]=swap;
step 9: j++
step 10: i++
step 11: stop

C CODE:

#include <stdio.h>
int main()
PAGE NO. 95
{
int a[100], n, i, j, position, swap;
clrscr();
printf("\nEnter number of elements\t");
scanf("%d", &n);
printf("Enter %d Numbers\n", n);

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


scanf("%d", &a[i]);

for(i = 0; i < n - 1; i++)


{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("Sorted Array:\n");
for(i = 0; i < n; i++)
printf("%d\t", a[i]);
getch();
return 0;
}
PAGE NO. 96

OUTPUT:
PAGE NO. 97

26.Implement insertion sort

ALGORITHM:

step 1: start
step 2: declare variablesn,,d,t,flag=0 & array[] of integers
step 3: read n as number of elements read n elements
step 4: set c=0, read n elements as array[c]
step 5: set c=1, repeat step 6 to 10 until c<=n-1
step 6: set t=array[c]
step 7: set d=c-1, repeat step 8 to 9 until d>=0
step 8:
if array[d] > t
set array[d+1] = array[d]
set flag = 1
else
break
if (flag)
set array[d+1] = t;
step 9: d--
step 10: c++
step 11: write sorted list array[c]
step 12: stop

C CODE:

#include <stdio.h>
int main()
{
PAGE NO. 98
int n, array[1000], c, d, t, flag = 0;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d Numbers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 1 ; c <= n - 1; c++)


{
t = array[c];

for (d = c - 1 ; d >= 0; d--)


{
if (array[d] > t)
{
array[d+1] = array[d];
flag = 1;
}
else
break;
}
if (flag)
array[d+1] = t;
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++)


PAGE NO. 99
printf("%d\t", array[c]);

getch();
return 0;
}

OUTPUT:
PAGE NO. 100

27.Implement Quick Sort

C CODE:

#include<stdio.h>
#include<conio.h>
void quick_sort(int, int);
int arr_sort[100];

int main()
{
int i,num;
clrscr();
printf("\nSimple Quick Sort Example - Functions and
Array\n");
printf("Enter The Total Number Of Elements\t");
scanf("%d",&num);
printf("\nEnter %d Elements for Sorting\n", num);
for (i = 0; i <num; i++)
scanf("%d", &arr_sort[i]);

printf("\nYour Data :");


for (i = 0; i < num; i++)
{
printf("\t%d", arr_sort[i]);
}

quick_sort(0,num-1);

printf("\n\nSorted Data :");


PAGE NO. 101
for (i = 0; i < num; i++)
{
printf("\t%d", arr_sort[i]);
}
getch();
return 0;
}

void quick_sort(int f, int l)


{
int i, j, t, p = 0;

if (f < l)
{
p = f;
i = f;
j = l;

while (i < j)
{
while (arr_sort[i] <= arr_sort[p] && i < l)
i++;
while (arr_sort[j] > arr_sort[p])
j--;
if (i < j)
{
t = arr_sort[i];
arr_sort[i] = arr_sort[j];
arr_sort[j] = t;
}
}
PAGE NO. 102
t = arr_sort[p];
arr_sort[p] = arr_sort[j];
arr_sort[j] = t;
quick_sort(f, j - 1);
quick_sort(j + 1, l);
}
}

OUTPUT:

M.A.N

You might also like