DS Part 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Queue using Quick sort stack using linked

Merge sort array technique list


1]#include<stdio.h> 2]#include<stdio.h> printf("elements in queue i=first; int choice=0;
void merge_sort(int i,int #define max 6 are\n"); j=last; clrscr();
j,int a[],int aux[]){
int queue[max]; while(i<=rear){ while(i<j){ while(choice!=4){
int mid,leftp,rightp,k;
if(j<=i){ int front=-1; printf("%d\n",queue[i]); while(a[i]<=a[ptr]&&i<last) printf("\n\n choose one from the
return;} int rear=-1; i=(i+1)%max;}}} i++; below option...\n");
mid=(i+j)/2; void enqueue(int element) int main(){ while(a[j]>a[ptr]) printf("\n1.Push\n2.Pop\n3.Sho
merge_sort(i,mid,a,aux); { int ch,x; j--; w\
merge_sort(mid+1,j,a,aux if(front==-1&rear==-1){ clrscr(); if(i<j){ n4.Exit");
);
front=0; while(1){ temp=a[i]; printf("\n Enter your choice:");
leftp=i;
rightp=mid+1; rear=0; printf("\n1.insert"); a[i]=a[j]; scanf("%d",&choice);
for(k=i;k<=j;k++){ queue[rear]=element;} printf("\n2.delete"); a[j]=temp;}} switch(choice){
if(leftp==mid+1){ else printf("\n3.display"); temp=a[ptr]; case 1:push();
aux[k]=a[rightp]; if((rear+1)%max==front){ printf("\n4.exit"); a[ptr]=a[j]; break;
rightp++;} printf("queue is full\n");} printf("\nenter your a[j]=temp; case 2:pop();
else if(rightp==j+1){
else{ choice:"); quicksort(a,first,j-1); break;
aux[k]=a[leftp];
leftp++;} rear=(rear+1)%max; scanf("%d",&ch); quicksort(a,j+1,last);}} case 3:display();
else if(a[leftp]<a[rightp]){ queue[rear]=element;}} switch(ch){ void main(){ break;
aux[k]=a[leftp]; void dequeue(){ case 1:printf("enter int i,n,a[25]; case 4:break;
leftp++;} if((front==-1)&&(rear==- elements to be inserted:"); clrscr(); default:printf("Please enter a
else{ 1)){ scanf("%d",&x); printf("Enter the size of the valid choice");}}}
aux[k]=a[rightp];
printf("queue is enqueue(x); array:"); void push(){
rightp++;}}
for(k=i;k<=j;k++){ empty\n"); break; scanf("%d",&n); int val;
a[k]=aux[k];}} } case 2:dequeue(); printf("Enter %d struct node*ptr=(struct
int main(){ else if(front==rear){ break; elements:",n); node*)malloc(sizeof(struct
int printf("element %d is case 3:display(); for(i=0;i<n;i++) node));
a[100],aux[100],n,i,d,swa dequeued\n",queue[front]) break; scanf("%d",&a[i]); if(ptr==NULL){
p;
; case 4:exit(0);}}} quicksort(a,0,n-1); printf("Unable to push the
clrscr();
printf("Enter number of front=-1;rear=-1;} printf("Order of Sorted elemen
elements in the array:\n"); else{ elements:"); t");}
scanf("%d",&n); front=(front+1)%max; for(i=0;i<n;i++) else{
printf("Enter %d printf("element %d is printf("%d\n",a[i]); printf("Enter the value:");
integers:\n",n);
3]#include<stdio.h>
dequeued\n",queue[front- #include<conio.h> getch();} scanf("%d",&val);
for(i=0;i<n;i++)
1]);}} void quicksort(int a[25],int if(head==NULL){
scanf("%d",&a[i]); 4]#include<stdio.h>
merge_sort(0,n-1,a,aux); void display(){ first,int last){ #include<stdlib.h> ptr->val=val;
printf("Printing the sorted int i=front; int i,j,ptr,temp; void push(); ptr->next=NULL;
array:\n"); if(front==-1&&rear==- if(first<last){ void pop(); head=ptr;}
for(i=0;i<n;i++) 1){ void display(); else{
printf("%d\n",a[i]);
ptr=first; struct node{
printf("queue is ptr->val=val;
getch(); int val;
empty\n");} ptr->next=head;
return 0;} struct node*next;};
else{ struct node*head; head=ptr;}
void main(){ printf("Item pushed");}}
Queue using void pop(){
Postfix expression
linked list
int item; 5]#include<stdio.h> else{ 6]#include<stdio.h> switch(c) {
struct node*ptr; #include<stdlib.h> printf("\nenter a value:\n") ; #include<conio.h> case'+':ans=b+a;
if(head==NULL){ struct node{ scanf("%d",&item); #include<string.h> break;
printf("Underflow");} int data; ptr->data=item; #define MAX 50 case'-':ans=b-a;
else{ struct node*next;}; if(front==NULL){ int stack[MAX]; break;
item=head->val; struct node*front; front=ptr; char post[MAX]; case'*':ans=b*a;
ptr=head; struct node*rear; rear=ptr; int top=-1; break;
head=head->next; void insert(); front->next=NULL; void pushstack(int temp); case'/':ans=b/a;
free(ptr); void del(); rear->next=NULL;} void evaluate(char c); break;
printf("Item %d void display(); else{ void main(){ case'^':ans=b^a;
popped",item) void main(){ rear->next=ptr; int i,l; break;
;}} int ch; rear=ptr; clrscr(); default:ans=0;
void display(){ clrscr(); rear->next=NULL;}}} printf("Insert a postfix }
int i; while(1){ void del(){ notation top++;
struct node*ptr; printf("\n1.insert an struct node*ptr; n:"); stack[top]=ans; }
ptr=head; element\n2.delete an if(front==NULL){ gets(post);
if(ptr==NULL){ element\n3.display the printf("\nUNDERFLOW\n" l=strlen(post);
printf("Stack is queue\n4.exit\n"); ); for(i=0;i<l;i++){
empty\n");} printf("\nenter your return;} if(post[i]>='0'&&post[i]<='9')
else{ choice:"); else{ {
printf("Printing Stack scanf("%d",&ch); ptr=front; pushstack(i);}
elements switch(ch){ printf("\n%d is if(post[i]=='+'||post[i]=='-
\n"); case1:insert(); deleted\n",ptr->data); '||post[i]=='*'||post[i]=='/'||post
while(ptr!=NULL){ break; front=front->next; [i
printf("%d\n",ptr- case2:del(); free(ptr);}} ]=='^'){
>val); break; void display(){ evaluate(post[i]);}}
ptr=ptr->next;}}} case3:display(); struct node*ptr; printf("\n\nResult::%d",stack[t
break; ptr=front; o
case4:exit(0); if(front==NULL){ p]);
break; printf("\nempty queue\n");} getch();}
default:printf("\nenter a else{ void pushstack(int temp){
valid choice:");}}} while(ptr!=NULL){ top++;
void insert(){ printf("\n%d\n",ptr->data); stack[top]=(int)(post[temp]-
struct node*ptr; ptr=ptr->next;}}} 48);}
int item; void evaluate(char c){
ptr=(struct int a,b,ans;
node*)malloc(sizeof(struc a=stack[top];
t node)); stack[top]='\0';
if(ptr==NULL){ top--;
printf("\nOVERFLOW\n" b=stack[top];
); stack[top]='\0';
return;} top--;
Queue using Quick sort stack using linked
Merge sort array technique list

7]#include <stdio.h> 8]#include<stdio.h>


#include <stdlib.h> typedef struct tnode{
struct node{ int data; #include <stdlib.h>
int data; struct tnode *right, *left;
struct node*next;}; } TNODE;
struct node*head; TNODE *CreateBST(TNODE *, int);
void beginsert(); void Inorder(TNODE *);
void random_delete(); void Preorder(TNODE *);
void display(); void Postorder(TNODE *);
void main(){ void main(){
int ch; TNODE *root = NULL;
while(1){ int choice=0, elem, n, i;
printf("\n\n******Main Menu*******\n"); clrscr();
printf("\n.Insert in beginning\n2.Delete a given while(choice<=2){
node\n3.Display\n4.Exit\n"); printf("\n\n Binary Search Tree Operations \n");
printf("\nEnter your choice:"); printf("\n 1.Creation of BST");
scanf("\n%d",&ch); printf("\n 2.Inorder, Preorder and Postorder Traversal");
switch(ch){ printf("\n 3.Exit");
case 1: printf("\n Enter Your choice: ");
beginsert(); scanf("%d", &choice);
break; switch (choice){
case 2: case 1:root = NULL;
random_delete(); printf("\n\nTotal number of nodes in BST :");
break; scanf("%d", &n);
case 3: for (i=1;i<=n;i++) {
display(); printf("\nEnter the element %d:", i);
break; scanf("%d", &elem);
case 4: root = CreateBST(root, elem);}
exit(0); printf("\nBST with %d nodes created\n", n);
default: break;
printf("Please enter valid choice..");}}} case 2: printf("\n Inorder Traversal:");
void beginsert(){ Inorder(root);
struct node*ptr; printf("\n Preorder Traversal: ");
int item; Queue using Preorder(root);
ptr=(struct node*)malloc(sizeof(struct node*)); printf("\n Postorder Traversal:"); Postfix expression
if(ptr==NULL){
linked list
(sizeof(structnode*)); Postorder(root);
printf("nOVERFLOW");} break;}}
else{ getch();}
printf("\nEnter value\n"); TNODE *CreateBST(TNODE *root, int elem){
scanf("%d",&item); if (root == NULL){
ptr->data=item; root = (TNODE *) malloc(sizeof(TNODE));
ptr->next=head; root->left = root->right = NULL;
head=ptr; root->data = elem;
printf("\n%d inserted",item);}} return root;}
void random_delete(){ else if (elem < root->data)
struct node*temp; root->left = CreateBST(root->left, elem);
struct node*del; else
int loc,i; root->right = CreateBST(root->right, elem);
printf("\nEnter the position to delete the node:\n"); return (root);}
scanf("%d",&loc); void Inorder(TNODE *root){
temp=head; if (root != NULL){
if(loc==0){ Inorder(root->left);
printf("\nElement deleted is %d\n",temp->data); printf(" %d ", root->data);
head=head->next; Inorder(root->right);}}
temp->next==NULL; void Preorder(TNODE *root){
free(temp);} if (root!= NULL){
else { printf(" %d ", root->data);
for(i=0;i<loc-1;i++){ Preorder(root->left);
temp=temp->next;} Preorder(root->right);}}
del=temp->next; void Postorder(TNODE *root){
temp->next=temp->next->next; if (root != NULL){
printf("\nElement deleted is:%d\n",del->data); Postorder(root->left);
del->next=NULL; Postorder(root->right);
free(del);}} printf(" %d ", root->data);}}
void display(){
struct node*ptr;
ptr=head;
if(ptr==NULL){
printf("No element found!\n");}
else{
printf("Elements are:\n");
while(ptr!=NULL){
printf("%d",ptr->data);
ptr=ptr->next;}}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy