You are on page 1of 78

Program 1: WAP to implement array as an ADT. #include<iostream.h> #include<conio.

h> void main() { clrscr(); int a[100] ,i, num, pos, item, size; cout<<"enter the no. of elements: "; cin>>size; cout<<"enter the elements of array"<<endl; for(i=0;i<size;i++) { cin>>a[i]; cout<<endl; } cout<<"now array is: "<<endl; for(i=0;i<size;i++) cout<<"Element at position"<<i+1<<"is: "<<a[i]<<endl; cout<<"Press any key to insert element"<<endl; getch( ); cout<<"enter another element"; cin>>num; cout<<"enter the position of element"; cin>>pos; cout<<endl; --pos; for(i=size;i>=pos;i--) a[i+1]=a[i]; a[pos]=num; size++; cout<<"After insertion array becomes: "<<endl; for(i=0;i<size;i++) {

cout<<"Element at position"<<i+1<<"is: "<<a[i]<<endl;} cout<<"press any key to delete element from array"<<endl; getch(); cout<<"enter the position number of the element to be deleted"; cin>>pos; --pos; item=a[pos]; for(i=pos;i<size;i++) a[i]=a[i+1]; size--; cout<<"deleted element: "<<item<<endl; cout<<endl<<"new array: "<<endl; for(i=0;i<size;i++) cout<<"element at position"<<i+1<<"is: "<<a[i]<<endl; getch(); }

Output: enter the no. of elements: 3 enter the elements of array: 4 3 2 Now array is: Element at position1 is: 4 Element at position2 is: 3 Element at position3 is: 2 Press any key to insert element Enter another element: 1 Enter position of element: 4 After insertion array becomes: Element at position1 is: 4 Element at position2 is: 3 Element at position3 is: 2 Element at position4 is: 1 Press any key to delete element from array Enter the position number of the element to be deleted: 1 Deleted element: 4 New array: Element at position1 is: 3 Element at position2 is: 2 Element at position3 is: 1

Program 2: WAP to implement stack using array implementation. #include<iostream.h> #include<conio.h> #include<process.h> #define MAXSIZE 10 void push(); void pop(); void display(); int top=-1; int stack[MAXSIZE]; int ele; void main() { int choice; while(1) { clrscr(); cout<<"enter ur choice:"<<endl; cout<<"1.push"<<endl <<"2.pop"<<endl<<"3.display"<<endl; cout<<"4.exit" <<endl; cin>>choice; switch(choice) { case 1:push(); break; case 2:pop(); break; case 3:display(); break; case 4: exit(0); break; default: cout<<"you entered wrong choice"; break; } getch();} } void push() { if(top==MAXSIZE-1) { cout<<"stack overflow"; } 4

else { cout<<"enter the element to insert:"; cin>>ele; top=top+1; stack[top]=ele; } } void pop() { if(top==-1) { cout<<"stack underflow:no element to delete"; } else { ele=stack[top]; top=top-1; } cout<<"deleted element: "<<ele; } void display() { if(top==-1) { cout<<"stack is empty"; } else { int i; cout<<"elements in stack are: "; for(i=top;i>=0;i--) { cout<<stack[i]<<endl; } } }

Output: enter ur choice: 1.Push 2.Pop 3.display 4.exit 1 Enter the element to insert 2 Enter ur choice: 1.Push 2.Pop 3.display 4.exit 1 Enter the element to insert 1 Enter ur choice: 1.Push 2.Pop 3.display 4.exit 3 Elements in stack are: 1 2

Program 3: WAP to implement stack using pointer implementation. #include<iostream.h> #include<conio.h> #include<malloc.h> #include<process.h> struct stack { int info; struct stack *next ; }; struct stack *top=NULL,*temp; void main() { int choice,ele; clrscr(); while(1) { cout<<"enter ur choice: "<<endl; cout<<"1.push"<<endl<<"2.pop"<<endl<<"3.display"<<endl<<"4.exit"; cin>>choice; switch(choice) { case 1: temp=(struct stack*)malloc(sizeof(struct stack)); cout<<"enter the element"; cin>>ele; temp->info=ele; temp->next=top; top=temp; break; case 2: if(top!=NULL) { cout<<"popped element is:"<<top->info; top=top->next; } else { cout<<"stack underflow"; } break; case 3: temp=top; 7

while(temp!=NULL) { cout<<temp->info<<endl; temp=temp->next; } if(top==NULL) { cout<<"stack underflow"; } break; case 4: exit(0); } getch(); } }

Output: enter ur choice: 1.Push 2.Pop 3.display 4.exit 1 Enter the element to insert 2 Enter ur choice: 1.Push 2.Pop 3.display 4.exit 1 Enter the element to insert 1 Enter ur choice: 1.Push 2.Pop 3.display 4.exit 3 Elements in stack are: 12

Program 4: WAP to implement linear queue using array implementation. #include<iostream.h> #include<conio.h> #include<process.h> #define MAX 5 void enqueue(int *,int,int *,int *); void dequeue(int *,int *,int *); void qdisplay(int *,int *,int *); void main() { clrscr(); int queue[MAX]; int front =-1; int rear=-1; int value,n; int choice,i; while(1) { cout<<"enter ur choice"; cout<<"1.insert"<<endl<<"2.deletion"<<endl<<"3.display"<<endl<<"4.exit"<<endl; cin>>choice; switch(choice) { case 1: cout<<"enter the value to insert"; cin>>value; enqueue(queue,value,&front,&rear); break; case 2: dequeue(queue,&front,&rear); break; case 3: qdisplay(queue,&front,&rear); break; case 4: 10

exit(0); break; } getch(); }} void enqueue(int *queue,int value,int *front,int *rear) { if(*rear==MAX-1) { cout<<"queue is full"<<endl; } else { *rear=*rear+1; queue[*rear]=value; if(*front==-1) *front=0; }} void dequeue(int *queue,int *front,int *rear) { int data; if(*front==-1) { cout<<"queue is empty"<<endl; } data=queue[*front]; queue[*front]=0; if(*front==*rear) { *front=-1; *rear=-1; } else (*front)++; cout<<data; } void qdisplay(int *queue,int *front,int *rear) { int i; if(*front==-1) { cout<<"queue is empty"<<endl; } else { cout<<"element in queue are"<<endl; 11

for(i=*front;i<=*rear;i++) cout<<queue[i]<<endl; } } Output: Enter ur choice: 1.insert 2.deletion 3.display 4.exit 1 Enter the value to insert 3 Enter ur choice: 1.insert 2.deletion 3.display 4.exit 1 Enter the value to insert 2 Enter ur choice: 1.insert 2.deletion 3.display 4.exit 3 Element in queue are: 32

12

Program 5: WAP to implement linear queue using pointer implementation. #include<iostream.h> #include<conio.h> #include<malloc.h> #include<process.h> struct queue { int info; struct queue *next; }; struct queue *front=NULL,*rear=NULL,*p,*temp; void enqueue(); void display(); void dequeue(); void main() { clrscr(); while(1) { int choice; cout<<"enter the choice"<<endl; cout<<"1.insert"<<endl; cout<<"2.deletion"<<endl; cout<<"3.display"<<endl; cout<<"4.exit"<<endl; cin>>choice; switch(choice) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: exit(0); break; default: cout<<"u entered a wrong choice"; break; } 13

getch(); } } void enqueue() { int item; cout<<"enter the item to insert"<<endl; cin>>item; p=(struct queue*)malloc(sizeof(struct queue)); p->info=item; p->next=NULL; if(front==NULL && rear==NULL) { front=p; rear=p; } else { rear->next=p; rear=p; } } void display() { if(front==NULL && rear==NULL) cout<<"no element in the list"; else { temp=front; cout<<"--------------------"<<endl; cout<<"element in queue are"<<endl; while(temp!=NULL) { cout<<temp->info<<endl; temp=temp->next; } } } void dequeue() { if(!(front==NULL && rear==NULL)) { if(front==rear) { front=NULL; rear=NULL; } front=front->next; } 14

else { cout<<"deletion not possible becoz no element in the list to delete"<<endl; } } Output: Enter ur choice: 1.insert 2.deletion 3.display 4.exit 1 Enter the value to insert 3 Enter ur choice: 1.insert 2.deletion 3.display 4.exit 1 Enter the value to insert 2 Enter ur choice: 1.insert 2.deletion 3.display 4.exit 3 Element in queue are: 3 2

15

Program 6: WAP to implement circular queue. #include<iostream.h> #include<conio.h> #include<process.h> #define MAX 10 void cenqueue(int *,int,int *,int *); void cdequeue(int *,int *,int *); void cqdisplay(int *,int *,int *); void main() { clrscr(); int i,front=-1,rear=-1; int cqueue[MAX]; int choice,item; while(1) { cout<<"enter ur choice"<<endl; cout<<"1.insert in circular queue"<<endl<<"2.deletion in cqueue"<<endl<<"3.display"<<endl<<"4.exit"<<endl; cin>>choice; switch(choice) { case 1: cenqueue(cqueue,item,&front,&rear); break; case 2: cdequeue(cqueue,&front,&rear); break; case 3: cqdisplay(cqueue,&front,&rear); break; case 4: exit(0); break; } getch(); } } void cqdisplay(int *cqueue,int *front,int *rear) { int i; cout<<"Front= "<<*front<<" "<<"Rear= "<<*rear<<endl; if(*front==-1) cout<<"Queue is empty now!!!"; 16

else { cout<<"queue is: "<<endl; for(i=*front;i<=*rear;i++) cout<<cqueue[i]<<" "; cout<<endl; }} void cenqueue(int *cqueue,int item,int *front,int * rear) { cout<<"Enter the item to insert"<<endl; cin>>item; if((*rear==MAX-1 && *front==0)||(*rear+1==*front)) { cout<<"Queue is full"; return; } if(*rear==MAX-1) *rear=0; else (*rear)++; cqueue[*rear]=item; if(*front==-1) *front=0; } void cdequeue(int *cqueue,int *front,int * rear) { int item; if(*front==-1) { cout<<"queue is empty."<<endl; } item=cqueue[*front]; cqueue[*front]=0; if(*front==*rear) { *front=-1; *rear=-1; } Else { if(*front==MAX-1) *front=0; else (*front)++; } 17

cout<<"Deleted item is: "<<item; } Output: Enter ur choice 1.insert in circular queue 2.deletion in cqueue 3.display 4.exit 1 Enter the item to insert 3 Enter ur choice 1.insert in circular queue 2.deletion in cqueue 3.display 4.exit 1 Enter the item to insert 2 Enter ur choice 1.insert in circular queue 2.deletion in cqueue 3.display 4.exit 3 Front=0 Rear=1 Queue is: 32

18

Program 7: WAP to implement singly linear linked list. #include<iostream.h> #include<conio.h> #include<malloc.h> #include<process.h> struct node { int info; struct node *next; }; struct node *head,*p,*temp; void ins_at_beg(); void ins_at_end(); void ins_after_ele(); void ins_before_ele(); void delete_at_beg(); void delete_at_end(); void delete_after_ele(); void delete_before_ele(); void display(); void main() { clrscr(); while(1) { int choice; cout<<"enter ur choice"<<endl; cout<<"1.insertion"<<endl<<"2.deletion"<<endl<<"3.display"<<endl<<"4.exit"<<endl; cin>>choice; switch(choice) { case 1: int ch; cout<<"enter ur choice"<<endl; cout<<"1.insertion at beginning"<<endl; cout<<"2.insertion at end"<<endl;

19

