Вы находитесь на странице: 1из 46

DATASTRUCTURE

III - B.Sc. (Maths with CA)


1.
2.
3.
4.

Write a program to sort a set of given String.


Create a Stack and add element into the stack. Delete elements from the same stack.
Create a Queue and add element into the queue. Delete elements from the same queue.
Create a singly linked list. Insert elements into the singly linked list and delete the

elements from the same list.


5. Write a program to implement the linked list using stack.
6. Write a program to sort a set of given numbers using selection sort.
7. Write a program to sort a set of given numbers using Bubble sort.
8. Write a C++ program to search the number from the given list using linear search.
9. Write a C++ program to search the number from the given list using Binary search.
10. Write a program to traverse a Binary tree in
a) In-Order Form
b) Pre-Order Form
c) Post-Order Form

Compilation & Execution of C / C++ Program


1. Write C / C++ program using Turbo Editor, Such as [Turbo C / C++]
2. Save program by using [ .c / .cpp] Extension.
3. Compiling C / C++ program using [Alt + F9].
4. If Compile time error occurs, Compile time errors are reported to user.
5. We have to review code and check for the solution.
6. If program is error free, Run C / C++ program using [ctrl+f9]

TABLE OF CONTENT
SNO

DATE

PROGRAM NAME

1.

Sort a set of given String

2.

Implementation of Stack Operations

3.

Implementation of Queue Operations

4.

Single Linked List using pointer

5.

Single Linked List using Stack

6.

Sort a set of given numbers using Selection sort

7.

Sort a set of given numbers using Bubble sort

8.

Search the numbers from the given list using


Linear Search

9.

Search the numbers from the given list using


Binary Search

10.

Binary tree Traversal

PAGE
NO.

SIGNATURE

Ex.No.1
Date:
Aim:

Algorithm:

Program:

SORT A SET OF GIVEN STRING

#include<iostream.h>
#include<conio.h>
#include<String.h>
void main()
{
char a[10][25],temp[25];
int i,n,j;
clrscr();
cout<<"\n String sorting \n";
cout<<"Enter maixmum number of names: ";
cin>>n;
cout<<"Enter names (Not exceeding 10 characters) " <<endl;
for(i=0;i<n;i++)
cin>>a[i];
for(j=0;j<n-1;j++)
{
for(i=j+1;i<n;i++)
if(strcmp(a[j],a[i])>0)
{
strcpy(temp,a[j]);
strcpy(a[j],a[i]);
strcpy(a[i],temp);
}
}
cout<<"\n Names in alphabetical order";//<<endl;
cout<<"\n ___________________________"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
}

Output:

Enter maixmum number of names: 5


Enter names (Not exceeding 10 characters)
Ravi
Priya
Sahana
Bala
Murali
Names in alphabetical order
Bala
Murali
Priya
Ravi
Sahana

Result:
Hence the program has been executed successfully.

Ex.No.2

Implementation of Stack Operations

Date:
Aim:

Algorithm:
.

Program:
#include<iostream.h>
#include<conio.h>

#include<math.h>
int stack[50],item;
int ch,n=0,top,i;
void main()
{
void create();
void add();
void display();
void deletel();
do
{
clrscr();
cout<<"\n Stack operation";
cout<<"\n ---------------";
cout<<"\n 1.Create";
cout<<"\n 2.Add";
cout<<"\n 3.Delete";
cout<<"\n 4.Display";
cout<<"\n 5.Exit";
cout<<"\n Enter your choice: \n";
cin>>ch;
if(ch==1)
{
cout<<"\n Enter the maximum size of the stack: \n";
cin>>n;
}
switch(ch)
{
case 1:create();getch();break;
case 2:add();getch();break;
case 3:deletel();getch();break;
case 4:display();getch();break;
case 5:break;
default:cout<<"\n Invalid choice. tryagain..";
getch();
}
}while(ch!=5);

}
void create()
{
cout<<"\n Creating a Stack";
cout<<"\n ----------------";
top=0;
cout<<"\n stack creation is over";
return;
}
void add()
{
cout<<"\n Adding element into a Stack";
cout<<"\n ---------------------------";
cout<<"\n Item that has to be inserted: ";
cin>>item;
if(top==n)
{
cout<<"\n Stack is Full";
getch();
return;
}
top++;
stack[top]=item;
cout<<"\n Inserted Item is"<<item;
cout<<"\n Items in the stack are :\n";
for(i=1;i<=top;i++)
cout<< stack[i]<<"\t";
return;
}
void deletel()
{
cout<<"\n Deleting element from a stack";
cout<<"\n -----------------------------";
if (top==0)
{
cout<<"\n Stack is empty";
getch();

return;
}
item=stack[top];
top--;
cout<<"\n Deleted item is: "<<item;
cout<<"\n Items in the Stack are :\n";
for(i=1;i<=top;i++)
cout<<stack[i]<<"\t";
return;
}
void display()
{
cout<<"\n Elements in the Stack are :";
cout<<"\n -------------------------\n";
for(i=1;i<=top;i++)
cout<<stack[i]<<"\t";
return;
}

