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

Data structures using c lab manual

The document outlines a series of lab exercises focused on data structures using C programming. It includes detailed algorithms and code implementations for various operations such as inserting and deleting elements in arrays, implementing stacks and queues, evaluating postfix expressions, and creating linked lists. Each exercise concludes with a result statement indicating successful execution of the program.

Uploaded by

rajsaini088
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Data structures using c lab manual

The document outlines a series of lab exercises focused on data structures using C programming. It includes detailed algorithms and code implementations for various operations such as inserting and deleting elements in arrays, implementing stacks and queues, evaluating postfix expressions, and creating linked lists. Each exercise concludes with a result statement indicating successful execution of the program.

Uploaded by

rajsaini088
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

DATASTRUCTURES USING C

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:

STEP1: Start the program.

STEP2: Declare the variable.

STEP3: Enter the size of the array and enter the elements one by one.

STEP4: Display the array elements before inserting.

STEP5: Inserting a new item between 0 to n positions.

STEP6: Display the array elements after inserting.

STEP7: Enter the position to delete between 0 to n elements.

STEP8: Display the array elements after performing the deletion operations

STEP9: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

int a[20],i,n,k,item;

clrscr();

printf(“Enter the size n of the array \n”);


scanf(“%d”,&n);

printf(“Enter the elements one by one \n”);

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

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

printf(“The array elements before inserting a new is \n”);

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

printf(“%d\n”,a[i]);

printf(“\n Enter the item to insert \n”);

scanf(“%d”,&item);

printf(“Enter the position to insert between 0 and n \n”);

scanf(“%d”,&k);

for(i=n-1;i>=k;i--)

a[i+1]=a[i];

a[k]=item;

n=n+1;

printf(“The array elements after inserting a new elements is \n”);

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();

printf(“Enter the size n of the array \n”);

scanf(“%d”,&n);

printf(“Enter the elements one by one \n”);

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

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

printf(“The array elements before deleting an element is \n”);

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

printf(“%d\n”,a[i]);

printf(“Enter the position to delete between 0 and (n-1) \n”);

scanf(“%d”,&k);

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

a[i]=a[i+1];

n=n-1;

printf(“The array elements is \n”);

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

printf(“%d\n”,a[i]);

getch();

}
OUTPUT:

RESULT:

Thus the above program was executed successfully.

EX NO 2: IMPLEMENTATION OF ARRAY USING ROWMAJOR, COLUMNMAJOR ORDER

AIM:

To implement an array using row major, column major order.

ALGORITHM:

STEP1: Start the program.

STEP2: Declare the variable.

STEP3: Enter the number of row and column in an array.

STEP4: Enter the data one by one.


STEP5: Performing the operations and display the row major order.

STEP6: Display the column major order elements

STEP7: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

int a[20][20],m,n,i,j;

clrscr();

printf(“Give the number of row value \n”);

scanf(“%d”,&m);

printf(“Give the number of column value \n”);

scanf(“%d”,&n);

printf(“Give the value one by one \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:

Thus the above program was executed successfully.

EX NO 3: CREATE A TWO DIMENSIONAL ARRAY WITH 10 ELEMENTS & SEARCH A


PARTICULAR ELEMENT PRINT POSITION & ADDRESS
AIM:

To write a C program to create a two dimensional array with 10 elements and search a
particular element.

ALGORITHM:

STEP1: Start the program.

STEP2: Declare the variables.

STEP3: Enter the number of row and column.

STEP4: Enter the data one by one in row wise.

STEP5: Enter the element to be search.

STEP6: Display the position and address of the searched item.

STEP7: Display the error message if the search item is not present.

STEP8: Stop the program.

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();

printf(“Enter the number of row \n”);

scanf(“%d”,&m);

printf(“Enter the number of column \n”);

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

printf(“Enter the search item \n”);

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(“The search item %d is the position %d %d”,item,i,j);

printf(“\n”);

printf(“\n The address of the search item %d is %d”,item,p);

break;

if(F!=’Y’)

printf(“\n The search item %d is not present in the array”,item);

getch();

}
OUTPUT:

RESULT:

Thus the above program was executed successfully.

Ex no 4: perform the push and pop operations in a stack using array

AIM:

To perform the push and pop operations in a stack using array.

ALGORITHM:

STEP1: Start the program.

STEP2: Declare the variable and initialize the top=-1

