Professional Documents
Culture Documents
Data Structure Lab Record - VSSUT, Burla
Data Structure Lab Record - VSSUT, Burla
CONTENTS
Sl Programs Date Page
No. Numb
er
1 Basic implementation of Stack: push, pop, display 3
2 Create a queue and do the following operation using array 5
and linked list. Add, Remove
3 Write a program to insert and delete elements from linked 7
list: First, last , at any position
4 Write a program to Represent Sparse matrix 11
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 empty\n"; return; }
while(top1 != NULL) {cout<< top1->info; top1 = top1->ptr; } }
int topelement() { return(top->info);}
void empty()
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
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
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:
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
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 Element\n";
p = head;
if (head == NULL)
Concern Teacher: Hitesh Mohapatra, Asst. Professor
12
{
cout<<"\nSparse Matrix is empty\n";
}
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
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.”;
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
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 space\n";
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)
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 IMPLEMENTATION\n";
do {
cout<<"\n\n1. INSERT\t 2. DELETE\t 3. FIND\t 4. PRINT\t 5. QUIT\n\nEnter 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 :: ";
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
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 space\n";
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<<"\n\n1. INSERT\t 2. DELETE\t 3. FIND\t 4. PRINT\t 5. QUIT\n\nEnter 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 :: ";
OUTPUT
CIRCULAR LINKED LIST IMPLEMENTATION
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);
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
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;
};
return root;
inorder(root);
}
OUTPUT
1 ->3 ->4 ->6 ->7 ->8 ->10 ->14 ->
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()
for(i=0;i<5;i++) scanf("%d",&a[i]);
if(n==5)
if(n<a[0] || n>a[5])
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
Binary Search
#include<stdio.h>
int main()
for(i=0;i<5;i++) scanf("%d",&a[i]);
low=0; high=4;
while(low<=high)
{ mid=(low+high)/2;
if(n==a[mid])
break;
else if (n>a[mid])
low=mid+1;
else
high=mid-1;
if(low>high)
return 0;
OUTPUT
Elements of the array: 1 2 3 4 5
4 is at position