0% found this document useful (0 votes)
44 views69 pages

Dsa Lab1

The document discusses programs to implement various data structures like arrays, linked lists, stacks, queues and binary search trees. It includes programs for operations like insertion, deletion and searching in these data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views69 pages

Dsa Lab1

The document discusses programs to implement various data structures like arrays, linked lists, stacks, queues and binary search trees. It includes programs for operations like insertion, deletion and searching in these data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 69

1.

WRITE A PROGRAM TO IMPLEMENT THE LIST ADT USING


ARRAYS AND LINKED LISTS

#include<iostream.h>

#define Max 10

#include<conio.h>

class arrayadt

public:

int a,i,size,n,p,e,f,pos,b[20],x;

void create()

cout<<"creating a list"<<endl;

cout<<"Enter the size of the array:";

cin>>size;

for(i=0;i<size;i++)

cin>>b[i];

void isfull()

if(size==0)
{

cout<<"the array isempty:"<<endl;

else

cout<<"the array isfull:"<<endl;

void isempty()

if(size==0)

cout<<"the array isempty"<<endl;

else

cout<<"the array isfull"<<endl;

void insert()

cout<<"enter the position of the array for the element to be inserted:";

cin>>pos;
if(pos>=size)

cout<<"invalid position"<<endl;

else

for(i=Max;i>=pos;i--)

b[i]=b[i-1];

cout<<"enter the element to be inserted:";

cin>>p;

b[pos]=p;

size++;

cout<<"list after insertion"<<endl;

for(i=0;i<size;i++)

cout<<b[i]<<"\t";

void deleted()

{
cout<<"\n enter the position of the array for the element to be deleted:";

cin>>pos;

if(pos>=size)

cout<<"invalid position"<<endl;

else

for(i=pos+1;i<size;i++)

b[i-1]=b[i];

size--;

cout<<"the list after deletion"<<endl;

for(i=0;i<size;i++)

cout<<b[i]<<"\t";

void find()

cout<<"\n enter the element to be searched:";


cin>>x;

for(i=0;i<size;i++)

if(b[i]==x)

cout<<"element is at position"<<i<<endl;

void findKTH()

cout<<"enter the position of the element to be serached:";

cin>>e;

cout<<"the element at the give position is:"<<b[e]<<endl;

void print()

cout<<"the final array"<<endl;

for(i=0;i<size;i++)

cout<<b[i]<<"\t";

}
};

int main()

arrayadt obj;

obj.create();

obj.isfull();

obj.isempty();

obj.insert();

obj.deleted();

obj.find();

obj.findKTH();

obj.print();

return 0;

}
OUTPUT:
2.WRITE A PROGRAM TO IMPLEMENT THE STACK ADT
USING ARRAYS AND LINKED LISTS
#include<iostream.h>

#include<conio.h>

class stack

int top;

public:

int array[100];

stack()

top=-1;

void push(int x);

int pop();

void isEmpty();

void display();

};

void stack::push(int x)

if(top>=100)

cout<<"stack overflow\n";
}

else

array[++top]=x;

cout<<"pushed"<<x<<"\n";

int stack::pop()

if(top<0)

cout<<"stack underflow\n";

return 0;

else

int d=array[top--];

return d;

void stack::isEmpty()

if(top<0)
{

cout<<"stack empty\n";

else

cout<<"stack not Empty\n";

void stack::display()

for(int i=0;i<=top;i++)

cout<<array[i]<<" ";

cout<<endl;

int main()

stack st;

st.push (10);

st.push (20);

st.push (30);

st.push (40);
st.push (50);

st.display ();

cout<<"popped "<<st.pop ()<<endl;

cout<<"popped "<<st.pop ()<<endl;

st.display ();

return 0;

}
OUTPUT:
3.WRITE A PROGRAM TO IMPLEMENT THE QUEUE ADT
USING ARRAYS AND LINKED LIST

#include<iostream.h>

#include<conio.h>

struct QNode

int data;

QNode*next;

QNode(int d)

data=d;

next=NULL;

};

struct Queue

QNode*front,*rear;

Queue()

front=rear=NULL;