STEP3: Display the menu for variable function in a stack

STEP4: Enter any one of the option in a menu bar

STEP5: Using switch statement performing the operations in various cases

STEP6: Display the result of the each case for every operations

STEP7: Stop the program.

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();

printf("Enter the size of STACK [MAX=100]:");

scanf("%d",&n);

printf("STACK OPERATIONS USING ARRAY");

printf("\n\t-----------------------------");

printf("\n 1.push\n 2.pop\n 3.display\n 4.exit\n");

do

printf("Enter the choice \n");

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:

printf("\n EXIT POINT");

break;

default:

printf("\n Please enter a valid choice (1/2/3/4)");

getch();

while(choice!=4);

}
void push()

if(top>=n-1)

printf("\n\t Stack is overflow");

getch();

else

printf("Enter the value to be pushed:");

scanf("\n %d",&x);

top++;

stack[top]=x;

void pop()

if(top<=-1)

printf("\n\t stack is underflow");

else

printf("The popped elements is %d",stack[top]);

top--;
}

void display()

if(top>=0)

printf("\n The elements in STACK");

for(i=top;i>=0;i--)

printf("\n%d",stack[i]);

printf("\n Press next choice");

else

printf("\n The stack is empty");

OUTPUT:
RESULT:

Thus the above program was executed successfully.


Ex no 5: display the reverse of a string using stack

AIM:

To display the reverse of a string using stack.

ALGORITHM:

STEP1: Start the program.

STEP2: Declare the variable and functions used in the program.

STEP3: Getting the string from the input statements.

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.

STEP5: Display the reverse of the given strings.

STEP6: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 20

int element;

int top=-1;

char stack_array[MAX];

void push(char element);

int isempty();

int isfull();

char pop();

void main()

{
char stack_string[30];

int count;

clrscr();

printf(“Enter the string \n”);

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();

void push(char element)

if(isfull())

printf(“\n stack overflow \n”);

else

{
top=top+1;

stack_array [top]=element;

char pop()

if(isempty())

printf(“\n stack underflow \n”);

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:

Thus the above program was executed successfully.

EX NO 6: evaluation of postfix expression

AIM:

To write a C program for evaluation of postfix expression

ALGORITHM:

STEP1: Start the program.

STEP2: Include the header file ctype.h

STEP3: declare the variables

STEP4: defining the functions for the name of push

STEP5: in main functions entering the statements

STEP6: using switch statements performing various cases

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();

printf(“Read the postfix expression \n”);

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;

printf(“\n Give postfix expression:%s\n”,potfix);


printf(“\n Result after evaluation is %d \n”,s[top]);

getch();

OUTPUT:

RESULT:

Thus the above program was executed successfully.

EX NO 7: create a queue containing ten elements and perform an insert,


delete operations using array

AIM:

To write a C program for create a queue containing ten elements and perform an insert, delete
operations using array.

ALGORITHM:

STEP1: Start the program.

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.

STEP5: using switch statements performing the various case

STEP6: each cases performing the different operations

STEP7: display the result for the each case

STEP8: Stop the program

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(“Select any one option \n”);

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:

printf(“Enter 10 data one by one \n”);

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

scanf(“%d”,&q.data[++q.rear]);

break;

case 2:

printf(“\n the data in the queue are \n”);

for(i=q.front+1;i<=q.rear;i++)

printf(“%d\n”,q.data[i]);

break;

case 3:

printf(“\n Enter the data to insert \n”);

scanf(“%d”,&item);

q.data[++q.rear]=item;

break;

case 4:

q.front++;

x=q.data[q.front];

printf(“\n The deleted item is \t%d\n”,x);

break;

}while(opt!=5);

getch();

}
OUTPUT:
RESULT:

Thus the above program was executed successfully.

EX NO 8: implementation of recursive function

AIM:

To write a C program implement a recursive function.

ALGORITHM:

STEP1: Start the program.

STEP2: in a main function to create a function name as fact

STEP3: declare the variables and get the values for the factorial variables

STEP4: call the function using function name fact

STEP5: in function definition using recursive function to calculate the factorial value for a given
value

STEP6: return the value and print them in main function

STEP7: Stop the program

PROGRAM:

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

main()

int fact(int);

int n,f;

clrscr();

printf(“Give the value of n \n”);

scanf(“%d”,&n);

f=fact(n);

printf(“Factorial of the given number is %d”,f);