cout<<"3.insertion after given element"<<endl; cout<<"4.insertion before given element"<<endl; cin>>ch; switch(ch) { case 1: ins_at_beg(); break; case 2: ins_at_end(); break; case 3: ins_after_ele(); break; case 4: ins_before_ele(); break; default: cout<<"invalid choice"<<endl; break; } break; case 2: int ch1; cout<<"enter ur choice:"<<endl; cout<<"1.deletion at beginning" <<endl; cout<<"2.deletion at end"<<endl; cout<<"3.deletion after given element"<<endl; cout<<"4.deletion before given element"<<endl; cin>>ch1; switch(ch1) { case 1: delete_at_beg(); break; case 2: delete_at_end(); break; case 3: delete_after_ele(); break; case 4: delete_before_ele(); break; default: cout<<"invalid choice"<<endl; break; } break; case 3: display(); break; case 4: exit(0); break; default: cout<<"u have entered a wrong choice"; break; 20

} getch(); } } void ins_at_beg() { int item; cout<<"enter the item to insert:" <<endl; cin>>item; p=(struct node *)malloc(sizeof(struct node)); p->info=item; p->next=head; head=p; } void display() { temp=head; if(temp==NULL) { cout<<"list is empty"<<endl; } else { cout<<"The linked list is"<<endl; while(temp!=NULL) { cout<<temp->info<<endl; temp=temp->next; } } } void ins_at_end() { temp=head; int item; cout<<"enter the element to insert at end" <<endl; cin>>item; p=(struct node *)malloc(sizeof(struct node)); p->info=item; p->next=NULL; if(head==NULL) { head=p; } 21

else { while(temp->next!=NULL) { temp=temp->next; } temp->next=p; } } void ins_after_ele() { int item,ele; temp=head; cout<<"enter the item to insert"<<endl; cin>>item; cout<<"enter the element after which insertion takes place"<<endl; cin>>ele; p=(struct node*)malloc(sizeof(struct node)); p->info=item; while(temp->info!=ele && temp!=NULL) { temp=temp->next; } if(temp!=NULL) { p->next=temp->next; temp->next=p; } else { cout<<"insertion is not possible due to non availability of the node="<<ele<<endl; } } void ins_before_ele() { int item,ele; cout<<"enter the item to insert"<<endl; cin>>item; cout<<"enter the element before which insertion takes place"<<endl; cin>>ele; p=(struct node*)malloc(sizeof(struct node)); p->info=item; if(head->info==ele) { p->next=head; 22

head=p; } else { temp=head; while(temp->next->info!=ele && temp!=NULL) { temp=temp->next; } if(temp!=NULL) { p->next=temp->next; temp->next=p; } else cout<<"insertion not possible"<<endl; } } void delete_at_beg() { if(head!=NULL) { head=head->next; } else cout<<"deletion not possible"; } void delete_at_end() { if(head->next==NULL) { head=NULL; } else { temp=head; while(temp->next->next!=NULL) { temp=temp->next; } temp->next=NULL; } } void delete_after_ele() { int ele; cout<<"enter the element after which deletion takes place"<<endl; cin>>ele; temp=head; while(temp->info!=ele && temp!=NULL) { 23

temp=temp->next; } if(temp!=NULL) { temp->next=temp->next->next; } else cout<<"given element not in the list"; } void delete_before_ele() { int ele; cout<<"enter the element before which deletion takes place"<<endl; cin>>ele; if(head->next->info==ele) { head=head->next; } else { temp=head; while(temp->next->next->info!=ele && temp!=NULL) { temp=temp->next; } if(temp!=NULL) { temp->next=temp->next->next; } else { cout<<"given element not in the list"; } } }

24

Output: Enter ur choice 1.insertion 2.deletion 3.display 4.exit 1 Enter ur choice 1.insertion at beginning 2.insertion at end 3.insertion after given element 4.insertion before given element 1 Enter the item to insert: 1 Enter ur choice 1.insertion 2.deletion 3.display 4.exit 1 Enter ur choice 1.insertion at beginning 2.insertion at end 3.insertion after given element 4.insertion before given element 2 Enter the element to insert at end: 2 The linked list is 1 2

25

Program 8: WAP to implement singly circular linked list. #include<iostream.h> #include<conio.h> #include<malloc.h> #include<process.h> struct node { int info; struct node *next; }; struct node *head,*p,*temp; void display(); void ins_at_beg(); void ins_at_end(); void ins_after_ele(); void ins_before_ele(); void del_at_beg(); void del_at_end(); void del_after_ele(); void del_before_ele(); void main() { clrscr(); int choice; while(1) { cout<<"enter ur choice"<<endl; cout<<"1.insertion"<<endl<<"2.deletion"<<endl<<"3.display"<<endl<<"4.exit"<<endl; cin>>choice; switch(choice) { case 1: int ch1; cout<<"enter your choice:"<<endl;

26

cout<<"1.insertion at beginning"<<endl<<"2.insertion at end"<<endl; cout<<"3.insertion after element"<<endl; cout<<"4.insertion before given element"<<endl; cin>>ch1; switch(ch1) { case 1: ins_at_beg(); break; case 2: ins_at_end(); break; case 3: ins_after_ele(); break; case 4: ins_before_ele(); break; default: cout<<"you entered a wrong choice"; break; } break; case 2: int ch2; cout<<"1.deletion at beginning"<<endl<<"2.deletion at end"<<endl; cout<<"3.deletion after given element"<<endl; cout<<"4.deletion before given element"<<endl; cin>>ch2; switch(ch2) { case 1: del_at_beg(); break; case 2: del_at_end(); break; case 3: del_after_ele(); break; case 4: del_before_ele(); break; default: cout<<"u entered a wrong choice"<<endl; break; } break; case 3: display(); break; case 4: exit(0); break; default: 27

