Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 92

DATA STRUCTURE

To combine similar or dissimilar data type in a single


unit is called data structure.
Data structure has well defined operation and behavior.

Data structure + algorithm=program


DATA STRUCTURE

Simple Compou
nd

Arra Structu Line Non-


Linear
y Operation re on data structurear
1 Insertion:- Insertion means insert an element in the
list of an existing element.
2 Deletion Stack element Linked
Queu
:- To delete an Tre
inListthe list of
Graph

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

Algorithm: It is a step by step process to solve a


particular problem.

Flowchart: It is a pictorial or graphical


representation to solve a particular problem.

Program: Collection of instruction that understand


by computer.
ADD TWO NO.

Void main()
{
Int a,b,c;
Printf(enter two no.);
Scanf(%d%d,&a,&b);
C=a+b;
Printf(sum =%d,c);
Getch();
}

Q. Write a algorithm to add two no.?

1) Start
2) Read 2 no. in a and b.
3) Add a and b store in c.
4) Print c.
5) End

Array: It is used to store multiple values under


single name.
1) One-d-array.
2) Two-d-array.

One-d-array: An array that has one subscript is


known as one-d-array.
They store data in the form of list or vector.

Int a[10];

0 1 2 3 4 5 6 7 8 9

Passing An array to a function:

We can pass an array to a function by


using array name. by default an array is a
reference type.

Void disp(int a[])


{
A[0]=19;
A[1]=57;
}
Void main()
{
Int p[]={5,7};
Disp(p);
Printf(%d,p[0]); =>19
Printf(%d,p[1]); =>57
}
Searching:- Searching means to search given
element in the list of existing element there are many
types of searching technique present but two are
mostly used
1. Linear Search
2. Binary Search
Linear Search:- In Linear search we search given
element in the list one by one if element found then
stop searching and display message element found
otherwise say not found. Linear search can work on
sorted or unsorted array both.

17 18 11 7 23 28 29 45
0 1 2 3 4 5 6 7
Size=8

LSearch(int a[],int maxsize,int item)

Program of linear search :-


#include<stdio.h>
#include<conio.h>
LSearch(int a[],int maxsize,int item)
{
int i,p=-1;
for(i=0;i<maxsize;i++)
{
if(a[i]==item)
{
p=i+1;
break;
}
}
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=LSearch(a,n,t);
if(k==-1)
printf(Not found);
else
printf(found at position %d,k);
getch();
}
LSearch(int a[],int maxsize,int item)
1.) Start
2.) For i=0 to maxsize
If a[i]==item
Set p=i+1;
Break;
3.) If(p==-1)
Printf not found

4.) Else found


5.) End

Binary Search:- Binary Search is somewhat difficult


comparison to linear search, It is also called Half-
Interval Search. Binary Search requires fewer
comparison than Linear Search as it works on Break
& Search Concept.
Each time this Algorithm matches Key element to
the middle element of Array , if it matches then
Value and index is returned othervise if Key element
is less than the middle element of array then
algorithm searches again in Sub Array of left side, in
case of greater Key element, Algorithm searches in
Sub Array of right side.
B l
17 18 25 35 42 47 48 65 77
0 1 2 3 4 5 6 7 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();
}

BSearch(int a[],int s,int item)


Sorting: - Sorting means to arrange array elements in
a specific order either ascending(A to Z) or
descending(Z to A).
There are many sorting technique present Like
Bubble Sort, Insertion Sort, Selection Sort, Heap
Sort, Radix sort, Quick sort, Merge Sort.etc.

Bubble Sort: - In bubble sort we compare adjacent


elements. If they are in improper order then swap
these elements.

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();
}

Selection Sort :- The algorithm divides the input list


