Data structures using c lab manual
Data structures using c lab manual
LAB EXERCISES
EX NO 1: PROGRAM TO INSERT DELETE AN ELEMENT IN AN ARRAY PRINT THE
POSITION OF PARTICULAR ELEMENT
AIM:
To write a C program to perform an insert, delete operation in an array also print the position
of a particular element.
ALGORITHM:
STEP3: Enter the size of the array and enter the elements one by one.
STEP8: Display the array elements after performing the deletion operations
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int a[20],i,n,k,item;
clrscr();
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
scanf(“%d”,&item);
scanf(“%d”,&k);
for(i=n-1;i>=k;i--)
a[i+1]=a[i];
a[k]=item;
n=n+1;
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
getch();
}
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int a[20],i,n,k,item;
clrscr();
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
scanf(“%d”,&k);
for(i=k;i<n;i++)
a[i]=a[i+1];
n=n-1;
for(i=0;i<;i++)
printf(“%d\n”,a[i]);
getch();
}
OUTPUT:
RESULT:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int a[20][20],m,n,i,j;
clrscr();
scanf(“%d”,&m);
scanf(“%d”,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“ROWMAJOR \n”);
for(i=0; i<m;i++)
for(j=0;j<n;j++)
printf(“%d\t”,a[i][j]);
printf(“\n”);
printf(“COLUMNMAJOR \n”);
for(i=0; i<m;i++)
for(j=0;j<n;j++)
printf(“%d\t”,a[j][i]);
printf(“\n”);
getch();
OUTPUT:
RESULT:
To write a C program to create a two dimensional array with 10 elements and search a
particular element.
ALGORITHM:
STEP7: Display the error message if the search item is not present.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int a[20][20],i,j,m,n,item;
char F;
int *p=&a[0][0];
clrscr();
scanf(“%d”,&m);
scanf(“%d”,&n);
printf(“Enter the data one by one in rowwise \n”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
scanf(“%d”,&item);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(a[i][j]==item)
p=p+(i*n+j)+16;
F=’Y’;
printf(“\n”);
break;
if(F!=’Y’)
getch();
}
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP6: Display the result of the each case for every operations
PROGRAM:
#include<stdio.h>
#include<conio.h>
int stack[100],choice,n;
int top=-1;
int x,i;
void push();
void pop();
void display();
void main()
clrscr();
scanf("%d",&n);
printf("\n\t-----------------------------");
do
scanf("\n %d",&choice);
switch(choice)
case 1:
push();
printf("\n");
break;
case 2:
{
pop();
printf("\n");
break;
case 3:
display();
printf("\n");
break;
case 4:
break;
default:
getch();
while(choice!=4);
}
void push()
if(top>=n-1)
getch();
else
scanf("\n %d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
top--;
}
void display()
if(top>=0)
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
else
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP4: Calculate the length of the string and to perform a reversing the given string by using
stack with various functions used in the stack conditions.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
int element;
int top=-1;
char stack_array[MAX];
int isempty();
int isfull();
char pop();
void main()
{
char stack_string[30];
int count;
clrscr();
scanf(“%s”,stack_string);
for(count=0;count<strlen(stack_string);count++)
push(stack_string[count]);
for(count=0;count<strlen(stack_string);count++)
stack_string[count]=pop();
printf(“%s”,stack_string);
printf(“\n”);
getch();
if(isfull())
else
{
top=top+1;
stack_array [top]=element;
char pop()
if(isempty())
return 1;
else
element=stack_array[top];
top=top-1;
return element;
int isempty()
if(top==-1)
return 1;
else
return 0;
}
int isfull()
if(top==MAX-1)
return 1;
else
return 0;
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP7: getting the postfix expression and display the evaluation of the given expression
STEP8: Stop the program
PROGRAM:
#define SIZE 50
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int s[SIZE];
int top=-1;
push(int elem)
s[++top]=elem;
return 0;
int pop()
return(s[top--]);
void main()
char postfix[50],ch;
int i=0,op1,op2;
clrscr();
scanf(“%s”,postfix);
while((ch=postfix[i++])!=’\0’)
{
if(isdigit(ch))
push(ch-‘0’);
else
{
op2=pop();
op1=pop();
switch(ch)
case ‘+’:
push(op1+op2);
break;
case ‘-’:
push(op1-op2);
break;
case ‘*’:
push(op1*op2);
break;
case ‘/’:
push(op1/op2);
break;
getch();
OUTPUT:
RESULT:
AIM:
To write a C program for create a queue containing ten elements and perform an insert, delete
operations using array.
ALGORITHM:
STEP2: create a structure for a name of queue and declaring an members of a queue.
STEP3: in main functions declare the structure variables and accessing a structure members
STEP4: entering the various statements for create insert, list, delete, exit.
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct queue
int data[50];
int front;
int rear;
};
void main()
int i,x,opt,item;
struct queue q;
q.front=-1;
q.rear=-1;
clrscr();
do
printf(“1.create \n”);
printf(“2.list \n”);
printf(“3.insert \n”);
printf(“4.delete \n”);
printf(“5.exit \n”);
scanf(“%d”,&opt);
switch(opt)
{
case 1:
for(i=0;i<10;i++)
scanf(“%d”,&q.data[++q.rear]);
break;
case 2:
for(i=q.front+1;i<=q.rear;i++)
printf(“%d\n”,q.data[i]);
break;
case 3:
scanf(“%d”,&item);
q.data[++q.rear]=item;
break;
case 4:
q.front++;
x=q.data[q.front];
break;
}while(opt!=5);
getch();
}
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP3: declare the variables and get the values for the factorial variables
STEP5: in function definition using recursive function to calculate the factorial value for a given
value
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
int fact(int);
int n,f;
clrscr();
scanf(“%d”,&n);
f=fact(n);
getch();
return 0;
int fact(int n)
if(n==0)
return 1;
else
return (n*fact(n-1);
OUTPUT:
RESULT:
AIM:
To creating a singly linked list containing at least five elements with more necessary
assumption.
ALGORITHM:
STEP2: creating the structure to assigning the members within the structure
STEP3: in main function declare and assigning the values to the variables
PROGRAM:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void main()
struct node
int num;
struct node*ptr;
};
typedef struct node NODE;
NODE *head,*first,*temp=0;
int count=0;
int choice=1;
first=0;
while(choice)
head=(NODE*)malloc(sizeof(NODE));
scanf(“%d”,head->num);
if(first!=0)
temp->ptr=head;
temp=head;
else
first=temp=head;
fflush(stdin);
scan(“%d”,&coice);
temp->ptr=0;
while(temp!=0)
printf(“%d=>”,temp->num);
count++;
temp=temp->ptr;
printf(“null \n”);
getch();
OUTPUT:
RESULT:
AIM:
To delete the first node that contains an integer data item of a singly linked list
ALGORITHM:
STEP2: using the structure and user defined functions to the program
STEP3: in main function declare the variable and assign the values
STEP5: using delete function to delete a first node from the list
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int info;
}node;
void createlist(node**,int);
void deleteatbeg(node**);
void display(node*);
void main()
{
node *start;
int item,n,i;
start=NULL;
clrscr();
printf(“Enter no of nodes:\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&item);
createlist(&start,item);
display(start);
getch();
deleteatbeg(&start);
display(start);
getch();
node *ptr,*last;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
ptr->next=NULL;
if(*start==NULL)
*start=ptr;
else
last=*start;
while(last->next!=NULL)
last=last=>next;
last->next=ptr;
void deleteatbeg(node*start)
node*ptr;
int temp;
ptr=*start;
temp=ptr->info;
*start=ptr->next;
free(ptr);
void display(node*start)
{
int n=0;
while(start!=NULL)
printf(“\t %d”,start->info);
n++;
start=start->next;
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP1: Start the program.
STEP3: using the structure and assigning the values to the variables
STEP4: using the switch case statements for various functions insert,create()
STEP5: defining the above functions for the given functions declarations
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct node
int value;
}*root=NULL,*temp=NULL;
void insert();
void create();
int flag=1;
void main()
int ch;
clrscr();
printf("\n operation--");
while(1)
scanf("%d",&ch);
switch(ch)
case 1:
insert();
break;
case 2:
create();
break;
case 3:
exit(0);
default:
break;
void insert()
int data;
printf("Enter data of node to be inserted:");
scanf("%d",&data);
temp->value=data;
temp->l=temp->L=NULL;
void create()
create();
if(root==NULL)
root=temp;
getch();
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP4: In main function using if else statements to insert a data on left and right side of the tree
STEP5: call the pre order function to arrange the tree elements in preorder
PROGRAM:
#include<stdio.h>
struct tnode
int data;
struct tnode*right;
struct tnode*left;
};
int main()
clrscr();
do
printf("\n 3.exit");
scanf("%d",&choice);
switch(choice)
case 1:
root=NULL;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&item);
root=CreateBST(root,item);
break;
case 2:
printf("\n Binary Search Tree traversal in preorder \n");
preorder(root);
break;
case 3:
break;
default:
break;
}while(choice!=3);
return 0;
if(root==NULL)
root->left=root->right=NULL;
root->data=item;
return root;
else
if(item<root->data)
root->left=CreateBST(root->left,item);
else if(item>root->data)
root->right=CreateBST(root->right,item);
else
return (root);
if(root!=NULL)
printf("%d",root->data);
preorder(root->left);
preorder(root->right);
getch();
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP5: using the statements to find the searching elements in the array
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int lb,ub,a[10],mid,x,n,i,j,k;
clrscr();
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
scanf(“%d”,&x);
lb=0;
ub=n-1;
while(lb<=ub)
mid=(lb+ub)/2;
if(a[mid]==x)
break;
}
else
if(a[mid]>k)
ub=mid-1;
else
lb=mid+1;
if(lb>ub)
getch();
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP4: using while loop statements and comparing the variables to sort the given elements
STEP5: using the statements to find the searching elements in the array
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,x,k,n,a[10];
clrscr();
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
k=a[i];
j=i-1;
a[j+1]=k;
printf(“%d\t”,a[i]);
getch();
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP3: using for loop and scanf function getting elements one by one
STEP4: using for loop statements to found the scan and compare the data
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n,a[20],temp;
clrscr();
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
temp=a[i];
a[i]=a[j+1];
a[j+1]=temp;
for(i=0;i<n;i++)
printf(“%d\t”,a[i]);
getch();
OUTPUT:
RESULT:
AIM:
ALGORITHM:
STEP4: using for loop statements and if conditions to perform in the selection operation
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n,k,a[10];
clrscr();
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
k=a[i];
a[i]=a[j];
a[j]=k;
for(i=0;i<n;i++)
printf(“%d\t”,a[i]);
getch();
}
OUTPUT:
RESULT: