Vous êtes sur la page 1sur 29

Data Structures & System Programming

S.NO PROGRAM PAGE


. NO.
1. Program to traverse an array 3
2. Program to insert an element in an array 4
3. Program to find a number using linear 5
search
4. Program to delete an element from an 6
array
5. Program to Binary search 7
6. Program to find the smallest and largest 8
number
7. Program to Bubble sort 9
8. Program to enter and traverse a linked list 10
9. Program to insert an element at begining, 12
at end, after a given element,
before a given element.
10. Progrm to delete at begning, at end, before 16
given element, given element and after the
given element.
11. Program to search in a linked list 20
12. Program of insertion two way link list 22
13. WAP to demonstrate Quick sort. 23
14. Program to implement stacks using array 26
15. Program to Stack using linked list 28

1
Data Structures & System Programming

//1.Program to traverse an array DATE:- 17/08/09


#include<iostream.h>
#include<conio.h>
void main()
{ int i,a[100],n;
clrscr();
cout<<"Enter the number of elements of array:";
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
cout<<" The entered array is \n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
}

Output
Enter the number of elements of array: 6
1
34
2
23
5
23
The entered array is
1 34 2 23 5 23

2
Data Structures & System Programming

//2.Program to insert an element in an array DATE:-17/08/09


#include<iostream.h>
#include<conio.h>
void main()
{int i,j,a[100],n,num,k;
clrscr();
cout<<"Enter the number of elements of array ";
cin>>n;
cout<<"enter the array elements\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"The given array is\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<"\n enter the location and then number to insert " ;
cin>>j>>num;
for(k=n;k>=j;k--)
a[k+1]=a[k];
a[j]=num;
cout<<" The array after intertion is \n";
for(i=0;i<n+1;i++)
cout<<a[i]<<"\t";
getch();
}

Output:
Enter the number of elements of array 5
Enter the array elements
43
32
41
65
67
The given array is
43 32 41 65 67
Enter the location then number to insert 2
23
The array after insertion is

3
Data Structures & System Programming
43 32 23 41 65 67

//3.Program to find a number using linear search DATE:-17/08/09


#include<iostream.h>
#include<conio.h>
void main()
{int i,a[100],n,num,k=0;
clrscr();
cout<<" Enter the number of elements";
cin>>n;
cout<<"Enter the array elements \n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter the element to search";
cin>>num;
for(i=0;i<n;i++)
{if(a[i]==num)
{cout<<"element "<<num<<"found at "<<i<<" position"<<endl;
k=k+1;
}}
cout<<"total "<<k<<" of "<<num<<"'s were found";
getch();
}

Output:
Enter the number of elements 7
Enter the array elements
5
4
7
2
9
7
9
enter the element to search 9
Element 9 found at 4 position
Element 9 found at 6 position
total 2 of 9’s were found

4
Data Structures & System Programming

4.// Program to delete an element from an array DATE:-17/08/09


#include<iostream.h>
#include<conio.h>
void main()
{int i,j,k,n,a[100];
clrscr();
cout<<"enter the number of array elements";
cin>>n;
cout<<" enter the array elements \n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<" enter the location to delete the element";
cin>>j;
for(;j<n;j++)
a[j]=a[j+1];
for(i=0;i<n-1;i++)
cout<<a[i]<<"\t";
getch();
}

Output
enter the number of array elements 4
enter the array elements
31
2
45
6
enter the location to delete the element 0
2 45 6

5
Data Structures & System Programming

5.// Binary search DATE:-24/08/09


#include<iostream.h>
#include<conio.h>
void main()
{int i,j,b=0,e,n,m,a[100],k=1;
clrscr();
cout<<"Enter the number of array elements: ";
cin>>n;
cout<<"Enter the array elements in order :\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the element to search ";
cin>>m;
e=n;
while(b<e)
{j=(b+e)/2;
if(a[j]==m)
{k=0;
break;}
if(m<a[j])
e=j-1;
if(m>a[j])
b=j+1;
}
if(k==0)
cout<<"\nElement "<<m<<"found at "<<j;
else
cout<<"\nElement not found";
getch();
}

Output
Enter the number of array elements: 3
Enter the array elements in order:
21
34
35
Enter the element to search34

6
Data Structures & System Programming
Element34 found at2

//6. Program to find the smallest and largest number DATE:-24/08/09


