Professional Documents
Culture Documents
#include<stdio.h>
#include<conio.h>
void main()
int a[10],i,n;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("print elements");
for(i=0;i<n;i++)
printf("\n %d",a[i]);
getch();
Output:-
Page 1 of 74
Page 2 of 74
HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int a[5],i,n,k,item;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
for(i=0;i<=n;i++)
printf("\n %d",a[i]);
scanf("%d",&k);
scanf("%d",&item);
Page 3 of 74
for(i=n;i>=k;i--)
a[i+1]=a[i];
a[k]=item;
n=n+1;
for(i=0;i<=n;i++)
getch();
Output:-
Page 4 of 74
HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int a[5],i,n,k;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
for(i=0;i<=n;i++)
printf("\n %d",a[i]);
scanf("%d",&k);
for(i=n;i>=k;i--)
a[k-1]=a[k];
Page 5 of 74
}
n=n-1;
for(i=0;i<=n;i++)
getch();
Output:-
Page 6 of 74
HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int a[20][20],j,i,m,n;
clrscr();
scanf("%d",&m);
scanf("%d",&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(i<=j)
a[i][j]=0;
else{
scanf("%d",&a[i][j]);
Page 7 of 74
}
for(i=0;i<m;i++)
for(j=0;j<m;j++)
printf("%d\t",a[i][j]);
printf("\n");
getch();
Output:-
Page 8 of 74
2. STACK
HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int s[10],maxsize=7,top,item,i;
clrscr();
printf("enter no");
scanf("%d",&top);
for(i=0;i<=top;i++)
scanf("%d",&s[i]);
printf("elements of stack");
for(i=0;i<=top;i++)
printf(" \n %d",s[i]);
scanf("%d",&item);
Page 9 of 74
if(top==maxsize)
printf("overflow");
else
s[top]=item;
top=top+1;
for(i=0;i<=top;i++)
printf("\n %d",s[i]);
getch();
Output:
Page 10 of 74
HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int s[10],top,item,i;
clrscr();
printf("enter no");
scanf("%d",&top);
for(i=0;i<=top;i++)
scanf("%d",&s[i]);
printf("elements of stack");
for(i=0;i<=top;i++)
printf(" \n %d",s[i]);
if(top==0)
Page 11 of 74
printf("underflow");
else
top=top-1;
item=s[top];
for(i=0;i<=top;i++)
printf("\n %d",s[i]);
getch();
Output:-
Page 12 of 74
HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int s[10],top,item,i;
clrscr();
printf("enter no");
scanf("%d",&top);
for(i=0;i<=top;i++)
scanf("%d",&s[i]);
printf("elements of stack");
for(i=0;i<=top;i++)
printf(" \n %d",s[i]);
if(top==0)
Page 13 of 74
{
printf("underflow");
else
item=s[top];
for(i=0;i<=top;i++)
printf("\n %d",s[i]);
getch();
Output:-
Page 14 of 74
HARSHARAN DEEP
4./* wap to reverse the string in stack. */
#include<stdio.h>
#include<conio.h>
#include<string.h>
char stack[20],i;
int top=-1;
void main()
void reverse(char[]);
char str[50];
clrscr();
printf("enter a string:");
gets(str);
reverse(str);
getch();
int i;
void push(char);
char pop();
Page 15 of 74
int isempty();
for(i=0;i<strlen(str);i++)
char ch=str[i];
push(ch);
while(! isempty())
printf("%c",stack[top]);
top=top-1;
void push(char c)
top=top+1;
stack[top]=c;
char pop()
char c=stack[top];
top=top-1;
return c;
int isempty()
if(top==-1)
return(1);
else
return(0);
Page 16 of 74
} Output:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct s
int info;
struct s *next;
} *top;
void create();
void push();
void pop();
void peek();
stack *start,*ptr,*loc;
int data,item,ele,exit;
void main()
int choice;
Page 17 of 74
char ch='y';
clrscr();
while(ch=='y')
printf("\n1. create \n2. push \n3. pop \n4. peek \n5. exit");
scanf("%d",&choice);
switch(choice)
case 1:
create();
break;
case 2:
push();
break;
case 3:
pop();
break;
case 4:
peek();
break;
case 5:
exit();
fflush(stdin);
Page 18 of 74
ch=getchar();
getch();
void create()
top=(stack*)malloc(sizeof(stack));
ptr=top;
scanf("%d",&data);
ptr->info=data;
ptr->next=NULL;
top=ptr;
peek();
void push()
loc=(stack*)malloc(sizeof(stack));
scanf("%d",&data);
loc->info=data;
loc->next=top;
top=loc;
peek();
void pop()
Page 19 of 74
{
if(top==NULL)
else
loc=top;
item=loc->info;
top=top->next;
free(loc);
peep();
void peep()
ptr=top;
while(ptr!=NULL)
ele=ptr->info;
printf("\n [%d]",ele);
ptr=ptr->next;
}Output:-
Page 20 of 74
6/*Program for parenthesis checking*/ HARSHARAN DEEP
#include<stdio.h>
#define MAX 30
int top=-1;
int stack[MAX];
void push(char);
char pop();
main()
char exp[MAX];
int valid;
gets(exp);
Page 21 of 74
valid=check(exp);
if(valid==1)
printf("Valid expression\n");
else
printf("Invalid expression\n");
return 0;
int i;
char temp;
for(i=0;i<strlen(exp);i++)
push(exp[i]);
return 0;
else
temp=pop();
if(!match(temp, exp[i]))
Page 22 of 74
printf("Mismatched parentheses are : ");
return 0;
printf("Balanced Parentheses\n");
return 1;
else
return 0;
return 1;
return 1;
return 1;
return 0;
Page 23 of 74
}
if(top==(MAX-1))
printf("Stack Overflow\n");
return;
top=top+1;
stack[top]=item;
char pop()
if(top==-1)
printf("Stack Underflow\n");
exit(1);
return(stack[top--]);
Output:-
Page 24 of 74
3.Queues
1./* program to process enqueue operation */HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int q[10],front=1,rear=0,maxsize=10,item,i;
clrscr();
printf("enter rear");
scanf("%d",&rear);
for(i=1;i<=rear;i++)
Page 25 of 74
scanf("%d",&q[i]);
for(i=1;i<=rear;i++)
printf("%d\n",q[i]);
printf("enter item");
scanf("%d",&item);
if(rear==maxsize)
printf("overflow");
else
rear=rear+1;
q[rear]=item;
for(i=1;i<=rear;i++)
printf("\n %d",q[i]);
getch();
Output:-
Page 26 of 74
2/* program to carry out dequeue operation */ HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int q[10],front,rear,maxsize=10,item,i;
clrscr();
printf("enter no.");
scanf("%d",&front);
for(i=1;i<=front;i++)
scanf("%d",&q[i]);
Page 27 of 74
}
printf("elements of queue");
for(i=1;i<=front;i++)
printf("%d\n",q[i]);
if(rear==0)
printf("underflow");
item=q[front];
if(front<rear);
front=front+1;
} }
else
front=1;
rear=0;
printf("element deleted");
for(i=1;i<=front;i++)
printf("%d",q[i]);
getch();
Page 28 of 74
Output:-
4.Linked list
1./* wap for traversing a link list */HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
#define NULL 0
int info;
}node;
void main()
Page 29 of 74
node* create();
void traverse(node*);
int data;
node *head,*loc,*ptr;
clrscr();
head=create();
traverse(head);
ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
free(head);
head=ptr;
getch();
node* create()
node *ptr,*head=NULL;
char ch='y';
int data;
while(ch=='y'||ch=='y')
if(head==NULL)
Page 30 of 74
{
head=(node*)malloc(sizeof(node));
ptr=head;
else
ptr->link=(node*)malloc(sizeof(node));
ptr=ptr->link;
scanf("%d",&data);
ptr->info=data;
fflush(stdin);
scanf("%c",&ch);
ptr->link=NULL;
return(head);
node* ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
Page 31 of 74
}
return;
Output:-
#include<stdio.h>
#include<conio.h>
#define NULL 0
int info;
}node;
Page 32 of 74
void main()
node* create();
void traverse(node*);
node* search(node*,int);
int item,data;
node *head,*loc,*ptr;
clrscr();
head=create();
traverse(head);
scanf("%d",&item);
search(head,item);
ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
free(head);
head=ptr;
getch();
node* create()
Page 33 of 74
node *ptr,*head=NULL;
char ch='y';
int data;
while(ch=='y'||ch=='y')
if(head==NULL)
head=(node*)malloc(sizeof(node));
ptr=head;
else
ptr->link=(node*)malloc(sizeof(node));
ptr=ptr->link;
scanf("%d",&data);
ptr->info=data;
fflush(stdin);
scanf("%c",&ch);
ptr->link=NULL;
return(head);
Page 34 of 74
{
node* ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
return;
node *loc,*ptr;
loc=NULL;
ptr=head;
while ((ptr!=NULL)&&(item!=ptr->info))
ptr=ptr->link;
printf("searched item=%d",item);
if(item==ptr->info)
loc=ptr;
else
printf("nothing found");
return(loc);
Page 35 of 74
}
Output:-
3./* wap to insert node at given location in link list */ HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
#define NULL 0
{
Page 36 of 74
int info;
}node
void main()
node *create();
void traverse(node*);
node *search(node*,int);
int item,data;
node *head,*loc,*ptr;
clrscr();
head=create();
traverse(head);
scanf("%d",&item);
loc=search(head,item);
scanf("%d",&data);
traverse(head);
ptr=head;
while(ptr!=NULL)
Page 37 of 74
ptr=ptr->link;
free=(head)
head=ptr;
getch();
node* create()
node* ptr*,head=NULL;
char ch='y';
int data
while(ch=='y'||ch=='y')
if(head==NULL)
head=(node*)malloc(sizeof(node));
ptr=head;
else
ptr->link=(node*)malloc(sizeof (node));
ptr=ptr->link;
scanf("%d",&data);
Page 38 of 74
ptr->info=data;
fflush(stdin);
scanf("%d",&ch);
ptr->link=NULL;
return(head);
node* ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
return;
node*loc;*ptr;
loc=NULL;
ptr=head;
while ((ptr!=NULL)&&(item!=ptr->info))
ptr=ptr->link;
if(item==ptr->info)
loc=ptr;
return(loc);
Page 39 of 74
}
node* new;
new=(node*)malloc(sizeof (node));
new->info=item;
if(loc==NULL)
new->link=*head;
else
new->link=loc->link;
loc->link=new;
} Output
#include<stdio.h>
#include<conio.h>
Page 40 of 74
#define NULL 0
int info;
}node;
void main()
node* create();
void traverse(node*);
node* search(node*,int);
int item,data;
node *head,*avail,*ptr;
clrscr();
head=create();
traverse(head);
scanf("%d",&item);
instbeg(&head,item,avail);
traverse(head);
ptr=head;
while(ptr!=NULL)
Page 41 of 74
{
ptr=ptr->link;
free(head);
head=ptr;
getch();
node* create()
node *ptr,*head=NULL;
char ch='y';
int data;
while(ch=='y'||ch=='y')
if(head==NULL)
head=(node*)malloc(sizeof(node));
ptr=head;
else
ptr->link=(node*)malloc(sizeof(node));
ptr=ptr->link;
scanf("%d",&data);
Page 42 of 74
ptr->info=data;
fflush(stdin);
scanf("%c",&ch);
ptr->link=NULL;
return(head);
node* ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
return;
node* new;
new=(node*)malloc(sizeof (node));
new->info=item;
if(avail==NULL)
printf("overflow");
Page 43 of 74
}
else
new->link=*head;
*head=new;
}}
Output:-
Page 44 of 74
5./* program to insert node at the end of link list */ HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
#define NULL 0
int info;
}node;
void main()
node* create();
void traverse(node*);
node* search(node*,int);
int item,data;
node *head,*avail,*ptr;
clrscr();
head=create();
traverse(head);
scanf("%d",&item);
instend(&head,item,avail);
Page 45 of 74
traverse(head);
ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
free(head);
head=ptr;
getch();
node* create()
node *ptr,*head=NULL;
char ch='y';
int data;
while(ch=='y'||ch=='y')
if(head==NULL)
head=(node*)malloc(sizeof(node));
ptr=head;
else
ptr->link=(node*)malloc(sizeof(node));
Page 46 of 74
ptr=ptr->link;
scanf("%d",&data);
ptr->info=data;
fflush(stdin);
scanf("%c",&ch);
ptr->link=NULL;
return(head);
node* ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
return;
node* new,*ptr;
new=(node*)malloc(sizeof (node));
Page 47 of 74
new->info=item;
if(avail==NULL)
printf("overflow");
else
new->link=NULL;
ptr=*head;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=new;
Output:-
Page 48 of 74
6./* deleting a given node in link list */ HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
#define NULL 0
int info;
}node;
void main()
node* create();
void traverse(node*);
node* search(node*,int);
int item,data;
node *head,*loc,*ptr;
clrscr();
head=create();
traverse(head);
Page 49 of 74
scanf("%d",&item);
loc=search(head,item);
scanf("%d",&data);
delloc(&head,data,loc);
traverse(head);
ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
free(head);
head=ptr;
getch();
node* create()
node *ptr,*head=NULL;
char ch='y';
int data;
while(ch=='y'||ch=='y')
if(head==NULL)
Page 50 of 74
head=(node*)malloc(sizeof(node));
ptr=head;
else
ptr->link=(node*)malloc(sizeof(node));
ptr=ptr->link;
scanf("%d",&data);
ptr->info=data;
fflush(stdin);
scanf("%c",&ch);
ptr->link=NULL;
return(head);
node* ptr=head;
while(ptr!=NULL)
ptr=ptr->link;
Page 51 of 74
return;
node *loc,*ptr;
loc=NULL;
ptr=head;
while ((ptr!=NULL)&&(item!=ptr->info))
ptr=ptr->link;
printf("searched item=%d",item);
if(item==ptr->info)
loc=ptr;
return(loc);
node* new,*ptr,*ploc,*avail;
new=(node*)malloc(sizeof (node));
new->info=item;
if(*head==NULL)
printf("underflow");
ptr=*head;
ploc=NULL;
while((ptr!=loc)&&(ptr->link!=NULL))
Page 52 of 74
{
ploc=ptr;
ptr=ptr->link;
if(ptr!=loc)
if(ploc==NULL)
*head=(*head)->link;
else
ploc->link=loc->link;
loc->link=avail;
avail=loc;
Page 53 of 74
}Output:-
#include<stdio.h>
#include<conio.h>
struct t
int info;
};
Page 54 of 74
typedef struct t tree;
tree *ptr,*pptr,*nptr,*root;
int data;
void main()
char ch='y';
clrscr();
root=NULL;
while(ch=='y')
scanf("%d",&data);
ptr=(tree *)malloc(sizeof(tree));
ptr->info=data;
ptr->left=NULL;
ptr->right=NULL;
if(root==NULL)
root=ptr;
else
pptr=NULL;
nptr=root;
while(nptr!=NULL)
Page 55 of 74
{
pptr=nptr;
if(data<nptr->info)
nptr=nptr->left;
else
nptr=nptr->right;
if(data<pptr->info)
pptr->left=ptr;
else
{pptr->right=ptr;
ch=getche();
inorder(root);
preorder(root);
Page 56 of 74
printf("\n postorder of tree is:");
postorder(root);
getch();
if(p!=NULL)
inorder(p->left);
printf("[%d]",p->info);
inorder(p->right);
if(p!=NULL)
printf("[%d]",p->info);
preorder(p->left);
preorder(p->right);
if(p!=NULL)
Page 57 of 74
postorder(p->left);
postorder(p->right);
printf("[%d]",p->info);
Output:-
#include<stdio.h>
Page 58 of 74
#include<conio.h>
void main()
int a[10],i,n,loc,item;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&item);
for(i=0;i<n;i++)
if(item==a[i])
break;
if(i==n)
getch();
Page 59 of 74
Output:-
#include<stdio.h>
Page 60 of 74
#include<conio.h>
#define MAX 10
void main()
int a[MAX],n,item,beg,end,mid,loc,i;
clrscr();
printf("Enter no of elments:");
scanf("%d",&n);
for(i=0;i<n;i++)
Printf(a[%d],i);
scanf("%d",&a[i]);
scanf("%d",&item);
beg=0;end=MAX-1;mid=(beg+end)/2;
while(beg<=end&&a[mid]!=item)
if(item<a[mid])
end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
if(a[mid]==item)
Page 61 of 74
printf("\n %d is present at %d location",item,mid+1);
else
printf("location is -1");
getch();
Output:
Page 62 of 74
#include<stdio.h>
#include<conio.h>
void main()
int a[10],i,n,j,temp;
clrscr();
printf("enter n:-");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
Printf(a[%d],i);
scanf("%d",&a[i]);
for(i=0;i<=n-1;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
Page 63 of 74
printf("after sorting array:-");
for(i=0;i<=n-1;i++)
printf("\na[%d]=%d"n,,a[i]);
getch();
Output:-
Page 64 of 74
5/* sorting by insertion sort */ HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int a[10],n,i,target,j;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=1;i<=n;i++)
Printf(a[%d],i);
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
target=a[i];
j=i-1;
while(target<a[j]&&j>=1)
a[j+1]=a[j];
j=j-1;
a[j+1]=target;
Page 65 of 74
printf("after sorted elements:");
for(i=1;i<=n;i++)
printf("\n%d",a[i]);
getch();
Output:-
Page 66 of 74
6./*sorting using selection sort.*/ HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
void main()
int a[20],i,n,j,temp,loc;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
loc=i;
for(j=i+1;j<=n;j++)
if (a[j]<a[loc])
loc=j;
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
Page 67 of 74
}
for(i=1;i<=n;i++)
printf("\n%d",a[i]);
getch();
Output:-
Page 68 of 74
7./* program to implement quick sort */ HARSHARAN DEEP
#include<stdio.h>
#include<conio.h>
Page 70 of 74
8./* program for merge sort */
#include <stdio.h>
#include<conio.h>
void main(void)
int array[100],n,i = 0;
clrscr();
scanf("%d",&n);
printf("\tArray[%d] = ",i);
scanf("%d",&array[i]);
}printf("\nBefore Mergesort:");
printf("%4d", array[i]);
printf("\n");
mergesort(array, 0, n - 1);
Page 71 of 74
{
printf("%4d", array[i]);
printf("\n");
getch();
int i = 0;
int length = h - l + 1;
int pivot = 0;
int merge1 = 0;
int merge2 = 0;
int temp[100];
if(l == h)
return;
pivot = (l + h) / 2;
mergesort(arr, l, pivot);
merge1 = 0;
merge2 = pivot - l + 1;
Page 72 of 74
if(merge2 <= h - l)
arr[i + l] = temp[merge2++];
else
arr[i + l] = temp[merge1++];
}}
else
arr[i + l] = temp[merge2++];
}}
else
arr[i + l] = temp[merge1++];
Page 73 of 74
}}} Output
Page 74 of 74