cout<<"invalid choice" <<endl; break; } getch(); } } void display() { cout<<"elements in list are:"<<endl; if(head->next==head) { cout<<head->info; } else { temp=head; while(temp->next!=head) { cout<<temp->info<<" "; temp=temp->next; } cout<<endl; } } void ins_at_beg() { int item; cout<<"enter the item to insert"<<endl; cin>>item; p=(struct node*)malloc(sizeof(struct node)); p->info=item; if(head==NULL) { head=p; p->next = head; return; } else { temp=head; while(temp->next!=head) { temp=temp->next; } temp->next=p; p->next=head; head=p; } } void ins_at_end() { int item; cout<<"enter the item to insert at end"<<endl; 28

cin>>item; p=(struct node*)malloc(sizeof(struct node)); p->info=item; if(head==NULL) { head=p; p->next=head; } else if(head->next==head) { head->next=p; p->next=head; } else { temp=head; while(temp->next!=head) { temp=temp->next; } temp->next=p; p->next=head; } } void ins_after_ele() { int item,ele; cout<<"enter the item to insert "<<endl; cin>>item; cout<<"enter the element after which insertion takes place"<<endl; cin>>ele; p=(struct node*)malloc(sizeof(struct node)); p->info=item; if(head==NULL) { cout<<"insertion is not possible"; } else { if(head->info==ele) { p->next=head->next; head->next=p; } else { temp=head->next; while(temp->info!=ele && temp!=head) { temp=temp->next; } 29

if(temp!=head) { p->next=temp->next; temp->next=p; } else cout<<"insertion can't take place due to the non availability of the element"<<endl; } }} void ins_before_ele() { int item,ele; cout<<"enter the item to insert "<<endl; cin>>item; cout<<"enter the element before which insertion takes place"<<endl; cin>>ele; p=(struct node*)malloc(sizeof(struct node)); p->info=item; if(head==NULL) { cout<<"insertion is not possible"; } else { if(head->info==ele) { p->next=head; temp=head; while(temp->next!=head) { temp=temp->next; } temp->next=p; head=p; } else { temp=head; while((temp->next->info!=ele)&&(temp->next!=head)) { temp=temp->next; } if(temp!=head) { p->next=temp->next; temp->next=p; } } } } void del_at_beg() { 30

if(head==NULL) cout<<"list is empty"<<endl; if(head==head->next) { head=NULL; } else { temp=head; while(temp->next!=head) { temp=temp->next; } temp->next=head->next; head=head->next; } } void del_at_end() { if(head==NULL) cout<<"list is empty"<<endl; if(head==head->next) { head=NULL; } else { temp=head; while(temp->next->next!=head) { temp=temp->next; } temp->next=temp->next->next; temp=head->next; } } void del_after_ele() { int ele; if(head==NULL) { cout<<"Deletion not possible"<<endl; } else { cout<<"enter the element after which deletion takes place"<<endl; cin>>ele; if(head->info==ele) { head->next=head->next->next; } 31

else { temp=head; while(temp->info!=ele && temp->next!=head) { temp=temp->next; } if(temp!=head) { temp->next=temp ->next->next; } else temp->next=head->next; head=head->next; } } } void del_before_ele() { int ele; if(head==NULL) { cout<<"Deletion not possible"<<endl; } else { cout<<"enter the element before which deletion takes place"<<endl; cin>>ele; if(head->info==ele) { temp=head; while(temp->next->next!=head) { temp=temp->next; } temp->next=head; } else { temp=head->next; while(temp->next->next->info!=ele && temp->next!=head) { temp=temp->next; } if(temp!=head) { temp->next=temp->next->next; } else cout<<"element not in the list" ; } } }

32

Output: Enter ur choice: 1.insertion 2.deletion 3.display 4.exit 1 Enter your choice: 1.insertion at beginning 2.insertion at end 3.insertion after element 4.insertion before given element 2 Enter the item to insert at end 11 Enter ur choice: 1.insertion 2.deletion 3.display 4.exit 1 Enter your choice: 1.insertion at beginning 2.insertion at end 3.insertion after element 4.insertion before given element 2 Enter the item to insert at end 12

Elements in list are: 11 12

33

Program 9: WAP to implement Doubly linear linked list.


#include <stdio.h> #include <stdlib.h> struct dllist { int number; struct dllist *next; struct dllist *prev; }; struct dllist *head, *tail; void append_node(struct dllist *lnode); void insert_node(struct dllist *lnode, struct dllist *after); void remove_node(struct dllist *lnode); int main(void) { struct dllist *lnode; int i = 0; /* add some numbers to the double linked list */ for(i = 0; i <= 5; i++) { lnode = (struct dllist *)malloc(sizeof(struct dllist)); lnode->number = i; append_node(lnode); } /* print the dll list */ for(lnode = head; lnode != NULL; lnode = lnode->next) { printf("%d\n", lnode->number); } /* destroy the dll list */ while(head != NULL) remove_node(head);

34

return 0; } void append_node(struct dllist *lnode) { if(head == NULL) { head = lnode; lnode->prev = NULL; } else { tail->next = lnode; lnode->prev = tail; } tail = lnode; lnode->next = NULL; } void insert_node(struct dllist *lnode, struct dllist *after) { lnode->next = after->next; lnode->prev = after; if(after->next != NULL) after->next->prev = lnode; else tail = lnode; after->next = lnode; } void remove_node(struct dllist *lnode) { if(lnode->prev == NULL) head = lnode->next; else

35

lnode->prev->next = lnode->next; if(lnode->next == NULL) tail = lnode->prev; else lnode->next->prev = lnode->prev; }

36

Program 10: WAP to implement Doubly circular linked list.

#include<stdio.h> #include<stdlib.h> struct node { struct node *prev,*next; int data; }; void create_list(struct node *list,int n) { struct node *n1,*n2; int i; n1=list; printf("enter the data for the 1 node:"); scanf("%d",&list->data); for(i=1;i<n;i++) { list->next=(struct node *)malloc(sizeof(struct node)); if(list->next==NULL) { printf("malloc not done"); exit(0); } n2=list; list=list->next; list->prev=n2; printf("enter the data for the %d node",i+1);

37

scanf("%d",&list->data); } list->next=n1; n1->prev=list; } void print_list(struct node *list) { struct node *temp; temp=list; if(list->next=temp) { printf("%d",list->data); } if(list->next!=temp) { printf("%d",list->data); if(list->next->next==temp) printf("%d",list->next->data); else print_list(list->next); } } int main() { int n; struct node *head; head=(struct node *)malloc(sizeof(struct node)); if(head==NULL) 38

{ printf("space unallocated"); exit (0); } printf("enter the no of nodes:"); scanf("%d",&n); create_list(head,n); print_list(head); return 0; }

39

Program 11: WAP to implement Binary Search tree