Output:
Stack operation

1.Create
2.Add
3.Delete
4.Display
5.Exit
Enter your choice: 1
Enter the maximum size of the stack: 3
Creating a Stack
stack creation is over

Result:
Hence the program has been executed successfully.

Ex.No.3

Implementation of Queue Operations

Date:
Aim:

Algorithm:

Program:
#include<iostream.h>
#include<conio.h>

#include<math.h>
int queue[50],item;
int ch,n=0,front,rear,i;
void main()
{
void create();
void add();
void display();
void deletel();
do
{
clrscr();
cout<<"\n Queue operation";
cout<<"\n ---------------";
cout<<"\n 1.Create";
cout<<"\n 2.Add";
cout<<"\n 3.Delete";
cout<<"\n 4.Display";
cout<<"\n 5.Exit";
cout<<"\n Enter your choice: \n";
cin>>ch;
if(ch==1)
{
cout<<"\n Enter the maximum size of the queue: \n";
cin>>n;
}
switch(ch)
{
case 1:create();getch();break;
case 2:add();getch();break;
case 3:deletel();getch();break;
case 4:display();getch();break;
case 5:break;
default:cout<<"\n Invalid choice. tryagain..";
getch();
}
}while(ch!=5);

}
void create()
{
cout<<"\n Creating a Queue:";
cout<<"\n ----------------";
front=0;rear=0;
cout<<"\n Queue creation is over";
return;
}
void add()
{
if(rear>=n)
{
cout<<"\n Queue is Full ";
getch();
return;
}
else
{
cout<<"\n Adding element into a Queue";
cout<<"\n ---------------------------";
cout<<"\n Item that has to be inserted: ";
cin>>item;
rear++;
queue[rear]=item;
if(front==0)
front=1;
cout<<"\n Iserted item is"<<item;
cout<<"\n Item in the Queue are: |\n";
for(i=front;i<=rear;i++)
cout<<queue[i]<<"\t";
return;
}
}
void deletel()
{

if(front==0)
{
cout<<"\n Queue is empty";
getch();
return;
}
else
{
cout<<"\n Deleting element from a queue";
cout<<"\n -----------------------------";
item=queue[front];
if(front==rear)
front=rear=0;
else
front++;
cout<<"\n Deleted item is: "<<item;
cout<<"\n Items in the queue are :\n";
for(i=front;i<=rear;i++)
cout<<queue[i]<<"\t";
return;
}
}
void display()
{
cout<<"\n Elements in the Queue are :";
cout<<"\n -------------------------\n";
for(i=front;i<=rear;i++)
cout<<queue[i]<<"\t";
return;
}

Output:
Queue operation
1.Create
2.Add
3.Delete
4.Display
5.Exit
Enter your choice: 1
Enter the maximum size of the queue: 3
Creating a Queue:
Queue creation is over

Result:
Hence the program has been executed successfully.

Ex.No.4
Date:
Aim:

Algorithm:

Single Linked List Using Pointer