#include<iostream.h>
#include<conio.h>
void main()
{int i,j,k,a[100],n;
clrscr();
cout<<"Enter the number of elements of array";
cin>>n;
cout<<"Enter the array elements\n ";
for(i=0;i<n;i++)
cin>>a[i];
j=a[0];
for(i=0;i<n;i++)
{if(j<a[i])
j=a[i];
}
cout<<"The greatest number is"<<j<<endl;
j=a[0];
for(i=0;i<n;i++)
{if(j>a[i])
j=a[i];
}
cout<<"The Smallest element is "<<j;
getch();
}

Output
Enter the number of array elements 7
Enter the array elements
64
67
4563
34
2
23
The greatest element is 4563
The smallest element is 2

7
Data Structures & System Programming

7//. Program to Bubble sort DATE:-24/08/09


#include<iostream.h>
#include<conio.h>
void main()
{int i,j,k,a[100],n,temp;
clrscr();
cout<<"Enter the number of array elements ";
cin>>n;
cout<<"Enter the array elements\n";
for(i=0;i<n;i++)
cin>>a[i];
for(j=0;j<n;j++)
{for(k=0;k<n-1;k++)
{if(a[k]>a[k+1])
{temp=a[k]+a[k+1];
a[k+1]=temp-a[k+1];
a[k]=temp-a[k+1];
}
}}
cout<<"The array after rearrangement is\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
}

Output
Enter the number of array elements 5
Enter the array elements
12
3
45
77
24
The array after rearrangement is
3 12 24 45 77

8
Data Structures & System Programming

//8.Program to enter and traverse a linked list DATE:-7/09/09

#include<iostream.h>
#include<conio.h>
struct node
{int item;
node*next;
};
void main()
{clrscr();
int a,i=0;
node*start=NULL;
node*temp2;
cout<<"enter the number of items:";
cin>>a;
do
{node*temp=new node;
cout<<"Enter the item no. ";
cin>>temp->item;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{for(temp2=start;temp2->next!=NULL;temp2=temp2->next);
temp2->next=temp;}
i++;
}while(i<a);
for(temp2=start;temp2!=NULL;temp2=temp2->next)
cout<<"The item no. is "<<temp2->item<<"\n";
getch();}
Output:
Enter the number of items 4
Enter the item no. 4
Enter the item no. 3
Enter the item no. 2
Enter the item no. 5
The item no. is 4
The item no. is 3

9
Data Structures & System Programming
The item no. is 2
The item no. is 5
//9.Program to insert an element at begining, at end, after a given element,
before a given element. DATE:-7/09/09
#include<iostream.h>
#include<conio.h>
#include<dos.h>
struct node
{int item;
node*next;
};
void insertb(node**x,int c)
{
node*temp=new node;
temp->item=c;
temp->next=*x;
*x=temp;
}

void insertbe(node**x,int c)
{node*temp2;
node*y=NULL;
int n,k=0;
cout<<"enter the item to insert";
cin>>n;
if((*x)->item==c)
insertb(x,n);
else{
node*temp=new node;
for(temp2=(*x);temp2!=NULL;temp2=temp2->next)
{if(temp2->item==c)
{k=1;
break;}
y=temp2;}
if(k==0)
cout<<"item not found";
if(k==1)
{temp->item=n;
temp->next=temp2;
y->next=temp;}}}

10
Data Structures & System Programming

void insertaf(node**x,int c)
{ int k=0,n;
node*temp2;
for(temp2=*x;temp2!=NULL;temp2=temp2->next)
{if(temp2->item==c)
{k=1;
break;}}
if(k==0)
cout<<"item not found";
if(k==1)
{cout<<"enter the item";
cin>>n;
node*temp=new node;
temp->item=n;
temp->next=temp2->next;
temp2->next=temp;
}
}
void main()
{clrscr();
int a,i=0;
char d;
node*start=NULL;
node*temp2;
cout<<"enter the number of nodes:";
cin>>a;
do
{node*temp=new node;
cout<<"enter he item no. ";
cin>>temp->item;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{for(temp2=start;temp2->next!=NULL;temp2=temp2->next);
temp2->next=temp;}
i++;
}while(i<a);

11
Data Structures & System Programming
cout<<"The item no. of items selected are\n";
for(temp2=start;temp2!=NULL;temp2=temp2->next)
cout<<temp2->item<<"\n";
int b,c;
do{
cout<<"What operation you want to perform \n1. Insert at begning \n";
cout<<"2. Insert at end \n3. Insert before given node \n";
cout<<"4. Insert after a given node\n5. Exit\n";
cout<<"enter your choice(1-5)";
cin>>b;
if(b==5)
break;
switch (b)
{case 1:cout<<"enter the item to insert";
cin>>c;
insertb(&start,c);
break;
case 2:cout<<"enter the item to insert";
cin>>c;
node*temp=new node;
temp->item=c;
temp->next=NULL;
for(temp2=start;temp2->next!=NULL;temp2=temp2->next);
temp2->next=temp;
break;
case 3:cout<<"enter the item for search";
cin>>c;
insertbe(&start,c);
break;
case 4:cout<<"enter the item for search";
cin>>c;
insertaf(&start,c);
break;

default:
cout<<"wrong choice\n";
}
cout<<"do you want to insert again (n/y)";
cin>>d;
sleep(2);

12
Data Structures & System Programming
clrscr();
}while(d=='y');
cout<<"the list after processing is\n";
for(temp2=start;temp2!=NULL;temp2=temp2->next)
cout<<temp2->item<<"\n";
getch();
}

