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
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
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