Program:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
class node
{
public:
int no;
node *next;
};
node *head,*ptr=NULL;
int i,ch;
void main()
{
void create();
void add();
void display();
void delete();
do
{
clrscr();
cout<<\n Linked list operation ;
cout<<\n\t 1- Create ;
cout<<\n\t 2-Addition;
cout<<\n\t 3-Deletion;
cout<<\n\t 4-Exit;
cout<<\n\n Enter your choice: ;
cin>>ch;
switch(ch)
{
case 1:
{
create();
display();
getch();
break;

}
case 2:
{
add();
display();
getch();
break;
}
case 3:
{
delete();
display();
getch();
break;
}
case 4:break;
default:cout<<\n\t Invalid Choice. Try again ;
getch();
}
}
while(ch!=4);
}
void create()
{
head=NULL;
cout<<\n Give a number to add a list (Type 999 to quit):;
cin>>i;
if(i!=999)
{
ptr=(node*)malloc(sizeof(node));
ptr->no=i;
ptr->next=NULL;
head=ptr;
cout<<\n Give a number again(Type 999 to quit);;
cin>>i;
}
while(i!=999)

{
ptr->next=(node*)malloc(sizeof(node));
ptr=ptr->next;
ptr->no=i;
ptr->next=NULL;
head=ptr;
cout<<\n Give a number again(Type 999 to quit):;
cin>>i;
}
}
void add()
{
node *pnode,*cur,*n;
int chl;
cur=(node*)malloc(sizeof(node));
cout<<\n Enter a new number(Type 999 to quit):;
cin>>cur->no;
cout<<\n Position of the node is(press 0 to add at the end of the list): ;
cin>>chl;
pnode=head;
if(chl==0)
head=cur;
else
{
while(chl>0)
{
n=pnode;
pnode=pnode->next;
chl--;
if(chl==0)
n->next=cur;
}
}
cur-> next=pnode;
}
void display()
{

getch();
clrscr();
cout<<\n\t Content of Linked list;
cout<<\n\t ----------------------;
ptr=head;
while(ptr)
{
cout << \t<<ptr->no;
ptr=ptr->next;
}
return;
}
void delete()
{
int i;
char flag,flagl;
node *pnode;
cout<<\n The Element to be Deleted :;
cin>>i;
ptr=head;
if(ptr->no==i)
{
head=ptr->next;
flag=y;
}
else
{
pnode=ptr;
ptr=ptr->next;
while((ptr->no!=i)&&(ptr))
{
pnode=ptr;
ptr=ptr->next;
}
if(ptr)
pnode->next=ptr->next;
flagl=y;

}
if((flag==n)||(flagl==n))
cout<<\n Key is not found in the Linked list : <<i;
}

Output:
Linked list operation
67 Create
2-Addition
3-Deletion
4-Exit
Enter your choice: 1
Give a number to add a list (Type 999 to quit):6
Give a number again(Type 999 to quit):8
Give a number again(Type 999 to quit):999
Content of Linked list
6

Result:
Hence the program has been executed successfully.

Ex.No.5
Date:
Aim:

Algorithm:

Single Linked List Using Stack

Program:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
class node
{
public:
int no;
node *next;
};
node *head,*ptr;
int i,ch;
void main()
{
void create();
void add();
void display();
void delete();
do
{
clrscr();
cout<<\n Stack Implementation using single Linked list;
cout<<\n ---------------------------------------------;
cout<<\n\t 1- Create ;
cout<<\n\t 2-Addition;
cout<<\n\t 3-Deletion;
cout<<\n\t 4-Exit;
cout<<\n\n Enter your choice: ;
cin>>ch;
switch(ch)
{
case 1:
{
create();
display();
getch();

break;
}
case 2:
{
add();
display();
getch();
break;
}
case 3:
{
delete();
display();
getch();
break;
}
case 4:break;
default:cout<<\n\t Invalid Choice. Try again ;
getch();
}
}
while(ch!=4);
}
void create()
{
head=NULL;
cout<<\n Give a number to add a list (Type 999 to quit):;
cin>>i;
while(i!=999)
{
ptr=(node*)malloc(sizeof(node));
ptr->no=i;
ptr->next=head;
head=ptr;
cout<<\n Give a number again(Type 999 to quit);;
cin>>i;
}

}
void add()
{
ptr=(node*)malloc(sizeof(node));
cout<<\n Enter a new number(Type 999 to quit):;
cin>>ptr->no;
ptr->next=head;
head=ptr;
return;
}
void display()
{
getch();
clrscr();
cout<<\n\t Content of Stack using single Linked list;
cout<<\n\t ------------------------------------------\n;
ptr=head;
if(head==NULL)
cout<<\n Stack is EmptyFurther Deletion is not Possible;
while(ptr)
{
cout<<\t<<ptr->no;
ptr=ptr->next;
}
}
void delete()
{
ptr=head;
head=ptr->next;
}

Output:
Stack Implementation using single Linked list
1- Create
2-Addition
3-Deletion
4-Exit
Enter your choice: 1
Give a number to add a list (Type 999 to quit):54
Give a number again(Type 999 to quit):86
Give a number again(Type 999 to quit):999
Content of stack using Linked list
67

54

Result:
Hence the program has been executed successfully.

Ex.No.6

Numbers Sorting Using Selection Sort

Date:
Aim:
Sort a set of given Numbers using Selection Sort

Algorithm:
Step 1 : Find the minimum value in the given list.
Step 2: Compare minimum value with the first position.
Step 3: If the minimum value is less than the first position, Swap both the numbers or
remain in same position.
Step 4: Repeat the above steps
Step 5: Finally we get the sorted list.

Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
int a[25],temp[25];
int i,n,j,min;
clrscr();
cout<<"\n Number sorting \n";
cout<<"Enter total number of items: ";
cin>>n;
cout<<"Enter the numbers one by one" <<endl;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
int min=i;
for(j=i;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
int temp=a[i];
a[i]=a[min];
a[min]=temp;
}
cout<<"\n Numbers in ascending order";
cout<<"\n ___________________________"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
}

Output:
Number sorting using selection sort
------------------------------------------Enter the Numbers one by one

5
7
9
4
3
Numbers in Ascending Order
3

Result:
Hence the program has been executed successfully.
Ex.No.7
Date:

Sort a set of given numbers using Bubble sort

Aim:

Algorithm:

Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()

{
int i,j,n,pass;
char change;
float temp,x[20];
clrscr();
cout<<"\n Enter the total number of items: ";
cin>>n;
cout<<"\n Enter a number one by one: ";
for(i=1;i<=n;i++)
cin>>x[i];
clrscr();
cout<<"\n SORT THE GIVEN NUMBERS USING BUBBLE SORT ";
cout<<"\n ------------------------------------------";
cout<<"\n The given list is ";
cout<<"\n -----------------\n";
for(i=1;i<=n;i++)
cout<<x[i]<<"\t";
cout<<"\n\n After Successive passes\n";
cout<<"\n----------------------------\n";
change='t';
pass=1;
while((pass<=(n-1)) && (change=='t'))
{
cout<<"\t";
for(j=1;j<=(n-pass);j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
change='t';
}
}
for(i=1;i<=n;i++)
cout<<x[i]<<"\t";
cout<<"\n";

pass++;
}
cout<<"\n\nSORTED LIST";
cout<<"\n------------\n";
for(i=1;i<=n;i++)
cout<<x[i]<<"\t";
getch();
}

Output:
Enter the total number of items: 5
Enter a number one by one:

9
3
7
1
8
SORT THE GIVEN NUMBERS USING BUBBLE SORT
---------------------------------------------------------------------The given list is
------------------9

After Successive passes


----------------------------3

SORTED LIST
-----------------1

Result:
Hence the program has been executed successfully.

Ex.No.8
Date:
Aim:

Search the numbers from the given list using Linear Search

Algorithm:

Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,n,x,temp;

clrscr();
cout<<"\n Linear Search";
cout<<"\n\n Enter the total number :";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\n Enter the "<<i<<" numbers:";
cin>>a[i];
}
cout<<"\nThe Given Array is: \n\n";
for(i=1;i<=n;i++)
cout<<a[i]<<"\t";
cout<<"\n\n Enter the key to be searched :\n";
cin>>x;
for(i=1;i<=n;i++)
{
if(a[i]==x)
{
temp=i;
cout<<"\n Founded Item is : " <<temp;
break;
}
}
getch();
}

Output:
Linear Search
Enter the total number : 4
Enter the 1 numbers: 8

Enter the 2 numbers: 3


Enter the 3 numbers: 9
Enter the 4 numbers: 1
The Given Array is:
8

Enter the key to be searched : 9


Founded Item is : 3

Result:
Hence the program has been executed successfully.

Ex.No.9
Date:
Aim:

Search the numbers from the given list using Binary Search

Algorithm:

Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int f[20],i,j,l,u,n,k,m,temp;

char done;
clrscr();
cout<<"\n Binery Search";
cout<<"\n\n Enter the total number :";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\n Enter the "<<i<<" numbers:";
cin>>f[i];
}
cout<<"\n\n Enter the key to be searched :\n";
cin>>k;
cout<<"\nThe Given list is: \n\n";
for(i=1;i<=n;i++)
cout<<f[i]<<"\t";
for(i=1;i<=n-1;i++)
{
for(j=i+1;j<=n;j++)
{
if(f[i]>f[j])
{
temp=f[i];
f[i]=f[j];
f[j]=temp;
}
}
}
cout<<"\n\n Sorted List is: ";
cout<<"\n\n --------------\n ";
for(i=1;i<=n;i++)
cout<<f[i]<<"\t";
cout<<"\n\n searched key is : "<<k;
l=1;u=n;
done='f';
while((l<=u)&&(done=='f'))
{
m=(l+u)/2;

if(k>f[m])
l=m+1;
if(k>f[m])
u=m-1;
if(k==f[m])
{
i=m;
done='t';
}
}
if(done=='f')
cout<<"\n\n" <<k<<" Key is not found in the file";
else
cout<<"\n\n"<<k<<" is founded in "<<i<<" position";
getch();
}

Output:
Binary Search
Enter the total number : 4
Enter the 1 numbers: 8
Enter the 2 numbers: 2
Enter the 3 numbers: 7

Enter the 4 numbers: 4


Enter the key to be searched : 9
Given List is:
8

Sorted List is
---------------2

Searched key is : 7
7 is founded in 3 Position

Result:
Hence the program has been executed successfully.

Ex.No10
Date:
Aim:

Binary Tree Traversal

Algorithm:

Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class treerec
{
public:
treerec *lchild;
char data;
treerec *rchild;
};
treerec *item;
treerec *rdata[50];
inorder(treerec *cnode)
{
if(cnode!=NULL)
{
inorder(cnode->lchild);
if(cnode->data!='-')
cout<<cnode->data<<"\t";
inorder(cnode->rchild);
}
return(1);
}
preorder(treerec *cnode)
{
if(cnode!=NULL)
{
if(cnode->data!='-')
cout<<cnode->data<<"\t";
preorder(cnode->lchild);
preorder(cnode->rchild);
}
return(1);
}

postorder(treerec *cnode)
{
if(cnode!=NULL)
{
postorder(cnode->lchild);
postorder(cnode->rchild);
if(cnode->data!='-')
cout<<cnode->data<<"\t";
}
return(1);
}
void main()
{
int i,n;
clrscr();
cout<<"\n Enter the number:";
cin>>n;
cout<<"\n Enter Data of the Tree :\n";
for(i=1;i<=n;i++)
{
rdata[i]=(treerec*)malloc(sizeof(treerec));
cin>>rdata[i]->data;
rdata[i]->rchild=NULL;
rdata[i]->rchild=NULL;
}
for(i=1;i<=n;i++)
{
rdata[i]->lchild=rdata[2*i];
rdata[i]->rchild=rdata[2*i+1];
}
item=rdata[1];
clrscr();
cout<<"\n TREE TRAVERSALS";
cout<<"\n ---------------";
cout<<"\n Given Data in tree";
cout<<"\n ------------------\n\t";

for(i=1;i<=n;i++)
cout<<rdata[i]->data<<"\t";
cout<<"\n\n";
cout<<"\n INORDER TREE TRAVERSAL";
cout<<"\n ----------------------\n\t";
inorder(item);
cout<<"\n PREORDER TREE TRAVERSAL";
cout<<"\n ----------------------\n\t";
preorder(item);
cout<<"\n POSTORDER TREE TRAVERSAL";
cout<<"\n ----------------------\n\t";
postorder(item);
getch();
}

Output:
Enter the number:3
Enter Data of the Tree :
3
4
5
TREE TRAVERSALS
Given Data in tree
3
4
5
INORDER TREE TRAVERSAL
4
3
5
PREORDER TREE TRAVERSAL
3
4
5
POSTORDER TREE TRAVERSAL
4
5
3

Result:
Hence the program has been executed successfully.

Вам также может понравиться