Output:
Enter the number of nodes 4
Enter the item no. 4
Enter the item no. 6
Enter the item no. 7
Enter the item no. 9
The item no. of items selected are
4
6
7
9
What operation you want to perform
1. Insert at beginning
2. Insert at end
3. Insert before given node
4. Insert after a given node
5. Exit.
Enter your choice (1-5) 1
Enter the item to insert 2
Do you want to insert again (n/y) y
What operation you want to perform
1. Insert at beginning
2. Insert at end
3. Insert before given node
4. Insert after a given node
5. Exit.
Enter your choice (1-5) 2
Enter the item to insert 10
Do you want to insert again (n/y) y
What operation you want to perform
6. Insert at beginning
7. Insert at end

13
Data Structures & System Programming
8. Insert before given node
9. Insert after a given node
10.Exit.
Enter your choice (1-5) 3
Enter the item for search 4
Enter the item to insert 3
Do you want to insert again (n/y) y
What operation you want to perform
1. Insert at beginning
2. Insert at end
3. Insert before a given node
4. Insert after a given node
5. Exit.
Enter your choice (1-5) 4
Enter the item for search 4
Enter the item 5
Do you want to insert again (n/y) y
What operation you want to perform
1. Insert at beginning
2. Insert at end
3. Insert before a given node
4. Insert after a given node
5. Exit.
Enter your choice (1-5) 5
The list after processing is
2
3
4
5
6
7
8
9
10

14
Data Structures & System Programming

10.// Progrm to delete at begning, at end, before given element, given


element and after the given element. DATE:-7/09/09
#include<iostream.h>
#include<conio.h>
struct node
{int item;
node*next;
};
void deleteb(node**x)
{node*temp;
temp=*x;
*x=(*x)->next;
delete temp;}

void deletebe(node**x)
{int c,k=0;
node*temp2,*temp=NULL,*temp3=NULL;
cout<<"enter the item before ehich you want to delete";
cin>>c;
for(temp2=(*x);temp2!=NULL;temp2=temp2->next)
{if(temp2->item==c)
{k=1;
break;}
temp3=temp;
temp=temp2;}
if(k==0)
cout<<"item not found";
if(k==1)
{if(temp==temp3)
cout<<"deletion not possible\n";
else if(temp3==NULL)
deleteb(x);
else{
temp3->next=temp2;
delete(temp);}}}

void deleteaf(node**x)
{int c,k=0;

15
Data Structures & System Programming
node*temp2,*temp;
cout<<"enter the item after which you want to delete";
cin>>c;
for(temp2=(*x);temp2->next!=NULL;temp2=temp2->next)
{if(temp2->item==c)
{k=1;
break;}}
if(k==0)
cout<<"deletion not possible\n";
else{
temp=temp2->next;
temp2->next=temp->next;
delete temp;}}

void atnode(node**x)
{int c,k=0;
node*temp2,*temp=NULL;
cout<<"enter the item which you want to delete";
cin>>c;
for(temp2=(*x);temp2!=NULL;temp2=temp2->next)
{if(temp2->item==c)
{k=1;
break;}
temp=temp2;}
if(k==0)
cout<<"Item not found";
else if(temp2==*x)
deleteb(x);
else{
temp->next=temp2->next;
delete temp2;}}

