Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Practical Software Lab BCA 205

1.1 AIM: Write a program for inserting an element into an array.

DEFINITION:-

ARRAY
An array can be defined as a finite collection of homogeneous element.

This means that an array can store either all integers, all floating point number, all
characters or anyother complex datatype but all of same type.
The individual elements within the array can be designated by an index. An index is also
called asubscript.
Array Elements
For Example:-

Array

Array Indexes 0 1 2 3 5 6

INSERTION
An element can be inserted in an array at a particular
location.

ALGORITHM

1. Start
2. Create an array of a desire datatype and size
3. Initialize a variable i as 0
4. Enter the element at ith index of the array
5. Increment i by 1
6. Repeat steps 4 and 5 until the end of the array
7. Stop

CODING

include<stdio.h>

int main()

int student[40],pos,i,size,value;

COMPUTER APPLICATION, DPG ITM, GURUGRAM 1


Practical Software Lab BCA 205

printf("enter no of elements in array of students:");


scanf("%d",&size);

printf("enter %d elements are: ",size);

for(i=0;i<size;i++)
scanf("%d",&student[i]);
printf("enter the position where you want to insert the element:");
scanf("%d",&pos);
printf("enter the value into that poition:");
scanf("%d",&value);
for(i=size-1;i>=pos-1;i--)
student[i+1]=student[i];
student[pos-1]= value;
printf("final array after inserting the value is ");

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

OUTPUT

COMPUTER APPLICATION, DPG ITM, GURUGRAM 2


Practical Software Lab BCA 205

1.2 AIM: Write a program to delete an element from an array.

DEFINITION :-

ARRAY
An array can be defined as an finite collection of homogeneous element.
This means that an array can store either all integers, all floating point number, all
characters orany other complex datatype but all of same type.

The individual elements within the array can be designated by an index. An index is also
called asubscript.
For Example: -

DELETION
It is the operation that removes an element from a given location of a list.

ALGORITHM
LA is a linear array with an elements and K is a positive integer such
that K<=N.We have to delete an array available at the kth position of
LA.
1. Start
2. Set J=K
3. Repeat steps 4 and 5 while J<N
4. Set LA [J] = LA[J+1]
5. Set J = J+1
6. Set N = N-1
7. Stop

COMPUTER APPLICATION, DPG ITM, GURUGRAM 3


Practical Software Lab BCA 205

CODING

#include <stdio.h>
#include <conio.h>
int main()
{
int arr[50];
int pos, i, num;
printf (" \n Enter the number of elements in an array: \n ");
scanf (" %d", &num);
printf (" \n Enter %d elements in array: \n ", num);
for (i = 0; i < num; i++ )
{ printf (" arr[%d] = ", i);
scanf (" %d", &arr[i]);
}
printf( " Define the position of the array element where you want to delete: \n ");
scanf (" %d", &pos);
if (pos >= num+1)
{ printf (" \n Deletion is not possible in the array.");
} else
{
for (i = pos - 1; i < num -1; i++)

arr[i] = arr[i+1];

}
printf (" \n The resultant array is: \n");

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


{
printf (" arr[%d] = ", i);
printf (" %d \n", arr[i]);
}
}

COMPUTER APPLICATION, DPG ITM, GURUGRAM 4


Practical Software Lab BCA 205

OUTPUT

COMPUTER APPLICATION, DPG ITM, GURUGRAM 5


Practical Software Lab BCA 205

2. AIM: Write a program in C to print the binary search no.

DEFINITION
Binary Search is a searching algorithm for finding an element's position in a sorted array. In
this approach, the element is always searched in the middle of a portion of an array.
Binary search can be implemented only on a sorted list of items. If the elements are not sorted
already, we need to sort them first.

Algorithm:-
Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
go to step 6
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1

print "value is not present in the array"


[end of if]
Step 6: exit

COMPUTER APPLICATION, DPG ITM, GURUGRAM 6


Practical Software Lab BCA 205

CODING
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(array[mid] < key) low = mid + 1;
else if (array[mid] == key)
{ printf("%d found at location %d.n", key, mid+1); break;

}
else high = mid - 1;
mid = (low + high)/2;
}

if(low > high)

printf("Not found! %d isn't present in the list.n", key);


return 0;
}

