Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
50 views56 pages

C Programs: Arrays, Queues, Linked Lists

BX4001 RECORD PROGRAM

Uploaded by

Arockia Pravina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views56 pages

C Programs: Arrays, Queues, Linked Lists

BX4001 RECORD PROGRAM

Uploaded by

Arockia Pravina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Ex. No.

1 Develop a program to perform various array operations

Date:

Aim:

Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,a[10],n,d;
clrscr();
printf("Enter the number of elements in array:");
scanf("%d",&n);
printf("Insert element in array:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be deleted in the array:");
scanf("%d",&d);
printf("Element in array after deletion:");
for(i=1;i<=5;i++)
{
if(d==a[i])
{
}
else
printf("\n%d",a[i]);
}
getch();
}
Output:
Enter the number of elements in array:5
Insert element in array:
8
2
5
6
1
Enter the element to be deleted in the array:2
Element in array after deletion:
8
5
6
1
Result:
Ex. No. 2 INFIX TO POSTFIX CONVERSION

Date:

Aim:

Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
char stk[20]={'$'},top=0;
void push(char c);
void pop();
int prec(char c);
void main()
{
int i=0;
char str[20];
clrscr();
printf("Enter the infix expression:\n");
scanf("%s",str);
while(str[i]!='\0')
{
if(toascii(str[i])<=122 &&toascii(str[i]>=97))
printf("%c",str[i]);
else if(str[i]=='(')
push(str[i]);
else if(str[i]==')')
{
while(stk[top]!='(')
pop();
top--;
}
else if(str[i]=='*'|| str[i]=='/')
{
if(prec(str[i])>prec(stk[top]))
push(str[i]);
}
else if(str[i]=='+'||str[i]=='-')
{
if(prec(str[i])>prec(stk[top]))
push(str[i]);
else
{
while(prec(stk[top])>prec(str[i]))
pop();
push(str[i]);
}
}
i++;
}
while(stk[top]!='$')
pop();
getch();
}
void push(char s)
{
top=top+1;
if(top==19)
printf("Full stack");
else
stk[top]=s;
}void pop()
{
if(top==0)
printf("Empty");
else
{
printf("%c",stk[top]);
top--;
}
}
int prec(char c)
{
int pre,pre1;
pre1=toascii(c);
if(pre1==36)
pre=0;
else if(pre1==43 && pre1==45)
pre=1;
else if(pre1==42 && pre1==47)
pre=2;
else if(pre1==40)
pre=0;
return pre;
}
Output:

Enter the infix expression:


A+B
AB+
Result:
Ex. No. 3 Array implementation of Circular Queue ADT

Date:

Aim:

Algorithm:
Program:
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
// Check if the queue is full
int isFull()
{
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
return 0;
}
// Check if the queue is empty
int isEmpty() {
if (front == -1) return 1;
return 0;
}
// Adding an element
void enQueue(int element)
{
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
// Removing an element
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
}
// Q has only one element, so we reset the
// queue after dequeing it. ?
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}

// Display the queue


void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE)
{
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}
int main()
{
// Fails because front = -1
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
// Fails to enqueue because front == 0 && rear == SIZE - 1
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
// Fails to enqueue because front == rear + 1
enQueue(8);
return 0;
}
Output:

Queue is empty !!

Inserted -> 1
Inserted -> 2
Inserted -> 3
Inserted -> 4
Inserted -> 5
Queue is full!!

Front -> 0
Items -> 1 2 3 4 5
Rear -> 4

Deleted element -> 1

Front -> 1
Items -> 2 3 4 5
Rear -> 4

Inserted -> 7
Front -> 1
Items -> 2 3 4 5 7
Rear -> 0

Queue is full!!
Result:
Ex. No. 4 Implementation of Polynomial Manipulation using Linked list

Date:

Aim:

