Data Structures - LAB-R18
Data Structures - LAB-R18
Course Objectives:
It covers various concepts of C programming language
It introduces searching and sorting algorithms
It provides an understanding of data structures such as stacks and queues.
Course Outcomes:
Ability to develop C programs for computing and real-life applications using basic
elements like control statements, arrays, functions, pointers and strings, and data
structures like stacks, queues and linked lists.
Ability to implement searching and sorting algorithms
Page
S.No. List of Experiments
Nos.
Write a program that use both recursive and non recursive functions
to perform the following searching operations for a Key value in a
7. 61-68
given list of integers:
i) Linear search ii) Binary search
A linked list is formed when many such nodes are linked together to form a
chain. Each node points to the next node present in the order. The first node is always
used as a reference to traverse the list and is called HEAD. The last node points to NULL.
Operations on Singly Linked List
1. Node Creation
struct node
{
int data;
struct node *next;
};
2. Insertion: The insertion into a singly linked list can be performed at different
positions.
Insertion in singly linked list at beginning
Insertion in singly linked list at the end
Insertion in singly linked list after specified Node
3. Deletion and Traversing: The Deletion of a node from a singly linked list can be
performed at different positions.
Deletion in singly linked list at beginning
Deletion in singly linked list at the end
Deletion in singly linked list after the specified node
Source Code:
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *next;
}*start=NULL,*q,*t;
int main()
{
int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_pos();
while(1)
{
printf("\n\nSingly Linked List(SLL) Menu ");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
printf("Enter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Insert Menu");
printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified
position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg(); break;
case 2: insert_end(); break;
case 3: insert_pos(); break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;
case 2: display(); break;
case 3: printf("\n Delete Menu");
printf("\n1.Delete from beginning\n2.Delete from end\n3.Delete from
specified position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: delete_beg(); break;
case 2: delete_end();break;
case 3: delete_pos();break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
}
return 0;
}
void insert_beg()
{
int num;
}
}
int insert_pos()
{
int pos,i,num;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
t->data=num;
q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t->next=q->next;
q->next=t;
return 0;
}
void display()
{
if(start==NULL)
{
printf("List is empty!!");
}
else
{
q=start;
printf("The linked list is:\n");
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
}
}
void delete_beg()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
start=start->next;
printf("Deleted element is %d",q->data);
free(q);
}
}
void delete_end()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
while(q->next->next!=NULL)
q=q->next;
t=q->next;
q->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}
int delete_pos()
{
int pos,i;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}
printf("Enter position to delete:");
scanf("%d",&pos);
q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t=q->next;
q->next=t->next;
printf("Deleted element is %d",t->data);
free(t);
return 0;
}
OUTPUT:
Singly Linked List(SLL) Menu
1.Insert
2.Display
3.Delete
4.Exit
Enter your choice(1-4):1
Insert Menu
1.Insert at beginning
2.Insert at end
3.Insert at specified position
4.Exit
Enter your choice(1-4):1
Enter data:4
Singly Linked List(SLL) Menu
1.Insert
2.Display
3.Delete
4.Exit
Enter your choice(1-4):2
The linked list is:
4->
Singly Linked List(SLL) Menu
1.Insert
2.Display
3.Delete
4.Exit
Enter your choice (1-4):4
Source Code:
CSE, SRITW Page 11
Data Structures Lab (R18) II-I (2019-20)
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\
n4.Delete from Beginning\n5.Delete from last\n6.Delete the node after the given data\
n7.Search\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1: insertion_beginning();
break;
case 2: insertion_last();
break;
case 3: insertion_specified();
break;
case 4: deletion_beginning();
break;
case 5: deletion_last();
break;
case 6: deletion_specified();
break;
case 7: search();
break;
case 8: display();
break;
case 9: exit(0);
break;
default: printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
}
OUTPUT:
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Insert at any random location
4. Delete from Beginning
5. Delete from last
6. Delete the node after the given data
7. Search
8. Show
9. Exit
Enter your choice? 8
printing values...
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Insert at any random location
4. Delete from Beginning
5. Delete from last
6. Delete the node after the given data
7. Search
8. Show
9. Exit
Enter your choice? 1
Enter Item value12
Node inserted
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Insert at any random location
4. Delete from Beginning
5. Delete from last
6. Delete the node after the given data
7. Search
8. Show
9. Exit
Enter your choice? 1
Enter Item value123
Node inserted
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Insert at any random location
4. Delete from Beginning
5. Delete from last
6. Delete the node after the given data
7. Search
8. Show
9. Exit
Enter your choice?
8
printing values...
123
12
*********Main Menu*********
7. Search
8. Show
9. Exit
Enter your choice? 5
node deleted
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Insert at any random location
4. Delete from Beginning
5. Delete from last
6. Delete the node after the given data
7. Search
8. Show
9. Exit
Enter your choice? 8
printing values...
123
12345
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Insert at any random location
4. Delete from Beginning
5. Delete from last
6. Delete the node after the given data
7. Search
8. Show
9. Exit
Enter your choice? 6
Enter the data after which the node is to be deleted : 123
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Insert at any random location
4. Delete from Beginning
5. Delete from last
6. Delete the node after the given data
7. Search
8. Show
9. Exit
Enter your choice? 8
printing values...
123
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Insert at any random location
4. Delete from Beginning
5. Delete from last
6. Delete the node after the given data
7. Search
8. Show
9. Exit
Enter your choice? 7
Enter item which you want to search?
123
item found at location 1
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
Source Code:
CSE, SRITW Page 27
Data Structures Lab (R18) II-I (2019-20)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete
from last\n5.Search for an element\n6.Show\n7.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1: beginsert();
break;
case 2: lastinsert();
break;
case 3: begin_delete();
break;
case 4: last_delete();
break;
case 5: search();
break;
case 6: display();
break;
case 7: exit(0);
break;
default: printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}
printf("\nnode inserted\n");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");
}
}
void search()
{
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location %d",i+1);
flag=0;
}
else
{
while (ptr->next != head)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
}
if(flag != 0)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");
while(ptr -> next != head)
{
printf("%d\n", ptr -> data);
ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}
}
OUTPUT:
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit
Enter your choice? 1
6. Show
7. Exit
Enter your choice? 3
node deleted
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit
Enter your choice? 4
node deleted
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit
Enter your choice? 5
Enter item which you want to search? 20
item found at location 1
*********Main Menu*********
Choose one option from the following list ...
1. Insert in begining
2. Insert at last
3. Delete from Beginning
4. Delete from last
Stack operations:
Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow
condition.
Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow
condition.
Top: Returns top element of stack.
isEmpty: Returns true if stack is empty, else false.
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]);
}
OUTPUT:
Enter the size of STACK [MAX=100]:10
STACK OPERATIONS USING ARRAY
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter the Choice: 1
Enter a value to be pushed: 12
STACK OPERATIONS USING ARRAY
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter the Choice: 1
Enter a value to be pushed: 24
STACK OPERATIONS USING ARRAY
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter the Choice: 1
Enter a value to be pushed: 98
STACK OPERATIONS USING ARRAY
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter the Choice: 3
The elements in STACK
98
24
12
STACK OPERATIONS USING ARRAY
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Press Next Choice
Enter the Choice: 2
The popped elements is 98
STACK OPERATIONS USING ARRAY
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter the Choice:3
The elements in STACK
24
12
Press Next Choice
STACK OPERATIONS USING ARRAY
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter the Choice: 4
EXIT POINT
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 3
void push(int i);
void pop(void);
int choice, i;
int *tos, *p1, arr_stack[MAX_SIZE];
int exit_p = 1;
int main() {
int value;
tos = arr_stack; /* tos points to the top of stack */
p1 = arr_stack; /* initialize p1 */
printf("\n Simple Stack Example - Pointers");
do {
printf("\nStack Pointer : Main Menu");
printf("\n1.Push \t2.Pop \tOthers to exit : Your Choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
default:
exit_p = 0;
break;
}
} while (exit_p);
return 0;
}
void push(int i) {
if (p1 == (tos + MAX_SIZE)) {
printf("\nStatus : Stack Overflow.\n");
} else {
*p1 = i;
printf("\nPush Value : %d ", *(p1));
p1++;
}
}
void pop(void) {
if (p1 == tos) {
printf("\nStatus : Stack Underflow.\n");
} else {
p1--;
printf("\nPop Value : %d ", *(p1));
}
}
OUTPUT:
Operation on a queue:
The basic operations that can be perform on queue are;
Insert an element in a queue.
Delete an element from the queue.
#include<stdio.h>
#include<conio.h>
#define SIZE 10
void enQueue(int);
void deQueue();
void display();
int queue[SIZE], front = -1, rear = -1;
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]);
}
}
OUTPUT:
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 3
#define true 1
#define false 0
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct q_point
{
int ele;
struct q_point* n;
};
struct q_point *f_ptr = NULL;
int e_que(void);
void add_ele(int);
int rem_ele(void);
void show_ele();
void main()
{
int ele,choice,j;
while(1)
{
clrscr();
printf("\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n");
printf("\n\t\t MENU\n");
printf("\n\t[1] To insert an element");
printf("\n\t[2] To remove an element");
printf("\n\t[3] To display all the elements");
printf("\n\t[4] Exit");
printf("\n\n\tEnter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\n\tElement to be inserted:");
scanf("%d",&ele);
add_ele(ele);
getch();
break;
case 2: if(!e_que())
{
j=rem_ele();
printf("\n\t%d is removed from the queue",j);
getch();
}
else
{
printf("\n\tQueue is Empty.");
getch();
break;
case 3: show_ele();
getch();
break;
case 4: exit(1);
break;
default: printf("\n\tInvalid choice.");
getch();
break;
}
}
}
int e_que(void)
{
if(f_ptr==NULL)
return true;
return false;
}
{
struct q_point *queue = (struct q_point*)malloc(sizeof(struct q_point));
queue->ele = ele;
queue->n = NULL;
if(f_ptr==NULL)
f_ptr = queue;
else
{
struct q_point* ptr;
ptr = f_ptr;
for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
ptr->n = queue;
}
}
int rem_ele()
{
struct q_point* queue=NULL;
if(e_que()==false)
{
int j = f_ptr->ele;
queue=f_ptr;
f_ptr = f_ptr->n;
free (queue);
return j;
}
else
{
printf("\n\tQueue is empty.");
return -9999;
}
}
void show_ele()
{
struct q_point *ptr=NULL;
ptr=f_ptr;
if(e_que())
{
printf("\n\tQUEUE is Empty.");
return;
}
else
{
printf("\n\tElements present in Queue are:\n\t");
while(ptr!=NULL)
{
printf("%d\t",ptr->ele);
ptr=ptr->n;
}
}
}
OUTPUT:
****IMPLEMENTATION OF QUEUE USING POINTERS****
MENU
[1] To insert an element
[2] To remove an element
[3] To display all the elements
[4] Exit
Enter your choice: 1
Element to be inserted: 23
****IMPLEMENTATION OF QUEUE USING POINTERS****
MENU
[1] To insert an element
[2] To remove an element
[3] To display all the elements
[4] Exit
OUTPUT:
Enter number of elements 5
Enter 5 integers
2
-4
7
8
4
Sorted list in ascending order:
-4
2
4
7
8
OUTPUT:
Enter number of elements 6
Enter 6 integers
2
1
10
5
8
3
Sorted list in ascending order:
1
2
3
5
8
10
7. Write a program that uses both recursive and non recursive functions to
perform the following searching operations for a Key value in a given list of
integers:
i) Linear search ii) Binary search
Introduction to Searching:
Searching is the process of finding some particular element in the list. If the
element is present in the list, then the process is called successful and the process
returns the location of that element, otherwise the search is called unsuccessful.
There are two popular search methods that are widely used in order to search some
item into the list. However, choice of the algorithm depends upon the arrangement of
the list.
Linear Search
Binary Search
Linear Search
Linear search is the simplest search algorithm and often called sequential search.
In this type of searching, we simply traverse the list completely and match each element
of the list with the item whose location is to be found. If the match found then location
of the item is returned otherwise the algorithm return NULL.
Binary Search
Binary search is the search technique which works efficiently on the sorted lists.
Hence, in order to search an element into some list by using binary search technique, we
must ensure that the list is sorted.
Binary search follows divide and conquer approach in which, the list is divided
into two halves and the item is compared with the middle element of the list. If the
match is found then, the location of middle element is returned otherwise, we search
into either of the halves depending upon the result produced through the match.
OUTPUT:
Enter number of elements 6
Enter 6 integers
20 27 40 50 58 99
Enter value to find 27
27 found at location 2
Recursive Programs:
iii. linear search using recursion
SOURCE CODE
#include<stdio.h>
int a[5];
int linear(int num,int position) //function definition
{
if(num==a[position])
{
return position;
}
else
{
position++;
return linear(num,position);
}
}
void main()
{
int i,n,ans;
printf("Enter 5 numbers\n");
for(i=0;i<=4;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter the number to be searched\n");
scanf("%d",&n);
ans=linear(n,0); //function call
if(ans<=4)
{
printf("Number (%d) found at position (%d)",n,ans+1);
}
else
{
printf("Didnot find the number (%d)",n);
}
}
OUTPUT:
Enter 5 numbers
10 12 4 78 99
Enter the number to be searched 99
Number 99 found at position 5
Enter 5 numbers
10 12 4 78 99
Enter the number to be searched 2
Didnot find the number 2
}
else
{
return binarySearch(a,beg,mid-1,item);
}
}
return -1;
}
OUTPUT:
Enter the item which you want to search
19
Item found at location 2
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
void printPostorder(struct node* node)
{
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->data);
}
void printInorder(struct node* node)
{
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
void printPreorder(struct node* node)
{
if (node == NULL)
return;
printf("%d ", node->data);
printPreorder(node->left);
printPreorder(node->right);
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("\nPreorder traversal of binary tree is \n");
printPreorder(root);
printf("\nInorder traversal of binary tree is \n");
printInorder(root);
printf("\nPostorder traversal of binary tree is \n");
printPostorder(root);
getch();
return 0
}
Output:
Preorder traversal of binary tree is
12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231
9. Write a program to implement the graph traversal methods.
Graph Traversal Methods:
Traversing the graph means examining all the nodes and vertices of the graph. There
are two standard methods by using which, we can traverse the graphs.
Breadth First Search
Depth First Search
Breadth First Search (BFS) Algorithm
Breadth first search is a graph traversal algorithm that starts traversing the
graph from root node and explores all the neighboring nodes. Then, it selects the
nearest node and explores all the unexplored nodes
The algorithm starts with examining the node A and all of its neighbors. In the
next step, the neighbors of the nearest node of A are explored and process continues in
the further steps. The algorithm explores all neighbors of all the nodes and ensures that
each node is visited exactly once and no node is visited twice.
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);
int queue[MAX], front = -1,rear = -1;
void insert_queue(int vertex);
int delete_queue();
int isEmpty_queue();
int main()
{
create_graph();
BF_Traversal();
return 0;
}
void BF_Traversal()
{
int v;
for(v=0; v<n; v++)
state[v] = initial;
printf("Enter Start Vertex for BFS: \n");
scanf("%d", &v);
BFS(v);
}
void BFS(int v)
{
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ",v);
state[v] = visited;
for(i=0; i<n; i++)
{
if(adj[v][i] == 1 && state[i] == initial)
{
insert_queue(i);
state[i] = waiting;
}
}
}
printf("\n");
}
void insert_queue(int vertex)
{
if(rear == MAX-1)
printf("Queue Overflow\n");
else
{
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
}
}
int isEmpty_queue()
{
}
else
{
adj[origin][destin] = 1;
}
}
}
OUTPUT:
void DFS(int);
G[10][10]
void main()
int i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
void DFS(int i)
int j;
printf("\n%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
OUTPUT:
Enter number of vertices: 8
Enter adjacency matrix of the graph:
01111000
10000100
10000100
10000010
10000010
01100001
00011001
00000110
0
1
5
2
7
6
3
4