getch();

return 0;

int fact(int n)

if(n==0)

return 1;

else

return (n*fact(n-1);

OUTPUT:
RESULT:

Thus the above program was executed successfully.

EX NO 9: to create a singly linked list containing at least five elements

AIM:

To creating a singly linked list containing at least five elements with more necessary
assumption.

ALGORITHM:

STEP1: Start the program.

STEP2: creating the structure to assigning the members within the structure

STEP3: in main function declare and assigning the values to the variables

STEP4: using the function statements getting the data.

STEP5: calculate the status and total no of nodes in the list

STEP6: to display the result

STEP7: Stop the program

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

printf(“Enter the data item \n”);

scanf(“%d”,head->num);

if(first!=0)

temp->ptr=head;

temp=head;

else

first=temp=head;

fflush(stdin);

printf(“Do you want to continue(Type 0 or 1)? \n”);

scan(“%d”,&coice);

temp->ptr=0;

/*reset temp to the beginning */


temp=first;

printf(“\n Status of the linked list is \n”);

while(temp!=0)

printf(“%d=>”,temp->num);

count++;

temp=temp->ptr;

printf(“null \n”);

printf(“no.of nodes in the list =%d\n”,count);

getch();

OUTPUT:

RESULT:

Thus the program was executed successfully.


EX NO 10: to delete the first node from the singly linked list

AIM:

To delete the first node that contains an integer data item of a singly linked list

ALGORITHM:

STEP1: Start the program.

STEP2: using the structure and user defined functions to the program

STEP3: in main function declare the variable and assign the values

STEP4: getting no of nodes and each nod from the user.

STEP5: using delete function to delete a first node from the list

STEP6: using display function to display the elements in the list

STEP7: Stop the program

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

typedef struct nodetype

int info;

struct nodetype *next;

}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++)

printf(“Item for node %d:\n”i+1);

scanf(“%d”,&item);

createlist(&start,item);

printf(“The list is:\n”);

display(start);

printf(“\n press any key to delete first node \n”);

getch();

deleteatbeg(&start);

printf(“\n the list after deletion at first is \n”);

display(start);

getch();

void createlist(node**start,int item)

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

printf(“\n deleted item is %d=\n”,temp);

void display(node*start)
{

int n=0;

while(start!=NULL)

printf(“\t %d”,start->info);

n++;

start=start->next;

printf(“\n total number of nodes are:%d”,n);

OUTPUT:

RESULT:

Thus the program was executed successfully.

EX NO 11: binary tree creation

AIM:

To write a C program for implementation of binary tree creation.

ALGORITHM:
STEP1: Start the program.

STEP2: declare the variables

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

STEP6: display the result

STEP7: Stop the program

PROGRAM:

#include<stdio.h>

#include<conio.h>

struct node

int value;

struct node *l,*L;

struct node *y;

}*root=NULL,*temp=NULL;

void insert();

void create();

int flag=1;

void main()

int ch;

clrscr();

printf("\n operation--");

printf("\n 1.insert an element into tree \n");


printf("\n 2.create \n");

printf("\n 3.exit \n");

while(1)

printf("\n Enter your choice:");

scanf("%d",&ch);

switch(ch)

case 1:

insert();

break;

case 2:

create();

break;

case 3:

exit(0);

default:

printf("k choice,please enter correct choice");

break;

void insert()

int data;
printf("Enter data of node to be inserted:");

scanf("%d",&data);

temp=(struct node*)malloc(sizeof(struct node));

temp->value=data;

temp->l=temp->L=NULL;

void create()

create();

if(root==NULL)

root=temp;

getch();

OUTPUT:
RESULT:

Thus the program was executed successfully.

EX NO 12: preorder traversal of binary tree

AIM:

To write a C program for implementation of preorder traversal of binary tree.

ALGORITHM:

STEP1: Start the program.

STEP2: defining a structure node and declaring the structure members

STEP3: to assigning the values for the structure members

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

STEP6: Stop the program

PROGRAM:

#include<stdio.h>

struct tnode

int data;

struct tnode*right;

struct tnode*left;

};

struct tnode *CreateBST(struct tnode*,int);

void preorder(struct tnode*);

int main()

struct tnode *root=NULL;


int choice,item,n,i;

clrscr();

do

printf("\n Binary search tree operations");

printf("\n 1.Creation of BST");