into two parts: the sublist of items already sorted,
which is built up from left to right at the front (left)
of the list, and the sublist of items remaining to be
sorted that occupy the rest of the list. Initially, the
sorted sublist is empty and the unsorted sublist is the
entire input list. The algorithm proceeds by finding
the smallest (or largest, depending on sorting order)
element in the unsorted sublist, exchanging it with
the leftmost unsorted element (putting it in sorted
order), and moving the sublist boundaries one
element to the right.

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;
}
}

Insertion Sort :- in insertion sort each successive


number compared its previous element and place at
appropriate position. These step repeat step until

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 an Array:- It means insert a


new element in the list of existing
elements is called insertion in an array.

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();
}

Deletion :- Deletion means to delete an


element from the list of existing
elements.
We can delete
1. by given location
2. by given element

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];

A 2-D Array store in :


1.) Row major form
2.) Column major form

1.) Row major form

Address of [I,j]th elements


=B+W[no. of column(I-Lr)+(j-Lc)]

2.) Column major form

Address of [I,j]th elements


=B+W[(I-Lr)+ No.of rows(j-Lc)]
Lr: Lower row
Lc: Lower column
B: Base address
W: Element size

An array stored a[10][5] in row major form an


element required 2 byte storage . The base address is
100 find the address of [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

Linked List: Linked list is a dynamic type of data


structure linked list is a collection of node each node
divided into two parts.

1.) Data part (hold the actual data)


2.) Link Part (hold the memory address of next
node).

There are 4 types of linked list.


1.) Singly linked list.
2.) circular linked list.
3.) doubly linked list.
4.) circular doubly linked list.

To create link list


1.) self referential structure
2.) pointer
3.) dynamic memory allocation
function malloc()

and deallocate function free()

Singly linked list:


1.) Insertion at end

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;
}

Insert at given location:

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;
}
}

Traverse in singly linked list:

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();
}

Circular Linked List:

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;
}
}

Traversal in circular linked list:


Void disp()
{
if(start==NULL)
{
Printf(no node to show);
}
Else
{
St *t;
While(t->next!=start)
Printf(%d,t->m);
T=t->next;
}
}

Deletion In Circular Linked List

Void delete()
{
if(start==NULL)
{
Printf(no node to delete);
}
Else
{
St *t;
T=start;
Start=start->next;
Free(t);
}
}

Doubly Linked List:

Struct student
{
Int m;
Struct student *next;
Struct student *pre;
};
Typedef struct student st;
St *next, *mv, *p;

Insertion at end in doubly linked list:


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;
Mv=p;
}
Else
{
p->pre=mv;
mv->next=p;
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;
}
}

Deletion in doubly linked list:


Void delete()
{
if(start==NULL)
{
Printf(no node to delete);
}
Else
{
St *t;
T=start;
Start=start->next;
Start->pre=NULL;
Free(t);
}
}

Traversal in Doubly Linked List:

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;
}
}
}

Circular Doubly Linked List:


Struct student
{
Int m;
Struct student *next;
Struct student *pre;
};
Typedef struct student st;
St *next, *mv, *p;
Insert at end in circular doubly linked list:
Void insert()
{
P=(st*)malloc(sizeof(st));
Printf(enter mark);
scanf(%d,&p->m);
if(start==NULL)
{
Mv=p;
Start=p;
Start->pre=p;
Start->next=p;
}
Else
{
p->pre=mv;
mv->next=p;
mv=p;
p->next=start;
start->pre=p;
}
}

Insert at beginning in circular doubly linked list:


Void insert()
{
P=(st*)malloc(sizeof(st));
Printf(enter mark);
scanf(%d,&p->m);
if(start==NULL)
{
Mv=p;
Start=p;
Start->pre=p;
Start->next=p;
}
Else
{
Start->pre=p;
p->next=start;
start=p;
p->pre=mv;
mv->next=start;
}
}

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 can be implemented by:


1) Linked list [Dynamic implementation of stack]
2) Array [static implementation of 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.

Dynamic Implementation of stack:

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();
}

Stack As array[static implementation of stack]