Algorithm:
Program:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int coeff;
int pow;
struct Node* next;
};
void readPolynomial(struct Node** poly)
{
int coeff, exp, cont;
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
*poly = temp;
do{
printf("\n Coeffecient: ");
scanf("%d", &coeff);
printf("\n Exponent: ");
scanf("%d", &exp);
temp->coeff = coeff;
temp->pow = exp;
temp-> next = NULL;
printf("\nHave more terms? 1 for y and 0 for no: ");
scanf("%d", &cont);
if(cont)
{
temp->next = (struct Node*)malloc(sizeof(struct Node));
temp = temp->next;
temp->next = NULL;
}
}while(cont);
}
void displayPolynomial(struct Node* poly)
{
printf("\nPolynomial expression is: ");
while(poly != NULL)
{
printf("%dX^%d", poly->coeff, poly->pow);
poly = poly->next;
if(poly != NULL)
printf("+");
}
}
void addPolynomials(struct Node** result, struct Node* first, struct Node* second)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->next = NULL;
*result = temp;
while(first && second)
{
if(first->pow > second->pow)
{
temp->coeff = first->coeff;
temp->pow = first->pow;
first = first->next;
}
else if(first->pow < second->pow)
{
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
}
else
{
temp->coeff = first->coeff + second->coeff;
temp->pow = first->pow;
first = first->next;
second = second->next;
}
if(first && second)
{
temp->next = (struct Node*)malloc(sizeof(struct Node));
temp = temp->next;
temp->next = NULL;
}
}
while(first || second)
{
temp->next = (struct Node*)malloc(sizeof(struct Node));
temp = temp->next;
temp->next = NULL;
if(first)
{
temp->coeff = first->coeff;
temp->pow = first->pow;
first = first->next;
}
else if(second)
{
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
}
}
}
int main()
{
struct Node* first = NULL;
struct Node* second = NULL;
struct Node* result = NULL;
printf("\nEnter the corresponding data:-\n");
printf("\nFirst polynomial:\n");
readPolynomial(&first);
displayPolynomial(first);
printf("\nSecond polynomial:\n");
readPolynomial(&second);
displayPolynomial(second);
addPolynomials(&result, first, second);
displayPolynomial(result);
return 0;
}

Output:

Enter the corresponding data:-


First polynomial:

Coeffecient: 5
Exponent: 2
Have more terms? 1 for y and 0 for no: 1
Coeffecient: 7
Exponent: 1
Have more terms? 1 for y and 0 for no: 6
Coeffecient: 6
Exponent: 0
Have more terms? 1 for y and 0 for no: 0
Polynomial expression is: 5X^2+7X^1+6X^0
Second polynomial:

Coeffecient: 8
Exponent: 2
Have more terms? 1 for y and 0 for no: 1
Coeffecient: 6
Exponent: 1
Have more terms? 1 for y and 0 for no: 1
Coeffecient: 7
Exponent: 0
Have more terms? 1 for y and 0 for no: 0
Polynomial expression is: 8X^2+6X^1+7X^0
Polynomial expression is: 13X^2+13X^1+13X^0
Result:

Ex. No. 5 Implementation of Singly Linked List

Date:

Aim:
Algorithm:

Program:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct nodetype
{
int info;
struct nodetype *next;
}
*head=NULL,*temp,*cur;
typedef struct nodetype node;
int ch,i=1,pos;
void insert_first()
{
temp=(node*)malloc(sizeof(node));
printf("\nEnter the value for newnode:");
scanf("%d",&temp->info);
temp->next=head;
head=temp;
}
void insert_last()
{
cur=head;
while(cur->next!=NULL)
cur=cur->next;
temp=(node*)malloc(sizeof(node));
printf("\nEnter the value for newnode:");
scanf("%d",&temp->info);
temp->next=cur->next;
cur->next=temp;
}
void insert_mid()
{
cur=head;
printf("\nEnter the position in which newnode to be insert:");
scanf("%d",&pos);
while(i!=pos-1&&cur!=NULL)
{
cur=cur->next;
i++;
}
if(i==pos-1)
{
temp=(node*)malloc(sizeof(node));
printf("\nEnter the value to insert:");
scanf("%d",&temp->info);
temp->next=cur->next;
cur->next=temp;
}
else
{
printf("\n The position is not available");
}
}
void delete_first()
{
if(head!=NULL)
{
temp=head;
head=head->next;
printf("\n The value %d is Successfully deleted",temp->info);
free(temp);
}
else
printf("\nList is Empty");
}
void delete_last()
{
if(head!=NULL)
{
cur=head;
while(cur->next->next!=NULL)
cur=cur->next;
temp=cur->next;
cur->next=NULL;
printf("\n The value %d is Successfully deleted",temp->info);
free(temp);
}
else
printf("\n List is Empty");
}
void delete_mid()
{
if(head!=NULL)
{
cur=head;
printf("\nEnter the position of node to be delete:");
scanf("%d",&pos);
while(i!=pos-1&&cur->next!=NULL)
{
cur=cur->next;
i++;
}
temp=cur->next;
cur->next=temp->next;
printf("\n The value %d is Successfully deleted",temp->info);
free(temp);
}
else
printf("\nList is Empty");
}
void display()
{
temp=head;
if(temp==NULL)
printf("\nList is empty");
else
{
printf("\nList Contains Are:");
while(temp!=NULL)
{printf("%d->",temp->info);
temp=temp->next;}
printf("NULL");
printf("\n");
}
}
int main()
{
clrscr();
head=NULL;
printf("\n Linked List");
printf("\n---------------");
printf("\n1.Insert First");
printf("\n2.Insert Last");
printf("\n3.Insert Middle");
printf("\n4.Delete First");
printf("\n5.Delete Last");
printf("\n6.Delete Middle");
printf("\n7.Display");
printf("\n8.Exit");
do{
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_first();
break;
case 2:
insert_last();
break;
case 3:
insert_mid();
break;
case 4:
delete_first();
break;
case 5:
delete_last();
break;
case 6:
delete_mid();
break;
case 7:
display();
break;
case 8:
return 0;
default:
printf("\nEnter your correct choice");
}
}
while(ch!=8);
getch();
}

Output:

Linked List
---------------
1.Insert First
2.Insert Last
3.Insert Middle
4.Delete First
5.Delete Last
6.Delete Middle
7.Display
8.Exit
Enter your choice:1
Enter the value for newnode:78
Enter your choice:1
Enter the value for newnode:34
Enter your choice:2
Enter the value for newnode:56
Enter your choice:3
Enter the position in which newnode to be insert:2
Enter the value to insert:45
Enter your choice:7
List Contains Are:34->45->78->56->NULL
Enter your choice:4
The value 34 is Successfully deleted
Enter your choice:6
Enter the position of node to be delete:2
The value 78 is Successfully deleted
Enter your choice:5
The value 56 is Successfully deleted
Enter your choice:7
List Contains Are:45->NULL
Enter your choice:8
Result:

Ex. No. 6 Binary Search

Date:

Aim:
Algorithm:

Program:
#include<stdio.h>
int main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter array element(ascending order)\n");
for(i=0;i<n;++i)
scanf("%d",&arr[i]);
printf("\nEnter the element to search:");
scanf("%d",&x);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid]){
flag=1;
break;
}
else
if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
if(flag==1)
printf("\nElement found at position %d",mid+1);
else
printf("\nElement not found");
return 0;
}

Output:

Enter size of array:5

Enter array element(ascending order)


2
4
5
7
9

Enter the element to search:7

Element found at position 4

Ex. No. 7 Bubble Sort

Date:

Aim:
Algorithm:

Program:
#include<stdio.h>
int main()
{
int a[50],n,i,j,temp;
clrscr();
printf("Enter the size of array");
scanf("%d",&n);
printf("Enter the array of elelments:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\n Array after sorting:");
for(i=0;i<n;++i)
printf("%d\t",a[i]);
return 0;
}

Output:

Enter the size of array5


Enter the array of elelments:
9
1
7
2
4

Array after sorting:1 2 4 7 9


Result:

Ex. No. 8 Selection Sort

Date:

Aim:
Algorithm:

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int size,i,j,temp,list[100];
clrscr();
printf("Enter the size of the List: ");
scanf("%d",&size);
printf("Enter %d integer values: ",size);
for(i=0; i<size; i++)
scanf("%d",&list[i]);
//Selection sort logic
for(i=0; i<size; i++){
for(j=i+1; j<size; j++){
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}

printf("List after sorting is: ");


for(i=0; i<size; i++)
printf(" %d",list[i]);

getch();
}

Output:
Enter the size of the List: 5
Enter 5 integer values:
9
2
6
3
5
List after sorting is: 2 3 5 6 9
Result:

Ex. No. 9 Insertion sort

Date:

Aim:
Algorithm:

Program:
#include<stdio.h>
#include<conio.h>
void main(){
int size, i, j, temp, list[100];
printf("Enter the size of the list: ");
scanf("%d", &size);
printf("Enter %d integer values: ", size);
for (i = 0; i < size; i++)
scanf("%d", &list[i]);
//Insertion sort logic
for (i = 1; i < size; i++) {
temp = list[i];
j = i - 1;
while ((temp < list[j]) && (j >= 0)) {
list[j + 1] = list[j];
j = j - 1;
}
list[j + 1] = temp;
}

printf("List after Sorting is: ");


for (i = 0; i < size; i++)
printf(" %d", list[i]);

getch();
}