#include<iostream.h> #include<conio.h> #include<stdlib.h> struct node { int data; struct node *left, *right; }; typedef struct node node; non recursive method : main() { node * root = NULL , *new = NULL *temp1 =NULL , * temp2 = NULL; int num =1; printf(" Enter the elements of the tree( enter 0 to exit)\n"); while(1) { scanf("%d", &num); if(num==0) break; new = malloc(sizeof(node)); new->left = new->right = NULL; new->data = num; if( root == NULL) root = new; else { temp1 = root; while(temp1!=NULL) { temp2 = temp1; if( new->data > temp1->data ) temp1 = temp1->right; else temp1 = temp1->left; } if( new->data > temp2->data ) 40

temp2->right = new; else temp2->left = new; } } } void main() { node * root = NULL , *new = NULL ; int num =1; printf(" Enter the elements of the tree( enter 0 to exit)\n"); while(1) { scanf("%d", &num); if(num==0) break; new = malloc(sizeof(node)); new->left = new->right = NULL; new->data = num; if( root == NULL) root = new; else { insert(new,root); } } } void insert( node * new , node *root) { if( new->data > root->data) { if( root->right == NULL) root->right = new; else insert( new,root->right); } if( new->data < root->data) { if( root->left == NULL) root->left = new; else insert( new,root->left); } } 41

Output: Specify the number of items to be inserted: 5 Enter the data: 4 Enter the data: 2 Enter the data: 6 Enter the data: 4 Enter the data: 1 Inorder traversal: 12446 Preorder traversal: 42164 Postorder traversal: 12464 Binary tree before deletion: 12446 Enter the node to delete: 4 Binary tree after deletion: 1246

42

Program 12: WAP to implement AVL tree # include<stdio.h> # include<malloc.h> # define F 0 # define T 1 struct NODE { char Info; int Flag; struct NODE *Left_Child; struct NODE *Right_Child; };

struct NODE *Binary_Tree (char , struct NODE *, int *); void Output(struct NODE *, int ); struct NODE *Balance_Right_Heavy(struct NODE *, int *); struct NODE *Balance_Left_Heavy(struct NODE *, int *); struct NODE *DELETE(struct NODE *, struct NODE *, int *); struct NODE *Delete_Element(struct NODE *, char , int *); struct NODE * Binary_Tree (char Info, struct NODE *Parent, int *H) { struct NODE *Node1; struct NODE *Node2; if(!Parent) { Parent = (struct NODE *) malloc(sizeof(struct NODE)); Parent->Info = Info; Parent->Left_Child = NULL; Parent->Right_Child = NULL; Parent->Flag = 0; *H = T; return (Parent); }

if(Info < Parent->Info) 43

{ Parent->Left_Child = Binary_Tree(Info, Parent->Left_Child, H); if(*H) { switch(Parent->Flag) { case 1: *H = F; break; case 0: Parent->Flag = -1; break; case -1 Node1 = Parent->Left_Child; if(Node1->Flag == -1) { printf("\n Left to Left Rotation\n"); Parent->Left_Child= Node1->Right_Child; Node1->Right_Child = Parent; Parent->Flag = 0; Parent = Node1; } else { printf("\n Left to right rotation\n"); Node2 = Node1->Right_Child; Node1->Right_Child = Node2->Left_Child; Node2->Left_Child = Node1; Parent->Left_Child = Node2->Right_Child; Node2->Right_Child = Parent; if(Node2->Flag == -1) Parent->Flag = 1; else Parent->Flag = 0; if(Node2->Flag == 1) Node1->Flag = -1; 44 Parent->Flag = 0;

else Node1->Flag = 0; Parent = Node2; }

Parent->Flag = 0; *H = F; } } }

if(Info > Parent->Info) { Parent->Right_Child = Binary_Tree(Info, Parent->Right_Child, H); if(*H) { switch(Parent->Flag) { case -1: Parent->Flag = 0; *H = F; break; case 0: break; Parent->Flag = 1;

case 1: Node1 = Parent->Right_Child; if(Node1->Flag == 1) { printf("\n Right to Right Rotation\n"); Parent->Right_Child= Node1->Left_Child; Node1->Left_Child = Parent; Parent->Flag = 0; Parent = Node1; } else 45

{ printf("\n Right to Left Rotation\n"); Node2 = Node1->Left_Child; Node1->Left_Child = Node2->Right_Child; Node2->Right_Child = Node1; Parent->Right_Child = Node2->Left_Child; Node2->Left_Child = Parent;

if(Node2->Flag == 1) Parent->Flag = -1; else Parent->Flag = 0; if(Node2->Flag == -1) Node1->Flag = 1; else Node1->Flag = 0; Parent = Node2; }

Parent->Flag = 0; *H = F; } } } return(Parent); } void Output(struct NODE *Tree,int Level) { int i; if (Tree) { Output(Tree->Right_Child, Level+1); printf("\n"); for (i = 0; i < Level; i++) printf(" "); printf("%c", Tree->Info); 46

Output(Tree->Left_Child, Level+1); } } struct NODE * Balance_Right_Heavy(struct NODE *Parent, int *H) { struct NODE *Node1, *Node2;

switch(Parent->Flag) { case -1: Parent->Flag = 0; break;

case 0: Parent->Flag = 1; *H= F; break;

case 1: Node1 = Parent->Right_Child; if(Node1->Flag >= 0) { printf("\n Right to Right Rotation\n"); Parent->Right_Child= Node1->Left_Child; Node1->Left_Child = Parent; if(Node1->Flag == 0) {

Parent->Flag = 1; Node1->Flag = -1; *H = F; } else { Parent->Flag = Node1->Flag = 0; } 47

Parent = Node1; } else { printf("\n Right to Left Rotation\n"); Node2 = Node1->Left_Child; Node1->Left_Child = Node2->Right_Child; Node2->Right_Child = Node1; Parent->Right_Child = Node2->Left_Child; Node2->Left_Child = Parent;

if(Node2->Flag == 1) Parent->Flag = -1; else Parent->Flag = 0; if(Node2->Flag == -1) Node1->Flag = 1; else Node1->Flag = 0; Parent = Node2; Node2->Flag = 0; } } return(Parent); }

