DS Programshj gfh df
-
Upload
jason-west -
Category
Documents
-
view
221 -
download
2
description
Transcript of DS Programshj gfh df
EXPERIMENTDETAILSExperiment No. 1Title: WAP to compute minimum/ maximum of a given array.#include
#include
Void main()
{
Inta[5],I,max,min=0;
Printf(enter the element of array);
For(i=0; inext;
printf("enter the new node : ");
item=(struct node *)malloc(sizeof(struct node));
scanf("%d",&item->info);
temp2->next=item;
item->next=temp;
c++;
}
temp=temp->next;
}
if(c==0)
printf("\nthe node not found");
}
void del_beg()
{
head=head->next;
}
void del_end(int n)
{
struct node *temp;
int i;
temp=head;
for(i=1;inext==tail)
tail=temp;
temp=temp->next;
}
tail->next=NULL;
}
void del_pos(int n)
{
struct node *temp2;
int i,c=0,pos;
temp=head;
printf("linked list is : \n");
for(i=1;iinfo);
temp=temp->next;
}
printf("\nenter the existing node after which you want delete a node : ");
scanf("%d",&pos);
temp=head;
for(i=1;iinfo==pos)
{
temp2=temp;
temp2=temp2->next;
temp2=temp2->next;
temp->next=temp2;
c++;
}
temp=temp->next;
}
if(c==0)
printf("\nthe node not found");
}
void display(int n)
{
temp=head;
printf("the list is : \n");
for(int i=1;iinfo);
temp=temp->next;
}
printf("NULL\n\nPress any Key");
}
void main()
{
int n,i,ch;
clrscr();
printf("enter the number of nodes : ");
scanf("%d",&n);
for(i=1;iinfo);
temp->next=NULL;
if(i==1)
head=tail=temp;
tail->next=temp;
tail=temp;
}
do
{
clrscr();
printf("\nPress 1 for insert a node at the beginning of the list");
printf("\nPress 2 for insert a node at the end of the list");
printf("\nPress 3 for insert a node after a given node of the list");
printf("\nPress 4 for delete a node at the beginning of the list");
printf("\nPress 5 for delete a node at the end of the list");
printf("\nPress 6 for delete a node after a given node of the list");
printf("\nPress 7 for Display");
printf("\nPress 8 for exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg();
n++;
break;
case 2: insert_end();
n++;
break;
case 3: insert_pos(n);
n++;
break;
case 4: del_beg();
n--;
break;
case 5: del_end(n);
n--;
break;
case 6: del_pos(n);
n--;
break;
case 7: display(n);
getch();
break;
case 8: exit(0);
default: printf("wrong choice, enter again");
getch();
}
}
while(1);
}Experiment No. 8Title: Write a program to performs following operations on doubly linked lists:
a. To insert a node at the beginning of the list.
b. To insert a node at the end of the list.
c. To insert a node after a given node of the list.
d. To delete a node at the beginning of the list.
e. To delete a node at the end of the list.
f. To delete a node after a given node of the list.
#include
#include
#include
#include
struct node
{
int info;
struct node *prev, *next;
}*head,*tail,*temp;
void insert_beg()
{
printf("enter the new node : ");
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->info);
temp->next=head;
head->prev=temp;
head=temp;
head->prev=NULL;
}
void insert_end()
{
printf("enter the new node : ");
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->info);
temp->next=NULL;
temp->prev=tail;
tail->next=temp;
tail=temp;
}
void insert_pos(int n)
{
struct node *item,*temp2;
int i,c=0,pos;
temp=head;
printf("linked list is : \n");
for(i=1;iinfo);
temp=temp->next;
}
printf("\n\nenter the existing node after which you want insert a new node : ");
scanf("%d",&pos);
temp=head;
for(i=1;iinfo==pos)
{
temp2=temp;
temp=temp->next;
printf("enter the new node : ");
item=(struct node *)malloc(sizeof(struct node));
scanf("%d",&item->info);
item->prev=temp2;
item->next=temp;
temp2->next=item;
temp->prev=item;
c++;
}
temp=temp->next;
}
if(c==0)
printf("\nthe node not found");
}
void del_beg()
{
head=head->next;
head->prev=NULL;
}
void del_end()
{
temp=tail->prev;
temp->next=NULL;
tail=temp;
}
void del_pos(int n)
{
struct node *temp2;
int i,c=0,pos;
temp=head;
printf("linked list is : \n");
for(i=1;iinfo);
temp=temp->next;
}
printf("\nenter the existing node after which you want delete a node : ");
scanf("%d",&pos);
temp=head;
for(i=1;iinfo==pos)
{
temp2=temp;
temp2=temp2->next;
temp2=temp2->next;
temp->next=temp2;
temp2->prev=temp;
c++;
}
temp=temp->next;
}
if(c==0)
printf("\nthe node not found");
}
void display()
{
int k;
printf("\nPress 1 for print the list from the begening");
printf("\nPress 2 for print the list from the end");
scanf("%d",&k);
printf("NULLNULL");
getch();
}
void main()
{
int n,i,ch;
clrscr();
printf("enter the number of nodes : ");
scanf("%d",&n);
for(i=1;iinfo);
temp->next=temp->prev=NULL;
if(i==1)
{
temp->prev=NULL;
head=tail=temp;
}
else
{
tail->next=temp;
temp->prev=tail;
tail=temp;
}
}
do
{
clrscr();
printf("\nPress 1 for insert a node at the beginning of the list");
printf("\nPress 2 for insert a node at the end of the list");
printf("\nPress 3 for insert a node after a given node of the list");
printf("\nPress 4 for delete a node at the beginning of the list");
printf("\nPress 5 for delete a node at the end of the list");
printf("\nPress 6 for delete a node after a given node of the list");
printf("\nPress 7 for Display");
printf("\nPress 8 for exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg();
n++;
break;
case 2: insert_end();
n++;
break;
case 3: insert_pos(n);
n++;
break;
case 4: del_beg();
n--;
break;
case 5: del_end();
n--;
break;
case 6: del_pos(n);
n--;
break;
case 7: display();
break;
case 8: exit(0);
default: printf("wrong choice, enter again");
getch();
}
}
while(1);
}Experiment No.9Title: Write a program to perform following operations on circular linked lists:
d. To insert a node at the beginning of the list.
e. To insert a node at the end of the list.
f. To insert a node after a given node of the list.
d. To delete a node at the beginning of the list.
e. To delete a node at the end of the list.
f. To delete a node after a given node of the list
#include
#include
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
void main()
{
int choice,n,m,po,i;
start=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after\n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Count\n");
printf("7.Reverse\n");
printf("8.exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;iinfo=num;
tmp->next=NULL;
if(start==NULL)
{
tmp->prev=NULL;
start->prev=tmp;
start=tmp;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=tmp;
tmp->prev=q;
}
}/*End of create_list()*/
void addatbeg(int num)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->prev=NULL;
tmp->info=num;
tmp->next=start;
start->prev=tmp;
start=tmp;
}/*End of addatbeg()*/
void addafter(int num,int c)
{
struct node *tmp,*q;
int i;
q=start;
for(i=0;inext;
if(q==NULL)
printf("There are less than %d elements\n",c);
}
tmp=malloc(sizeof(struct node) );
tmp->info=num;
q->next->prev=tmp;
tmp->next=q->next;
tmp->prev=q;
q->next=tmp;
}/*End of addafter() */
void del(int num)
{
struct node *tmp,*q;
if(start->info==num)
{
tmp=start;
start=start->next; /*first element deleted*/
start->prev = NULL;
free(tmp);
return;
}
q=start;
while(q->next->next!=NULL)
{
if(q->next->info==num) /*Element deleted in between*/
{
tmp=q->next;
q->next=tmp->next;
tmp->next->prev=q;
free(tmp);
}
q=q->next;
}
if(q->next->info==num) /*last element deleted*/
{ tmp=q->next;
free(tmp);
q->next=NULL;
}
printf("Element %d not found\n",num);
}/*End of del()*/
void display()
{
struct node *q;
if(start==NULL)
printf("List is empty\n");
}
q=start;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->next;
}
printf("\n");
}/*End of display() */
void count()
{ struct node *q=start;
int cnt=0;
while(q!=NULL)
{
q=q->next;
cnt++;
}
printf("Number of elements are %d\n",cnt);
}/*End of count()*/
void rev()
{
struct node *p1,*p2;
p1=start;
p2=p1->next;
p1->next=NULL;
p1->prev=p2;
while(p2!=NULL)
{
p2->prev=p2->next;
p2->next=p1;
p1=p2;
p2=p2->prev; /*next of p2 changed to prev */
}
start=p1;
}/*End of rev()*/.Experiment No: 10Title: Write a menu-based program to implement stack operations: PUSH, POP using array implementation of stack.#include
#include
#define max 10
int a[max],top=-1;
void push();
void pop();
void display();
void peek();
void main()
{
int ch;
clrscr();
printf("STACK USING ARRAY\n\n");
while(1)
{
printf("Enter 1 for push\n");
printf("Enter 2 for pop\n");
printf("Enter 3 for display\n");
printf("Enter 4 for peek\n");
printf("Enter 5 for exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4 : peek();
break;
case 5 : exit(1);
default : printf("Wrong choice please enter correct\n\n");
}
}
}
void push()
{
int n,s,i;
if(top==(max-1))
{
printf("stack is full\n");
}
else
{
printf("How many no you want insert: ");
scanf("%d",&n);
for(i=0;inext=temp;
top=temp;
}
}
printf("\ntemp->num:%d\npt1->num:%d\n",temp->num,pt1->num);
}
void pop()
{
struct stack *pt1,*pt,*temp;
pt=pt1;
//clrscr();
printf("\n@%d**",pt1->num);
/*while(pt->next!=top)
{
pt=pt->next;
}
temp=pt->next;
pt->next=NULL;
top=pt;
printf("Element %d is pop\n",temp->num);
free(temp);*/
}
void display()
{
struct stack *pt,*pt1;
pt=pt1;
//clrscr();
if(top==NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Element is:\n");
while(pt!=NULL)
{
printf("%d\n",pt->num);
pt=pt->next;
}
//printf("%d\n",pt->num);
}
}
void peek()
{
//clrscr();
if(top==NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Top element is %d\n",top->num);
}
}
Experiment No: 12
Title: Write a program to convert infix expression into postfix expression and then to evaluate resultant postfix expression.#include
#include
#include
#include
#include
#define N 64
#define LP 10
#define RP 20
#define OPERATOR 30
#define OPERAND 40
#define LPP 0
#define AP 1
#define SP AP
#define MP 2
#define DP MP
#define REMP 2
#define NONE 9
static char infix[N+1],stack[N],postfix[N+1];
static int top;
void infixtopostfix(void);
int gettype(char);
void push(char);
char pop(void);
int getprec(char);
void main()
{
char ch;
do
{
top=-1;
printf("\nEnter an infix expression\n");
fflush(stdin);gets(infix);
infixtopostfix();
printf("\ninfix = %s\npost fix =%s\n",infix,postfix);
printf("\nDo you wish to continue(y/n):");
ch=getche();
} while(ch=='Y' || ch=='y');
}
void infixtopostfix(void)
{
int i,p,l,type,prec;
char next;
i=p=0;
l=strlen(infix);
while(i-1 && prec -1)
postfix[p++]=pop();
postfix[p]='\0';
}int gettype(char sym)
{
switch(sym)
{
case '(':
return(LP);
case ')':
return(RP);
case '+':
case '-':
case '*':
case '/':
case '%':
return(OPERATOR);
default :
return(OPERAND);
} }
void push(char sym)
{
if(top>N)
{ printf("\nStack is full\n");
exit(0);
}
else
stack[++top]=sym;
}
char pop(void)
{
if(top0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}
}
Sample output:
Enter the range of the Fibonacci series: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89
Experiment No: 14
Title: Write a program to solve Towers of Hanoi Problem.#include#include#includevoid hanoi(int x, char from, char to, char aux){ if(x==1) printf("Move Disk From %c to %c\n",from,to); else { hanoi(x-1,from,aux,to); printf("Move Disk From %c to %c\n",from,to); hanoi(x-1,aux,to,from); }}void main( ){ int disk; int moves; clrscr(); printf("Enter the number of disks you want to play with:"); scanf("%d",&disk); moves=pow(2,disk)-1; printf("\nThe No of moves required is=%d \n",moves); hanoi(disk,'A','C','B'); getch( );}
Output:-Enter the number of disks you want to play with: 3
The No of moves required is=7Move Disk from A to CMove Disk from A to BMove Disk from C to BMove Disk from A to CMove Disk from B to AMove Disk from B to CMove Disk from A to CExperiment No: 15
Title: Write a menu-based program to implement linear queue operations: INSERTION, DELETION using array implementation of queue.#include
#include
#define max 10
int front=-1,rear=-1,a[max];
void creat();
void display();
void delet();
void peek();
void main()
{
int ch;
printf("***QUEUE USING ARRAY***\n");
while(1)
{
printf("Enter 1 for creat\n");
printf("Enter 2 for display\n");
printf("Enter 3 for delet\n");
printf("Enter 4 for peek\n");
printf("Enter 5 for exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : creat();
break;
case 2 : display();
break;
case 3 : delet();
break;
case 4 : peek();
break;
case 5 : exit(0);
}
}
}
void creat()
{
int i,n1,n;
if(rear==(max-1))
{
printf("queue is full\n");
}
else
{
printf("How many no you want insert: ");
scanf("%d",&n);
for(i=0;inext=NULL;
if(front==NULL&&rear==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
}
void show()
{
struct queue *pt;
clrscr();
pt=front;
//printf("%d**%d",pt->num,rear->num);
if(front==NULL&&rear==NULL)
{
printf("queue is empty\n");
}
else
{
printf("Data is:\n");
while(pt!=NULL)
{
printf("%d\n",pt->num);
pt=pt->next;
}
}
}
void delet()
{
struct queue *temp;
clrscr();
if(front==NULL&&rear==NULL)
{
printf("Queue is empty\n");
}
else if(front==rear)
{
temp=front;
front=NULL;
rear=NULL;
printf("%d element is delet\n",temp->num);
free(temp);
}
else
{
temp=front;
front=front->next;
printf("%d element is delet\n",temp->num);
free(temp);
}
}
void peek()
{
clrscr();
printf("Front element is:%d",front->num);
}Experiment No: 17
Title:Write a menu-based program to implement circular queue operations: INSERTION, DELETION.
#include
#define max 3
int q[10],front=0,rear=-1;
void main()
{
int ch;
void insert();
void delet();
void display();
clrscr();
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3:display();
break;
case 4:exit();
default:printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit();
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
exit();
}
if(front>rear)
{
for(i=0;iRight());
else {
Node* n = new Node();
n->setKey(key);
leaf->setRight(n);
}
}
}
// Print the tree in-order
// Traverse the left sub-tree, root, right sub-tree
void Tree::inOrder(Node* n) {
if ( n ) {
inOrder(n->Left());
cout Key() Right());
}
}
// Print the tree pre-order
// Traverse the root, left sub-tree, right sub-tree
void Tree::preOrder(Node* n) {
if ( n ) {
cout Key() Left());
preOrder(n->Right());
}
}
// Print the tree post-order
// Traverse left sub-tree, right sub-tree, root
void Tree::postOrder(Node* n) {
if ( n ) {
postOrder(n->Left());
postOrder(n->Right());
cout Key() addNode(30);
tree->addNode(10);
tree->addNode(20);
tree->addNode(40);
tree->addNode(50);
cout Root());
cout data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void main()
{
node *root;
node *tmp;
//int i;
root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
/* Printing nodes of tree */
printf("Pre Order Display\n");
print_preorder(root);
printf("In Order Display\n");
print_inorder(root);
printf("Post Order Display\n");
print_postorder(root);
/* Search node into tree */
tmp = search(&root, 4);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
/* Deleting all nodes of tree */
deltree(root);
}Experiment No: 20
Title: Write a program to traverse a graph using breadth-first search (BFS), depth-first search (DFS).// BFS
#include
#include
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
for(i=1;i