Data Structure File MPCT
Data Structure File MPCT
S. N. Title REMARK
void main ( )
{
clrscr( );
for ( ; ; )
{
printf (“stack of integers demo :”);
printf (“Menu Press …\n”);
printf (“1. push\n 2. pop\n 3. Display\n 4. Exit\n”);
scanf (“%d” , &num);
switch (num)
{
case 1:
push ( );
break;
case 2:
if (tos= = -1)
printf (“stack is empty. \n”);
else
printf (“removed %d from position %d\n”, pop( ), tos+1);;
break;
case 3:
disp( );
break;
case 4:
exit (0);
break;
default:
printf (“invalid option entered. \n”);
break;
} //switch
} //for
} //main
void push (void)
{
if (tos == MAX-1)
{
printf (“you have entered the top of the stack.”);
printf (“can not add any more elements. \n”);
}
else
{
printf (“add to stack. Enter an integer to add to stack :”);
scanf (“%d”, &num);
tos ++;
stack [tos] =num;
printf (“%d added to position %d in the stack \n”, num, tos+1);
}
}
int pop (void)
{
int j;
j =(tos = =-1)?0: stack [tos];
if ( tos != -1)
{
stack[tos] =0;
tos- -;
}
return j;
}
void disp (void)
{
int i ;
if (tos >=0)
{
for (i =0; i <=tos; i + +)
printf (“%d in position in the array \n”, stack[i], i);
}
else
printf (“stack is empty \n Nothing to display! \n”);
}
# define MAX 10
void main ( )
{
int arr[MAX];
int front = -1, rear = -1, I;
clrscr ( );
getch( );
}
if (*pfront = = -1)
*pfront = 0;
}
/* removes an element from the queue */
int delq (int *arr, int *pfront, int *prear)
{
int data;
if (*pfront = = -1)
{
printf (“\n Queue is empty.”);
return NULL;
}
data = arr[*pfront];
arr[*pfront] = 0;
if (*pfront = = *prear)
*pfront = *prear = -1;
else
(*pfront) + +;
return data;
}
Inorder traversal
Initially push NULL onto STACK and then set PTR:
= ROOT. Then repeat the following steps until NULL is popped from STACK.
a. Proceed down the left-most path rooted at PTR, pushing each node N
onto STACK and stopping when a node N with no left child is pushed onto
STACK.
b. [Backtracking.] Pop and process the nodes on STACK. If NULL is popped,
then Exit. If a node N with a right child R (N) is processed, set PTR =R (N)
(by assigning PTR: = RIGHT [PTR]) and return to step (a).
Insertion in trees
INSBT (INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM, LOC)
Else:
[End of if structure.]
5) Exit.
Deletion in trees
DEL (INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM)
A binary search tree T is in memory and an ITEM of information is given. This
algorithm deletes ITEM from the tree.
1) [finds the loations os ITEM and its parent]
Call FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2) [ITEM in tree?]
If LOC = NULL, then: write: ITEM not in tree, and Exit.
3) [Delete node containing ITEM.]
If RIGHT [LOC] =! NULL and LEFT [LOC] =! NULL, then:
Call CASEB (INFO, LEFT, RIGHT, ROOT, LOC, PAR).
Else:
Call CASEA (INFO, LEFT, RIGHT, ROOT, LOC, PAR).
[End of if structure.]
4) [Return deleted node to the AVAIL list.]
Set LEFT [LOC]: = AVAIL and AVAIL: = LOC.
5) Exit.
Algorithm and program of converting infix to postfix and
prefix expression
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 15
#define true 1
#define false 0
/*Structure Declaration*/
typedef struct
{
char data[MAX];
char top;
}STK;
/*Function Declarations*/
void input(char str[]);
void intopre(char str1[],char pre[]);
void intopost(char str1[],char post[]);
int isoperand(char sym);
int prcd(char sym);
void push(STK *s1,char elem);
int pop(STK *s1);
int empty(STK *s2);
int full(STK *s2);
void dis(char str[]);
void main()
{
STK s;
int cs,ans;
char str[MAX],pre[MAX],post[MAX];
clrscr();
do /*Using Do-while Loop*/
{
clrscr( );
printf("
-----Program for Expressions-----");
printf("
printf("Input The String:");
MENU:
");
printf("1.Infix to Prefix
");
printf("2.Infix to Postfix");
printf("
3.Exit");
cs=getche();
getch();
}
/**************************************************/
/*To Input String*/
/**************************************************/
void input(char str)
{
printf("Enter the Infix String:");
scanf("%s",str);
}
/**************************************************/
/*To Covert Infix To Prefix*/
/**************************************************/
void intopre(STK s1,char str1[],char pre[])
{
int len,flag;
len=strlen(str1);
int check=0,cnt=len-1,pos=0;
char elem;
}
}
/**************************************************/
/*To Convert Infix To Postfix*/
/**************************************************/
void intopost(STK s1,char str1[],char post[])
{
int len;
len=strlen(str1);
int check=0,cnt=len-1,pos=0;
}
/**************************************************/
/*To Check For Operand*/
/**************************************************/
int isoperand(char sym)
{
if('A'<sym<'Z'||'a'<sym<'z')
return(true);
return(false);
}
/**************************************************/
/*To Check The Precedence*/
/**************************************************/
int prcd(char sym)
{
/**************************************************/
/*To Display String*/
/**************************************************/
void dis(char str[])
{
}
/******************************************/
/*Push Function Definition*/
/******************************************/
void push(STK *s1,char elem)
{
if(!full(s1))
{
s1->top++; /*Incrementing top*/
s1->data[s1->top]=elem; /*Storing element*/
}
else
printf("
Stack is Full!");
}
/******************************************/
/*Full Function Definition*/
/******************************************/
int full(STK *s2)
{
if(s2->top==MAX) /*Condition for Full*/
return(true);
return(false);
}
/******************************************/
/*Pop Function Definition*/
/******************************************/
int pop(STK *s1)
{
char elem;
if(!empty(s1))
{
elem=s1->data[s1->top]; /*Storing top stack element in elem*/
s1->top--; /*Decrementing top*/
return(elem);
}
return(false);
}
/******************************************/
/*Empty Function Definition*/
/******************************************/
int empty(STK *s2)
{
if(s2->top==-1) /*Condition For Empty*/
return(true);
return(false);
}
#include<stdio.h>
Void main()
{
Int a [100],n,I;
Printf(“how many element any array”);
Scanf(“%d”,&n);
For(i=0;i<=0;i++);
{
Scanf(“%d”,& a[i]);
}
Bubble-short(a,n);
Printf(“shorted line is as follows”)
For(i=0;i<n;i++);
Printf(“%d”,a[i]);
}
Void bubble-short(int a[],int n)
{
Int temp ,I, j;
For(i=0;i<n;i++);
{
For(j=0;j<n-i-1;j++)
{
If(a[j]>a[j+1])
{
Temp=a[j];
A[j]a[j+1];
A[j]a[j+1];
=temp;
}
}
}
}
Selection sort:-
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a[100],n,I;
Printf(“how many elements”);
Scanf(“%d”,&a[i]);
For(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
Selection-sort(a,n);
For(i=0;i<n;i++)
Printf(“soted array in”);
For(i=0;i<=n;i++)
{
Printf(“%d”,a[i]);
}
}
Void selection-sort (int a[],int n)
{
Int min, loc,temp,I,j;
Min =a[0];
For(i=0;i<=n-1;i++)
{
Min=a[i];
Loc=I;
For(j=i+1;i<=n-1;i++)
{
If(a[j]<min)
{
Min=a[j];
Loc=j;
}
}
If(loc!=i);
{
Temp=a[i];
A[i]=a[loc];
A[loc]=temp;
}
}
}
Quick sort:-
#include<stdio.h>
#include<conio.h>
#define max 100
Int a[max],n,I,l,h;
Void main()
{
Void input(void);
Input();
Getch();
}
Void input(void)
{
Void quick_sort(inta[]),int l,int h);
Void output(int a[],int n);
Printf(“how many element in the array”);
Scanf(“%d”,&n);
For(i=0;i<=n-1;i++)
{
Scanf(“%d”,&a[i]);
}
L=0
H=n-1;
Quck_sort(a,l,h);
Printf(“sorted array:\n”);
Output(a,n);
}
Void quick_sort(int a[],intl,inth)
{
Int temp,key,low,high;
Low=l;
High=h;
Key=a[(low+high)/2];
Do
{
While(key>a[low])
{
Low++;
}
While(key<a[high])
{
High--;
}
If(low<=high)
{
Temp=a[low];
A[low++]=a[high];
A[high--]=temp;
}
}
While(low<=high);
If(l<=high)
Quick_sort(a,l,high);
If(low<h)
Quick_sort(a,low,h);
}
Void output(int a[],int n)
{
For(j=i+1;i<=n-1;i++)
{
Printf(“%d\n”,a[i]);
}
}
Heap sort:-
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int *x,i,n;
int temp;
void heap(int *,int);
clrscr();
fflush(stdin);
printf("
Heap Sort
");
printf("
Enter How many Numbers : ");
scanf("%d",&n);
x = (int *)malloc(n * sizeof(int));
for(i=0;i<n;i++)
{fflush(stdin);
scanf("%d",&x[i]);
}
heap(x,n);
for(i=n-1;i>=1;i--)
{
temp = x[i];
x[i] = x[0];
x[0] = temp;
heap(x,i-1);
}
printf("
Resultant Array
");
for(i=0;i<n;i++)
printf("%d ",x[i]);
free(x);
getch();
}
void heap(int *a,int n)
{
int i,temp;
for(i=n/2;i>=0;i--)
{
if(a[(2*i)+1] < a[(2*i)+2] && (2*i+1)<=n && (2*i+2)<=n)
{
temp = a[(2*i)+1];
a[(2*i)+1] = a[(2*i)+2];
a[(2*i)+2] = temp;
}
if(a[(2*i)+1] > a[i] && (2*i+1)<=n && i<=n)
{
temp = a[(2*i)+1];
a[(2*i)+1] = a[i];
a[i] = temp;
}
}
}
#define MAX 10
class DFS
{
private : int n;
int adj[MAX][MAX];
int visited[MAX];
public : void dfs(int);
void readmatrix();
};
int main()
{
int source;
DFS depth;
depth.readmatrix();
cout << "\nEnter the Source : ";
cin >> source;
cout << "\nThe nodes visited in the DFS order is : ";
depth.dfs(source);
return 0;
}
#include<stdio.h>
#inclde<conio.h>
#include<alloc.h>
#definr true 1
#define false 0
#define max 8
Struct node
{
Int data;
Struct node *next;
};
Int visited[max];
Void dfs(int,truct node**);
Struct node*getnode_write(int);
Void del(strct node*);
Void main()
Sruct node arr[max];
Struct node *v1,*v2,*v3,*v4;
Int I;
Clrscr();
V1=getnode_write(2)
Arr[0]=v1;
V1->next=v2=getnode_write(8);
V2->next=null;
V1=getnode_write(1)
Arr[1]=v1;
V1->next=v2=getnode_write(3);
V2->next=v3=getnode_write(8);
V3->next=null;
V1=getnode_write(1)
Arr[2]=v1;
V1->next=v2=getnode_write(4);
V2->next=v3=getnode_write(8);
V3->next=null;
V1=getnode_write(1)
Arr[3]=v1;
V1->next=v2=getnode_write(7);
V2->next=null;
V1=getnode_write(6)
Arr[4]=v1;
V1->next=v2=getnode_write(7);
V2->next=null;
V1=getnode_write(5)
Arr[5]=v1;
V1->next=null;
V1=getnode_write(4)
Arr[6]=v1;
V1->next=v2=getnode_write(5);
V2->next=v3=getnode_write(8);
V3->next=null;
V1=getnode_write(1)
Arr[7]=v1;
V1->next=v2=getnode_write(2);
V2->next=v3=getnode_write(3);
V3->next=v3=getnode_write(7);
V4->next=null;
Dfs(1,arr);
For(i=0;i<max;i++)
Del(arr[i]);
Getch();
}
Void dfs(int v ,struct node**p)
{
Struct node *q;
Visited[v-1]=true;
Printf(“%d\t”,v);
Q=*(p+v-1);
While(q!=null)
{
If(visited->data-1]==false)
Dfs(q->data,p);
Else q=q->next;
}
Struct node*getnode_write(int val)
{
Struct node *newnode;
Newnode=(struct node*) malloc(sizeof(struct node));
Newnode->data=val;
Return newnode;
}
Void del(struct node*n)
{
Struct node*temp;
While(n!=null){
Temp=n->next;
Free(n);
N=temp;
}
}
#define max 10
Void addatqatbeg(int*,int,int*,int*);
Void addatqatend(int*,int,int*,int*);
Void addatqatbeg(int*,int*,int*);
Void addatqatbend(int*,int*,int*);
Void display(int*);
Int count(int*);
Void main();
{
Int arr[max];
Int front,rear,I,n;
Clrscr();
Front=rear=-1;
For(i=0;max;i++)
Arr[i]=0;
Addq(arr,17,&front,&rear);
Addq(arr,10,&front,&rear);
Addq(arr,8,&front,&rear);
Addq(arr,-9,&front,&rear);
Addq(arr,13,&front,&rear);
Addq(arr,28,&front,&rear);
Addq(arr,14,&front,&rear);
Addq(arr,5,&front,&rear);
Addq(arr,25,&front,&rear);
Addq(arr,6,&front,&rear);
Addq(arr,21,&front,&rear);
Addq(arr,11,&front,&rear);
Printf(“\nleelment in deque” );
Display (arr);
N=count(arr);
Printf(“\n total number of element indeque,%d”,n);
I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);
I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);
I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);
I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);
Addqatend(arr,16,&front,&rear);
Addqatend(arr,7,&front,&rear);
I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);
I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);
N=count(arr);
Printf(“\n total number of element in deqeue: %d”,n);
Getch();
}
Void addqatbeg(int*arr,int ietm,int,int *pfront,int *prear)
{
Int I,,k,c;
If(*pfront==0&&*prear==max-1)
{
Printf(“\ndeque is full.\n”);
Return;
}
If(pfront==-1)
{
*pfront=*prear=0;
Arr[*pfront]=item;
Return;
}
If(*prear!=max-1)
{
C=count(arr);
K=*prear+1;
For(i=1;i<=c;i++)
{
Arr[k]=arr[k-1]
;
k--;
}
Arr[k]=item;
*pfront=k;
(prear)++;
}
Else
{
(*prear)--;
Arr[*pfront]=item;
}
If(*pfront==-1)
{
*prear=*pfront=0;
Arr[*prear]=item;
Return;
}
I(*rear==max-1)
{
K=*pfront-1for(i=*pfront-1;i<*prear;i++)
{
K=I;
If(k==max-1)
Arr[k]=0;
}
(*prear)--;
(*pfront--;
}
(*prear)++;
Arr[*prear]=item;
}
If(*pfront==*prear)]
*pfront=*prear=-1;
Else
(*pfront)++;
Return item;
}
(prear)--;
If (*pfront==-1)
(*pfront)=-1;
Return item;
}
Void display(int&arr
{
Int j;
Printf(“\n front->”);
For(i=0;i<max;i++)
Printf(“\t%d”arr[j]);
Printf(“<-rear”);
}
Int count(int*arr)
{
Int c=0;
For(i=0;i<max;i++)
{
If (Arr[i]!=0)
C++;
}
Return c;
}
Algorithm:-
1.[cheque overflow ?]
If ptr =null,then
Print , overflow
Exit
Else
Ptr =(node*)malloc(size of node)).
End it
2. set ptr_> info=item
3.Set ptr->nxt=start
4.set start=ptr
Algorithm:-
1.[cheque overflow ?]
If ptr =null,then
Print , overflow
Exit
Else
Ptr =(node*)malloc(size of node)).
End it
2. set ptr_> info=item
3.Set ptr->next=null
4.if start =null and if then set start=p;
5.set loc=start
6.repeat step 7 untill loc-next!=null
7.set loc=loc-next
8.set loc-next=p
Algorithm:-
1.[cheque overflow ?]
If ptr =null,then
Print , overflow
Exit
Else
Ptr =(node*)malloc(size of node)).
End if
2. set ptr_> info=item
3.Set ptr->next=null then
Set start =p
Setp-> next=null
End if
4.inilisize the counter (1) and pointers
(node* temp)
Set i=o
Set temp=start
5.repeat step6 and 7 untill 1< next
7.stepi=i+1
8.set p->next=temp->next
9.set temp->next=p;
Algorithm:-
Voiddele_beg(void)
{
Node*p;
If(start= =null);
(Return);
Else
{
P=start;
Start=start->next;
Printf(“element deleted is=y.d”)p->num;
Free(p);
}
Algorithm:-
Void dele_end()
{
Node*p,*loc;
(return:)
Else if (start->next= =null)
{
P=stat
Start=null
Printf(“element deleted is =%d”,p->num
Free(p);
}
Else
{
Loc=start;
while(P=start->next=null)
{
Loc=start->next;
}
Loc->next=null;
Free(p);
}
}
Algorithm:-
1.[check for under flow?]
If ptr==null ,then
Print “underflow”
Exit
2.[initialize the countr , 1 and pointer]
Node *temp,node*ptr;
Set i=0
Set*ptr=start
3.repeat step 4 to9 untill i<=loc
4.set temp=ptr
5.set ptr=ptr-.next
6.set i=i+1
7.print”element deleted is”,
pts=tyle8.set temp->next=ptr->new
9.free (ptr).
Dele_spe()
{
Node8p,*temp;
Int I,loc;
Printf(“enter the position to delet\n”);
Scnf(“y.d”,& loc);
If(start==num)
{
Printf(“empty list”)
}
Else
{
Ptr= start;
For(i=1;I,=loc;i++)
{
Temp=ptr->next;
}
Printf(“no deleted is=%d”,pts->num)
Temp->next=ptr->next;
Free(ptr);
}
}