COMPUTER APPLICATION, DPG ITM, GURUGRAM 7


Practical Software Lab BCA 205

Output

IF THE KEY IS PRESENT:

IF THE KEY IS NOT PRESENT:

COMPUTER APPLICATION, DPG ITM, GURUGRAM 8


Practical Software Lab BCA 205

3. AIM: Write a program in C for implementation of linked list using array.

LINKED LIST
Link structures are a completely different way to represent a list of items. Each elements of
the listis made to point to the next element in the list.
An element in the list is called a
node. The Node have two part’s :
Data and Next
Data parts contain the information about the element.
Next part contains a pointer to the next elements or a node in the list.

A list consisting four names i.e. A, B, C and D is represented using link structures
such a list iscalled linked list or a linear linked list.

CREATION OF A LINKED LIST


A node of a linked list is essentially a structure because its contains data of different
types. Information parts contains a pointer that can point to a node i.e. to itself or some
other node suchstructure are called self referential structure.

ALGORITHM
1. Create a class node which has two attributes data and next. Next is a pointer to
the nextnode in the linked list.
2. Create another class which has two attributes head and tail.
3. Add node function will add a new node to the list.
a) Create a new node.
b) It first checks, whether the head = NULL
which means the list is empty.
c) If the list is empty, both head and tail will
point to the newly added node.
d) List ≠ empty the new node will be added to
end of the list such that tails next will
point to a newly added node. This new node will become the new tail of the list.
4. Repeat step 2 and 3 till whole of list is constructed.
5. Point tail = NULL
6. Stop

COMPUTER APPLICATION, DPG ITM, GURUGRAM 9


Practical Software Lab BCA 205

CODING

#include<stdio.h>

#include<conio.h>

#define TRUE 1
#define SIZE 10
struct link
{ int info;
int next; };
struct link node[SIZE];
int Getnode();
void Createlist();
void Freenode(int);
void Display();
void Insert(int,int);
void Delete(int);
int p, avail=0;
void main()
{ int
.ch=1,i,n,x;
clrscr();
/*Creation of available list*/
for(i=0;i<SIZE-1;i++)
node[i].next=i+1;
node[SIZE-1].next=-1;
printf("\n Create a List:");
Createlist();

while(ch!=4)
{
printf("\n1-DISPLAY");
printf("\n2-INSERT");
printf("\n3-DELETE");
printf("\n4-QUIT");
printf("\n Enter your choice:");
scanf("%d",&ch);

switch(ch)

{
case 1 :
Display();

COMPUTER APPLICATION, DPG ITM, GURUGRAM 10


Practical Software Lab BCA 205

break;
case 2:
printf("\n Node insertion:after which node:");
scanf("%d",&n);
p=n;
printf("\n Enter the item for insertion:");
scanf("%d",&x);
Insert(p,x);
break;
case 3:
printf("\n Enter the node after which the node will be deleted:");
scanf("%d",&n);
p=n;
Delete(p);
break;
case 4:
break;
default:
printf("\n Wrong choice! Try again:");
}
}}
int Getnode()
{
if (avail==-1)
{
printf("\n Overflow:");
exit(0);
}
p=avail;
avail=node[avail].next;
return p;
}
void Freenode(int q)
{
node[q].next=avail;
avail=q;
return;
}
void Createlist()
{ int
x;
char c;

COMPUTER APPLICATION, DPG ITM, GURUGRAM 11


Practical Software Lab BCA 205

p=Getnode();
printf("\n Enter an item to be inserted:");
scanf("%d", &x);
node[p].info=x ;
node[p].next=-1;
while(TRUE)
{
printf("\n Enter the choice(y/n):");
fflush(stdin);
c=getchar();
if(c=='y'||c=='Y')
{
printf("\n Enter an item to be inserted:");
scanf("%d",&x);
Insert(p,x);
node[p].next= -1;
}
else
return;
}}
void Display()
{
p=0;
while(node[p].next!=-1)
{ printf("\n%d\t%d\t%d:",p,node[p].info,node[p].next);
p=node[p].next;
}
printf("\n%d\t%d\t%d:",p,node[p].info,node[p].next);
}
void Insert(int r,int x)
{ int q;
if(r==-1)
{
printf("\n void insertion:");
return;
}
q=Getnode();
node[q].info=x;
node[q].next=node[r].next;
node[r].next=q;
return;
}