void main()
{clrscr();
int a,i=0;
node*start=NULL;
node*temp2,*temp;
cout<<"enter the number of nodes:";
cin>>a;
do

16
Data Structures & System Programming
{node*temp=new node;
cout<<"enter he item no. ";
cin>>temp->item;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{for(temp2=start;temp2->next!=NULL;temp2=temp2->next);
temp2->next=temp;}
i++;
}while(i<a);
cout<<"The item no. of items selected are\n";
for(temp2=start;temp2!=NULL;temp2=temp2->next)
cout<<temp2->item<<"\n";
int b;
cout<<"What operation you want to perform \n1. Delete at begning \n";
cout<<"2. Delete at end \n3. Delete before given node \n";
cout<<"4. Delete after a given node\n5. Delete a node with given piece of
information\n";
cout<<"6. Exit\nenter your choice(1-6)";
cin>>b;
if(start==NULL)
cout<<"Deletion not possible";
switch (b)
{case 1:deleteb(&start);
break;
case 2: for(temp2=start;temp2->next!=NULL;temp2=temp2->next)
temp=temp2;
delete (temp2);
temp->next=NULL;
break;
case 3:deletebe(&start);
break;
case 4:deleteaf(&start);
break;
case 5:atnode(&start);
break;
case 6: break;
default:cout<<"wrong choice";
}

17
Data Structures & System Programming
Cout<< ”The list after processing is\n”;
for(temp2=start;temp2!=NULL;temp2=temp2->next)
cout<<temp2->item<<"\n";
getch();
}

Output
Enter the number of nodes 5
Enter the item no. 3
Enter the item no. 5
Enter the item no. 7
Enter the item no. 9
Enter the item no. 11
The item no. of items selected are
3
5
7
9
11
What operation do you want to perform
1. Delete at beginning
2. Delete at end
3. Delete before given node
4. Delete after a given node
5. Delete a node with given piece of information
6. Exit.
Enter your choice (1-6) 5
Enter the item which you want to delete 3
The list after processing is
5
7
9
11

18
Data Structures & System Programming

//11. Program to search in a linked list DATE:-7/09/09


#include<iostream.h>
#include<conio.h>
struct node
{int item;
node*next;};
void main()
{clrscr();
int a,i=0,c,k=0;
node*start=NULL;
node*temp2;
cout<<"enter the number of nodes:";
cin>>a;
do
{node*temp=new node;
cout<<"enter the item no. ";
cin>>temp->item;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{for(temp2=start;temp2->next!=NULL;temp2=temp2->next);
temp2->next=temp;}
i++;
}while(i<a);
cout<<"enter the item to search ";
cin>>c;
for(temp2=start;temp2!=NULL;temp2=temp2->next)
{if(temp2->item==c)
{cout<<"item found";
k=1;
break;}}
if(k==0)
cout<<"item not found";

getch();
}

19
Data Structures & System Programming
Output
Enter the number of nodes 6
Enter the item no. 12
Enter the item no. 23
Enter the item no. 34
Enter the item no. 45
Enter the item no. 56
Enter the item no. 67
Enter the item to search 34
Item found

20
Data Structures & System Programming

12. //Program of insertion two way link list DATE:-7/09/09


#include<iostream.h>
#include<conio.h>
struct node
{int item;
node*forw;
node*rev;
};
void main()
{int a,i=0;
node*header;
node*temp,*temp2;
header->item=4355;
header->forw=NULL;
header->rev=NULL;
cout<<"enter the number of nodes";
cin>>a;
do{
node*temp=new node;
cout<<"Enter the item code";
cin>>temp->item;
temp->forw=NULL;
temp->rev=NULL;
if(header->forw==NULL)
{header->forw=temp;
temp->rev=header;}
else{
for(temp2=header;temp2->forw!=NULL;temp2=temp2->forw);
temp2->forw=temp;
temp->rev=temp2;
}
i++;
}while(i<a);
cout<<"The given list is\n";
for(temp2=header->forw;temp2!=NULL;temp2=temp2->forw)
cout<<temp2->item<<endl;
getch();}

21
Data Structures & System Programming

13. // WAP to demonstrate Quick sort. DATE:-5/10/09


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int top=NULL,lower[10],beg,end,loc;
int higher[10],q[20],n;
cout<<"This is programme of quick sort"<<endl;
cout<<"Enter the no of element "<<endl;
cin>>n;
cout<<"Enter the element in integer"<<endl;
for(int i=1;i<=n;i++)
{
cin>>q[i];
}
int quick_sort(int [20],int,int,int,int);
if(n>1)
{
++top;
lower[1]=1;
higher[1]=n;
}
while(top!=NULL)
{
beg=lower[top];
end=higher[top];
--top;
loc=quick_sort(q,n,beg,end,loc);
if(beg<loc-1)
{
++top;
lower[top]=beg;
higher[top]=loc-1;
}
if(loc+1<end)
{
++top;
lower[top]=loc+1;

22
Data Structures & System Programming
higher[top]=end;
}
}
cout<<"The sorted list is following"<<endl;
for(i=1;i<=n;i++)
{
cout<<q[i]<<"\t";
}
getch();
}

