SlideShare a Scribd company logo
Concern Teacher: Hitesh Mohapatra, Asst. Professor
1
VEER SURENDRA SAI UNIVERSITY OF
TECHNOLOGY, BURLA
Department of Computer Science and Engineering
DSA Lab Record
B.Tech. III Semester
Concern Teacher: Hitesh Mohapatra, Asst. Professor
2
CONTENTS
Sl
No.
Programs Date Page
Numb
er
1 Basic implementation of Stack: push, pop, display 3
2 Create a queue and do the following operation using array
and linked list. Add, Remove
5
3 Write a program to insert and delete elements from linked
list: First, last , at any position
7
4 Write a program to Represent Sparse matrix 11
5 Create a stack by using linked list 13
6 Basic operation on Doubly linked list 16
7 Basic operation in Circular linked list 20
8 Write a program for binary traversal for : inorder, preorder.
postorder
23
9 Implementation of binary search tree 25
10 Write a Program to implement searching techniques
according to nature of list. Sorted list, unsorted list
27
Concern Teacher: Hitesh Mohapatra, Asst. Professor
3
Prog. 1
Basic implementation of Stack: push, pop, display
#include<iostream.h>
#include<stdio.h>
using namespace std;
struct node
{int info;
struct node *ptr;}*top, *top1, *temp;
int topelement(); void push(); void pop(); void empty(); void display(); void create();
void create()
{top == NULL;}
void push(int data)
{if(top == NULL)
{top = (struct node *)malloc(1*sizeof(struct node));
top ->ptr = NULL; top ->info = data;}
else
{temp = (struct node *)malloc(sizeof(struct node));
temp -> ptr = top; t emp -> info = data; top = temp;}}
void pop()
{top1 = top;
if (top1 == NULL)
{cout<<"n EMPTY STACK!!"; return; }
else
{top1 = top1->ptr;}
cout<<“n Popped value: "<< top->info;
free(top);top = top1;}
void display()
{top1=top;
if (top1 == NULL) { cout<<"nStack is emptyn"; return; }
while(top1 != NULL) {cout<< top1->info; top1 = top1->ptr; } }
int topelement() { return(top->info);}
void empty()
Concern Teacher: Hitesh Mohapatra, Asst. Professor
4
{if (top == NULL)
{cout<<"n Stack is empty";}
else
cout<<"n Stack is not empty"; }
void main()
{int no, ch, e;
cout<<"n 1.Push 2.Pop 3.Top 4.Empty 5.Exit 6.Display "; create();
while(1)
{cout<<"n Enter choice: "; cin>>ch;
switch(ch){case 1:
cout<<"Enter data: "; cin>>no; push(no); break;
case 2:
pop();
break;
case 3:
if(top == NULL)
cout<<"nNo elements in the stack.n";
else
{e = topelement(); cout<<"n Top Element: ";}
` break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display(); break;
default:
cout<<"n Wrong choice!!!!!!"; } } }
Output
1.Push 2.Pop 3.Top 4.Empty 5.Exit 6.Display
Enter choice: 1 Enter data: 10
Enter choice: 1 Enter data: 20
Enter choice: 6
Concern Teacher: Hitesh Mohapatra, Asst. Professor
5
23 12
Enter choice: 2 Popped value: 20
Prog. 2
Create a queue and do the following operation using array and linked list.
Add, Remove
#include<iostream>
#include<stdlib.h>
using namespace std;
class queue
{int queue1[10]; int rear, front;
public:
queue()
{rear -= 1; front -= 1; }
void insert(int x)
{if (rear > 9){cout << "queue OVERFLOW"; front = rear = -1; return;}
queue1[++rear]=x; cout <<"inserted: " << x;}
void delete()
{if (front == rear)
{cout << "queue UNDERFLOW"; return;}
cout <<"deleted: "<<queue1[front++];}
void display()
{if (rear == front)
{cout << "queue EMPTY" ; return;}
for (int i=front+1;i<=rear;i++)
{cout<<queue1[i]<<" ";}}};
int main()
{int ch; queue qu;
while(1)
{
cout<< "n 1.insert 2.delete 3.display 4.exit n Enter your choice: "; cin >> ch;
switch(ch)
{
case 1: cout <<"Enter the element: "; cin >> ch; qu.insert(ch); break;
case 2: qu.delet();
Concern Teacher: Hitesh Mohapatra, Asst. Professor
6
break;
case 3: qu.display();
break;
case 4: exit(0);
}
}
return 0;
}
OUTPUT
1.insert 2.delete 3.display 4.exit
Enter your choice: 1
Enter the element: 32
inserted: 32
1.insert 2.delete 3.display 4.exit
Enter your choice: 1
Enter the element: 54
inserted: 54
1.insert 2.delete 3.display 4.exit
Enter your choice: 3
32 54
1.insert 2.delete 3.display 4.exit
Enter your choice: 2
deleted: 32
Concern Teacher: Hitesh Mohapatra, Asst. Professor
7
Prog. 3
Write a program to insert and delete elements from linked list:
First, last , at any position
#include<iostream>
#include<stdio.h>
using namespace std;
void createlist(); void displaylist(); void insert_begin(); void insert_end(); void insert_pos();
void delete_begin(); void delete_end(); void delete_pos();
struct node
{ int info; struct node *next;};
struct node *start = NULL;
int main(){ int choice;
while(1)
{ cout<<"1.Create a list n 2.Display n 3.Insert at beginning n 4.Insert at end n 5.Insert at position n 6.
Delete from beginning n 7.Delete from end n 8.Delete from position n 9.Exit n";
cout<<"Enter your choice: "; cin>> choice;
switch(choice)
{case 1:
createlist(); break;
case 2:
displaylist(); break;
case 3:
insert_begin(); break;
case 4:
insert_end(); break;
case 5:
insert_pos(); break;
case 6:
delete_begin(); break;
case 7:
delete_end(); break;
case 8:
Concern Teacher: Hitesh Mohapatra, Asst. Professor
8
delete_pos(); break;
case 9:
exit(0); break;
default:
cout<<"n Wrong choice!!!!!!!!!!!"; } }
return 0;}
void createlist()
{struct node *temp, *ptr; temp =(struct node *)malloc(sizeof(struct node));
if (temp==NULL)
{cout<<"n Out of memory spaceL n"; exit(0);}
cout<<"n Enter the data: "; cin>>temp->info; temp -> next == NULL;
if (start == NULL)
{start = temp; }
else
{ptr = start;
while(ptr -> next != NULL){ptr = ptr -> next;}
ptr -> next = temp;}}
void displaylist()
{struct node *ptr;
if (start == NULL){ cout<<"n list is empty n"; return;}
else{ptr = start; cout<<"n The elements are: ";
while(ptr != NULL)
{cout<< ptr->info<<”t”); ptr = ptr -> next; } } }
void insert_begin()
{struct node *temp; temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL)
{cout<<"n Out of memory space n"; return; }
cout<<"n Enter the data: " ; cin>>temp->info; temp->next = NULL;
if(start == NULL)
{start = temp;}
else {temp -> next = start; start = temp;}}
void insert_end()
Concern Teacher: Hitesh Mohapatra, Asst. Professor
9
{struct node *temp, *ptr; temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL){cout<<"n Out of memory space n"; return; }
cout<<"n Enter the data: " ; cin>>temp->info; temp->next = NULL;
if(start == NULL){start = temp;}
else {ptr = start;
while(ptr -> next !=NULL)
{ptr = ptr ->next;}
ptr ->next = temp;}}
void insert_pos(){struct node *temp, *ptr; int i,pos;
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL) { cout<<"n Out of memory space n"; return;}
cout<<"n Enter the position: " ;cin>>pos;
cout<<"n Enter the data: "; cin>>temp->info; temp->next = NULL;
if(pos == 0){temp -> next = start;start = temp;}
else
{for (i = 0,ptr = start;i<pos;i++){ptr = ptr ->next;
if(ptr == NULL)
{cout<<"n Position not found!! n"; return;}}
temp -> next = ptr -> next;ptr ->next = temp;}
}
void delete_begin()
{struct node *ptr;
if(ptr = NULL)
{cout<<"n List is empty"); return;}
else
{ptr = start;start = start->next; cout<<"n The deleted element is "<< ptr->info<<”t”;free(ptr);}
}
void delete_end()
{struct node *temp, *ptr;
if(start == NULL){printf("n List is empty ");exit(0);}
else if (start -> next == NULL)
{ptr = start;start = NULL;cout<<"n The deleted element is"<<ptr ->info<<”t”; }
Concern Teacher: Hitesh Mohapatra, Asst. Professor
10
else
{ptr = start;
while(ptr -> next != NULL){temp = ptr;ptr = ptr -> next;}
temp -> next = NULL; cout<<"n The deleted Element is"<<ptr->info; free(ptr);}}
void delete_pos(){ int i,pos; struct node *temp,*ptr;
if(start==NULL) {cout<<"nThe List is Empty:n"; exit(0); }
else { cout<<"nEnter the position of the node to be deleted:t"; cin>>&pos;
if(pos==0)
{ ptr=start; start=start->next ; cout<<"nThe deleted element is:"<<,ptr->info ; free(ptr);}
else{ ptr=start; for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;
if(ptr==NULL){ cout<<"nPosition not Found:n"; return;} }
temp->next =ptr->next ; cout<<"nThe deleted element is:"<<ptr->info ; free(ptr); }}
}
O UTPUT
1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning
7.Delete from end 8.Delete from position9.Exit
Enter your choice: 1 Enter the data: 43
1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning
7.Delete from end 8.Delete from position9.Exit
Enter your choice: 3 Enter the data: 65
1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning
7.Delete from end 8.Delete from position9.Exit
Enter your choice: 4 Enter the data: 66
1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning
7.Delete from end 8.Delete from position9.Exit
Enter your choice: 5 Enter the Position: 2 Enter the data: 74
1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning
7.Delete from end 8.Delete from position9.Exit
Enter your choice: 2
65 74 43 66
1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning
7.Delete from end 8.Delete from position9.Exit
Concern Teacher: Hitesh Mohapatra, Asst. Professor
11
Enter your choice: 8
Enter the position of the node to be deleted: 3
The deleted Element is 43
Prog. 4
Write a program to Represent Sparse matrix
#include<iostream>
#include<stdio.h>
using namespace std;
struct node
{int row;int col; int data; struct node *link;}*head =NULL, *curr = NULL, *p = NULL;
void main()
{int i,j,m,n,a[50][50], counter = 0;
couut<<" n Sparse matrix n";cout<<"n Enter the number of rows n";cin>>m;
cout<<"n Enter the number of columns n";cin>>n; couut<<"Enter the elements: ";
for (i = 0; i<m;i++)
{for (j = 0; j<n; j++)
{scanf("%d", &a[i][j]);
if (a[i][j] != 0)
{curr = (struct node*)malloc(sizeof(struct node));curr -> row = i;curr -> col = j;
curr -> data = a[i][j];curr -> link = NULL;
if (head == NULL){head = curr;}
else{p = head;
while(p->link != NULL)
p = p->link;
p ->link = curr;}}
if (a[i][j] == 0){counter = counter + 1;}
}
}
couut<<"n The non zero elements are: n Row Column Elementn";
p = head;
if (head == NULL)
Concern Teacher: Hitesh Mohapatra, Asst. Professor
12
{
cout<<"nSparse Matrix is emptyn";
}
else{
while(p -> link != NULL)
{
cout<< p->row<<”t”<< p->col<<”t”<< p->data<<”n”;
p = p->link;
}
cout<< p->row<<””t<<p->col<<”t”<<p->data<<”n”;}
if (counter >(m*n)/2)
{cout<<"n It is a sparse matrix n";
}
else{
cout<<"n It is NOT a sparse matrix n";
}
}
OUTPUT
Sparse matrix
Enter the number of rows 2
Enter the number of columns 2
Enter the elements: 1 0 0 0
The non zero elements are:
Row Column Element
0 0 1
It is a sparse matrix
Concern Teacher: Hitesh Mohapatra, Asst. Professor
13
Prog. 5
Create a stack by using linked list
#include<iostream>
#include<conio.h>
#include <stdio.h>
using namespace std;
struct node{ int info; struct node *ptr;}*top,*top1,*temp;
int topelement(); void push(int data); void pop(); void empty();void display();void stack_count();
void create();int count = 0;
void main(){iint no, ch, e;
cout<<"n 1 - Push"; cout<<"n 2 - Pop"; cout<<"n 3 - Top"; cout<<"n 4 - Empty";
cout<<"n 5 - Exit"; cout<<"n 6 - Dipslay"; cout<<"n 7 - Stack Count";
create();
while (1) {cout<<"n Enter choice : "; cin>>ch;
switch (ch){ case 1:
cout<<"Enter data : "; cin>>no); push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
cout<<"No elements in stack";
else{e = topelement(); cout<<"n Top element : "<< e; }
break;
case 4:
empty(); break;
case 5:
exit(0);
case 6:
display(); break;
case 7:
Concern Teacher: Hitesh Mohapatra, Asst. Professor
14
stack_count(); break;
case 8:
destroy(); break;
default :
cout<<" Wrong choice, Please enter correct choice ";
break; } }}
void create(){ top = NULL;}
void stack_count()
{ cout<<"n No. of elements in stack :"<<count; }
void push(int data){if (top == NULL)
{top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL; top->info = data; }
else {temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top; temp->info = data;
top = temp; }
count++;}
void display()
{ top1 = top;
if (top1 == NULL) { cout<<"Stack is empty"; return; }
while (top1 != NULL) { cout<< top1->info; top1 = top1->ptr; }
}
void pop(){ top1 = top;
if (top1 == NULL){cout<<"n Error : Trying to pop from empty stack"; return; }
else
top1 = top1->ptr;
cout<<"n Popped value :"<< top->info; free(top); top = top1; count--;}
int topelement() { return(top->info); }
void empty()
{ if (top == NULL)
cout<<"n Stack is empty";
else
cout<<"n Stack is not empty with “<<count<<”elements.”;
Concern Teacher: Hitesh Mohapatra, Asst. Professor
15
}
OUTPUT
1 - Push2 - Pop3 - Top4 - Empty5 - Exit6 - Dipslay7 - Stack Count
Enter choice : 1 Enter data : 56 Enter choice : 1 Enter data : 80 Enter choice : 2 Popped value : 80
Enter choice : 3 Top element : 56 Enter choice : 1 Enter data : 78 Enter choice : 1 Enter data : 90
Enter choice : 6
90 78 56
Concern Teacher: Hitesh Mohapatra, Asst. Professor
16
Prog. 6
Basic operation on Doubly linked list
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node
{
int e; Position previous; Position next;};
void Insert(int x, List l, Position p){ Position TmpCell;
TmpCell = (struct Node*) malloc(sizeof(struct Node));
if(TmpCell == NULL)
cout<<"Memory out of spacen";
else { TmpCell->e = x; TmpCell->previous = p;
TmpCell->next = p->next; p->next = TmpCell;}
}
int isLast(Position p){ return (p->next == NULL);}
Position Find(int x, List l){Position p = l->next;
while(p != NULL && p->e != x)
p = p->next;
return p;
}
void Delete(int x, List l)
{ Position p, p1, p2; p = Find(x, l);
if(p != NULL)
Concern Teacher: Hitesh Mohapatra, Asst. Professor
17
{ p1 = p -> previous;p2 = p -> next; p1 -> next = p -> next;
if(p2 != NULL)
p2 -> previous = p -> previous;
}
else
cout<<"Element does not exist!!!n";
}
void Display(List l){ cout<<"The list element are :: "; Position p = l->next;
while(p != NULL){ cout<<p->e; p = p->next; }
}
void main(){ int x, pos, ch, i;List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->previous = NULL; l->next = NULL;
List p = l;
cout<<"DOUBLY LINKED LIST IMPLEMENTATIONn";
do {
cout<<"nn1. INSERTt 2. DELETEt 3. FINDt 4. PRINTt 5. QUITnnEnter the choice :: ";
cin>>ch;
switch(ch)
{ case 1:
p = l; cout<<"Enter the element to be inserted :: "; cin>>x;
cout<<"Enter the position of the element :: ";cin>>pos;
for(i = 1; i < pos; i++)
{
p = p->next;
}
Insert(x,l,p);
break;
case 2:
p = l;
cout<<"Enter the element to be deleted :: ";
Concern Teacher: Hitesh Mohapatra, Asst. Professor
18
cin>>x;
Delete(x,p);
break;
case 3:
p = l;
cout<<"Enter the element to be searched :: ";
cin>>x;
p = Find(x,p);
if(p == NULL)
cout<<"Element does not exist!!!n";
else
cout<<"Element exist!!!n";
break;
case 4:
Display(l);
break;
}
}
while(ch<5);
}
OUTPUT
DOUBLY LINKED LIST IMPLEMENTATION
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice :: 1
Enter the element to be inserted :: 10
Enter the position of the element :: 1
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice :: 1
Enter the element to be inserted :: 20
Enter the position of the element :: 2
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice :: 1
Concern Teacher: Hitesh Mohapatra, Asst. Professor
19
Enter the element to be inserted :: 30
Enter the position of the element :: 3
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice :: 4
The list element are :: 10 -> 20 -> 30 ->
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice :: 3
Enter the element to be searched :: 20
Element exist!!!
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Concern Teacher: Hitesh Mohapatra, Asst. Professor
20
Prog. 7
Basic operation in Circular linked list
#include<iostream>
#include<stdio.h>
#include<conio.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node{int e;Position next;};
void Insert(int x, List l, Position p){Position TmpCell;
TmpCell = (struct Node*) malloc(sizeof(struct Node));
if(TmpCell == NULL)
cout<<"Memory out of spacen";
else {TmpCell->e = x; TmpCell->next = p->next; p->next = TmpCell; }
}
int isLast(Position p, List l){ return (p->next == l);}
Position FindPrevious(int x, List l){ Position p = l;
while(p->next != l && p->next->e != x)
p = p->next;
return p;
}
Position Find(int x, List l){Position p = l->next;
while(p != l && p->e != x)
p = p->next;
return p;
}
void Delete(int x, List l)
Concern Teacher: Hitesh Mohapatra, Asst. Professor
21
{ Position p, TmpCell; p = FindPrevious(x, l);
if(!isLast(p, l))
{
TmpCell = p->next;
p->next = TmpCell->next;
free(TmpCell);
}
else
cout<<"Element does not exist!!!n";
}
void Display(List l){ cout<<"The list element are :: "; Position p = l->next;
while(p != l){cout<<p->e; p = p->next; }
}
void main(){ int x, pos, ch, i; List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->next = l; List p = l;
cout<<"CIRCULAR LINKED LIST IMPLEMENTATION n";
do {cout<<"nn1. INSERTt 2. DELETEt 3. FINDt 4. PRINTt 5. QUITnnEnter the choice :: ";
cin>>ch;
switch(ch)
{ case 1:
p = l;
cout<<"Enter the element to be inserted :: "; cin>>x;
cout<<"Enter the position of the element :: "; cin>>pos;
for(i = 1; i < pos; i++) { p = p->next;}
Insert(x,l,p); break;
case 2:
p = l; cout<<"Enter the element to be deleted :: ";
scanf("%d",&x); Delete(x,p);
break;
case 3:
p = l; cout<<"Enter the element to be searched :: ";
Concern Teacher: Hitesh Mohapatra, Asst. Professor
22
cin>>x; p = Find(x,p);
if(p == l)
cout<<"Element does not exist!!!n";
else
cout<<"Element exist!!!n";
break;
case 4:
Display(l); break;
}
}while(ch<5);
return 0;
}
OUTPUT
CIRCULAR LINKED LIST IMPLEMENTATION
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice :: 1
Enter the element to be inserted :: 10
Enter the position of the element :: 1
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice :: 1
Enter the element to be inserted :: 20
Enter the position of the element :: 2
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice :: 1
Enter the element to be inserted :: 30
Enter the position of the element :: 3
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice :: 4
The list element are :: 10 -> 20 -> 30 ->
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Concern Teacher: Hitesh Mohapatra, Asst. Professor
23
Prog. 8
Write a program for binary traversal for : inorder, preorder.
postorder
#include<iostream>
#include<stdio.h>
#include<conio.h>
struct node
{ int data; struct node* right; struct node* left;};
struct node *newNode(int item)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=item;
temp->left=NULL;
temp->right=NULL;
return temp;
}
struct node* insert(struct node* node,int data)
{
if(node==NULL)
return newNode(data);
if(data<node->data)
node->left=insert(node->left,data);
else if(data>node->data)
node->right=insert(node->right,data);
return node;
}
void inorder(struct node* root)
{
if(root!=NULL)
{
inorder(root->left);
Concern Teacher: Hitesh Mohapatra, Asst. Professor
24
cout<<root->data;
inorder(root->right); }
}
void preorder(struct node* root)
{
cout<<"Inordeer... n";
if(root!=NULL) {cout<<root->data);preorder(root->left); preorder(root->right);}
}
void postorder(struct node* root)
{
if(root!=NULL) { postorder(root->left); postorder(root->right);cout<<root->data;}
}
int main()
{
struct node *root=NULL;
root=insert(root,50);
insert(root,30); insert(root,20);insert(root,40);insert(root,70); insert(root,60); insert(root,80);
cout<<"INORDER... n";inorder(root);
cout<<"POSTORDER...n"; postorder(root);
cout<<"PREORDER...n"; preorder(root);
return 0;
}
OUTPUT
INORDER...
20 30 40 50 60 70 80
POSTORDER...
20 40 30 60 80 70 50
PREORDER...
50 30 20 40 70 60 80
Concern Teacher: Hitesh Mohapatra, Asst. Professor
25
Prog. 9
Implementation of binary search tree
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* createNode(value){
struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insert(struct node* root, int data)
{
if (root == NULL) return createNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;
Concern Teacher: Hitesh Mohapatra, Asst. Professor
26
}
void inorder(struct node* root){
if(root == NULL) return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}
int main(){
struct node *root = NULL;
root = insert(root, 8);
insert(root, 3);insert(root, 1);insert(root, 6);insert(root, 7);insert(root, 10);insert(root, 14);insert(root, 4);
inorder(root);
}
OUTPUT
1 ->3 ->4 ->6 ->7 ->8 ->10 ->14 ->
Concern Teacher: Hitesh Mohapatra, Asst. Professor
27
Prog. 10
Write a Program to implement searching techniques according to
nature of list. Sorted list, unsorted list. (Linear Search and Binary
Search)
Linear Search
#include<iostream>
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i,n,ch,ctr=0; cout<<"enter the array elements:";
for(i=0;i<5;i++) scanf("%d",&a[i]);
cout<<"1:Sorted array, 2:Unsorted array: "; cin>>ch;
cout<<"enter the element to search:";cin>>n;
if (ch==1) {for(i=0;i<5;i++) { if(a[i]==n){ cout<<"element is found at pos: “<<i+1; break; } }
if(n==5)
cout<<"element not found";
}
else if (ch==2){ for(i=0;i<5;i++){
if(a[i]==n) {cout<<"element is found at pos:”<<i+1; break;}
}
if(n<a[0] || n>a[5])
cout<<"element not found"; }
return 0;
}
OUTPUT
enter the array elements:4 3 56 2 6
1:Sorted array, 2:Unsorted array: 2
enter the element to search:56
Concern Teacher: Hitesh Mohapatra, Asst. Professor
28
element is found at pos: 3
Binary Search
#include<stdio.h>
int main()
{
int a[5],i, n,low,high,mid; cout<<"Elements of the array: ";
for(i=0;i<5;i++) scanf("%d",&a[i]);
cout<<"enter the element to search :"; cin>>n;
low=0; high=4;
while(low<=high)
{ mid=(low+high)/2;
if(n==a[mid])
{
cout<<n<<”is found at position "<<mid+1;
break;
}
else if (n>a[mid])
low=mid+1;
else
high=mid-1;
}
if(low>high)
cout<<"element not found";
return 0;
}
OUTPUT
Elements of the array: 1 2 3 4 5
Concern Teacher: Hitesh Mohapatra, Asst. Professor
29
Enter the element to be found 4
4 is at position

More Related Content

What's hot

5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
varadasuren
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
gkgaur1987
 
Session07 recursion
Session07 recursionSession07 recursion
Session07 recursion
HarithaRanasinghe
 
C programs
C programsC programs
C programs
Vikram Nandini
 
Networking Core Concept
Networking Core ConceptNetworking Core Concept
Networking Core Concept
Rays Technologies
 
Tu1
Tu1Tu1
The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189
Mahmoud Samir Fayed
 
Travel management
Travel managementTravel management
Travel management
1Parimal2
 
Student Data Base Using C/C++ Final Project
Student Data Base Using C/C++ Final ProjectStudent Data Base Using C/C++ Final Project
Student Data Base Using C/C++ Final Project
Haqnawaz Ch
 
Burrowing through go! the book
Burrowing through go! the bookBurrowing through go! the book
Burrowing through go! the book
Vishal Ghadge
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
mohamed sikander
 
Cquestions
Cquestions Cquestions
Cquestions
mohamed sikander
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
mohamed sikander
 
Static and const members
Static and const membersStatic and const members
Static and const members
mohamed sikander
 
The Ring programming language version 1.5.3 book - Part 20 of 184
The Ring programming language version 1.5.3 book - Part 20 of 184The Ring programming language version 1.5.3 book - Part 20 of 184
The Ring programming language version 1.5.3 book - Part 20 of 184
Mahmoud Samir Fayed
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
C++ programs
C++ programsC++ programs
C++ programs
Mukund Gandrakota
 
C programs Set 4
C programs Set 4C programs Set 4
C programs Set 4
Koshy Geoji
 
Java Program
Java ProgramJava Program
Java Program
Sudeep Singh
 
Nested loops
Nested loopsNested loops
Nested loops
Adnan Ferdous Ahmed
 

What's hot (20)

5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Session07 recursion
Session07 recursionSession07 recursion
Session07 recursion
 
C programs
C programsC programs
C programs
 
Networking Core Concept
Networking Core ConceptNetworking Core Concept
Networking Core Concept
 
Tu1
Tu1Tu1
Tu1
 
The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189
 
Travel management
Travel managementTravel management
Travel management
 
Student Data Base Using C/C++ Final Project
Student Data Base Using C/C++ Final ProjectStudent Data Base Using C/C++ Final Project
Student Data Base Using C/C++ Final Project
 
Burrowing through go! the book
Burrowing through go! the bookBurrowing through go! the book
Burrowing through go! the book
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Cquestions
Cquestions Cquestions
Cquestions
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
The Ring programming language version 1.5.3 book - Part 20 of 184
The Ring programming language version 1.5.3 book - Part 20 of 184The Ring programming language version 1.5.3 book - Part 20 of 184
The Ring programming language version 1.5.3 book - Part 20 of 184
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
C++ programs
C++ programsC++ programs
C++ programs
 
C programs Set 4
C programs Set 4C programs Set 4
C programs Set 4
 
Java Program
Java ProgramJava Program
Java Program
 
Nested loops
Nested loopsNested loops
Nested loops
 

Similar to Data Structure

DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
forladies
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
Chaitanya Kn
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
Syed Mustafa
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
Santhiya S
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Single linked list
Single linked listSingle linked list
Single linked list
Sayantan Sur
 
C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdf
anupambedcovers
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
AkhilaaReddy
 
Stack prgs
Stack prgsStack prgs
Stack prgs
Ssankett Negi
 
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
pasqualealvarez467
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdf
amarndsons
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Lakshmi Sarvani Videla
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
Sheikh Monirul Hasan
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
SANTOSH RATH
 
Qprgs
QprgsQprgs
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
SajalFayyaz
 
week-16x
week-16xweek-16x
take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdf
fastechsrv
 

Similar to Data Structure (20)

DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Single linked list
Single linked listSingle linked list
Single linked list
 
C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdf
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdf
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Qprgs
QprgsQprgs
Qprgs
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 
week-16x
week-16xweek-16x
week-16x
 
take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdf
 

More from Hitesh Mohapatra

Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Virtualization: A Key to Efficient Cloud Computing
Virtualization: A Key to Efficient Cloud ComputingVirtualization: A Key to Efficient Cloud Computing
Virtualization: A Key to Efficient Cloud Computing
Hitesh Mohapatra
 
Automating the Cloud: A Deep Dive into Virtual Machine Provisioning
Automating the Cloud: A Deep Dive into Virtual Machine ProvisioningAutomating the Cloud: A Deep Dive into Virtual Machine Provisioning
Automating the Cloud: A Deep Dive into Virtual Machine Provisioning
Hitesh Mohapatra
 
Harnessing the Power of Google Cloud Platform: Strategies and Applications
Harnessing the Power of Google Cloud Platform: Strategies and ApplicationsHarnessing the Power of Google Cloud Platform: Strategies and Applications
Harnessing the Power of Google Cloud Platform: Strategies and Applications
Hitesh Mohapatra
 
Scheduling in Cloud Computing
Scheduling in Cloud ComputingScheduling in Cloud Computing
Scheduling in Cloud Computing
Hitesh Mohapatra
 
Cloud-Case study
Cloud-Case study Cloud-Case study
Cloud-Case study
Hitesh Mohapatra
 
RAID
RAIDRAID
Load balancing in cloud computing.pptx
Load balancing in cloud computing.pptxLoad balancing in cloud computing.pptx
Load balancing in cloud computing.pptx
Hitesh Mohapatra
 
Cluster Computing
Cluster ComputingCluster Computing
Cluster Computing
Hitesh Mohapatra
 
ITU-T requirement for cloud and cloud deployment model
ITU-T requirement for cloud and cloud deployment modelITU-T requirement for cloud and cloud deployment model
ITU-T requirement for cloud and cloud deployment model
Hitesh Mohapatra
 
Leetcode Problem Solution
Leetcode Problem SolutionLeetcode Problem Solution
Leetcode Problem Solution
Hitesh Mohapatra
 
Leetcode Problem Solution
Leetcode Problem SolutionLeetcode Problem Solution
Leetcode Problem Solution
Hitesh Mohapatra
 
Trie Data Structure
Trie Data Structure Trie Data Structure
Trie Data Structure
Hitesh Mohapatra
 
Reviewing basic concepts of relational database
Reviewing basic concepts of relational databaseReviewing basic concepts of relational database
Reviewing basic concepts of relational database
Hitesh Mohapatra
 
Reviewing SQL Concepts
Reviewing SQL ConceptsReviewing SQL Concepts
Reviewing SQL Concepts
Hitesh Mohapatra
 
Advanced database protocols
Advanced database protocolsAdvanced database protocols
Advanced database protocols
Hitesh Mohapatra
 
Measures of query cost
Measures of query costMeasures of query cost
Measures of query cost
Hitesh Mohapatra
 
Involvement of WSN in Smart Cities
Involvement of WSN in Smart CitiesInvolvement of WSN in Smart Cities
Involvement of WSN in Smart Cities
Hitesh Mohapatra
 
Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its Fundamentals
Hitesh Mohapatra
 
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
WORKING WITH FILE AND PIPELINE PARAMETER BINDINGWORKING WITH FILE AND PIPELINE PARAMETER BINDING
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
Hitesh Mohapatra
 

More from Hitesh Mohapatra (20)

Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Virtualization: A Key to Efficient Cloud Computing
Virtualization: A Key to Efficient Cloud ComputingVirtualization: A Key to Efficient Cloud Computing
Virtualization: A Key to Efficient Cloud Computing
 
Automating the Cloud: A Deep Dive into Virtual Machine Provisioning
Automating the Cloud: A Deep Dive into Virtual Machine ProvisioningAutomating the Cloud: A Deep Dive into Virtual Machine Provisioning
Automating the Cloud: A Deep Dive into Virtual Machine Provisioning
 
Harnessing the Power of Google Cloud Platform: Strategies and Applications
Harnessing the Power of Google Cloud Platform: Strategies and ApplicationsHarnessing the Power of Google Cloud Platform: Strategies and Applications
Harnessing the Power of Google Cloud Platform: Strategies and Applications
 
Scheduling in Cloud Computing
Scheduling in Cloud ComputingScheduling in Cloud Computing
Scheduling in Cloud Computing
 
Cloud-Case study
Cloud-Case study Cloud-Case study
Cloud-Case study
 
RAID
RAIDRAID
RAID
 
Load balancing in cloud computing.pptx
Load balancing in cloud computing.pptxLoad balancing in cloud computing.pptx
Load balancing in cloud computing.pptx
 
Cluster Computing
Cluster ComputingCluster Computing
Cluster Computing
 
ITU-T requirement for cloud and cloud deployment model
ITU-T requirement for cloud and cloud deployment modelITU-T requirement for cloud and cloud deployment model
ITU-T requirement for cloud and cloud deployment model
 
Leetcode Problem Solution
Leetcode Problem SolutionLeetcode Problem Solution
Leetcode Problem Solution
 
Leetcode Problem Solution
Leetcode Problem SolutionLeetcode Problem Solution
Leetcode Problem Solution
 
Trie Data Structure
Trie Data Structure Trie Data Structure
Trie Data Structure
 
Reviewing basic concepts of relational database
Reviewing basic concepts of relational databaseReviewing basic concepts of relational database
Reviewing basic concepts of relational database
 
Reviewing SQL Concepts
Reviewing SQL ConceptsReviewing SQL Concepts
Reviewing SQL Concepts
 
Advanced database protocols
Advanced database protocolsAdvanced database protocols
Advanced database protocols
 
Measures of query cost
Measures of query costMeasures of query cost
Measures of query cost
 
Involvement of WSN in Smart Cities
Involvement of WSN in Smart CitiesInvolvement of WSN in Smart Cities
Involvement of WSN in Smart Cities
 
Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its Fundamentals
 
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
WORKING WITH FILE AND PIPELINE PARAMETER BINDINGWORKING WITH FILE AND PIPELINE PARAMETER BINDING
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
 

Recently uploaded

Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
Gas agency management system project report.pdf
Gas agency management system project report.pdfGas agency management system project report.pdf
Gas agency management system project report.pdf
Kamal Acharya
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
Kamal Acharya
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
uqyfuc
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
vmspraneeth
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
harshapolam10
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
Dwarkadas J Sanghvi College of Engineering
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
cannyengineerings
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
ijseajournal
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
um7474492
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
Prakhyath Rai
 

Recently uploaded (20)

Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
Gas agency management system project report.pdf
Gas agency management system project report.pdfGas agency management system project report.pdf
Gas agency management system project report.pdf
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
 

Data Structure

  • 1. Concern Teacher: Hitesh Mohapatra, Asst. Professor 1 VEER SURENDRA SAI UNIVERSITY OF TECHNOLOGY, BURLA Department of Computer Science and Engineering DSA Lab Record B.Tech. III Semester
  • 2. Concern Teacher: Hitesh Mohapatra, Asst. Professor 2 CONTENTS Sl No. Programs Date Page Numb er 1 Basic implementation of Stack: push, pop, display 3 2 Create a queue and do the following operation using array and linked list. Add, Remove 5 3 Write a program to insert and delete elements from linked list: First, last , at any position 7 4 Write a program to Represent Sparse matrix 11 5 Create a stack by using linked list 13 6 Basic operation on Doubly linked list 16 7 Basic operation in Circular linked list 20 8 Write a program for binary traversal for : inorder, preorder. postorder 23 9 Implementation of binary search tree 25 10 Write a Program to implement searching techniques according to nature of list. Sorted list, unsorted list 27
  • 3. Concern Teacher: Hitesh Mohapatra, Asst. Professor 3 Prog. 1 Basic implementation of Stack: push, pop, display #include<iostream.h> #include<stdio.h> using namespace std; struct node {int info; struct node *ptr;}*top, *top1, *temp; int topelement(); void push(); void pop(); void empty(); void display(); void create(); void create() {top == NULL;} void push(int data) {if(top == NULL) {top = (struct node *)malloc(1*sizeof(struct node)); top ->ptr = NULL; top ->info = data;} else {temp = (struct node *)malloc(sizeof(struct node)); temp -> ptr = top; t emp -> info = data; top = temp;}} void pop() {top1 = top; if (top1 == NULL) {cout<<"n EMPTY STACK!!"; return; } else {top1 = top1->ptr;} cout<<“n Popped value: "<< top->info; free(top);top = top1;} void display() {top1=top; if (top1 == NULL) { cout<<"nStack is emptyn"; return; } while(top1 != NULL) {cout<< top1->info; top1 = top1->ptr; } } int topelement() { return(top->info);} void empty()
  • 4. Concern Teacher: Hitesh Mohapatra, Asst. Professor 4 {if (top == NULL) {cout<<"n Stack is empty";} else cout<<"n Stack is not empty"; } void main() {int no, ch, e; cout<<"n 1.Push 2.Pop 3.Top 4.Empty 5.Exit 6.Display "; create(); while(1) {cout<<"n Enter choice: "; cin>>ch; switch(ch){case 1: cout<<"Enter data: "; cin>>no; push(no); break; case 2: pop(); break; case 3: if(top == NULL) cout<<"nNo elements in the stack.n"; else {e = topelement(); cout<<"n Top Element: ";} ` break; case 4: empty(); break; case 5: exit(0); case 6: display(); break; default: cout<<"n Wrong choice!!!!!!"; } } } Output 1.Push 2.Pop 3.Top 4.Empty 5.Exit 6.Display Enter choice: 1 Enter data: 10 Enter choice: 1 Enter data: 20 Enter choice: 6
  • 5. Concern Teacher: Hitesh Mohapatra, Asst. Professor 5 23 12 Enter choice: 2 Popped value: 20 Prog. 2 Create a queue and do the following operation using array and linked list. Add, Remove #include<iostream> #include<stdlib.h> using namespace std; class queue {int queue1[10]; int rear, front; public: queue() {rear -= 1; front -= 1; } void insert(int x) {if (rear > 9){cout << "queue OVERFLOW"; front = rear = -1; return;} queue1[++rear]=x; cout <<"inserted: " << x;} void delete() {if (front == rear) {cout << "queue UNDERFLOW"; return;} cout <<"deleted: "<<queue1[front++];} void display() {if (rear == front) {cout << "queue EMPTY" ; return;} for (int i=front+1;i<=rear;i++) {cout<<queue1[i]<<" ";}}}; int main() {int ch; queue qu; while(1) { cout<< "n 1.insert 2.delete 3.display 4.exit n Enter your choice: "; cin >> ch; switch(ch) { case 1: cout <<"Enter the element: "; cin >> ch; qu.insert(ch); break; case 2: qu.delet();
  • 6. Concern Teacher: Hitesh Mohapatra, Asst. Professor 6 break; case 3: qu.display(); break; case 4: exit(0); } } return 0; } OUTPUT 1.insert 2.delete 3.display 4.exit Enter your choice: 1 Enter the element: 32 inserted: 32 1.insert 2.delete 3.display 4.exit Enter your choice: 1 Enter the element: 54 inserted: 54 1.insert 2.delete 3.display 4.exit Enter your choice: 3 32 54 1.insert 2.delete 3.display 4.exit Enter your choice: 2 deleted: 32
  • 7. Concern Teacher: Hitesh Mohapatra, Asst. Professor 7 Prog. 3 Write a program to insert and delete elements from linked list: First, last , at any position #include<iostream> #include<stdio.h> using namespace std; void createlist(); void displaylist(); void insert_begin(); void insert_end(); void insert_pos(); void delete_begin(); void delete_end(); void delete_pos(); struct node { int info; struct node *next;}; struct node *start = NULL; int main(){ int choice; while(1) { cout<<"1.Create a list n 2.Display n 3.Insert at beginning n 4.Insert at end n 5.Insert at position n 6. Delete from beginning n 7.Delete from end n 8.Delete from position n 9.Exit n"; cout<<"Enter your choice: "; cin>> choice; switch(choice) {case 1: createlist(); break; case 2: displaylist(); break; case 3: insert_begin(); break; case 4: insert_end(); break; case 5: insert_pos(); break; case 6: delete_begin(); break; case 7: delete_end(); break; case 8:
  • 8. Concern Teacher: Hitesh Mohapatra, Asst. Professor 8 delete_pos(); break; case 9: exit(0); break; default: cout<<"n Wrong choice!!!!!!!!!!!"; } } return 0;} void createlist() {struct node *temp, *ptr; temp =(struct node *)malloc(sizeof(struct node)); if (temp==NULL) {cout<<"n Out of memory spaceL n"; exit(0);} cout<<"n Enter the data: "; cin>>temp->info; temp -> next == NULL; if (start == NULL) {start = temp; } else {ptr = start; while(ptr -> next != NULL){ptr = ptr -> next;} ptr -> next = temp;}} void displaylist() {struct node *ptr; if (start == NULL){ cout<<"n list is empty n"; return;} else{ptr = start; cout<<"n The elements are: "; while(ptr != NULL) {cout<< ptr->info<<”t”); ptr = ptr -> next; } } } void insert_begin() {struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); if (temp == NULL) {cout<<"n Out of memory space n"; return; } cout<<"n Enter the data: " ; cin>>temp->info; temp->next = NULL; if(start == NULL) {start = temp;} else {temp -> next = start; start = temp;}} void insert_end()
  • 9. Concern Teacher: Hitesh Mohapatra, Asst. Professor 9 {struct node *temp, *ptr; temp = (struct node *)malloc(sizeof(struct node)); if (temp == NULL){cout<<"n Out of memory space n"; return; } cout<<"n Enter the data: " ; cin>>temp->info; temp->next = NULL; if(start == NULL){start = temp;} else {ptr = start; while(ptr -> next !=NULL) {ptr = ptr ->next;} ptr ->next = temp;}} void insert_pos(){struct node *temp, *ptr; int i,pos; temp = (struct node *)malloc(sizeof(struct node)); if (temp == NULL) { cout<<"n Out of memory space n"; return;} cout<<"n Enter the position: " ;cin>>pos; cout<<"n Enter the data: "; cin>>temp->info; temp->next = NULL; if(pos == 0){temp -> next = start;start = temp;} else {for (i = 0,ptr = start;i<pos;i++){ptr = ptr ->next; if(ptr == NULL) {cout<<"n Position not found!! n"; return;}} temp -> next = ptr -> next;ptr ->next = temp;} } void delete_begin() {struct node *ptr; if(ptr = NULL) {cout<<"n List is empty"); return;} else {ptr = start;start = start->next; cout<<"n The deleted element is "<< ptr->info<<”t”;free(ptr);} } void delete_end() {struct node *temp, *ptr; if(start == NULL){printf("n List is empty ");exit(0);} else if (start -> next == NULL) {ptr = start;start = NULL;cout<<"n The deleted element is"<<ptr ->info<<”t”; }
  • 10. Concern Teacher: Hitesh Mohapatra, Asst. Professor 10 else {ptr = start; while(ptr -> next != NULL){temp = ptr;ptr = ptr -> next;} temp -> next = NULL; cout<<"n The deleted Element is"<<ptr->info; free(ptr);}} void delete_pos(){ int i,pos; struct node *temp,*ptr; if(start==NULL) {cout<<"nThe List is Empty:n"; exit(0); } else { cout<<"nEnter the position of the node to be deleted:t"; cin>>&pos; if(pos==0) { ptr=start; start=start->next ; cout<<"nThe deleted element is:"<<,ptr->info ; free(ptr);} else{ ptr=start; for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ; if(ptr==NULL){ cout<<"nPosition not Found:n"; return;} } temp->next =ptr->next ; cout<<"nThe deleted element is:"<<ptr->info ; free(ptr); }} } O UTPUT 1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning 7.Delete from end 8.Delete from position9.Exit Enter your choice: 1 Enter the data: 43 1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning 7.Delete from end 8.Delete from position9.Exit Enter your choice: 3 Enter the data: 65 1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning 7.Delete from end 8.Delete from position9.Exit Enter your choice: 4 Enter the data: 66 1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning 7.Delete from end 8.Delete from position9.Exit Enter your choice: 5 Enter the Position: 2 Enter the data: 74 1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning 7.Delete from end 8.Delete from position9.Exit Enter your choice: 2 65 74 43 66 1.Create a list 2.Display3.Insert at beginning 4.Insert at end 5.Insert at position 6. Delete from beginning 7.Delete from end 8.Delete from position9.Exit
  • 11. Concern Teacher: Hitesh Mohapatra, Asst. Professor 11 Enter your choice: 8 Enter the position of the node to be deleted: 3 The deleted Element is 43 Prog. 4 Write a program to Represent Sparse matrix #include<iostream> #include<stdio.h> using namespace std; struct node {int row;int col; int data; struct node *link;}*head =NULL, *curr = NULL, *p = NULL; void main() {int i,j,m,n,a[50][50], counter = 0; couut<<" n Sparse matrix n";cout<<"n Enter the number of rows n";cin>>m; cout<<"n Enter the number of columns n";cin>>n; couut<<"Enter the elements: "; for (i = 0; i<m;i++) {for (j = 0; j<n; j++) {scanf("%d", &a[i][j]); if (a[i][j] != 0) {curr = (struct node*)malloc(sizeof(struct node));curr -> row = i;curr -> col = j; curr -> data = a[i][j];curr -> link = NULL; if (head == NULL){head = curr;} else{p = head; while(p->link != NULL) p = p->link; p ->link = curr;}} if (a[i][j] == 0){counter = counter + 1;} } } couut<<"n The non zero elements are: n Row Column Elementn"; p = head; if (head == NULL)
  • 12. Concern Teacher: Hitesh Mohapatra, Asst. Professor 12 { cout<<"nSparse Matrix is emptyn"; } else{ while(p -> link != NULL) { cout<< p->row<<”t”<< p->col<<”t”<< p->data<<”n”; p = p->link; } cout<< p->row<<””t<<p->col<<”t”<<p->data<<”n”;} if (counter >(m*n)/2) {cout<<"n It is a sparse matrix n"; } else{ cout<<"n It is NOT a sparse matrix n"; } } OUTPUT Sparse matrix Enter the number of rows 2 Enter the number of columns 2 Enter the elements: 1 0 0 0 The non zero elements are: Row Column Element 0 0 1 It is a sparse matrix
  • 13. Concern Teacher: Hitesh Mohapatra, Asst. Professor 13 Prog. 5 Create a stack by using linked list #include<iostream> #include<conio.h> #include <stdio.h> using namespace std; struct node{ int info; struct node *ptr;}*top,*top1,*temp; int topelement(); void push(int data); void pop(); void empty();void display();void stack_count(); void create();int count = 0; void main(){iint no, ch, e; cout<<"n 1 - Push"; cout<<"n 2 - Pop"; cout<<"n 3 - Top"; cout<<"n 4 - Empty"; cout<<"n 5 - Exit"; cout<<"n 6 - Dipslay"; cout<<"n 7 - Stack Count"; create(); while (1) {cout<<"n Enter choice : "; cin>>ch; switch (ch){ case 1: cout<<"Enter data : "; cin>>no); push(no); break; case 2: pop(); break; case 3: if (top == NULL) cout<<"No elements in stack"; else{e = topelement(); cout<<"n Top element : "<< e; } break; case 4: empty(); break; case 5: exit(0); case 6: display(); break; case 7:
  • 14. Concern Teacher: Hitesh Mohapatra, Asst. Professor 14 stack_count(); break; case 8: destroy(); break; default : cout<<" Wrong choice, Please enter correct choice "; break; } }} void create(){ top = NULL;} void stack_count() { cout<<"n No. of elements in stack :"<<count; } void push(int data){if (top == NULL) {top =(struct node *)malloc(1*sizeof(struct node)); top->ptr = NULL; top->info = data; } else {temp =(struct node *)malloc(1*sizeof(struct node)); temp->ptr = top; temp->info = data; top = temp; } count++;} void display() { top1 = top; if (top1 == NULL) { cout<<"Stack is empty"; return; } while (top1 != NULL) { cout<< top1->info; top1 = top1->ptr; } } void pop(){ top1 = top; if (top1 == NULL){cout<<"n Error : Trying to pop from empty stack"; return; } else top1 = top1->ptr; cout<<"n Popped value :"<< top->info; free(top); top = top1; count--;} int topelement() { return(top->info); } void empty() { if (top == NULL) cout<<"n Stack is empty"; else cout<<"n Stack is not empty with “<<count<<”elements.”;
  • 15. Concern Teacher: Hitesh Mohapatra, Asst. Professor 15 } OUTPUT 1 - Push2 - Pop3 - Top4 - Empty5 - Exit6 - Dipslay7 - Stack Count Enter choice : 1 Enter data : 56 Enter choice : 1 Enter data : 80 Enter choice : 2 Popped value : 80 Enter choice : 3 Top element : 56 Enter choice : 1 Enter data : 78 Enter choice : 1 Enter data : 90 Enter choice : 6 90 78 56
  • 16. Concern Teacher: Hitesh Mohapatra, Asst. Professor 16 Prog. 6 Basic operation on Doubly linked list #include<iostream> #include<stdio.h> #include<conio.h> using namespace std; struct Node; typedef struct Node * PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; struct Node { int e; Position previous; Position next;}; void Insert(int x, List l, Position p){ Position TmpCell; TmpCell = (struct Node*) malloc(sizeof(struct Node)); if(TmpCell == NULL) cout<<"Memory out of spacen"; else { TmpCell->e = x; TmpCell->previous = p; TmpCell->next = p->next; p->next = TmpCell;} } int isLast(Position p){ return (p->next == NULL);} Position Find(int x, List l){Position p = l->next; while(p != NULL && p->e != x) p = p->next; return p; } void Delete(int x, List l) { Position p, p1, p2; p = Find(x, l); if(p != NULL)
  • 17. Concern Teacher: Hitesh Mohapatra, Asst. Professor 17 { p1 = p -> previous;p2 = p -> next; p1 -> next = p -> next; if(p2 != NULL) p2 -> previous = p -> previous; } else cout<<"Element does not exist!!!n"; } void Display(List l){ cout<<"The list element are :: "; Position p = l->next; while(p != NULL){ cout<<p->e; p = p->next; } } void main(){ int x, pos, ch, i;List l, l1; l = (struct Node *) malloc(sizeof(struct Node)); l->previous = NULL; l->next = NULL; List p = l; cout<<"DOUBLY LINKED LIST IMPLEMENTATIONn"; do { cout<<"nn1. INSERTt 2. DELETEt 3. FINDt 4. PRINTt 5. QUITnnEnter the choice :: "; cin>>ch; switch(ch) { case 1: p = l; cout<<"Enter the element to be inserted :: "; cin>>x; cout<<"Enter the position of the element :: ";cin>>pos; for(i = 1; i < pos; i++) { p = p->next; } Insert(x,l,p); break; case 2: p = l; cout<<"Enter the element to be deleted :: ";
  • 18. Concern Teacher: Hitesh Mohapatra, Asst. Professor 18 cin>>x; Delete(x,p); break; case 3: p = l; cout<<"Enter the element to be searched :: "; cin>>x; p = Find(x,p); if(p == NULL) cout<<"Element does not exist!!!n"; else cout<<"Element exist!!!n"; break; case 4: Display(l); break; } } while(ch<5); } OUTPUT DOUBLY LINKED LIST IMPLEMENTATION 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT Enter the choice :: 1 Enter the element to be inserted :: 10 Enter the position of the element :: 1 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT Enter the choice :: 1 Enter the element to be inserted :: 20 Enter the position of the element :: 2 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT Enter the choice :: 1
  • 19. Concern Teacher: Hitesh Mohapatra, Asst. Professor 19 Enter the element to be inserted :: 30 Enter the position of the element :: 3 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT Enter the choice :: 4 The list element are :: 10 -> 20 -> 30 -> 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT Enter the choice :: 3 Enter the element to be searched :: 20 Element exist!!! 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
  • 20. Concern Teacher: Hitesh Mohapatra, Asst. Professor 20 Prog. 7 Basic operation in Circular linked list #include<iostream> #include<stdio.h> #include<conio.h> struct Node; typedef struct Node * PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; struct Node{int e;Position next;}; void Insert(int x, List l, Position p){Position TmpCell; TmpCell = (struct Node*) malloc(sizeof(struct Node)); if(TmpCell == NULL) cout<<"Memory out of spacen"; else {TmpCell->e = x; TmpCell->next = p->next; p->next = TmpCell; } } int isLast(Position p, List l){ return (p->next == l);} Position FindPrevious(int x, List l){ Position p = l; while(p->next != l && p->next->e != x) p = p->next; return p; } Position Find(int x, List l){Position p = l->next; while(p != l && p->e != x) p = p->next; return p; } void Delete(int x, List l)
  • 21. Concern Teacher: Hitesh Mohapatra, Asst. Professor 21 { Position p, TmpCell; p = FindPrevious(x, l); if(!isLast(p, l)) { TmpCell = p->next; p->next = TmpCell->next; free(TmpCell); } else cout<<"Element does not exist!!!n"; } void Display(List l){ cout<<"The list element are :: "; Position p = l->next; while(p != l){cout<<p->e; p = p->next; } } void main(){ int x, pos, ch, i; List l, l1; l = (struct Node *) malloc(sizeof(struct Node)); l->next = l; List p = l; cout<<"CIRCULAR LINKED LIST IMPLEMENTATION n"; do {cout<<"nn1. INSERTt 2. DELETEt 3. FINDt 4. PRINTt 5. QUITnnEnter the choice :: "; cin>>ch; switch(ch) { case 1: p = l; cout<<"Enter the element to be inserted :: "; cin>>x; cout<<"Enter the position of the element :: "; cin>>pos; for(i = 1; i < pos; i++) { p = p->next;} Insert(x,l,p); break; case 2: p = l; cout<<"Enter the element to be deleted :: "; scanf("%d",&x); Delete(x,p); break; case 3: p = l; cout<<"Enter the element to be searched :: ";
  • 22. Concern Teacher: Hitesh Mohapatra, Asst. Professor 22 cin>>x; p = Find(x,p); if(p == l) cout<<"Element does not exist!!!n"; else cout<<"Element exist!!!n"; break; case 4: Display(l); break; } }while(ch<5); return 0; } OUTPUT CIRCULAR LINKED LIST IMPLEMENTATION 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT Enter the choice :: 1 Enter the element to be inserted :: 10 Enter the position of the element :: 1 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT Enter the choice :: 1 Enter the element to be inserted :: 20 Enter the position of the element :: 2 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT Enter the choice :: 1 Enter the element to be inserted :: 30 Enter the position of the element :: 3 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT Enter the choice :: 4 The list element are :: 10 -> 20 -> 30 -> 1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
  • 23. Concern Teacher: Hitesh Mohapatra, Asst. Professor 23 Prog. 8 Write a program for binary traversal for : inorder, preorder. postorder #include<iostream> #include<stdio.h> #include<conio.h> struct node { int data; struct node* right; struct node* left;}; struct node *newNode(int item) { struct node *temp=(struct node*)malloc(sizeof(struct node)); temp->data=item; temp->left=NULL; temp->right=NULL; return temp; } struct node* insert(struct node* node,int data) { if(node==NULL) return newNode(data); if(data<node->data) node->left=insert(node->left,data); else if(data>node->data) node->right=insert(node->right,data); return node; } void inorder(struct node* root) { if(root!=NULL) { inorder(root->left);
  • 24. Concern Teacher: Hitesh Mohapatra, Asst. Professor 24 cout<<root->data; inorder(root->right); } } void preorder(struct node* root) { cout<<"Inordeer... n"; if(root!=NULL) {cout<<root->data);preorder(root->left); preorder(root->right);} } void postorder(struct node* root) { if(root!=NULL) { postorder(root->left); postorder(root->right);cout<<root->data;} } int main() { struct node *root=NULL; root=insert(root,50); insert(root,30); insert(root,20);insert(root,40);insert(root,70); insert(root,60); insert(root,80); cout<<"INORDER... n";inorder(root); cout<<"POSTORDER...n"; postorder(root); cout<<"PREORDER...n"; preorder(root); return 0; } OUTPUT INORDER... 20 30 40 50 60 70 80 POSTORDER... 20 40 30 60 80 70 50 PREORDER... 50 30 20 40 70 60 80
  • 25. Concern Teacher: Hitesh Mohapatra, Asst. Professor 25 Prog. 9 Implementation of binary search tree #include<iostream> #include<stdio.h> #include<conio.h> using namespace std; struct node { int data; struct node* left; struct node* right; }; struct node* createNode(value){ struct node* newNode = malloc(sizeof(struct node)); newNode->data = value; newNode->left = NULL; newNode->right = NULL; return newNode; } struct node* insert(struct node* root, int data) { if (root == NULL) return createNode(data); if (data < root->data) root->left = insert(root->left, data); else if (data > root->data) root->right = insert(root->right, data); return root;
  • 26. Concern Teacher: Hitesh Mohapatra, Asst. Professor 26 } void inorder(struct node* root){ if(root == NULL) return; inorder(root->left); printf("%d ->", root->data); inorder(root->right); } int main(){ struct node *root = NULL; root = insert(root, 8); insert(root, 3);insert(root, 1);insert(root, 6);insert(root, 7);insert(root, 10);insert(root, 14);insert(root, 4); inorder(root); } OUTPUT 1 ->3 ->4 ->6 ->7 ->8 ->10 ->14 ->
  • 27. Concern Teacher: Hitesh Mohapatra, Asst. Professor 27 Prog. 10 Write a Program to implement searching techniques according to nature of list. Sorted list, unsorted list. (Linear Search and Binary Search) Linear Search #include<iostream> #include<stdio.h> #include<conio.h> int main() { int a[5],i,n,ch,ctr=0; cout<<"enter the array elements:"; for(i=0;i<5;i++) scanf("%d",&a[i]); cout<<"1:Sorted array, 2:Unsorted array: "; cin>>ch; cout<<"enter the element to search:";cin>>n; if (ch==1) {for(i=0;i<5;i++) { if(a[i]==n){ cout<<"element is found at pos: “<<i+1; break; } } if(n==5) cout<<"element not found"; } else if (ch==2){ for(i=0;i<5;i++){ if(a[i]==n) {cout<<"element is found at pos:”<<i+1; break;} } if(n<a[0] || n>a[5]) cout<<"element not found"; } return 0; } OUTPUT enter the array elements:4 3 56 2 6 1:Sorted array, 2:Unsorted array: 2 enter the element to search:56
  • 28. Concern Teacher: Hitesh Mohapatra, Asst. Professor 28 element is found at pos: 3 Binary Search #include<stdio.h> int main() { int a[5],i, n,low,high,mid; cout<<"Elements of the array: "; for(i=0;i<5;i++) scanf("%d",&a[i]); cout<<"enter the element to search :"; cin>>n; low=0; high=4; while(low<=high) { mid=(low+high)/2; if(n==a[mid]) { cout<<n<<”is found at position "<<mid+1; break; } else if (n>a[mid]) low=mid+1; else high=mid-1; } if(low>high) cout<<"element not found"; return 0; } OUTPUT Elements of the array: 1 2 3 4 5
  • 29. Concern Teacher: Hitesh Mohapatra, Asst. Professor 29 Enter the element to be found 4 4 is at position