void enQueue(int x)
{

QNode*temp=new QNode(x);

if(rear==NULL)

front=rear=temp;

return;

rear->next=temp;

rear=temp;

void deQueue()

if(front==NULL)

return;

QNode*temp=front;

front=front->next;

if(front==NULL)

rear=NULL;

delete(temp);

};

int main()

{
Queue q;

q.enQueue(10);

q.enQueue(20);

q.deQueue();

q.deQueue();

q.enQueue(30);

q.enQueue(40);

q.enQueue(50);

q.deQueue();

cout<<"Queue front:"<<((q.front!=NULL)?(q.front)->data:-1)<<endl;

cout<<"Queue rear:"<<((q.rear!=NULL)?(q.rear)->data:-1);

}
OUTPUT:
4.WRITE A PROGRAM THAT READS AN INFIX EXPRESSION,
CONVERTS THE EXPRESSION TO POSTFIX FORM AND THEN
EVALUATES THE POSTFIX EXPRESSION (USE STACK ADT)

#include<iostream.h>

#include<ctype.h>

#include<string.h>

#include<conio.h>

void main()

int prece(char);

char infix[10],postfix[10],st[10];

int top=0,i=0,j=0;

st[top]='s';

clrscr();

cout<<"\n\t\t CONVERSION OF INFIX TO POSTFIX";

cout<<"\n\t\t ******************************";

cout<<"\n \t\t INPUT";

cout<<"\n enter the infix expression(end with#):";

cin>>infix;

char scanchar;

while(infix[i]!='#')

scanchar=infix[i];
if(isalpha(scanchar))

postfix[j]=scanchar;

j++;

else if(scanchar=='(')

top=top+1;

st[top]=scanchar;

else if(scanchar==')')

do

postfix[j]=st[top];

top=top-1;

j++;

while(st[top]!='(');

top=top-1;

else

{
while(prece(st[top])>=prece(scanchar))

postfix[j]=st[top];

top=top-1;

j++;

top=top+1;

st[top]=scanchar;

i++;

while(top!=0)

postfix[j]=st[top];

top=top-1;

i++;

cout<<"\n OUTPUT:";

cout<<"\n *******";

cout<<"\n Given Infix Expression\n";

for(int k=0;k<i;k++)

cout<<infix[k];

cout<<"\n Given Postfix Expression\n";


for(k=0;k<j;k++)

cout<<postfix[k];

getch();

int prece(char expression)

int value;

switch(expression)

case'^':

value=4;

break;

case'*':

value=3;

break;

case'/':

value=3;

break;

case'+':

value=2;

break;

case'-':

value=2;
break;

case'(':

value=1;

break;

case'$':

value=0;

break;

return(value);

}
OUTPUT:

5.WRITE A PROGRAM TO PERFORM THE FOLLOWING


OPERATIONS
A . INSERT AN ELEMENT INTO A DOUBLY LINKED LIST.

#include<iostream.h>

#include<stdio.h>

#include<conio.h>

struct Node

int data;

struct Node*next;

struct Node*prev;

};

void insertNode(struct Node** start,int value)

if(*start==NULL)

struct Node*new_node=new Node;

new_node->data=value;

new_node->next=new_node->prev=new_node;

*start=new_node;

return;

Node*last=(*start)->prev;
struct Node*new_node=new Node;

new_node->data=value;

new_node->next=*start;

(*start)->prev=new_node;

new_node->prev=last;

last->next=new_node;

void displayList(struct Node*start)

struct Node*temp=start;

while(temp->next !=start)

print("%d",temp->data);

temp=temp->next;

print("%d",temp->data);

int searchList(struct Node* start,int search)

struct Node*temp=start;

int count=0,flag=0,value;

if(temp==NULL)

return-1;
else

while(temp->next!=start)

count++;

if(temp->data==search)

flag=1;

count--;

break;

temp=temp->next;

if(temp->data==search)

count++;

flag=1;

if(flag==1)

cout<<"\n"<<search<<"found at location"<<count<<endl;

else

cout<<"\n"<<search<<"not found"<<endl;

}
}

int main()

struct Node*start=NULL;