int quick_sort(int q[20],int n,int beg,int end,int loc)


{
int left,right;
left=beg;
right=end;
loc=beg;
while(loc!=NULL)
{
while(q[loc]<=q[right]&&loc!=right)
{
--right;
}
if(loc==right)
{
return loc;
}
if(q[loc]>q[right])
{
int temp;
temp=q[loc];
q[loc]=q[right];
q[right]=temp;
loc=right;
}
while(q[left]<=q[loc]&&left!=loc)
{
++left;
}
if(loc==left)

23
Data Structures & System Programming
return loc;
if(q[left]>q[loc])
{
int temp;
temp=q[loc];
q[loc]=q[left];
q[left]=temp;
loc=left;
}
}
}
Output:
This is the program of quick sort
Enter the number of elements
5
Enter the element in integer
12
33
44
11
16
The sorted list is following
11 12 16 33 44

24
Data Structures & System Programming

//14.Program to implement stacks using array DATE:-12/10/09


#include<iostream.h>
#include<conio.h>
int push(int a[],int top)
{if(top==20)
cout<<"operation not possible";
else{
cout<<"Enter the element to push";
cin>>a[top];
top=top+1;}
return top;
}
int pop(int top)
{if(top==0)
cout<<"operation not possible";
else
top=top-1;
return top;}
void main()
{int a[20],i,top,c,v=0;
char ch;
clrscr();
cout<<"Enter the no. of stack elements(>19)";
cin>>top;
cout<<"Enter the elements\n";
for(i=0;i<top;i++)
cin>>a[i];

do{cout<<"What operation do you want to perform\n";


cout<<"1. Push\n2. Pop\n3.Exit\n enter your choice(1-3) ";
cin>>c;
switch (c)
{case 1:top=push(a,top);
break;
case 2:top=pop(top);
break;
case 3: v=1;
break;
default: cout<<"wrong choice";

25
Data Structures & System Programming
break;}
if(v==1)
break;
cout<<"Do you want to perform operation again(n/y)";
cin>>ch;
}while(ch=='y');
cout<<"\nThe stack elements are\n";
for(i=0;i<top;i++)
cout<<a[i]<<endl;
getch();
}

Output
Enter the no. of stack elements (<20) 3
Enter the elements
1
2
3
What operation do you want to perform
1. Push
2. Pop
3. Exit
Enter your choice (1-3) 1
Enter element to push 34
Do you want to perform operation again (n/y) y
What operation do you want to perform
4. Push
5. Pop
6. Exit
Enter your choice (1-3) 2
Do you want to perform operation again (n/y) n

The stack elements are


1
2
3

26
Data Structures & System Programming

15.//Stack using linked list DATE:-12/10/09


#include<iostream.h>
#include<conio.h>
struct stack
{
int info;
stack *next;
};
class sta
{
stack *temp,*top,*ptr;
public:
sta ()
{
top=NULL;
}
void push();
void pop();
void display();
};
void main()
{
clrscr();
sta st;
int ch;
char c='y';
cout<<"This is the programme of stack"<<endl;
do
{
cout<<"The following choice in stack"<<endl;
cout<<"1. Push()"<<endl<<"2. Pop"<<endl<<"3. Display"<<endl<<"4.
Exit"<<endl;
cout<<"Enter the chioce (1-4) :"<<endl;
cin>>ch;
switch(ch)
{
case 1:st.push();
getch();
break;

27
Data Structures & System Programming
case 2:st.pop();
getch();
break;
case 3:st.display();
getch();
break;
case 4:c='n';
break;
default:
cout<<"You enter the wrong choice"<<endl;
}
}while(c=='y'||c=='Y');
cout<<"thank you"<<endl;
getch();
}
void sta::push()
{
temp=new stack;
cout<<"Enter the info of the new node "<<endl;
cin>>temp->info;
temp->next=NULL;
if(top==NULL)
top=temp;
else
{
temp->next=top;
top=temp;
}
}
void sta::pop()
{
if(top==NULL)
cout<<"The stack is underflow"<<endl;
else
{
top=top->next;
cout<<"The pop opreation of stack is complete"<<endl;
}
}
void sta::display()

28
Data Structures & System Programming
{
if(top==NULL)
cout<<"The stack is empty"<<endl;
else
{
cout<<"The element in stack is following"<<endl;
ptr=top;
while(ptr!=NULL)
{
cout<<ptr->info<<"\t";
ptr=ptr->next;
}
cout<<endl;
}}

29

Vous aimerez peut-être aussi