COMPUTER APPLICATION, DPG ITM, GURUGRAM 12


Practical Software Lab BCA 205

void Delete(int r)
{
int q;
if(r==-1||node[r].next==-1)
{
printf("\n void deletion:");
return;
}
q=node[r].next;
node[r].next=node[q].next;
Freenode(q);
return;
}

OUTPUT

COMPUTER APPLICATION, DPG ITM, GURUGRAM 13


Practical Software Lab BCA 205

4. AIM: Write a program in C to insert a node at the beginning of Singly Linked List.

LINKED LIST

Link structures are a completely different way to represent a list of items. Each element of
the listis made to point to the next element in the list.
An element in the list is called a
node. The Node have two part’s :
Data and Next
Data parts contain the information about the element.
Next part contains a pointer to the next elements or a node in the list.

A list consisting four names i.e. A, B, C and D is represented using link structures
such a list iscalled linked list or a linear linked list.

INSERTION IN A LINKED LIST


It involves following steps:

Take a node store data into nodes.


Search the location in the list where the node is to be inserted.
Insert the node.
Location where the node is to be inserted in a linked list can either be at the
beginningOr at any other position in the list.

ALGORITHM INSERT AT HEAD ()


{
Step
1. Take a node in ptr
2. Read data ( ptr )
3. if ( first = = NULL ) then
{
3.1 first = ptr;

COMPUTER APPLICATION, DPG ITM, GURUGRAM 14


Practical Software Lab BCA 205

3.2 NEXT ( first ) = NULL;


}
else
{
3.1 Next (ptr) = first;
3.2 first = ptr;
}
4. Stop
}

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

struct node
{
int data;
struct node*next;
}*head;
void createlist(int n);
void insertNodeAtBeginning(int data);
void displayList();
int main()
{
int n,data;

printf("enter the total number of nodes:");

scanf("%d",&n);

createlist(n);

printf("\ndata in the list\n");

displayList();

printf("\nenter data to insert at beginning of the list:");

scanf("%d",&data);

insertNodeAtBeginning(data);

COMPUTER APPLICATION, DPG ITM, GURUGRAM 15


Practical Software Lab BCA 205

printf("\n data 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;
}
}

COMPUTER APPLICATION, DPG ITM, GURUGRAM 16


Practical Software Lab BCA 205

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");


}
}
void insertNodeAtBeginning(int data)
{
struct node*newNode;
newNode=(struct node*)malloc(sizeof(struct node));
if(newNode==NULL)
{
printf("unable to allocate memory");
}
else
{
newNode->data=data;
newNode->next=head;
head=newNode;
printf("DATA INSERTED SUCCESSFULLY\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;
}
}
}

COMPUTER APPLICATION, DPG ITM, GURUGRAM 17


Practical Software Lab BCA 205

OUTPUT

COMPUTER APPLICATION, DPG ITM, GURUGRAM 18


Practical Software Lab BCA 205

5. AIM: Write a program in C to delete a first node from singly linked list.

LINKED LIST
Link structures are a completely different way to represent a list of items. Each element of
the listis made to point to the next element in the list.
An element in the list is called a
node. The Node have two part’s :
Data and Next
Data parts contain the information about the element.
Next part contains a pointer to the next elements or a node in the list.

A list consisting four names i.e. A, B, C and D is represented using link structures
such a list iscalled linked list or a linear linked list.

DELETING A NODE FROM A LINKED LIST


It involves two steps:

1. Search the list for the node which is to be deleted.


2. Delete the node.

ALGORITHM DELNODE ()
{

1. If ( Data ( First ) = ‘val’ )


/ * check if the starting node is desired one * /

1.1 ptr = first ;


1.2 first = next ( first );
1.3 free ( ptr );
1.4 Stop

COMPUTER APPLICATION, DPG ITM, GURUGRAM 19


Practical Software Lab BCA 205

2. while ( ptr ! = NULL )


2.1 if ( Data ( ptr ) = ‘val’ )
{
Next ( Back ) = Next ( ptr
);Free ( ptr );
Break;
}
2.2 back = ptr ;
2.3 ptr = Next ( ptr );
}
}
3. Stop
}

