DSU Practicals Manual Writing Material-All Titles.
DSU Practicals Manual Writing Material-All Titles.
Delete Algorithm:
Let A [Max] is an array and size specifies length of array.
1. Accept the location into loc where element to be deleted.
2. Repeat for i=loc+l to size-l.
3. Set a[i-1]=a[i].
4. Set size=size-1.
Insert Algorithm : -
Let A [Max] is an array and size specifies length of an array.
1. Accept the item to be inserted into element.
2. Accept the location into loc where element to be inserted.
3. Repeat for i=size-l down to loc.
4. Set a[i+l]=a[i].
5. Set size=size+1.
6. Set a[oc]=element.
Program : Menu driven program for array to create, insert, delete and
display elements in array.
#include <stdio.h>
#include <process.h>
#define MAX 100 //l(maximum number of elements that can be stored
int num=0,a[MAX];
void create();
void insert();
void delete();
void display();
int main()
{
int ch;
setbuf(stdout, NULL) ; / /Turn off buffering of stdout
Printf ( “*** Array Menu ***” );
printf ( " \n1.Create \n 2.Insert \n 3.Delete \n 4.Display \n 5.Exit " );
while(1) //infinite loop
{
void delete( )
{
int i,index;
printf("Enter index of the element to be deleted :\n");
scanf("%d", &index);
for (i = index+1; i <= num-l; i++)
{
a[i-1] = a[i];
}
num--; // No of elements reduced by 1
}
void display()
{
int i;
printf ( *** Elements in Array***\n" );
for(i=0; i<num; i++)
printf("%d "a[i] );
}
#include <stdio.h>
#include <conio.h>
int main()
{
int a[100], find, i, n;
setbuf(stdout, NULL); / /Turn off buffering of stdout
printf("Enter the size of array: \n");
scanf("%d", &n);
printf("Enter n elements: \n");
for (i = 0 i < n; i++)
scanf("%d", &a[i]);
printf("Enter value to be search \n “);
scanf("%d", &find);
for (i = 0; i < n; i++)
{
if (a[i] == find) /* if the key element is found */
{
printf("Element found at index %d= \n”,i);
break;
}
}
if (i == n)
printf("Element Not found in Array”);
return 0;
}
Advantages of Linear Search:
#include <stdio.h>
#include <conio.h>
int main()
{
int first, last, middle, n, i, find, a[100];
printf(“Enter the size of array: \n”);
scanf (“%d”,&n);
printf("Enter n elements in Ascending order: \n”);
for (i = 0; i < n; i++)
scanf(“%d"&a[i]);
printf (“Enten value to be seanch : \n”);
scanf (“%d", &find);
first = 0;
last=n-1;
middle = (first+last)/2;
while (first <= last)
{
if (a[middle] < find)
first= middle+1;
else if (a[middle] == find)
{
printf(,,Element found at index Xd.\n,,,middle);
break;
}
else
last= middle-1;
middle = (first + last)/2;
}
if (first > tast)
pnintf (“Element Not found in the list.”);
return 0;
}
Output:
Enter the size of array: 5
Enter n elements in Ascending order: 1 4 7 9 11
Enter value to be search: 7
Element found at index 2
Flow Chart : -
Flow Chart : -
PUSH: The process of adding a new element to the top of stack is called push operation. After every push operation the
top is incremented by one.
Algorithm :
PUSH (stack, TOP, size, item)
Step 1: Check for stack overflow,
If TOP > size
message "stack overflow”
return
Step 2: Set the position of TOP pointer
Set TOP = TOP + 1
Step 3: Apply insertion
stack [TOP] = item
Step 4: Exit
1. POP: The process of deleting an element from the top of stack is called pop operation. After every POP
operation the top pointer of the stack is decremented by one.
Algorithm:
POP (Stack, TOP, size, item)
Step 1: Check underflow condition,
If TOP = -1 then
message "underflow”
exit
Step 2: Delete the TOP element.
Set item = Stack [TOP].
Step 3: Set the value of TOP
Set TOP = TOP - 1
Step 4: Return.
#include <stdio.h>
#include <stdlib.h>
#include<process. h>
#define MAX 5 //Maximun number of elements that can be stored
int top=-1,stack[MAX];
void push();
void pop();
void display();
int main()
{
int ch;
printf("\n *** Stack Menu ***”);
printf (" \n1 . Push\n2 . Pop\n3. Display\n4. Exit" );
while(1) //infinite loop
{
printf ("\n Enter your choice(1-4) : ");
scanf( "%d", &ch );
switch ( ch )
{
case 1; push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf(“\n Wrong Choice!!”);
}
}
return 0;
}
void push( )
{
int val;
if(top== MAX-1);
printf( "\n Stack is full ! !'” );
else
{
printf ( "\n Enter element to push :”);
scanf( "%d ", &val ); I
top= top+1;
stack[top ] = val;
}
}
void pop( )
{
if(toP ==-1);
printf( "\n Stack is Empty ! “);
else
{
printf("\n Deleted element is %d", stack[top]); //delete top element
top= top-1;
}
}
void display()
{
int i;
if(top==-1);
printf("\n Stack is Empty!!”);
else
{
printf( " \n** Elements in Stack**. \n” );
for(i=top; i>=0; i- - )
printf( "%d\n", stack[i]);
}
Output:
*** Stack Menu *x*
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4) :1
Enter element to push:10
Enter your choice(1-a):1
Enter element to push:20
Enter your choice(1-4) :1
Enter element to push:30
Enter your choice(1-4):3
*** Elements in stack***.
30
20
10
Enter your choice(1-4) :2
Deleted element is 30
Enter your choice(1-4):3
*** Elements in stack***.
20
10
Practical No: 08.
Insert and Delete operation on Queue using array.
Algorithm:
Let queue [Max] is an array for implementation of queue. Let front and queue initialize to -1.
1 if rear=MAX-l then //[check queue is overflow]
2. print queue is overflow
3. else
4. if front = -1 then set front to 0
5. set rear = rear + l
6. Set Queue[Rear] = Item //Insert element at rear position of queue.
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}
Practical No: 09.
Insert and Delete operation on Circular Queue using array.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 5
Int queue[MAX],front=-l,rear=-l;
void enqueue();
void dequeue();
void display();
int main()
{
int ch;
setbuf(stdout, NULL); //turn off buffering of stdout
printf("\n*** Circular Queue Menu ***");
printf("\nl.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(l)
{
printf("\nEnter the Choice(l-4):" ) ;
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong Choice!!");
}
}
return 0;
}
void enqueue()
{
if((front == rear + 1) || (front == 0 && rear == MAX-1))
printf("\n Queue is Full!!");
else
{
if(front = = -1)
front=0;
rear=(rear+l)%MAX;
printf("\n Enter element to Insert:");
scanf("%d",&queue[rear]);
}
printf("\nAfter Insertion position of Front=%d and Rear=%d", front, rear);
}
void dequeue()
if(front= =-l)
printf("\n Queue is Empty!!");
else
{
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
new_node->next=start;
start=new_node;
}
}
void LastNodeDeletion()
{
struct node *Last, *preNode;
if(stnode == NULL)
{
printf(" There is no element in the list.");
}
else
{
Last = stnode;
preNode = stnode;
/* Traverse to the last node of the list*/
while(Last->nextptr != NULL)
{
preNode = Last;
Last = Last->nextptr;
}
if(Last == stnode)
{
stnode = NULL;
}
else
{
/* Disconnects the link of second last node with last node */
preNode->nextptr = NULL;
}
Create
First check the status of last pointer, if it is NULL means, we are inserting first node in the list and that new node (tmp)
will be the Iast node in the list as shown in Fig
void create()
{
struct node *tmp;
printf("Enter element”);
scanf("%d", &data);
tmp=malloc(sizeof(struct node));
tmp->info=data;
if(last==NULL)
{
last=tmp;
tmp->link=last;
}
}
Let tmp is the node which we want to insert at beginning of iist. Store the address of (Last->link) in the
tmp->link. And update first node(last->link) as shown in Fig above.
C code is given below:
void del_at_end()
{
struct node *q;
q=last->link;
if(last==NULL)
{
printf("List is empty");
}
if(last->link==last) //single node in the list
{
last=NULL;
free(last);
printf("List is empty!!");
return;
}
while(q->link!=last)//traverse upto Last pointer
{
q=q->link;
}
q->link=last->link;
free(last);
last=q;
printf("Last element deleted!!\n");
display();
return;
}
Program : Menu driven program for binary search tree creation and tree traversing. [Recursive]
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node *right;
struct node *left;
};