Practical File of DS
Practical File of DS
INDEX
S.No: Practical name Signature
1. WAP to generate Fibonacci Series using recursion.
2. Write a function that interchanges the first element
with last element, second element with second last
element and so on.
3. WAP to multiply two Matrices.
4. Write a Function that removes all duplicate
elements from an Array.
5. WAP that insert an element in beginning of Linear
Link List.
6. WAP that delete an element from the beginning of
the Linear Link List.
7. WAP that delete an element from the end of the
Linear Link List.
8. WAP that delete an element after a given element
of the given Linear Link List.
9. WAP that reverse the element of the Linear Link
List.
10. WAP that concatenate two Linear Linked List.
11. WAP to remove the Top element of Stack.
12. WAP to insert (or push) an element at the Top of
Stack.
13. WAP to insert an element at the end of queue.
14. WAP to remove the first element of the queue.
15. WAP to illustrate the implementation of Binary
Search Tree
16. WAP to sort an array of integer in Ascending Order
using Bubble Sort.
17. WAP to sort an array of integer in Ascending Order
using Insertion Sort.
18. WAP to sort an array of integer in Ascending Order
using Quick Sort.
19. WAP to search an element using Linear Search
Method
20. WAP to search an element using Binary Search
Method.
21. WAP to check weather number is palindrome or
not.
22. WAP to insert an element in array
23. WAP to delete an element from array.
24. WAP to add to two matrix.
25. WAP to subtract two matrix.
26. WAP to implement string operation in c.
OUTPUT
2.Write a function that interchanges the first element
with last element, second element with second last
element and so on.
INPUT
#include <stdio.h>
void Array_Swap(int *array , int n)
{
int i=0,temp=0;
for(i=0 ; i<n/2 ; i++)
{
temp = array[i];
array[i] = array[n-i-1];
array[n-i-1] = temp;
}
}
int main()
{
int array_1[30] = {0};
int i=0 ,n=0;
printf("\nEnter the number of elements for the array : ");
scanf("%d",&n);
printf("\nEnter the elements for array_1..\n");
for(i=0 ; i<n ; i++)
{
printf("array_1[%d] : ",i);
scanf("%d",&array_1[i]);
}
Array_Swap(array_1 , n);
printf("\nThe array after swap is..\n");
for(i=0 ; i<n ; i++)
{
printf("\narray_1[%d] : %d",i,array_1[i]);
}
return 0;
}
OUTPUT
OUTPUT
OUTPUT
5.WAP that insert an element in the beginning of Linear
linked list.
INPUT
#include <stdio.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void createNodeList(int n);
void NodeInsertatBegin(int num);
void displayList();
int main()
{
int n,num;
printf("\n\n Linked List : Insert a new node at the beginning of a Singly
Linked List:\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input data to insert at the beginning of the list : ");
scanf("%d", &num);
NodeInsertatBegin(num);
printf("\n Data after inserted in the list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL;
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void NodeInsertatBegin(int num)
{
struct node *fnNode;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
fnNode->num = num;
fnNode->nextptr = stnode;
stnode = fnNode;
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}
OUTPUT
6.WAP to delete an element an element from beginning of
linear linked list.
INPUT
#include<stdio.h>
#include<stdlib.h>
typedef struct nodetype
{ int info;
struct nodetype *next;
} node ;
void linked_list(node **,int );
void deletion_at_beg(node **);
void display(node *);
void main()
{
node *start;
int item,n,p;
start=NULL;
printf("Enter number of nodes :\n");
scanf("%d",&n);
for(p= 0 ;p< n ;p++)
{
printf("Enter item for node %d :\n",p+1);
scanf("%d",&item);
linked_list(&start,item) ;
}
printf("The list is like that after inserting element :\n");
display(start);
printf("\nPress any key to delete first node");
deletion_at_beg(&start);
printf("\nThe list after the deletion at first node is :\n");
display(start);
}
void linked_list(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 deletion_at_beg(node **start)
{
node *ptr;
int temp;
ptr = *start ;
temp = ptr->info;
*start = ptr->next ;
free(ptr);
printf("\nDeleted item of first node is = %d",temp);
}
void display(node *start)
{
int n = 0;
while(start !=NULL)
{
printf("\t %d",start->info);
n++;
start = start->next;
}
printf("\nTotal number of nodes : %d",n);
}
OUTPUT
7.WAP to delete an element from the end of linear linked
list.
INPUT
#include <stdio.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void deleteLastNode();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete last node: ");
scanf("%d", &choice);
if(choice == 1)
deleteLastNode();
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void deleteLastNode()
{
struct node *toDelete, *secondLastNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
secondLastNode = head;
while(toDelete->next != NULL)
{
secondLastNode = toDelete;
toDelete = toDelete->next;
}
if(toDelete == head)
{
head = NULL;
}
else
{
secondLastNode->next = NULL;
}
free(toDelete);
printf("SUCCESSFULLY DELETED LAST NODE OF LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
OUTPUT
OUTPUT
for(i=2;i<=n;i++)
{
printf("Enter the element to be inserted : ");
scanf("%d",&data);
start=addatend(start,data);
}
return start;
}
void display(struct node *start)
{
struct node *p;
if(start==NULL)
{
printf("List is empty\n");
return;
}
p=start;
while(p!=NULL)
{
printf("%d ", p->info);
p=p->link;
}
printf("\n");
}
struct node *addatbeg(struct node *start,int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}
struct node *addatend(struct node *start, int data)
{
struct node *p,*tmp;
tmp= (struct node *)malloc(sizeof(struct node));
tmp->info=data;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=tmp;
tmp->link=NULL;
return start;
}
OUTPUT
}Stack;
void init(Stack *s)
{
s->TOP = -1;
}
int isFull(Stack *s)
{
if(s->TOP == MAX-1)
return 0;
return -1;
}
int isEmpty(Stack *s)
{
if(s->TOP == -1)
return 0;
return -1;
}
void push(Stack *s, int item)
{
if( !isFull(s) )
{
printf("\nStack is full");
return;
}
s->TOP = s->TOP + 1;
s->ele[s->TOP] = item;
}
int pop(Stack *s, int *item)
{
if(!isEmpty(s))
{
printf("\nStack is empty");
return -1;
}
*item = s->ele[s->TOP];
s->TOP = s->TOP - 1;
return 0;
}
int main()
{
Stack s;
int item;
init(&s);
push(&s,10);
push(&s,20);
push(&s,30);
pop(&s,&item);
printf("\nPoped Item : %d",item);
pop(&s,&item);
printf("\nPoped Item : %d",item);
pop(&s,&item);
printf("\nPoped Item : %d",item);
return 0;
return 0;
}
OUTPUT
12. Write a program to insert an element at the top of stack.
INPUT
#include <stdio.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
void display(void);
int pop(void);
void main ()
{
int choice;
s.top = -1;
do
{
push();
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &choice);
}while(choice==1);
pop();
display(); }
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to push\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n Current stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
} printf ("\n");}
OUTPUT
OUTPUT
OUTPUT
OUTPUT
16. WAP to sort an array of integer in Ascending Order
using Bubble Sort
INPUT
#include<stdio.h>
#define MAX 5
void bubble_sort(int arr[]);
int main(void)
{
int arr[MAX];
for(int i = 0; i < MAX; i++)
{
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\nUnsorted array: \n");
for(int i = 0; i < MAX; i++)
{
printf("%d ", arr[i]);
}
bubble_sort(arr);
printf("\n\nSorted array: \n");
for(int i = 0; i < MAX; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
void bubble_sort(int arr[])
{
int tmp,
is_swapped;
for(int i = 0; i < MAX; i++)
{
is_swapped = 0;
for(int j = 0; j < MAX - 1 - i; j++)
{
if(arr[j] > arr[j+1])
{
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
is_swapped = 1;
}
}
if (!is_swapped)
{
break;
}
}
}
OUTPUT
OUPUT
OUTPUT
21.WAP for Palindrome in c.
INPUT
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
OUTPUT
OUTPUT
OUPUT
OUTPUT
OUTPUT
26. Implementation of string operation in c.
INPUT
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}
OUTPUT