struct NODE * Balance_Left_Heavy(struct NODE *Parent, int *H) { struct NODE *Node1, *Node2;

switch(Parent->Flag) { case 1: Parent->Flag = 0; break;

48

case 0: Parent->Flag = -1; *H= F; break;

case -1: Node1 = Parent->Left_Child; if(Node1->Flag <= 0) { printf("\n Left to Left Rotation\n"); Parent->Left_Child= Node1->Right_Child; Node1->Right_Child = Parent; if(Node1->Flag == 0) { Parent->Flag = -1; Node1->Flag = 1; *H = F; } else { Parent->Flag = Node1->Flag = 0; } Parent = Node1; } else { printf("\n Left to Right Rotation\n"); Node2 = Node1->Right_Child; Node1->Right_Child = Node2->Left_Child; Node2->Left_Child = Node1; Parent->Left_Child = Node2->Right_Child; Node2->Right_Child = Parent;

if(Node2->Flag == -1) Parent->Flag = 1; else 49

Parent->Flag = 0;

if(Node2->Flag == 1) Node1->Flag = -1; else Node1->Flag = 0; Parent = Node2; Node2->Flag = 0; } } return(Parent); } struct NODE * DELETE(struct NODE *R, struct NODE *Temp, int *H) { struct NODE *Dnode = R; if( R->Right_Child != NULL) { R->Right_Child = DELETE(R->Right_Child, Temp, H); if(*H) R = Balance_Left_Heavy(R, H); } else { Dnode = R; Temp->Info = R->Info; R = R->Left_Child; free(Dnode); *H = T; } return(R); } struct NODE * Delete_Element(struct NODE *Parent, char Info, int *H) { struct NODE *Temp; if(!Parent) { 50

printf("\n Information does not exist"); return(Parent); } else { if (Info < Parent->Info ) { Parent->Left_Child = Delete_Element(Parent>Left_Child, Info, H); if(*H) Parent = Balance_Right_Heavy(Parent, H); } else if(Info > Parent->Info) { Parent->Right_Child=Delete_Element(Parent->Right_Child, Info, H); if(*H) Parent =Balance_Left_Heavy(Parent, H); } else { Temp= Parent; if(Temp->Right_Child == NULL) { Parent = Temp->Left_Child; *H = T; free(Temp); } else if(Temp->Left_Child == NULL) { Parent = Temp>Right_Child; *H = T; free(Temp); } else { 51

Temp->Left_Child =DELETE(Temp->Left_Child, Temp, H); if(*H) Parent =Balance_Right_Heavy(Parent, H); } } } return(Parent); } void main() { int H; char Info ; char choice; struct NODE *Tree = (struct NODE *)malloc(sizeof(struct NODE)); Tree = NULL; printf("\n Input choice 'b' to break:"); while(choice != 'b') { fflush(stdin); printf("\n Input information of the node: "); scanf("%c", &Info); Tree = Binary_Tree(Info, Tree, &H); printf("\n Tree is:\n"); Output(Tree, 1); fflush(stdin); printf("\n Input choice 'b' to break:"); choice = getchar(); } fflush(stdin); while(1) { printf("\n Input choice 'b' to break:"); printf("\n Input the key value want to deletedir:"); scanf("%c", &Info); if (Info == 'b') break; 52 choice = getchar();

Tree = Delete_Element(Tree, Info, &H); printf("\n Tree is:\n"); Output(Tree, 1); } } Output:

Left rotation along 6 AVL tree: 5 6 10 12 13 15 20 25 27 29 32 AVL tree after deletion of a node: 5 6 10 13 15 25 27 29 32

53

Program 13: WAP to implement B Tree #include<iostream.h> #include<conio.h> #include<malloc.h> # define MAX 4 # define MIN 2 struct btnode { int count; int value[MAX+1]; struct btnode *child[MAX+1]; }; struct btnode *insert(int,struct btnode *); int setval(int,struct btnode *,int *,struct btnode **); struct btnode *search(int,struct btnode *,int *); int searchnode(int,struct btnode *,int *); void fillnode(int,struct btnode *,struct btnode *,int); void split(int,struct btnode *,struct btnode *,int,int *,struct btnode **); struct btnode *delet(int,struct btnode *); int delheap(int,struct btnode *); void clear(struct btnode*,int); void copysucc(struct btnode *,int); void restore(struct btnode *,int); void rightshift(struct btnode *,int); void leftshift(struct btnode *,int); void merge(struct btnode *,int); void display(struct btnode*);