CODING

#include <stdio.h>
#include <stdlib.h>
struct node
{ int data;
struct node *next;
} *head;
void createList(int n);
void deleteFirstNode();
void displayList();
int main()
{ int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\n Data in the list \n");

COMPUTER APPLICATION, DPG ITM, GURUGRAM 20


Practical Software Lab BCA 205

displaylist();
printf("\nPress 1 to delete first node: ");
scanf("%d", &choice);
if(choice == 1)
deleteFirstNode();
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 deleteFirstNode()
{

COMPUTER APPLICATION, DPG ITM, GURUGRAM 21


Practical Software Lab BCA 205

struct node *toDelete;


if(head == NULL)
{ printf("List is already empty.");
} else
{ toDelete = head;
head = head->next;
printf("\nData deleted = %d\n", toDelete->data);
free(toDelete);
printf("SUCCESSFULLY DELETED FIRST NODE FROM 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

COMPUTER APPLICATION, DPG ITM, GURUGRAM 22


Practical Software Lab BCA 205

6. Aim: Write a program in C to print the stack by using an array.

DEFINITION :-
Stack is a LIFO (last in first out) structure. It is an ordered list of the same type of elements.
A stack is a linear list where all insertions and deletions are permitted only at one end of the
list. When elements are added to the stack it grows at one end. Similarly, when elements are
deleted from a stack, it shrinks at the same end.

Algorithm push() :
{
Step1. if (Top>=N) then
{ print “stack full”;
exist:
}
Step 2. Top=Top+1;
Step3. Stack[Top]=item;
Step4. Stop;
}

Algorithm pop() :
{
Step1. if (Top<0) then

COMPUTER APPLICATION, DPG ITM, GURUGRAM 23


Practical Software Lab BCA 205

{ print “stack empty”;


else:
}

Step 2. Item=Stack[Top];
Top=Top-1;
Step3. Stop;
}

CODING
#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\n\t 4.EXIT");
do
{ printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{ case 1:
{ push();
break; }
case 2:
{ pop();
break;}
case 3:
{display();
break;}
case 4:
{ printf("\n\t EXIT POINT ");

COMPUTER APPLICATION, DPG ITM, GURUGRAM 24


Practical Software Lab BCA 205

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

COMPUTER APPLICATION, DPG ITM, GURUGRAM 25


Practical Software Lab BCA 205

OUTPUT

COMPUTER APPLICATION, DPG ITM, GURUGRAM 26


Practical Software Lab BCA 205

7. AIM: Write a program in C to show the implementation of Queue.

DEFINITION:-
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. This
means that the first element added to the queue will be the first one to be removed.

Algorithms enqueue() :
void enqueue(int element)

COMPUTER APPLICATION, DPG ITM, GURUGRAM 27


Practical Software Lab BCA 205

{
if (isFull()) {
printf("Error: Queue is full\n");
return;
}
rear = (rear + 1) % MAX_SIZE;
queue[rear] = element;
count++;
}

Algorithms dequeue() :
int dequeue()
{
if (isEmpty())
{
printf("Error: Queue is empty\n");
return -1;
}
int element = queue[front];
front = (front + 1) % MAX_SIZE;
count--;
return element;
}

CODING
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
Void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{ int choice;
while (1)
{ printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)

COMPUTER APPLICATION, DPG ITM, GURUGRAM 28


Practical Software Lab BCA 205

{ case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
} } } void insert() {
int item; if(rear == MAX - 1)
printf("Queue Overflow n");
else{
if(front== - 1)
front = 0;
printf("Inset the element in queue : ");

scanf("% d", &item);

rear = rear + 1;
queue_array[rear] = item;
}}
void delete()
{
if(front == -
1 || front >
rear)
{ printf("Queue Underflow n");
return;
} else
{
printf("Element deleted from queue is : %dn", queue_array[front]);
front = front + 1;
} } void
display()
{ int i;
if(front == 1)
printf("Queue
is empty n");
else
{ printf("Queue is : n");
for(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}

COMPUTER APPLICATION, DPG ITM, GURUGRAM 29


Practical Software Lab BCA 205

OUTPUT

COMPUTER APPLICATION, DPG ITM, GURUGRAM 30

You might also like