int stack[50],top=-1,maxsize;
{
Int m;
}
Void push()
{
If(top==maxsize-1)
{
Printf(overflow can not insert);
}
Else
{
Printf(enter inserted element);
Scanf(%d,&t);
top=top+1;
stack[top]=t;
printf(element successfully inserted);
}
}
Void pop()
If(top==-1)
{
Printf(\n underflow can not delete );
}
Else
{
T=stack[top];
Top=top-1;
Printf(deleted element=%d,t);
}
}

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:

Notation are 3 types:


1) Infix notation
2) Prefix notation
3) Postfix 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

Scanned operation Stack intermediate


symbol result

5 push into stack 5


6 5,6
2 5,6,2
+ pop to operand 5 6+2=8
Push result 5,8
* pop to operand empty 5*8=40
Push row 40
12 push 40,12
4 push 40,12,4
/ pop 40 12/4=3
Push result 40,3
pop to operand empty 40-3=37
push 37

(A+B*C-D/F)
Tabular form infix to postfix

Input Stack O/P

( (
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

NOT A OR NOT B NOT C


[PRIORITY ORDER IS NOT AND OR]

(A NOT ) OR (B NOT) AND(C NOT)


(A NOT ) OR (B NOT CNOT AND)
A NOT B NOT C NOT AND OR

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.

Queue as linked List:

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:

A circular queue is one in which the insertion of a


new element is done at very first location of the
queue if the last location of the queue is full.
Void insert()
{
If(front==0&&raer==maxsize-1)||rear==front-1))
{
Printf(overflow can not insert);
}
Else
{
Printf(enter inserted element);
Scanf(%d,&t);
If(front==-1)
{
Front=0;
Rear=0;
}
Else if(rear==maxsize-1)
{
Rear=0;
}
Else
{
Rear=rear+1;
}
Qu[rear]=t;
printf(element successfully inserted);
}
Void delete()
If(front==-1)
{
Printf(underflow can not delete );
}
Else
{
T=qu[front];
Printf(deleted element=%d,t);
If(front==rear)
{
Front=-1;
Rear=-1;
}
Else if(front==maxsize-1)
{
Front=0;
}
Else
{
Front=front+1;
}
}
}
Void disp()
{
If(front==-1)
{
Printf(no element to display);
}
Else
{
If(rear>=front)
{
For(i=front;i<=rear;i++)
Printf(%d,qu[i]);
}
Else
{
For(i=front;i<=rear;i++)
{
Printf(%d,qu[i]);
}
For(i=0;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();
}

Double Ended Queue:

It is also a homogeneous list of elements which


insertion and deletion operations are performed from
both ends .we can insert element from the rear and
or form the front end. Hence it is called double
ended queue.

It is commonly known as Dequeue


There are two types of dequeue:
1) Input restricted queue.
2) output restricted queue.

1.) Insertion of an element at the rear end of


queue.
2.) deletion of an element at the front end of
queue.
3.) Insertion of an element at the front end of
queue.
4.) Deletion of an element at the rear end of
queue.

IN input restricted queue 1,2 and 4


operation are valid and in output
restriced queue 1,2,3 are valid
Pg. No. 273
1,2,3,4

Double ended queue


deque (usually pronounced like "deck") is
an irregular acronym of double-ended queue.
Double-ended queues are sequence containers
with dynamic sizes that can be expanded or
contracted on both ends (either its front
or its back).

Specific libraries may implement deques in


different ways, generally as some form of
dynamic array. But in any case, they allow
for the individual elements to be accessed
directly through random access iterators,
with storage handled automatically by
expanding and contracting the container as
needed.

Therefore they provide a similar


functionality as vectors, but with
efficient insertion and deletion of
elements also at the beginning of the
sequence, and not only at its end. But,
unlike vectors, deques are not guaranteed
to store all its elements in contiguous
storage locations, thus not allowing direct
access by offsetting pointers to elements.

#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]);
}
}

You might also like