void main() { struct btnode *root; root=NULL; clrscr();

root=insert(27,root);

54

root=insert(42,root); root=insert(22,root); root=insert(47,root); root=insert(32,root); root=insert(2 ,root); root=insert(51,root); root=insert(40,root); root=insert(13,root); cout<<"B-tree of order 5:\n"; display(root); root=delet(22,root); root=delet(11,root); cout<<"\n\nAfter deletion of values:\n"; display(root); getch(); } struct btnode *insert(int val,struct btnode *root) { int i; struct btnode *c,*n; int flag; flag=setval(val,root,&i,&c); if(flag) { n=(struct btnode*)malloc(sizeof(struct btnode)); n->count=1; n->value[1]=i; n->child[0]=root; n->child[1]=c; return n; } return root; } int setval(int val,struct btnode *n,int *p,struct btnode **c) { int k; if(n==NULL) { 55

*p=val; *c=NULL; return 1; } else { if(searchnode(val,n,&k)) cout<<"\nKey value already exists.\n"; if(setval(val,n->child[k],p,c)) { if(n->count<MAX) { fillnode(*p,*c,n,k); return 0; } else { split(*p,*c,n,k,p,c); return 1; } } return 0; } } struct btnode *search(int val,struct btnode *root,int *pos) { if(root==NULL) return NULL; else { if(searchnode(val,root,pos)) return root; else return search(val,root->child[*pos],pos); } } int searchnode(int val,struct btnode *n,int *pos) { if(val<n->value[1]) { *pos=0; return 0; } else { *pos=n->count; 56

while((val<n->value[*pos])&& *pos>1) (*pos)--; if(val==n->value[*pos]) return 1; else return 0; } } void fillnode(int val,struct btnode *c,struct btnode *n,int k) { int i; for(i=n->count;i>k;i--) { n->value[i+1]=n->value[i]; n->child[i+1]=n->child[i]; } n->value[k+1]=val; n->child[k+1]=c; n->count++; } void split(int val,struct btnode *c,struct btnode *n,int k,int *y,struct btnode **newnode) { int i,mid; if(k<=MIN) mid=MIN; else mid=MIN+1; *newnode=(struct btnode *)malloc(sizeof(struct btnode)); for(i=mid+1;i<=MAX;i++) { (*newnode)->value[i-mid]=n->value[i]; (*newnode)->child[i-mid]=n->child[i]; } (*newnode)->count=MAX-mid; n->count=mid; if(k<=MIN) fillnode(val,c,n,k); else fillnode(val,c,*newnode,k-mid); 57

*y=n->value[n->count]; (*newnode)->child[0]=n->child[n->count]; n->count--; } struct btnode *delet(int val,struct btnode *root) { struct btnode *temp; if(!delheap(val,root)) cout<<"\nvalue "<<val<<" not found"; else { if(root->count==0) { temp=root; root=root->child[0]; free(temp); } } return root; } int delheap(int val,struct btnode *root) { int i; int flag; if(root==NULL) return 0; else { flag=searchnode(val,root,&i); if(flag) { if(root->child[i-1]) { copysucc(root,i); flag=delheap(root->value[i],root->child[i]); if(!flag) cout<<"\nvalue "<<val<<" not found"; } else clear(root,i); } else flag=delheap(val,root->child[i]); if(root->child[i]!=NULL) { 58

if(root->child[i]->count<MIN) restore(root,i); } return flag; } } void clear(struct btnode *node,int k) { int i; for(i=k+1;i<=node->count;i++) { node->value[i-1]=node->value[i]; node->child[i-1]=node->child[i]; } node->count--; } void copysucc(struct btnode *node,int i) { struct btnode *temp; temp=node->child[i]; while(temp->child[0]) temp=temp->child[0]; node->value[i]=temp->value[1]; } void restore(struct btnode *node,int i) { if(i==0) { if(node->child[1]->count>MIN) leftshift(node,1); else merge(node,1); } else { if(i==node->count) { if(node->child[i-1]->count>MIN) rightshift(node,i); else merge(node,i); } else { if(node->child[i-1]->count>MIN) 59

rightshift(node,i); else { if(node->child[i+1]->count>MIN) leftshift(node,i+1); else merge(node,i); } } } } void rightshift(struct btnode *node,int k) { int i; struct btnode *temp; temp=node->child[k]; for(i=temp->count;i<0;i--) { temp->value[i+1]=temp->value[i]; temp->child[i+1]=temp->child[i]; } temp->child[1]=temp->child[0]; temp->count++; temp->value[1]=node->value[k]; temp=node->child[k-1]; node->value[k]=temp->value[temp->count]; node->child[k]->child[0]=temp->child[temp->count]; temp->count--; } void leftshift(struct btnode *node,int k) { int i; struct btnode *temp; temp=node->child[k-1]; temp->count++; temp->value[temp->count]=node->value[k]; temp->child[temp->count]=node->child[k]->child[0]; temp=node->child[k]; node->value[k]=temp->value[1]; temp->child[0]=temp->child[1]; 60

temp->count--; for(i=1;i<=temp->count;i++) { temp->value[i]=temp->value[i+1]; temp->child[i]=temp->child[i+1]; } } void merge(struct btnode *node,int k) { int i; struct btnode *temp1,*temp2; temp1=node->child[k]; temp2=node->child[k-1]; temp2->count++; temp2->value[temp2->count]=node->value[k]; temp2->child[temp2->count]=node->child[0]; for(i=1;i<temp1->count;i++) { temp2->count++; temp2->value[temp2->count]=temp1->value[i]; temp2->child[temp2->count]=temp1->child[i]; } for(i=k;i<node->count;i++) { node->value[i]=node->value[i+1]; node->child[i]=node->child[i+1]; } node->count--; free(temp1); } void display(struct btnode *root) { int i; if(root!=NULL) { for(i=0;i<root->count;i++) 61

{ display(root->child[i]); cout<<root->value[i+1]<<" "; } display(root->child[i]); } }

Output: B-tree of order 5: 2 13 22 27 32 40 42 47 51 Value 11 not found After deletion of values: 2 13 27 32 40 42 47 51

62

Program14: WAP to sort a given list using insertion sort


#include<conio.h> #include<stdio.h> #include<graphics.h> #define max 20 void main() { int a[max],i,j,k=0,s,n; clrscr(); printf("\n Enter the number of elements : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter element %d] ",i+1); scanf(" %d",&a[i]); } for(j=1;j<n;j++) { k=a[j]; for(i=j-1;i>=0&&k<a[i];i--) a[i+1]=a[i]; a[i+1]=k; s=i+1; printf("\n Pass %d, Element insertetd in proper place : %d\n",j,k); printf("\n Pass %d : ",j); // to see how elements get arranged as loop // counter increases for(i=0;i<n;i++) { printf(" %d ",a[i]); } printf("\n"); } printf("\n sorted list is : \n"); for(i=0;i<n;i++) printf(" %d ",a[i]); printf("\n");

63

getch(); }

Output: Enter the no. of elements: 54176 Array before sorting: 54176 After after sorting: 14567

64

Program 15: WAP to sort a given list using selection sort #include<stdio.h> #include<stdlib.h> void swap(int *x,int *y) { int temp; temp = *x; *x = *y; *y = temp; } void selection_sort(int list[], int n) { int i, j, min; for (i = 0; i < n - 1; i++) { min = i; for (j = i+1; j < n; j++) { if (list[j] < list[min]) { min = j; } } swap(&list[i], &list[min]); } } void printlist(int list[],int n) { int i; for(i=0;i<n;i++) printf("%d\t",list[i]); } void main() 65