insertNode(&start,4);

insertNode(&start,5);

insertNode(&start,7);

insertNode(&start,8);

insertNode(&start,6);

cout<<"Created circular doubly linked list is:";

displayList(start);

searchList(start,5);

return 0;

OUTPUT:
B. DELETE AN ELEMENT FROM A DOUBLY LINKED LIST.
#include<iostream.h>

#include<conio.h>

struct Node

int data;

Node*prev,*next;

};

void deleteNode(Node** head_ref,Node*del)

if(*head_ref==NULL)

return;

if(*head_ref==del)

*head_ref=del->next;

if(del->next!=NULL)

del->prev->next=del->next;

return;
}

void insertNode(Node**head_ref,int new_data)

Node*new_node=new Node();

new_node->data=new_data;

new_node->prev=NULL;

new_node->next=(*head_ref);

if((*head_ref)!=NULL)

(*head_ref)->prev=new_node;

(*head_ref)=new_node;

void printLinkedList(Node*node)

while(node!=NULL)

cout<<node->data<<" ->";

node=node->next;

int main()

{
Node*head=NULL;

insertNode(&head,1);

insertNode(&head,2);

insertNode(&head,3);

insertNode(&head,4);

insertNode(&head,5);

cout<<"Linked List before deletion:"<<endl;

printLinkedList(head);

deleteNode(&head,head->next);

cout<<"\nLinked List after deletion:"<<endl;

printLinkedList(head);

return 0;

}
OUTPUT:
C. SEARCH FOR A KEY ELEMENT IN A DOUBLY LINKED LIST.
#include<iostream.h>

#include<stdio.h>

#include<conio.h>

struct Node

int data;

struct Node*next;

struct Node*prev;

};

void insertNode(struct Node** start,int value)

if(*start==NULL)

struct Node*new_node=new Node;

new_node->data=value;

new_node->next=new_node->prev=new_node;

*start=new_node;

return;

Node*last=(*start)->prev;

struct Node*new_node=new Node;

new_node->data=value;
new_node->next=*start;

(*start)->prev=new_node;

new_node->prev=last;

last->next=new_node;

void displayList(struct Node*start)

struct Node*temp=start;

while(temp->next !=start)

print("%d",temp->data);

temp=temp->next;

print("%d",temp->data);

int searchList(struct Node* start,int search)

struct Node*temp=start;

int count=0,flag=0,value;

if(temp==NULL)

return-1;

else

{
while(temp->next!=start)

count++;

if(temp->data==search)

flag=1;

count--;

break;

temp=temp->next;

if(temp->data==search)

count++;

flag=1;

if(flag==1)

cout<<"\n"<<search<<"found at location"<<count<<endl;

else

cout<<"\n"<<search<<"not found"<<endl;

int main()
{

struct Node*start=NULL;

insertNode(&start,4);

insertNode(&start,5);

insertNode(&start,7);

insertNode(&start,8);

insertNode(&start,6);

cout<<"Created circular doubly linked list is:";

displayList(start);

searchList(start,5);

return 0;

}
OUTPUT:
6. WRITE A PROGRAM TO PERFORM THE FOLLOWING
OPERATIONS:
A. INSERT AN ELEMENT INTO A BINARY SEARCH TREE.
#include <iostream.h>
#include<conio.h>
class Node {
public:
int val;
Node* left;
Node* right;
Node(int val)
: val(val) , left(NULL), right(NULL)
{
}
};
void insert(Node*& root, int key)
{
if (!root) {
root = node;
return;
}
Node* prev = NULL;
Node* temp = root;
while (temp) {
if (temp->val > key) {
prev = temp;
temp = temp->left;
}
else if (temp->val < key) {
prev = temp;
temp = temp->right;
}
}
if (prev->val > key)
prev->left = node;
else
prev->right = node;
}
void inorder(Node* root)
{
Node* temp = root;
stack<Node*> st;
while (temp != NULL || !st.empty()) {
if (temp != NULL) {
st.push(temp);
temp = temp->left;
}
else {
temp = st.top();
st.pop();
cout << temp->val << " ";
temp = temp->right;
}
}
}
int main()
{
Node* root = NULL;
insert(root, 30);
insert(root, 50);
insert(root, 15);
insert(root, 20);
insert(root, 10);
insert(root, 40);
insert(root, 60);
inorder(root);
return 0;
}
OUTPUT:
B. DELETE AN ELEMENT FROM A BINARY SEARCH TREE.

#include<iostream.h>

#include<conio.h>

class Tree

public:

int data;

Tree*left,*right;

Tree(int x)

data=x;

left=NULL;

right=NULL;

};

