Upload
vikalp-kulshrestha
View
221
Download
0
Embed Size (px)
Citation preview
8/14/2019 data structure file mpct
1/49
Submitted to- submitted by-
Mr. AKSHAYA SAHAY Mr. VIKALPKULSHRESTHA
Lect. Of C.S.E/I.T. deptt. Roll no.- 0903EC061114
8/14/2019 data structure file mpct
2/49
Subject Data Structure
S. N. Title REMARK
Program to implement insertion (PUSH) and deletion(POP) in stacks.
Program to implement insertion and deletion usingpointers in queues.
Traversal in binary trees and insertion and deletion oftrees.
Algorithm and program of converting infix to postfixand prefix expression.
Program to implement dqueues in arrays.
Program to insert and delete nodes from a singlylinked list at
1. Beginning2. End3. specified position
Program for linear hashing.
Algorithm for BFS and DFS.
Write program for following sorting techniques
1. quick sort2. bubble sort3. selection sort
8/14/2019 data structure file mpct
3/49
4. heap sort
Program to implement insertion (PUSH) and
deletion (POP) in stacks
# include
# include
# define MAX 3
int stack [MAX], tos = -1, num = 0;
void push (void);int pop (void);
void disp (void);
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)
8/14/2019 data structure file mpct
4/49
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);
8/14/2019 data structure file mpct
5/49
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
8/14/2019 data structure file mpct
6/49
Program to implement insertion and deletion
in queues
# include
# include
# define MAX 10
void addq (int*, int, int*, int*);
int delq (int*, int*, int*);
void main ( )
{
int arr[MAX];
int front = -1, rear = -1, I;
clrscr ( );
addq (arr, 23, &front, &rear);
addq (arr, 9, &front, &rear);
addq (arr, 11, &front, &rear);addq (arr, -10, &front, &rear);
addq (arr, 25, &front, &rear);
addq (arr, 16, &front, &rear);
addq (arr, 17, &front, &rear);
addq (arr, 22, &front, &rear);
addq (arr, 19, &front, &rear);
addq (arr, 30, &front, &rear);
addq (arr, 32, &front, &rear);
8/14/2019 data structure file mpct
7/49
i =delq (arr, &front, &rear);
printf (\n Item deleted: %d, i);
i=delq (arr, &front, &rear);
printf (\n Item deleted: %d, i);
i=delq (arr, &front, &rear);
printf (\n Item deleted: %d, i);
getch( );
}
/* adds an element to the queue */
void addq (int *arr, int item, int *pfront, int *prear)
{
if (*prear = = MAX-1)
{
printf (\n Queue is full.);
return;
}
(*prear)+ +;
arr[*prear] =item;
if (*pfront = = -1)
*pfront = 0;
}
/* removes an element from the queue */
int delq (int *arr, int *pfront, int *prear)
8/14/2019 data structure file mpct
8/49
{
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;
}
Traversal in binary trees and insertion and
deletion of trees
Preorder traversal
Initially push NULL onto STACK andthen set PTR: = ROOT. Then repeat the following steps until PTR= NULL or, equivalently, while PTR=! NULL.
8/14/2019 data structure file mpct
9/49
a. Proceed down the left-most path rooted at PTR, processingeach node N on the path and pushing each right child R(N), if any, onto STACK. The traversing ends after a node Nwith no left child L (N) is processed. (Thus PTR is updatedusing the assignment PTR: = LEFT [PTR], and thetraversing stops when LEFT [PTR] = NULL.)
b. [Backtracking.] Pop and assign to PTR the top element onSTACK. If PTR =! NULL, then return to step (a); otherwiseExit.
Inorder traversal
Initially push NULL ontoSTACK 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 withno left child is pushed onto STACK.
b. [Backtracking.] Pop and process the nodes on STACK. IfNULL 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).
Post order traversal
Initially push NULL ontoSTACK and then set PTR: = ROOT. Then repeat the followingsteps until NULL is popped from STACK.
a. Proceed down the left-most path rooted at PTR. At eachnode N of the path, push N onto STACK and, if N has aright child R(N), push R(N) onto STACK.
b. [Backtracking.] Pop and process positive nodes on STACK.If NULL is popped, then Exit. If a negative node is popped,that is, PTR= -N for some node N, set PTR= N and return tostep (a).
Insertion in trees
8/14/2019 data structure file mpct
10/49
INSBT (INFO, LEFT, RIGHT, ROOT,
AVAIL, ITEM, LOC)
A binary search tree T is in memory and an ITEM of information is
given. This algorithm finds the location LOC of ITEM as a new node
in T at location LOC.1) Call FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR).2) If LOC=! NULL, then Exit.3) [Copy ITEM into new node in AVAIL list.]
If AVAIL = NULL, then: Write: OVERFLOW, and Exit.
Set NEW: = AVAIL, AVAIL: = LEFT [AVAIL] and INFO [NEW]:= ITEM.
Set LOC: = NEW, LEFT [NEW]: = NULL and RIGHT [NEW]:=NULL.
4) [Add ITEM to tree.]
If PAR = NULL, then:
Set ROOT: = NEW
Else if ITEM < INFO [PAR], then:
Set LEFT [PAR]: = NEW.
Else:
Set RIGHT [PAR]: = NEW.
[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 isgiven. 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.
8/14/2019 data structure file mpct
11/49
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
Algorithm for infix to postfix
Q-giving infix expression.P-To be generated postfix expression.1. Add a ( to the left and ) to the right of the given infix
Expression q.2. Scan q element by element from left to right on repeat the
Step 3,4,5,6 till stack is not empty.3. If and operand push is found then push add to p
[And of it block]
4. If ( is encountered then push it in to stack.[End of block]5. If an operator op is encountered then.
a. pop all the operator from the stack and add them to p whichhave same precedence
then the encountered operator till) is found .b. add the operator to the stack .[end of the block ]
6. if a) is encountered then.a. pop all the operator from top of the stack and add them to p
( is encountered.b. remove the ( from the stack
[it is not to be added any where][end of if block]
8/14/2019 data structure file mpct
12/49
7. Display expression P as the result.8. Return.
Program
#include
#include
#include
#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);
8/14/2019 data structure file mpct
13/49
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();
switch(cs) /*Using Switch Case*/
{
case 1:
8/14/2019 data structure file mpct
14/49
intopre(str,pre);
break;
case 2:
intopost(str,post);
break;
case 3:
break;
default:
printf("
Enter a Valid Choise!"); /*Default Case*/break;
}
printf("
Do you wish to Continue?(y/n)");
ans=getche();
}while(ans=='y'||ans=='Y'); /*Condition for Do-while loop*/
getch();
}
/**************************************************/
/*To Input String*/
/**************************************************/
void input(char str)
{
printf("Enter the Infix String:");
scanf("%s",str);
}
8/14/2019 data structure file mpct
15/49
8/14/2019 data structure file mpct
16/49
if(flag==0)
{
elem=pop(&s1);
printf("%c",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'
8/14/2019 data structure file mpct
17/49
/**************************************************/
/*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!");
8/14/2019 data structure file mpct
18/49
}
/******************************************/
/*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)
8/14/2019 data structure file mpct
19/49
{
if(s2->top==-1) /*Condition For Empty*/
return(true);
return(false);
}
Write program for following sorting techniques.
Bubble sort: -#include
Void main(){Int a [100],n,I;Printf(how many element any array);Scanf(%d,&n);
For(i=0;i
8/14/2019 data structure file mpct
20/49
}}
}}
Selection sort:-
#include#includeVoid main(){Int a[100],n,I;Printf(how many elements);Scanf(%d,&a[i]);For(i=0;i
8/14/2019 data structure file mpct
21/49
If(loc!=i);{Temp=a[i];
A[i]=a[loc];A[loc]=temp;
}}
}
Quick sort:-
#include#include#define max 100Int 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;ia[low])
8/14/2019 data structure file mpct
22/49
{Low++;}While(key
8/14/2019 data structure file mpct
23/49
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=1;i--)
{
temp = x[i];
x[i] = x[0];
x[0] = temp;
heap(x,i-1);
}
printf("
Resultant Array
");
for(i=0;i
8/14/2019 data structure file mpct
24/49
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)
8/14/2019 data structure file mpct
25/49
Algorithm for BFS and DFS.
1. Breadth-first search algorithm
Our breadth-first search algorithm will use a queue ofIntegers to store the most recently visited
vertices and use the head of this queue to dictate how to explore the graph further.
static public int BFS(Graph G, int v, int[] LevelOrder)
{
int n = G.order();
for (int i=0; i
8/14/2019 data structure file mpct
26/49
if ( LevelOrder[u] == 0 ) // i.e., not visited yet
{
LevelOrder[u] = ++cnt;
toGrow.enque(new Integer(u));
}
}
}
return cnt;
}
#include
#define MAX10
class DFS
{ private:intn; intadj[MAX][MAX]; intvisited[MAX];
public :voiddfs(int); voidreadmatrix();
};
voidDFS :: readmatrix()
{ inti,j;cout n;cout
8/14/2019 data structure file mpct
27/49
{inti;visited[source]=1;cout
8/14/2019 data structure file mpct
28/49
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;
8/14/2019 data structure file mpct
29/49
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
8/14/2019 data structure file mpct
30/49
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){
8/14/2019 data structure file mpct
31/49
Temp=n->next;
Free(n);
N=temp;
}
}
Program to implement dqeues in arrays.
#include#include
#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;
8/14/2019 data structure file mpct
32/49
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);
8/14/2019 data structure file mpct
33/49
I=delqatbeg(arr,&front,&rear);
Printf(item extracted %d,i);
Print(\n lement in the deque after deletion)
Display(arr);
Addqatend(arr,16,&front,&rear);
Addqatend(arr,7,&front,&rear);
Print(\n lement in the deque after addition)
Display(arr);
I=delqatbeg(arr,&front,&rear);
Printf(item extracted %d,i);
I=delqatbeg(arr,&front,&rear);
Printf(item extracted %d,i);
Print(\n lement in the deque after deletion)
Display(arr);
N=count(arr);
Printf(\n total number of element in deqeue: %d,n);
Getch();
}
8/14/2019 data structure file mpct
34/49
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
8/14/2019 data structure file mpct
35/49
{
(*prear)--;
Arr[*pfront]=item;
}
}
Void addq(int*arr,int item,int*pfront,int*pear)
{
Int I,k;{
Printf(\n dqeue is full);
Return;
}
If(*pfront==-1)
{
*prear=*pfront=0;
Arr[*prear]=item;
Return;
}
I(*rear==max-1)
{
K=*pfront-1for(i=*pfront-1;i
8/14/2019 data structure file mpct
36/49
(*prear)--;
(*pfront--;
}
(*prear)++;
Arr[*prear]=item;
}
int addqatbeg(int*arr, int,int *pfront,int *prear)
{
Int item;If (*pfront==-1)
{
Printf(\n qeue is empty);
Return 0;
}
item =arr[*pfront]
Arr[*pfront]=0;
If(*pfront==*prear)]
*pfront=*prear=-1;
Else
(*pfront)++;
Return item;
}
int addqatbeg(int*arr, int,int *pfront,int *prear)
{
8/14/2019 data structure file mpct
37/49
Int item;
If (*pfront==-1)
{
Printf(\n qeue is empty);
Return 0;
}
item =arr[*pfront]
Arr[*pfront]=0;
(prear)--;If (*pfront==-1)
(*pfront)=-1;
Return item;
}
Void display(int&arr
{
Int j;
Printf(\n front->);
For(i=0;i
8/14/2019 data structure file mpct
38/49
For(i=0;i info=item
3.Set ptr->nxt=start
4.set start=ptr
8/14/2019 data structure file mpct
39/49
The c code for above algorithms:-
Void insertat begine(int item)
{
Node*p;
P=(node*)malloce(sizeof(node))
p->info=item;
if(start== null);
p->next=null;
elsep-.next*p= start;
start=*p;
}
Inserting a node at the end:-
Algorithm:-
8/14/2019 data structure file mpct
40/49
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
The c code for above algorithms:-
Void insert_at _end(int item)
{
Node*p,*loc;P=(node*)malloce(sizeof(node))
p->info=item;
if(start== null);
p->next=null;
if(start== null)
{start=p:}
Else
8/14/2019 data structure file mpct
41/49
{
Loc=start;
While(loc-> next!=null)
{oc=loc->next=null}
Loc->next=p
}
}
8/14/2019 data structure file mpct
42/49
Inserting a node at the specified position:-
Algorithm:-
1.[cheque overflow ?]
If ptr =null,thenPrint , 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->next9.set temp->next=p;
8/14/2019 data structure file mpct
43/49
The c code for above algorithms:-
Void insert_spe(int item,int loc)
{
Node*p,*temp;
Int k;
For(k=o,temp=start;knext;
If(tep==null);
{
Printf(node in the list at less then one\n);
Return;
}
}
P=(node*)mallo(sizeof(node));
p->info=item;
p->next=loc->next;;
loc->next=p;
}
deletiona node at the Beginning:-
8/14/2019 data structure file mpct
44/49
Algorithm:-
1.[check for under flow ?]If start =null ,then
Printlined list empty
ExitEnd if2.set ptr=start3.set start=start->next4.print element deleted is,prs->next5.free (ptr)
The c code for above algorithms:-
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);
}
}
8/14/2019 data structure file mpct
45/49
deletiona node at the end:-
Algorithm:-
1.[check for under flow ?]If start =null ,then
Printlined list emptyExit
End if2.if start-.next=null,then;Set ptr=startSet start =nullPrint element deleted is =ptr->infoFree (ptrEnd if3. Set ptr=start4.repeat steps 5 and 6 will ptr ->next !=null5.set loc=ptr6.set ptr=ptr ->next7.set loc->next=null8.free(ptr);
The c code for above algorithms:-
Void dele_end()
{
Node*p,*loc;
(return:)
Else if (start->next= =null)
{
P=stat
Start=nullPrintf(element deleted is =%d,p->num
Free(p);
8/14/2019 data structure file mpct
46/49
8/14/2019 data structure file mpct
47/49
Deletion node at the specified position:-
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 inext=ptr->new
9.free (ptr).
8/14/2019 data structure file mpct
48/49
The c code for above algorithms:-
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);
}
}
8/14/2019 data structure file mpct
49/49