{ const int MAX_ELEMENTS = 10; int list[MAX_ELEMENTS]; int i = 0; // generate random numbers and fill them to the list for(i = 0; i < MAX_ELEMENTS; i++ ){ list[i] = rand(); } printf("The list before sorting is:\n"); printlist(list,MAX_ELEMENTS); // sort the list selection_sort(list,MAX_ELEMENTS); // print the result printf("The list after sorting:\n"); printlist(list,MAX_ELEMENT; } Output: Enter the no. of elements: 54176 Array before sorting: 54176 After after sorting: 14567

66

Program 16: WAP to sort a given list using quick sort

#include <iostream.h> #include <stdlib.h> void quicksort(int* array, int left, int right) { if(left >= right) return; int index = partition(array, left, right); quicksort(array, left, index - 1); quicksort(array, index + 1, right); } int partition(int* array, int left, int right) {

findMedianOfMedians(array, left, right); int pivotIndex = left, pivotValue = array[pivotIndex], index = left, i;

swap(array, pivotIndex, right); for(i = left; i < right; i++) { if(array[i] < pivotValue) { swap(array, i, index); index += 1; } } swap(array, right, index);

return index; }

int findMedianOfMedians(int* array, int left, int right) { if(left == right)

67

return array[left];

int i, shift = 1; while(shift <= (right - left)) { for(i = left; i <= right; i+=shift*5) { int endIndex = (i + shift*5 - 1 < right) ? i + shift*5 - 1 : right; int medianIndex = findMedianIndex(array, i, endIndex, shift); swap(array, i, medianIndex); } shift *= 5; }

return array[left]; }

//Find the index of the Median of the elements //of array that occur at every "shift" positions. int findMedianIndex(int* array, int left, int right, int shift) { int i, groups = (right - left)/shift + 1, k = left + groups/2*shift; for(i = left; i <= k; i+= shift) { int minIndex = i, minValue = array[minIndex], j; for(j = i; j <= right; j+=shift) if(array[j] < minValue) { minIndex = j; minValue = array[minIndex]; } swap(array, i, minIndex); }

return k; }

68

//Swap integer values by array indexes void swap(int * array, int a, int b) { int tmp = array[a]; array[a] = array[b]; array[b] = tmp; }

Output: Enter size of array: 5 Enter 5 elements: 4 3 6 1 8 Sorted elements: 1 3 4 6 8

69

Program 17: WAP to sort a given list using merge sort #include<iostream.h> #include<conio.h> Void mergesort(int *,int); Void main() { Int x[max],n,i,j; Char ans; Clrscr(); Cout<<enter the length of array:; Cin>>n; For(i=0;i<n;i++) { Cout<<enter element:<<i+1<<=; Cin>>x[i]; } Mergesort(x,n); Cout<<\n<<sorted array; For(i=0;i<n;i++) Cout<<\n; Cout<<x[i]; Getch(); } Void mergesort(int x[],int n) { Int sub[max]; Int i,j,k,list1,list2,u1,u2,size=1; While(size<n) { List1=0; K=0; While((list1+size)<n) { 70

List2=list1+size; U1=list2-1; U2=((list2+size-1)<n)?(list2+size-1):(n-1); For(i=list1,j=list2;i<=u1&&j<=u2;k++) If(x[i]<=x[j]) Sub[k]=x[i++]; Else Sub[k]=x[j++]; For(;i<=u1;k++) Sub[k]=x[i++]; For(;j<=u2;k++) Sub[k]=x[j++]; List1=u2+1; } For(i=list1;k<n;i++) Sub[k++]=x[i]; For(i=0;i<n;i++) X[i]=sub[i]; Size*=2; } }

71

Output: No. of elements in first array: 4 No. of elements in second array: 4 First array: 3 2 4 1 Second array: 6 7 5 8 Array after sorting 12345678

72

Program 18: WAP to sort a given list using radix sort #include "stdio.h" #define MAX 100 #define SHOWPASS void print(int *a, int n) { int i; for (i = 0; i < n; i++) printf("%d\t", a[i]); }

void radix_sort(int *a, int n) { int i, b[MAX], m = 0, exp = 1; for (i = 0; i < n; i++) { if (a[i] > m) m = a[i]; }

while (m / exp > 0) { int box[10] = { 0 }; for (i = 0; i < n; i++) box[a[i] / exp % 10]++; for (i = 1; i < 10; i++) box[i] += box[i - 1]; for (i = n - 1; i >= 0; i--) b[--box[a[i] / exp % 10]] = a[i]; for (i = 0; i < n; i++) a[i] = b[i]; exp *= 10;

#ifdef SHOWPASS printf("\n\nPASS : "); print(a, n); #endif

73

} }

int main() { int arr[MAX]; int i, num;

printf("\nEnter total elements (num < %d) : ", MAX); scanf("%d", &num);

printf("\nEnter %d Elements : ", num); for (i = 0; i < num; i++) scanf("%d", &arr[i]);

printf("\nARRAY : "); print(&arr[0], num);

radix_sort(&arr[0], num);

printf("\n\nSORTED : "); print(&arr[0], num);

return 0; }

74

Program 19: WAP to sort a given list using heap sort #include<stdio.h> #include<conio.h> void heapsort(int a[],int n); void heapcreate(int a[],int n); void adjust(int a[],int n); int main() { int a[20],n,temp,i; printf("enter number of elements :"); scanf("%d",&n); printf("enter the elements\n"); for(i=0;i <n;i++) scanf("%d",&a[i]); heapsort(a,n); printf("sorted array is \n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } getch(); return(0); } void heapsort(int a[],int n) { int i,temp; heapcreate(a,n); for(i=n-1;i>0;i--) 75

{ temp=a[0]; a[0]=a[i]; a[i]=temp; adjust(a,i); } //return; } void heapcreate(int a[],int n) { int i,j,k,item; for(k=1;k { item=a[k]; i=k; j=(i-1)/2; while(i>0&&item>a[j]) { a[i]=a[j]; i=j; j=(i-1)/2; } a[i]=item; } //return; } void adjust(int a[],int n) { 76

int i,j,item; j=0; item=a[j]; i=2*j+1; while(i<=n-1) { if(i+1<=n-1) if(a[i] i++ ; if(item { a[j]=a[i]; j=i; i=2*j+1; } else break; } a[j]=item; //return; }

Output: Enter the no. of elements in array 5 Enter the elements 32618 Before sorting 86312 77

After heap sort 12368

78

You might also like