Tree*Delete(Tree*root,int x)

if(root==NULL)

cout<<"Node not found";

return NULL;

}
if(root->data<x)

root->left=Delete(root->left,x);

else if(root->data<x)

root->right=Delete(root->right,x);

else

if(root->left==NULL)

Tree*temp=root->right;

return temp;

else if(root->right==NULL)

Tree*temp=root->right;

return temp;

else

Tree*temp=root->right;
while(temp->left!=NULL)

temp=temp->left;

root->data=temp->data;

root->right=Delete(root->right,temp->data);

return root;

void inorder_traversal(Tree*root)

if(root==NULL)

return;

inorder_traversal(root->left);

cout<<root->data<<" ";

inorder_traversal(root->right);

int main()

Tree*root=new Tree(15);

root->left=new Tree(13);

root->right=new Tree(18);

root->left->left=new Tree(8);

root->left->right=new Tree(14);
root->right->left=new Tree(16);

root->right->right=new Tree(19);

cout<<"Inorder Traversal of the Binary Search Tree:";

inorder_traversal(root);

cout<<endl;

cout<<"Value to be deleted :18\n";

Delete(root,18);

cout<<"Inordere Traversal:";

inorder_traversal(root);

cout<<endl;

return 0;

}
OUTPUT:
C . INORDER, PREORDER AND POSTORDER TRAVERSALS OF
A BINARY SEARCH TREE
#include<iostream.h>

#include<conio.h>

struct Node

int data;

struct Node*left,*right;

Node(int data)

this->data=data;

left=right=NULL;

};

void printPostorder(struct Node*node)

if(node==NULL)

return;

printPostorder(node->left);

printPostorder(node->right);

cout<<node->data<<" ";

void printInorder(struct Node*node)


{

if(node==NULL)

return;

printInorder(node->left);

cout<<node->data<<" ";

printInorder(node->right);

void printPreorder(struct Node*node)

if(node==NULL)

return;

cout<<node->data<<" ";

printPreorder(node->left);

printPreorder(node->right);

void main()

struct Node*root=new Node(1);

clrscr();

root->left=new Node(2);

root->right=new Node(3);

root->left->left=new Node(4);

root->left->right=new Node(5);
cout<<"\n\t\t Binary Search Tree Traversal";

cout<<"\n\t\t**";

cout<<"\n\t Preorder Traversal of Binary Tree is:\n";

printPreorder(root);

cout<<"\n Inorder Traversal of Binary Tree is:\n";

printInorder(root);

cout<<"\n Postorder Traversal of Binary Tree is:\n";

printPostorder(root);

getch();

}
OUTPUT:
7. WRITE A PROGRAMS FOR THE IMPLEMENTATION OF BFS
AND DFS FOR A GIVEN GRAPH
BFS
PROGRAM CODING
#include<stdio.h>

int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;

void bfs(int v)

for(i=1;i<=n;i++)

if(a[v][i] && !visited[i])

q[++r]=i;

if(f<=r)

visited[q[f]]=1;

bfs(q[f++]);

void main()

int v;

printf("\n Enter the number of vertices:");

scanf("%d",&n);