printf("\n 2.Traverse in preorder");

printf("\n 3.exit");

printf("\n Enter choice:");

scanf("%d",&choice);

switch(choice)

case 1:

root=NULL;

printf("\n Binary Search Tree for How many nodes?\n");

scanf("%d",&n);

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

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

scanf("%d",&item);

root=CreateBST(root,item);

printf("\n Binary Search Tree with %d nodes is ready to use!!\n",n);

break;

case 2:
printf("\n Binary Search Tree traversal in preorder \n");

preorder(root);

break;

case 3:

printf("\n Terminating \n");

break;

default:

printf("\n\n invalid option!!! try again!!\n\n");

break;

}while(choice!=3);

return 0;

struct tnode *CreateBST(struct tnode *root,int item)

if(root==NULL)

root=(struct tnode*)malloc(sizeof(struct tnode));

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

printf("Duplicate Element !! not allowed!!! ");

return (root);

void preorder(struct tnode*root)

if(root!=NULL)

printf("%d",root->data);

preorder(root->left);

preorder(root->right);

getch();

OUTPUT:
RESULT:

Thus the program was executed successfully.

EX NO 13: binary search

AIM:

To perform in a C program for implementation of binary searching.

ALGORITHM:

STEP1: Start the program.

STEP2: declare the variables

STEP3: get the elements one by one using array

STEP4: enter the element for the search

STEP5: using the statements to find the searching elements in the array

STEP6: if the element is present print the position of the element

STEP7: otherwise to print the statement as search failed


STEP8: Stop the program

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

int lb,ub,a[10],mid,x,n,i,j,k;

clrscr();

printf(“Enter the maximum number of elements \n”);

scanf(“%d”,&n);

printf(“Enter the elements one by one \n”);

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

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

printf(“Enter the elements to search \n”);

scanf(“%d”,&x);

lb=0;

ub=n-1;

while(lb<=ub)

mid=(lb+ub)/2;

if(a[mid]==x)

printf(“The element is present at %d position”,mid);

break;

}
else

if(a[mid]>k)

ub=mid-1;

else

lb=mid+1;

if(lb>ub)

printf(“\n Search Failed”);

getch();

OUTPUT:

RESULT:

Thus the program was executed successfully.

EX NO 14: insertion sort

AIM:

To write a C program for implementation of insertion sort.

ALGORITHM:

STEP1: Start the program.

STEP2: declaring the variables


STEP3: get the number of elements and getting the each element using array

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

STEP6: Stop the program

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

int i,j,x,k,n,a[10];

clrscr();

printf(“Insertion Sort \n”);

printf(“Give the number of elements \n”);

scanf(“%d”,&n);

printf(“Give the elements one by one \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(“The Sorted list \n”);


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

printf(“%d\t”,a[i]);

getch();

OUTPUT:

RESULT:

Thus the program was executed successfully.

EX NO 15: bubble sort

AIM:

To write a C program for implementation of bubble sort.

ALGORITHM:

STEP1: Start the program.

STEP2: declare the variables

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

STEP5: using if statement to compare and sort the elements

STEP6: to print the sorted list of elements from an array

STEP7: Stop the program

PROGRAM:
#include<stdio.h>

#include<conio.h>

void main()

int i,j,n,a[20],temp;

clrscr();

printf(“Bubble Sort \n”);

printf(“Give the number of data \n”);

scanf(“%d”,&n);

printf(“Give the data one by one \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;

printf(“The Sorted list \n”);

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

printf(“%d\t”,a[i]);
getch();

OUTPUT:

RESULT:

Thus the above program was executed successfully.

EX NO 16: selection sort

AIM:

To write a C program for implementation of selection sort.

ALGORITHM:

STEP1: Start the program.

STEP2: declaring the variables

STEP3: get the n number of elements from an array

STEP4: using for loop statements and if conditions to perform in the selection operation

STEP5: to display in the sorted list of elements

STEP6: Stop the program

PROGRAM:

#include<stdio.h>

#include<conio.h>
void main()

int i,j,n,k,a[10];

clrscr();

printf(“Selection Sort \n”);

printf(“Give the number of data \n”);

scanf(“%d”,&n);

printf(“Give the elements one by one \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;

printf(“The Sorted list \n”);

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

printf(“%d\t”,a[i]);

getch();

}
OUTPUT:

RESULT:

Thus the above program was executed successfully.

You might also like