Professional Documents
Culture Documents
Data Structure
Data Structure
Simple Compou
nd
existing element e e
3 Searching:- To search given element in the list of
existing element
4 Sorting :- To arrange elements in a specific order
either ascending or descending
5 Traversal:- To access element of the list one by
one
6 Merging:- To combine two or more list in a single
list
Void main()
{
Int a,b,c;
Printf(enter two no.);
Scanf(%d%d,&a,&b);
C=a+b;
Printf(sum =%d,c);
Getch();
}
1) Start
2) Read 2 no. in a and b.
3) Add a and b store in c.
4) Print c.
5) End
Int a[10];
0 1 2 3 4 5 6 7 8 9
17 18 11 7 23 28 29 45
0 1 2 3 4 5 6 7
Size=8
Mid=(b+l)/2=(0+8)/2=4
Mid=(5+8)/2=15/2=6
Program of Binary search :-
#include<stdio.h>
#include<conio.h>
BSearch(int a[],int max,int item)
{
int i,p=-1;
int l,b,mid;
b=0;
l=max-1;
while(b<=l)
{
mid=(b+l)/2;
if(a[mid]==item)
{
p=mid+1;
break;
}
else if(item>a[mid])
b=mid+1;
else
l=mid-1;
}
}
Return p;
}
void main()
{
int a[50],n,i,t,k;
printf(enter array size);
scanf(%d,&n);
printf(enter array element);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
}
printf(enter search elemnt );
scanf(%d,&t);
k=BSearch(a,n,t);
if(k==-1)
printf(Element not Found);
else
printf(Found at the position = %d,k);
getch();
}
19 37 17 5 2 3
0 1 2 3 4 5
First Pass:
19 19 19 19 19 19
37 37 17 17 17 17
17 17 37 5 5 5
5 5 5 37 2 2
2 2 2 2 37 3
3 3 3 3 3 37
#include<stdio.h>
#include<conio.h>
BSort(int a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void main()
{
int a[50],n,i;
printf(enter array size);
scanf(d,&n);
printf(enter array element);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
}
BSort(a,n);
printf(After sorting list =\n);
for(i=0;i<n;i++)
{
printf(%d,a[i]);
printf(\n);
}
getch();
}
35 16 7 17 22 5 9
0 1 2 3 4 5 6
5
16 7 17 22 35 9
#include<stdio.h>
#include<conio.h>
SSort(int a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
t=a[i];
for(j=i+1;j<n;j++)
{
if(t>a[j])
{
t=a[j];
j=i;
}
}
a[j]=a[i];
a[i]=t;
}
}
15 18 11 22 35 16 19 44 48 7 22
0 1 2 3 4 5 6 7 8 9 10
15,18,11,22,35,16,19,44,48,7,22
15,18,11,22,35,16,19,44,48,7,22
11,,15,18,22,35,16,19,44,48,7,22
11,,15,18,22,35,16,19,44,48,7,22
11,,15,18,22,35,16,19,44,48,7,22
11,,15,16,18,22,35,19,44,48,7,22
Insertion Sort
#include<stdio.h>
#include<conio.h>
insertion_sort(int a[],int n)
{
int I,j,t;
for(i=0;i<n;i++)
{
T=a[i];
while(j>=0&&t<a[j])
a[j+1]=a[j];
j--;
}
A[j+1]=t;
}
Insertion in array
#include<stdio.h>
#include<conio.h>
insert(int a[],int n, int item, int loc)
{
int i;
if(loc>n)
{
printf("can not insert");
}
else
{
loc--;
for(i=n;i>loc;i--)
{
a[i]=a[i-1];
}
a[loc]=item;
}
}
void main()
{
int a[50],n,i,item,loc;
printf("enter array size");
scanf("%d",&n);
printf("enter array element");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter new element");
scanf("%d",&item);
printf("enter Location where you want
insert ");
scanf("%d",&loc);
insert(a,n,item,loc);
printf("\nAfter Insertion Array=");
for(i=0;i<n+1;i++)
{
printf("%d",a[i]);
}
getch();
}
BY given Location
#include<stdio.h>
#include<conio.h>
delete(int a[],int n, int loc)
{
If(loc>n)
{
Printf(can not delete);
}
Else
{
Loc--;
For(i=loc;i<n;i++)
{
A[i]=a[i+1];
}
}
void main()
{
int a[50],n,i,t,loc;
printf(enter array size);
scanf(%d,&n);
printf(enter array element);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
}
printf(enter Location where you want
delete );
scanf(%d,&loc);
delete(a,n,loc);
printf("After deletion List =\n\n");
for(i=0;i<n-1;i++)
{
printf(%d,a[i]);
}
getch();
}
By given element
#include<stdio.h>
#include<conio.h>
delete(int a[],int n,int item)
{
int n;
int loc=-1;
for(k=0;k<n;k++)
{
if(a[k]==item)
loc=k;
break;
}
}
if(loc==-1)
{
printf("not found cannot delete");
exit(0);
}
else
{
for(i=loc;i<n;i++)
{
a[i]=a[i+1];
}
}
void main()
{
int a[50],n;
printf("enter array size");
scanf("d",&n);
printf("enter array element");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter deleted element");
scanf("%d",&t);
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
}
2-D Array:
An array that has two subscript is known as 2-D
array. They store data in the form of matrix or table.
Where first subscript represent row and second
represent column.
Syntax to declare 2-D- Array:
Datatype Array name[row size][column size]
Int a[3][4];
Lr=0
Ur=9
Total row =[9-0]+1=10
Lc=0
Uc=4
Total column=[4-0]+1
B=100
W=2
Address of [3][4]=100+2[5(3-0)+(4-0)]
=100+2[5*3+4]
=100+2[19]
=100+38
=138
Struct student
{
Char n[20];
Int m,r;
Struct student *next;
};
Typedef struct student st;
St *start, *mv , *p;
Void insert()
{
P=(st*)malloc (size of (st));
Printf(enter name);
Gets(p->n);
Printf(enter mark);
Scanf(%d,p->m);
Printf(enter roll no.);
Scanf(%d,p->r);
P->next =NULL;
If(start==Null)
{
Start=p;
Mv=p;
}
Else
{
mv->next=p;
mv=p;
}
}
Insert at beginning:
If(start==NULL)
{
Start=p;
}
Else
{
P->next=start;
Start=p;
}
Insert at beginning:
If(start==null)
{
Start=p;
{
Else
{
p->next=start;
start=p;
}
St *k;
K=start;
While(k!=NULL)
{
If(k->m==t)
{
P=(st*)malloc(size of(st));
p->next=k->next;
k->next=p;
}
Else
{
K=k->next;
}
}
If(start==null)
Printf(no node to display);
Else
{
St *t;
T=start;
While(t!=NULL)
{
Printf(%s,t->m);
T=t->next;
}
Deletion:
If(start==NULL)
Printf(no node to delete);
Else
{
St *t;
T=start;
Start=start->next;
Free(t);
}
Void main()
{
Start=NULL;
Int ch;
Char cho;
Do
{
Printf(1 for insertion);
Printf(2 for deletion);
Printf(3 for traversal);
Printf(enter your choice);
Scanf(%d,&ch);
If(ch==1)
Insert();
Else if(ch==2)
Delete();
Else if(ch==3)
Disp();
Printf(Do you want continue Y/N);
Scanf(%c,&cho);
}
While(cho==y||cho==Y);
Getch();
}
Struct student
{
Int m;
Struct student *next;
};
Typedef struct student st;
St *start ,*mv,*p;
Insert at end
Void insert()
{
P=(st*)malloc(sizeof(st));
Printf(enter values);
scanf(%d,&p->m);
if(start==NULL)
{
Start=p;
Mv=p;
p->next=start;
}
Else
{
Mv->next=p;
p->next=start;
mv=p;
}
}
Insert at beginning:
Void insert()
{
P=(st*)malloc(sizeof(st));
Printf(enter mark);
scanf(%d,&p->m);
if(start==NULL)
{
Start=p;
Mv=p;
p->next=p;
}
Else
{
p->next=start;
start=p;
mv->next=p;
}
}
Void delete()
{
if(start==NULL)
{
Printf(no node to delete);
}
Else
{
St *t;
T=start;
Start=start->next;
Free(t);
}
}
Struct student
{
Int m;
Struct student *next;
Struct student *pre;
};
Typedef struct student st;
St *next, *mv, *p;
Insert at beginning:
Void insert()
{
P=(st*)malloc(sizeof(st));
Printf(enter mark);
scanf(%d,&p->m);
p->pre=NULL:
p->next=NULL;
if(start==NULL)
{
Start=p;
}
Else
{
Start->pre=p;
p->next=start;
start=p;
}
}
Void disp()
{
if(start==NULL)
{
Printf(no node to display);
}
Else
{
St *t;
T=start;
While(t!=NULL)
{
Printf(%d,t->m);
T=t->next;
}
}
}
Void main()
{
Start=NULL;
Int ch;
Char cho;
Do
{
Printf(1 for insertion);
Printf(2 for deletion);
Printf(3 for traversal);
Printf(enter your choice);
Scanf(%d,&ch);
If(ch==1)
Insert();
Else if(ch==2)
Delete();
Else if(ch==3)
Disp();
Printf(Do you want continue Y/N);
Scanf(%c,&cho);
}
While(cho==y||cho==Y);
Getch();
}
Stack and Queue:
Stack:
stack is a linear type of LIFO [last in first out] type
of data structure where insertion and deletion
possible from one end called top of the stack.
Stack Operation:
1) Push: to insert a new element in the stack is
called Push.
2) Pop: to delete a element in the stack is called
Pop.
Stack Terminology:
1) Stack Overflow: when stack is full and user
want to insert a new element in stack . it is
called stack overflow.
2) Stack Underflow: when stack is empty and
user want to delete an element in stack . it is
called stack underflow.
Top 5 4 3 2 1
Struct stack
{
Int m;
Struct stack *next;
};
Typedef struct stack st;
St *top, *p;
Void push()
{
P=(st*)malloc (size of (st));
Printf(enter the value);
Scanf(%d,p->m);
p->next=NULL;
If(top==Null)
{
top=p;
}
Else
{
p->next=top;
top=p;
}
}
Void pop()
If(top==NULL)
{
Printf(\n underflow can not delete );
}
Else
{
St *t;
T=top;
Top=top->next;
Free(t);
}
}
Void disp()
{
If(top==NULL)
Printf(no element to display);
Else
{
St *t;
T=top;
While(t!=NULL)
{
Printf(%d,t->m);
T=t->next;
}
Void main()
{
top=NULL;
Int ch;
Char cho;
Do
{
Printf(1 for Push);
Printf(2 for pop);
Printf(3 for display);
Printf(enter your choice);
Scanf(%d,&ch);
If(ch==1)
push();
Else if(ch==2)
pop();
Else if(ch==3)
Disp();
Printf(Do you want continue Y/N);
Scanf(%c,&cho);
}
While(cho==y||cho==Y);
Getch();
}
Void disp()
{
If(top==-1)
Printf(element not found can not display);
Else
{
Printf(\nList=\n);
For(i=top;i>=0;i--)
{
Printf(%d,stack[top]);
}
}
Void main()
{
Int ch;
Char cho;
Printf(enter stack size);
Scanf(%d,&maxsize);
Do
{
Printf(1 for Push);
Printf(2 for pop);
Printf(3 for display);
Printf(enter your choice);
Scanf(%d,&ch);
If(ch==1)
push();
Else if(ch==2)
pop();
Else if(ch==3)
Disp();
Printf(Do you want continue Y/N);
Scanf(%c,&cho);
}
While(cho==y||cho==Y);
Getch();
}
Stack Application:
1) Reverse a string:
Enter a string
2.) Notation:
A+B->Infix
+AB ->prefix
AB+->postfix
Conversion Rules:
1) Parenthesis(())
2) Exponent (
3) Multiplication or divide
4) Addition or subtraction [left to right]
A*B-C/D+F
CONVERT THIS INFIX EXPRESSION TO POSTFIX
(AB*)-C/D+F
K-C/D+F K=AB*
K-(CD/)+F M=CD/
K-M+F T=KM-
(KM-)+F
T+F
(TF+)
KM-F+
AB*CD/-F+
(A*B-C/D+F)
CONVERT INFIX EXPRESSION INTO PREFIX
(*AB)-C/D+F K=*AB
K-C/D+F M=/CD
K-(/CD)+F T=-KM
K-M+F
(-KM)+F
T+F
(+TF)
+-KMF
+-*AB/CDF PREFIX
Q-5,6,2,+,*,12,4,/,-
Prefix to infix
(A+B*C-D/F)
Tabular form infix to postfix
( (
A ( A
+ (+ A
B (+ AB
* (+* AB
C (+* ABC
- (- ABC*+
D (- ABC*+D
/ (-/ ABC*+D
F (-/ ABC*+DF
) ABC*+DF/-
(A+B*C-D/F)
POSTFIX
A+(BC*)-D/F K=BC*
A+K-D/F
A+K-(DF/) M=DF-
A+K-M
(AK+)-M T=AK+
T-M
TM-
AK+M-
ABC*+DF/-
Give postfix form of expression for the following
QUEUE:
Queue is a linear type of data structure where
insertion is possible from one end called rear and
deletion is possible from another end called front.
Queue is a first in first out (FIFO) type of data
structure.
Queue implementation:
1) Dynamic implementation [queue as linked
list].
2) Static implementation [queue as Array].
Queue Operation:
1) Insert(): to insert a new element in the
queue.
2) Delete():to delete a new element in the
queue.
Structure queue
{
Int m;
Struct queue *next;
};
Typedef struct queue st;
St *front, *area,*p;
Void insert()
{
P=(st*)malloc (size of (st));
Printf(enter the data);
Scanf(%d,p->m);
p->next=NULL;
If(front==Null)
{
front=p;
rear=p;
}
Else
{
rear->next=top;
rear=p;
}
}
Void delete()
If(front==NULL)
{
Printf(\n No Node to delete );
}
Else
{
St *t;
T=front;
front=front->next;
Free(t);
}
}
Void disp()
{
If(fornt==NULL)
Printf(no node to display);
Else
{
St *t;
T=front;
While(t!=NULL)
{
Printf(%d,t->m);
T=t->next;
}
}
}
Void main()
{
front=NULL;
Int ch;
Char cho;
Do
{
Printf(1 for insert in queue);
Printf(2 for delete in queue);
Printf(3 for display);
Printf(enter your choice);
Scanf(%d,&ch);
If(ch==1)
insert();
Else if(ch==2)
delete();
Else if(ch==3)
Disp();
Printf(Do you want continue Y/N);
Scanf(%c,&cho);
}
While(cho==y||cho==Y);
Getch();
}
QUEUE AS ARRAY:
We can also implement queue as array.
4
3
2
1
0 max size=5 rear=-1 front=-1
int a[50],maxsize,t,front,rear;
front=-1;
rear=-1;
Void insert()
{
If(rear==maxsize-1)
Printf(can not insert queue is full);
Else
{
Printf(enter inserted element);
Scanf(%d,&t);
If(rear==-1)
{
Front=0;
Rear=0;
}
Else
{
Rear=rear+1;
}
Qu[rear]=t;
printf(element successfully inserted);
}
Void delete()
If(front==-1)
Printf(can not delete );
Else
{
T=qu[front];
front=front+1;
Printf(deleted element=%d,t);
}
}
Void disp()
{
If(front==-1)
{
Printf(no element to display);
}
Else
{
For(i=front;i<=rear;i++)
{
Printf(%d,qu[i]);
}
}
}
Void main()
{
Int ch;
Char cho;
Printf(enter queue size);
Scanf(%d,&maxsize);
Do
{
Printf(1 for insert);
Printf(2 for delete);
Printf(3 for display);
Printf(enter your choice);
Scanf(%d,&ch);
If(ch==1)
insert();
Else if(ch==2)
delete();
Else if(ch==3)
Disp();
Printf(Do you want continue Y/N);
Scanf(%c,&cho);
}
While(cho==y||cho==Y);
Getch();
}
CIRCULAR QUEUE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
# define max[5]
int deque[max];
int front=-1,rear=-1;
void display();
void insertfront();
void insertrear();
void deletefront();
void deleterear();
int choice,item;
void main()
{
clrscr();
while(1)
{
printf("\n Menu");
printf("\n 1.Insert from
Front:");
printf("\n 2.Insert from
Rear:");
printf("\n 3.Delete from
Front:");
printf("\n 4.Delete from
Rear:");
printf("\n 5.Display");
printf("\n 6.Exit");
scanf("%d",&choice);
switch(choice)
{
case 1:
insertfront();
getch();
break;
case 2:
insertrear();
getch();
break;
case 3:
deletefront();
getch();
break;
case 4:
deleterear();
getch();
break;
case 5:
display();
getch();
break;
case 6:
exit(0);
default:
printf("\n Invalid Choice");
getch();
break;
}
}
}
void insertfront()
{
if(front==0)
{
printf("\n Queue is FULL");
}
Else
{
front=front-1;
printf("\n Enter a no:");
scanf("%d",&item);
deque[front]=item;
}
}
void insertrear()
{
if(rear==max)
{
printf("\n Queue is FULL");
}
else
{
rear=rear+1;
printf("\n Enter a no.:");
scanf("%d",&item);
deque[rear]=item;
}
}
void deletefront()
{
if(front==max)
{
printf("\n Queue is
EMPTY");
}
Else
{
item=deque[front];
front=front+1;
printf("\n No. deleted is
%d",item);
}
}
void deleterear()
{
if(rear==0)
{
printf("\n Queue is
EMPTY");
}
Else
{
item=deque[rear];
rear=rear-1;
printf("\n No. deleted is
%d",item);
}
}
void display()
{
int i;
printf("\n The Queue is::");
for(i=front;i<=rear;i++)
{
printf("%d \n ", deque[i]);
}
}