for(i=1;i<n;i++)
{ q[i]=0;

visited[i]=0;

printf("\n Enter graph data in matrix form:\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

scanf("%d",&a[i][i]);

printf("\n Enter the starting vertex:");

scanf("%d",&v);

bfs(v);

printf("\n The nodes which are reachable are:\n");

for(i=1;i<=n;i++)

if(visited[i])

printf("%d \t",i);

else

printf("\n Bfs is not possible.Not all nodes are reachable");


break;

}
OUTPUT:
DFS
PROGRAM CODING
#include<stdio.h>

void dfs(int);

int G[10][10],visited[10],n;

void main()

int i,j;

printf("enter number of vertices:");

scanf("%d'",&n);

printf("\n enter adjecency matrix of the graph:");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

scanf("%d",&G[i][j]);

for(i=0;i<n;i++)

visited[i]=0;

dfs(0);

void dfs(int i)

int j;

printf("\n%d",i);

visited[i]=1;
for(j=0;j<n;j++)

if(!visited[j]&&G[i][j]==1)

dfs(j);

}
OUTPUT:
8.WRITE A PROGRAMS FOR IMPLEMENTING THE
FOLLOWING SEARCHING METHODS:

A.LINEAR SEARCH

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int a[100], i,n, item,s=0, ch, beg,end,mid;
clrscr();
printf("Enter no.of Elements:") ;
scanf("℅d",&n);
printf("\nEnter Elements:\n") ;
for(i=1;i<=n;i++)
{
scanf("℅d", &a[i]);
}
while(1)
{
printf("\n1.Linear search\n2.Binary search\n3.Exit\n") ;
printf("Enter your choice:") ;
scanf("℅d", &ch);
switch(ch)
{
case 1:
printf("<-----------------LINEAR SEARCH---------------->\n");
printf("\n Enter Element you want to search:");
scanf("℅d",&item);
for(i=1;i<=n;i++)
{
if(a[i]==item)
{
printf("\nData is found at location:℅d",i);
s=1;
break;
}
}
if(s==0)
{
printf("Data is not found");
}
break;
case 2:
printf("<---------------BINARY SEARCH--------------->\n");
printf("\nEnter item you want to search:");
scanf("℅d",&item);
beg=1;
end=n;
mid=(beg+end)/2;
while(beg<=end&&a[mid]!=item)
{
if(a[mid]<item)
beg=mid+1;
else end=mid-1;
mid=(beg+end)/2;
}
if(a[mid]==item)
{
printf("\nData is found at location:℅d", mid);
}
else
{
printf("Data is not found");
}
break;
case 3:
default:exit(0);
}
}
getch();
}
OUTPUT:
9.WRITE A PROGRAMS FOR IMPLEMENTING THE
FOLLOWING SORTING METHODS:
A.BUBBLE SORT
#include<iostream.h>

#include<conio.h>

int main()

int a[50],n,i,j,temp;

cout<<"enter the size of array:";

cin>>n;

cout<<"enter the array elements:";

for(i=0;i<n;++i)

cin>>a[i];

for(i=0;i<n;++i)

for(j=0;j<(n-i-1);++j)

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}
cout<<"array after bubble sort:";

for(i=0;i<n;++i)

cout<<" "<<a[i];

return 0;

}
OUTPUT:
B.SELECTION SORT
#include<iostream.h>

#include<conio.h>

int smallest(int[ ], int, int) ;

int main()

int a[10]={10, 9,7,101,23,44,12,78,34,23};

int i,j,k, pos,temp;

for(i=0;i<10;i++)

pos=smallest(a,10,i);

temp=a[i];

a[i]=a[pos];

a[pos]=temp;

cout<<"\nprinting sorted elements....\n";

for(i=0;i<10;i++)

cout<<a[i]<<"\n";

return 0;

int smallest(int a[ ],int n,int i)


{

int small,pos,j;

small=a[i];

pos=i;

for(j=i+1;j<10;j++)

if(a[j]<small)

small=a[j];

pos=j;

return pos;

}
OUTPUT:
C .INSERTION SORT
#include<iostream.h>

#include<conio.h>

int main()

int i,j,k,temp;

int a[10]={10,9,7,101,23,44,12,78,34,23};

cout<<"\n printing sorted elements...\n;

for(k=1;k<10;k++)

temp=a[k];

j=k-1;

while(j>=0&&temp<=a[j])

a[j+1]=a[j];

j=j-1;

a[j+1]=temp;

for(i=0;i<10;i++)

cout<<a[i]<<"\n";

}
return 0;

}
OUTPUT:

You might also like