XII - CS 2014-15 DATA STRUCTURE in C++ (A COLLECTION OF PROGRAMS- UPDATED on 23-12-2014 )
Syllabus for XII CS
2014-15 (DATA STRUCTURES)
Introduction to data
structure – arrays, stacks, queuesArrays: One and two Dimensional arrays:
Sequential allocation and address calculation;One dimensional array: Traversal,
Searching (Linear, Binary Search), Insertion of an element in an array,deletion
of an element from an array, Sorting (Insertion, Selection,
Bubble)Two-dimensional arrays: Traversal Finding sum/difference of two NxM
arrays containing numericvalues,Interchanging Row and Column elements in a two
dimensional array;Stack (Array and Linked implementation of Stack):
Introduction to stack (LIFO_Last in FirstOutOperations) Operations on Stack
(PUSH and POP) and its Implementation in C++, Convertingexpressionsfrom INFIX
to POSTFIX notation and evaluation of Postfix expression;Queue: (Circular Array
and Linked Implementation): Introduction to Queue (FIFO - First in
Firstoutoperations) Operations on Queue (Insert and Delete and its
Implementation in C++.
………. BUBBLE SORT
……
#include<conio.h>
#include<iostream.h>
void
main(){
int
a[50];
int
i,j,n,temp;
cout<<"\n
BUBBLE SORT \n";
cout<<"
ENTER TOTAL NO. OF ELEMENTS ( N<=50 )TO BE SORTED : ";
cin>>n;
cout<<"
NOW START DATA ENTRY... \n";
for(j=0;j<n;j++)
{
cout<<"\n Enter element no.
"<<(j+1)<<'\t';
cin>>a[j];
}
for(i=0;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;
}
}
cout<<"\n
RESULT : ARRAY AFTER SORTING IS \n";
for(j=0;j<n;j++)
cout<<a[j]<<" ";
cout<<endl;
getch();
}
……… INSERTION SORT
……………
#include<conio.h>
#include<iostream.h>
void
main()
{int
a[50];
int
i,j,n,temp;
clrscr();
cout<<"\n
INSERTION SORT \n";
cout<<"
ENTER TOTAL NO. OF ELEMENTS ( N<=50 )TO BE SORTED : ";
cin>>n;
cout<<"
NOW START DATA ENTRY... \n";
for(j=0;j<n;j++)
{ cout<<"\n Enter element no. "<<(j+1)<<'\t';
cin>>a[j];
}
for(i=0;i<n-1;i++)
{
temp=a[i+1];
j=i;
while((a[j]>temp)&&(j>=0))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout<<"\n
RESULT : ARRAY AFTER SORTING IS \n";
for(j=0;j<n;j++)
cout<<a[j]<<"
";
cout<<endl;
getch();
}
………………………
SELECTION SORT ……………………..
#include<conio.h>
#include<iostream.h>
void
main()
{
int
a[50];
int
i,j,n,smallest,temp;
//clrscr();
cout<<"\n
SELECTION SORT \n";
cout<<"
ENTER TOTAL NO. OF ELEMENTS ( N<=50 )TO BE SORTED : ";
cin>>n;
cout<<"
NOW START DATA ENTRY... \n";
for(j=0;j<n;j++)
{ cout<<"\n Enter element no.
"<<(j+1)<<'\t';
cin>>a[j];
}
for(i=0;i<n-1;i++)
{
smallest=i;
for(j=i+1;j<n;j++)
if(a[smallest]>a[j])
smallest=j;
temp=a[i];
a[i]=a[smallest] ;
a[smallest]=temp;
}
cout<<"\n
RESULT : ARRAY AFTER SORTING IS \n";
for(j=0;j<n;j++)
cout<<a[j]<<" ";
cout<<endl;
getch();
}
…………… BINARY
SEARCH ……..........
#include<conio.h>
#include<iostream.h>
void
main() {
int
a[50];
int
j,n,data,found,first,mid,last;
//clrscr();
cout<<"\n
BINARY SEARCH \n";
cout<<"
ENTER TOTAL NO.OF ELEMENTS ( N<=50 )PRESENT IN THE ARRAY (IN ASCENDING
ORDER) : ";
cin>>n;
for(j=0;j<n;j++)
{
cout<<"\n Enter element no. "<<(j+1)<<'\t';
cin>>a[j];
}
cout<<" NOW ENTER A DATA TO BE SEARCHED...
\n";
cin>>data;
first=0; last=n-1; found=0;
while(first<=last)
{
mid=(first+last)/2;
if(data==a[mid]) {found=1;break;}
if(data<a[mid]) last=mid-1;
else first=mid+1;
}
if(found) cout<<" ELEMENT FOUND AT POSITION
"<<(mid+1);
else cout<<" ELEMENT NOT FOUND";
cout<<endl;
getch();
}
// A Program To Add Elements (Add At
Beginning) In Dynamic Linked List And
// Display Its Content..(Ok..Tested)
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL;
//GLOBAL POINTER VARIABLE
node * create(int
d)
{
node * p;
p=new node;
p->data=d;
p->next=NULL;
return p;
}
void
insert_at_begining(node * p)
{
if(start==NULL)
start=p;
else
p->next=start;
start=p;
}
void
display(node * q)
{
while(q)
{ cout<<q->data<<" --->";
q=q->next;
}
}
void
main()
{ node *
newptr;
char
ch='y';int d;
while(ch=='y'|| ch=='Y' )
{
cout<<"\n Enter a data item in LINKED LIST
: ";
cin>>d;
newptr= create(d);
insert_at_begining(newptr);
cout<<"\n Do you want to enter more elements ?\n (press y or Y
to continue ...) ";
cin>>ch;
}
cout<<"\n NOW CONTENT OF LINKED LIST IS : \n" ;
display(start);
getch();
}
// A PROGRAM TO PERFORM PUSH() ,POP() In LINKED
STACK ( Dynamic Stack) AND //DISPLAY( ) ITS CONTENT..(OK..TESTED)
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL ,
*p; //..GLOBAL POINTER VARIABLE
node * create(int
d)
{
p=new node;
p->data=d;
p->next=NULL;
return p;
}
void
PUSH(node * p)
{
if(start==NULL)
start=p;
else
p->next=start;
start=p;
}
void POP()
{
if(start==NULL) cout<<"\n EMPTY STACK ..";
else
{
p=start;
start=start->next;
delete p;
}
}
void
display()
{
node * p;
if(start==NULL) cout<<"\n EMPTY STACK ..";
else
{
p=start;
while(p)
{ cout<<p->data<<" --->";
p=p->next;
}
}
}
void
main()
{
node * newptr;
char ch='y';int d;
while(ch=='y'|| ch=='Y' )
{
cout<<"\n Enter a data item in LINKED STACK
: ";
cin>>d;
newptr= create(d);
PUSH(newptr);
cout<<"\n Do you want to enter more elements ? (press y
or Y to continue ...) ";
cin>>ch;
}
cout<<"\n NOW CONTENT OF STACK IS : \n" ;
display();
cout<<"\n Do you want to POP more elements ? (press y or Y to
continue ...) ";
cin>>ch;
while(ch=='y'|| ch=='Y' )
{
POP();
cout<<"\n Do you want to POP more elements ? (press y
or Y to continue ...) ";
cin>>ch;
}
cout<<"\n NOW FINALLY CONTENT OF STACK IS : \n" ;
display();
getch();
}
// A Program To Add Elements
(At End ) In Linked List And
//Display( ) Its Content..(Ok..Tested)
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL ,
*end=NULL; //GLOBAL POINTER VARIABLES
node * create(int
d)
{
node *p;
p=new node;
p->data=d;
p->next=NULL;
return p;
}
void insert_at_end(node * p) // ADD AT
THE END
{
if(start==NULL) { start=p; end=p; } // first node
else
{
end->next=p;
end=p;
}
}
void display(node
* q)
{
while(q)
{ cout<<p->data<<" --->";
p=p->next;
}
}
void main( )
{
node
* newptr;
char
ch='y';int d;
while(ch=='y'|| ch=='Y' )
{
cout<<"\n Enter a data item in LINKED LIST
: ";
cin>>d;
newptr= create(d);
insert_at_end(newptr);
cout<<"\n Do you want to enter more elements ? (press y
or Y to continue ...) ";
cin>>ch;
}
cout<<"\n NOW CONTENT OF LINKED LIST IS : \n" ;
display(start);
getch();
}
.......................................................................
// PROGRAM TO INSERT( ) & DELETE( )
ELEMENTS in LINKED QUEUE
// (DYNAMIC QUEUE) and DISPLAY( )
CONTENT..(OK..TESTED)
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL ,
*end=NULL; //GLOBAL POINTER VARIABLE
node * create(int
d)
{
node *p;
p=new node;
p->data=d;
p->next=NULL;
return p;
}
void insert(node * p) // ADD AT THE END
{
if(start==NULL) { start=p; end=p; }
else
{
end->next=p;
end=p;
}
}
void
display(node * p)
{
if(p==NULL) {cout <<"\n IT IS EMPTY..."; return ;}
while(p)
{
cout<<p->data<<"
--->";
p=p->next;
}
}
void
delete_Node( )
{
node * ptr;
ptr=start;
if(start == NULL) cout <<"\n IT IS
EMPTY...";
else
{ start=start->next; delete
ptr;}
}
void main()
{
node * newptr;
char ch='y';int d;
while(ch=='y'|| ch=='Y' )
{
cout<<"\n Enter a data item in LINKED LIST
: ";
cin>>d;
newptr= create(d);
insert(newptr);
cout<<"\n Do you want to INSERT more elements in
QUEUE ?\n (press y or Y to continue ...) ";
cin>>ch;
}
cout<<"\n NOW CONTENT OF LINKED QUEUE IS : \n" ;
display(start);
cout<<"\n Do you want to DELETE elements from
QUEUE ?\n (press y or Y to continue ...) ";
cin>>ch;
while(ch=='y'|| ch=='Y' )
{
delete_Node();
cout<<"\n Do you want to DELETE elements from
QUEUE ? \n (press y or Y to continue ...) ";
cin>>ch;
}
cout<<"\n NOW CONTENT OF LINKED QUEUE IS : \n" ;
display(start);
getch();
}
………..SATATIC STACK - AN
ARRAY IMPLEMENTATION..........
#include
<iostream.h>
#include<ctype.h>
#include<conio.h>
#define
MAXSIZE 10
int
stack[MAXSIZE];
int
top=-1; //index pointing to the top of stack
int
main()
{
void
push(int);
int
pop();
char
ch='y';int d;
while(ch=='y')
{
cout<<"\n Enter a data to be entered/pushed in
stack : ";
cin>>d;
push(d);
cout<<"\n press y to continue PUSH ";
cin>>ch;
}
ch='y';
while(ch=='y')
{
cout<<"\n NOW- CURRENT data present at top of
stack IS: ";
cout<<pop();
cout<<"\n press y to continue POP
";
cin>>ch;
}
getch();
return
0;
}
void
push(int y)
{
if(top==MAXSIZE-1)
{
cout<<endl<<"STACK FULL--STACK
OVERFLOW OCCURED";
return;
}
else
{
top++;
stack[top]=y;
}}
int
pop( )
{
int a;
if(top==-1)
{
cout<<endl<<"STACK EMPTY--STACK UNDERFLOW OCCURED";
return 0; }
else { a=stack[top];top--; }
return a;
} /*
Circular Queue : Implementation using Array */
#include<iostream.h>
#include<conio.h>
const int MAX = 5;
int a[MAX],front=-1,rear=-1;
void insert(int val)
{
if((front==0 && rear==MAX-1) ||
(rear+1==front))
cout<<" Circular
Queue is Full";
else
{
if(rear==MAX-1)
rear=0;
else
rear++;
a[rear]=val;
}
if(front==-1)
front=0;
}
int deletion()
{
int temp;
if(front==-1)
cout<<"Circular Queue is
Empty";
else
{
temp=a[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
}
}
return temp;
}
void display()
{
int i;
if(front==-1)
cout<<"Circular Queue is
Empty";
else
{
if(rear < front)
{
for(i=front;i<=MAX-1;i++)
cout<<a[i]<<"
";
for(i=0;i<=rear;i++)
cout<<a[i]<<"
";
}
else
{
for(i=front;i<=rear;i++)
cout<<a[i]<<"
";
cout<<endl;
} }
}
void main()
{
int choice,val;
char option='N';
do
{
clrscr();
cout<<"-----------Menu------------";
cout<< "
1.Insertion \n"
<<
" 2.Deletion \n"
<<
" 3.Display \n"
<<
" 4.Exit \n";
cout<<"Enter Your Choice
<1..4> ?";
cin>>choice;
switch(choice)
{ case 1 :
cout<<"Enter Element to Insert ?";
cin>>val;
insert(val);
break;
case 2 : val=deletion();
cout<<"Deleted
Element :"<<val<<endl;
break;
case 3 : display();
break;
}
cout<<"Do you want to
continue<Y/N> ?";
cin>>option;
}while(option=='Y' ||
option=='y');
getch(); }
…… DYNAMIC QUEUE.(LATEST updated) .......
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL,* end=NULL;
void insert(int d)
{
node * p;
p=new node;
p->data=d;
p->next=NULL;
if(start==NULL) {start=p; end=p;}
else
{end->next=p; end=p;}
}
void display(node * p)
{
while(p)
{ cout<<p->data<<'\t';
p=p->next;
}
}
int remove() {
node * p; int d;
p=start; d=start->data;
start=start->next; delete p; return d;
}
void main()
{
char ch='y';int d;
while(ch=='y')
{
cout<<"\n INSERT a data in DYNAMIC QUEUE
: ";
cin>>d;
insert(d);
cout<<"\n CONTENT OF
DYNAMIC QUEUE IS : \n";
display(start);
cout<<"\n
press y to continue .....";
cin>>ch;
}
ch='y';
while(ch=='y')
{ if(start==NULL) cout<<"QUEUE is
empty...underflow";
else cout<<"\n currently
element removed from queue is "<<remove();
cout<<"\n
press y to continue .....";
cin>>ch;
}
getch();
}
………DYNAMIC
STACK.(LATEST updated) ……..
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL;
void push(int d)
{
node * p;
p=new node;
p->data=d;
p->next=NULL;
if(start==NULL) start=p;
else
{p->next=start; start=p;}
}
void display(node * p)
{
while(p)
{ cout<<p->data<<'\t';
p=p->next;
}
}
int pop()
{
node * p; int d;
p=start; d=start->data;
start=start->next; delete p; return d;
}
void main()
{
char ch='y';int d;
while(ch=='y')
{
cout<<"\n PUSH a data in DYNAMIC STACK
: ";
cin>>d;
push(d);
cout<<"\n CONTENT OF
DYNAMIC STACK IS : \n";
display(start);
cout<<"\n
press y to continue .....";
cin>>ch;
}
ch='y';
while(ch=='y')
{
if(start==NULL) cout<<"STACK is
empty...underflow";
else cout<<"\n currently
element removed from stack is "<<pop();
cout<<"\n
press y to continue .....";
cin>>ch;}
getch();}
:: POSITIVELY
NOT THE END ::
NOTE : Some more programs will be added soon to
cover entire Syllabus in DATA STRUCTURE