Output:

Enter the size of the list: 5


Enter 5 integer values:
9
2
8
3
4
List after Sorting is: 2 3 4 8 9
Result:

Ex. No. 10 Implementation of Binary Search Trees

Date:

Aim:
Algorithm:

Program:
#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
/* To insert a node in the tree */
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
/* To create a node */
void create()
{
int data;
printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
temp->value = data;
temp->l = temp->r = NULL;
}
/* Function to search the appropriate position to insert the new node */
void search(struct btnode *t)
{
if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert
at right */
search(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value
insert at left */
search(t->l);
else if ((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}
void inorder(struct btnode *t) /* recursive function to perform inorder traversal of tree */
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
printf("%d -> ", t->value);
if (t->r != NULL)
inorder(t->r);
}
void delete() /* To check for the deleted node */
{
int data;
if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the data to be deleted : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}
/* To find the preorder traversal */
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
printf("%d -> ", t->value);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
/* To find the postorder traversal */
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->value);
}
/* Search for the appropriate position to insert the new node */
void search1(struct btnode *t, int data)
{
if ((data>t->value))
{
t1 = t;
search1(t->r, data);
}
else if ((data < t->value))
{
t1 = t;
search1(t->l, data);
}
else if ((data==t->value))
{
delete1(t);
}
}
/* To delete a node */
void delete1(struct btnode *t)
{
int k;
/* To delete leaf node */
if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{
t1->l = NULL;
}
else
{
t1->r = NULL;
}
t = NULL;
free(t);
return;
}
/* To delete node having one left hand child */
else if ((t->r == NULL))
{
if (t1 == t)
{
root = t->l;
t1 = root;
}
else if (t1->l == t)
{
t1->l = t->l;
}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
/* To delete node having right hand child */
else if (t->l == NULL)
{
if (t1 == t)
{
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
}
/* To delete node having two child */
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{
k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value = k;
}
}
/* To find the smallest element in the right sub tree */
int smallest(struct btnode *t)
{
t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value);
}
/* To find the largest element in the left sub tree */
int largest(struct btnode *t)
{
if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
else
return(t->value);
}

Output:

OPERATIONS ---
1 - Insert an element into tree
2 - Delete an element from the tree
3 - Inorder Traversal
4 - Preorder Traversal
5 - Postorder Traversal
6 - Exit

Enter your choice : 1


Enter data of node to be inserted : 34

Enter your choice : 1


Enter data of node to be inserted : 89

Enter your choice : 1


Enter data of node to be inserted : 23

Enter your choice : 3


23 -> 34 -> 89 ->
Enter your choice : 4
34 -> 23 -> 89 ->
Enter your choice : 5
23 -> 89 -> 34 ->
Enter your choice : 2
Enter the data to be deleted : 34

Enter your choice : 3


23 -> 89 ->
Enter your choice : 6
Result:

Ex. No. 11 Graph representation and Traversal algorithms

Date:

Aim:

Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
/* ADJACENCY MATRIX */
int source,V,E,time,visited[20],G[20][20];
void DFS(int i)
{
int j;
visited[i]=1;
printf(" %d->",i+1);
for(j=0;j<V;j++)
{
if(G[i][j]==1&&visited[j]==0)
DFS(j);
}
}
int main()
{
int i,j,v1,v2;
clrscr();
printf("\t\t\tGraphs\n");
printf("Enter the no of edges:");
scanf("%d",&E);
printf("Enter the no of vertices:");
scanf("%d",&V);
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
G[i][j]=0;
}
/* creating edges :P */
for(i=0;i<E;i++)
{
printf("Enter the edges (format: V1 V2) : ");
scanf("%d%d",&v1,&v2);
G[v1-1][v2-1]=1;

for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
printf(" %d ",G[i][j]);
printf("\n");
}
printf("Enter the source: ");
scanf("%d",&source);
DFS(source-1);
return 0;
}

Output:

Graphs
Enter the no of edges:5
Enter the no of vertices:5
Enter the edges (format: V1 V2) : 1 2
Enter the edges (format: V1 V2) : 1 3
Enter the edges (format: V1 V2) : 2 4
Enter the edges (format: V1 V2) : 3 5
Enter the edges (format: V1 V2) : 4
5
0 1 1 0 0
0 0 0 1 0
0 0 0 0 1
0 0 0 0 1
0 0 0 0 0
Enter the source: 1
1-> 2-> 4-> 5-> 3->
Result:

You might also like