Dst External Program (1)
Dst External Program (1)
h>
void create();
void insertion();
void deletion();
void display();
int a[20],i,n,pos,item;
int main()
{
int choice;
printf("\n\n*************** MENU *****************");
printf("\n1. Create");
printf("\n2. Display");
printf("\n3. insertion");
printf("\n4. deletion");
printf("\n\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : create();
break;
case 2 : printf("\n************ DISPLAY DATA *************");
printf("\nEntered Data : ");
display();
break;
case 3 : insertion();
break;
case 4 : deletion();
break;
default : printf("\nEnter Valid Choice");
}
main();
return 0;
}
void create()
{
}
void insertion()
{
printf("\n insertion operation");
printf("\n enter the number to be inserted");
scanf("\n%d",&item);
printf("\nenter the position to insert an element");
scanf("\n%d",&pos);
if(pos<=n)
{
n++;
for(i=n-1;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos-1]=item;
}
else
{
printf("insertion is not possible");
}
}
void deletion()
{
printf("enter the position to be deleted");
scanf("\n%d",&pos);
if(pos>=n+1)
{
printf("\ndeletion is not possible");
}
else{
for(i=pos-1;i<n-1;i++ )
{
a[i]=a[i+1];
}n--;
}
}
void display()
{
printf("\n\narray is=====");
for(i=0;i<n;i++)
{
printf("\n\n%d",a[i]);
}
}
Bubble sort
//Bubble sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,j,temp;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<N;i++)
{
for(j=0;j<N-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
prinƞ("\nSorted Array Elements:");
for(i=0;i<N;i++)
{
prinƞ("%d\t",a[i]);
}
getch();
}
//SelecƟon sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,j,k,temp;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<N-1;i++)
{
k=i;
for(j=i+1;j<N;j++)
{
if(a[j]<a[k])
{
k=j;
}
}
if(k!=i)
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
prinƞ("\nSorted Array Elements:");
for(i=0;i<N;i++)
{
prinƞ("%d\t",a[i]);
}
getch();
}
//InserƟon sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,j,k,temp;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<N;i++)
{
temp=a[i];
for(j=i-1;j>=0&&a[j]>temp;j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
prinƞ("\nSorted Array Elements:");
for(i=0;i<N;i++)
{
prinƞ("%d\t",a[i]);
}
getch();
}
//Linear Search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,key,flag=0;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
prinƞ("\nEnter key element for searching:");
scanf("%d",&key);
for(i=0;i<N;i++)
{
if(key==a[i])
{
flag=1;
break;
}
}
if(flag==1)
{
prinƞ("\nElement is found");
}
else
{
prinƞ("\nElement is not found");
}
}
//Binary Search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],N,i,j,mid,key,temp;
clrscr();
prinƞ("\nEnter Array Size:");
scanf("%d",&N);
prinƞ("\nEnter Array Elements:");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
prinƞ("\nEnter key element for searching:");
scanf("%d",&key);
for(i=1;i<N;i++)
{
for(j=0;j<N-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
i=0;
j=N-1;
mid=(i+j)/2;
while(key!=a[mid]&&i<=j)
{
if(key<a[mid])
{
j=mid-1;
}
else
{
i=mid+1;
}
mid=(i+j)/2;
}
if(i<=j)
{
printf("\nElement is Found");
}
else
{
prinƞ("\nElement is not Found");
}
getch();
}
Stack operation
#include<stdio.h>
#include<conio.h>
#define MAX 10
int data[MAX],top;
void initialize();
int empty();
int full();
void push();
void pop();
void print();
int main()
{
int ch;
initialize();
do{
printf("\n *******STACK OPERATION****** ");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.PRINT");
printf("\n4.EXIT");
printf("\n\nENTER YPUR CHOICE");
scanf("\n%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:print();
break;
case 4: printf("thenks for using our software");
break;
default:printf("enter the invalid choice");
break;
}
}while(ch!=4);
return 0;
}
void initialize()
{
top=-1;
}
int empty()
{
if(top==-1)
{
return(1);
}
else{
return(0);
}
}
int full()
{
if(top==MAX-1)
{
return(1);
}
else{
return(0);
}
}
void push()
{
int x;
if(full()==0)
{
printf("\n enter the data");
scanf("%d",&x);
top=top+1;
data[top]=x;
printf("your data is pushed successfully");
}
else{
printf("stack is full");
}
}
void pop()
{
int x;
if(empty()==0)
{
x=data[top];
top=top-1;
printf("popped data =%d",x);
}
else{
printf("stack is empty()");
}
}
void print()
{
int i;
printf("stack dta");
for(i=top;i>=0;i--)
{
printf("%d \t",data[i]);
}
}
Postfix expression
#include<stdio.h>
#define MAX 20
typedef
struct stack
{
int data[MAX];
int top;
}stack;
int main()
{
stack s;
char x;
int op1,op2,val;
init(&s);
printf("Enter the expression(eg: 59+3*)\nSingle digit operand and operators
only:");
while((x=getchar())!='\n')
{
if(isdigit(x))
push(&s,x-48); //x-48 for removing the effect of ASCII
else
{
op2=pop(&s);
op1=pop(&s);
val=evaluate(x,op1,op2);
push(&s,val);
}
}
val=pop(&s);
printf("\nValue of expression=%d",val);
return 0;
}
Queue operations
#include <stdio.h>
#define MAX 5
int data[MAX],rear,front;
void initialize();
int full();
int empty();
void insertion();
void deletion();
void print();
int main()
{
int ch;
initialize();
do{
printf("\n 1. insertion");
printf("\n 2. deletion");
printf("\n 3. display");
printf("\n 4. exit");
printf("\n . enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:insertion();
break;
case 2:deletion();
break;
case 3:print();
break;
case 4:printf("\nthanks for using my software");
default:printf("\nenter valid choice");
}
}while(ch!=4);
return 0;
}
void initialize()
{
rear=front=-1;
}
int empty()
{
if(rear==-1)
{
return(1);
}
else{
return(0);
}
}
int full()
{
if (rear==MAX-1)
{
return(1);
}
else{
return(0);
}
}
void insertion()
{
int x;
if(full()==0)
{
printf("\nenter the data");
scanf("%d",&x);
if(rear==-1)
{
front=rear=0;
data[rear]=x;
}
else{
rear=rear+1;
data[rear]=x;
}
printf("\ndata inserted succesfully");
}
else{
printf("\nqueue is full");
}
}
void deletion()
{
int x;
if (empty()==0)
{
x=data[front];
if(rear==front)
{
rear=front=-1;
}
else{
front=front+1;
}
printf("\n\ndata deleted sucessfully=%d",x);
}
else{
printf("\nqueue is empty");
}
}
void print()
{
int i;
printf("\n queue data");
for(i=front;i<=rear;i++)
{
printf("\n%d",data[i]);
}
}
Linked list
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
int i;
struct node* head = NULL;
struct node
{
int data;
struct node* next;
};
int main()
{
int choice;
while(1)
{
printf("\n****LINKED LIST PROGRAM****\n");
printf("1. Create\n");
printf("2. display\n");
printf("3. Insert Node at beginning\n");
printf("4. Insert Node in specific position\n");
printf("5. Insert Node at end of LinkedList\n");
case 9: exit(0);
default:printf("\n Wrong Choice");
break;
}
}
}
//creates a node
void create()
{
struct node* temp;
//creating new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL) {
head = temp;
}
else{
struct node* ptr = head;
while(ptr->next!=NULL)
{
ptr = ptr->next;
}
ptr->next = temp; //inserting at end of List
}
}
// prints the entire LinkedList
void display()
{
if(head==NULL)
{
printf("Linked List is Empty\n");
return;
}
printf("LinkedList: ");
struct node* ptr = head;
while(ptr!=NULL) // start from first node
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
printf("\n");
}
// to insert node at start of LinkedList
void insert_begin()
{
struct node* temp;
// creating a new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp;
return;
}
else
{
temp->next = head; //point it to old head node
head = temp; //point head to new first node
}
}
// to insert node at given position
void insert_pos()
{
struct node* temp;
// creating a new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL) // if list empty we return
{
head = temp;
return;
}
else
{
struct node* prev_ptr;
struct node* ptr = head;
int pos;
printf("Enter position: ");
scanf("%d",&pos);
for(i=0;i<pos;i++)
{
prev_ptr = ptr;
ptr = ptr->next;
}
//new node pointing to node in that pos
temp->next = ptr;
//prevptr pointing to new node
prev_ptr->next = temp;
}
}
// to insert node at end of LinkedList
void insert_end()
{
struct node* temp;
//creating new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp; //if list is empty, we return
return;
}
else{
struct node* ptr = head;
while(ptr->next!=NULL)
{
ptr = ptr->next;
}
// tail node pointing to new node
ptr->next = temp;
}
}
Tree Traversal
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *left , *right;
};
int main() {
struct node *root = create_node(1);
insert_left(root , 12);
insert_right(root , 9);
insert_left(root->left , 5);
insert_right(root->left , 6);
return 0;
}
Adjacency matrix
#include <stdio.h>
int graph[MAX_VERTICES][MAX_VERTICES];
int main() {
int numVertices, numEdges;
return 0;
}
# viva…