Data Structure File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 65

INSERTION AT THE END OF THE ARRAY

1 If UB=max then write araay is overflow and stop 2 Read data 3 K<UB 4 Repeat step (5) while k>=LB 5 A(K+1)<-A(K) K<-K-1 6 A(LB)<-DATA 7 STOP

INSERTION AT THE GIVEN ELEMENT 1 2 3 4 5 6 IF UB=max then array is overflow and stop Read data Read LOCon location at where insertion will be made K<-UB Repeat step (6) while K>=LOC A[K+1<-A[K] K<-K-1 7 A(LOC)<-DATA 8 STOP

DELETION FROM THE BEGINNING

1 If N=0 then array is underflow and stop 2 x<-LB 3 Repeat step 4 while x<UB 4 A(x)<-A(x+1) X=x+1 5 A(UB)<-NULL UB<-UB-1 6 STOP

DELETION FROM THE END

1 IF N=0 then the array is underflow and stop 2 A[UB]->NULL UB->UB-1 3 STOP

CREATION AND TRAVERSAL OF SINGLE LINKED LIST

void create() { struct node*ptr,*cpt; char ch; ptr=(struct node*)malloc(sizeof(struct node)); printf("\n input the info of first node"); scanf("%d",&ptr->info); first=ptr; do { cpt=(struct node*)malloc(sizeof(struct node)); printf("\n input the info of next node"); scanf("%d",&cpt->info); ptr->link=cpt; ptr=cpt;

printf("press<y/n> for more nodes"); ch=getch(); }while(ch=='y'); ptr->link=NULL; }

void traverse() { struct node*ptr,*cpt; ptr=first; printf("traversing of nodes"); while(ptr!=NULL) { printf("%d\n",ptr->info); ptr=ptr->link; } }

SORTING OF A LINKED LIST

Void sorting() { Int temp; Struct node *ptr,*cpt; Ptr =first; While(ptr->!=NULL) { Cpt=ptr->link; While(cpt!=NULL) { If(ptr->info>cpt->info) { Temp=ptr->info; Ptr->info=cpt->info;

Cpt->info=temp; } Cpt=cpt->link } }; Ptr=ptr->link; }

REVERSE OF A LINKED LIST

Reverse_list() { Struct node *ptr,*cpt,*tpt; Ptr=first Tpt=NULL; While(ptr!=NULL) { Cpt=ptr->link; Ptr->link=tpt; Tpt=ptr; Ptr=cpt; } }

CREATION AND TRAVERSAL OF A POLYNOMIAL

void create() { struct node*ptr,*cpt; char ch; ptr=(struct node*)malloc(sizeof(struct node)); printf("\n enter the first term of expression"); scanf("%d%d",&ptr->coeff,&ptr->expo); first=ptr; do { cpt=(struct node*)malloc(sizeof(struct node)); printf("\n enter the next term of expression"); scanf("%d%d",&cpt->coeff,&cpt->expo); ptr->link=cpt; ptr=cpt;

printf("press<y/n>for more nodes"); ch=getch(); }while(ch=='y'); ptr->link=NULL; } void traverse() { struct node*ptr; printf("\n traversal of terms of expreesion"); printf("\n"); ptr=first; while(ptr!=NULL) { printf("coefficient:%d \n",ptr->coeff); printf("exponent:%d\n",ptr->expo); ptr=ptr->link; } }

CONCATINATION OF LINKED LIST


void concatenation() { Struct node *ptr; Ptr=first While(ptr->link!=null) Ptr=ptr->link Ptr->link=first2; }

INSERTION AND DELTION IN A CIRCULAR LINKED LIST

void insert_beg() { struct node*ptr,*cpt; printf("\n input the info of new node"); scanf("%d",&ptr->info); cpt=first; while(cpt->link!=first) { cpt=cpt->link; } ptr->link=first; first=ptr; cpt->link=first; printf("\n after insertion ");

display() } void delete_end() { struct node*ptr,*cpt; cpt=first; while(cpt->link!=first) { ptr=cpt; cpt=cpt->link; } ptr->link=first; free(cpt); printf("\n after deletion"); display(); }

INSERTION AT THE BEGINNING OF A DOUBLY LINKED LIST

Void insert_at_begining() { Struct node*ptr; Ptr=(struct node*)malloc(sizeof(struct node)) If(ptr==NULL) { Printf(overflow); Return; } Else { Printf(\n enter the new node info); Scanf(%d \n,& ptr->info); { Ptr->lpt=NULL;

Ptr->rpt=first; First->lpt=ptr; First=ptr; } } }

DELETION FROM THE END IN DOUBLY LINKED LIST

Void delete_end() { Struct node *ptr,*cpt; Ptr=first; While(ptr->rpt!=NULL) { Cpt=ptr; Ptr =ptr->rpt; } Cpt->rpt=NULL; Free(ptr); }

INSERTION AT THE BIGINNING IN A DOUBLY CIRCULAR LINKED LIST

Void insert() { Struct node *ptr,*tpt; Ptr=(structnode*)malloc(sizeof(structnode)); Printf(enter first mode); Scanf(%d,7ptr->info); Tpt=first->lpt; Ptr->rpt=first; First->lpt=ptr; Ptr->lpt=tpt; Tpt->rpt=ptr; First=ptr; }

DELETION AT THE BIGINNING IN A DOUBLY CIRCULAR LINKED LIST

Void delete() { Struct node *ptr;*cpt;*tpt; Ptr=((struct node*)malloc(sizeof(struct node)); Tpt=first Ptr=first->rpt; Cpt=first->lpt; Ptr->lpt=cpt; Cpt->rpt=ptr; First=ptr; Tpt->lpt=NULL Tpt->rpt=NULL Free(tpt)

STATIC IMPLEMENTATION OF STACK

PUSH OPERATION 1 initialize set top=-1 2 repeat step(3) until top<maxsize-1 Read item 3 set top=top+1 Set stack[top]=item 4 print stack overflow

POP OPERATION 1 repeat step 2to4 until top>=0 2 set item=stack[top] 3 set top=top-1 4 print,no deleted is,item 5 print stack underflow occurs

STATIC IMPLEMENTATION OF QUEUE

PUSH OPERATION 1 if rear =max-1 then write queue is overflow and stop 2 read data 3 if front=-1 then Front=front+1 Rear=rear+1 Q(rear)=data Else Rear=rear+1 Q(rear)=data 4 stop

POP OPERATION 1 If front =-1 then write queue as underflow and stop. 2 if front=rear then Q(front)<-NULL

Front<- -1 Else Q(front)<-NULL Front=front+1 3 stop

LINKED REPRESENTATION OF STACK

void create() { struct node*ptr,*cpt; char ch; ptr=(struct node*)malloc(sizeof(struct node)); printf("\n enter the info of first node"); scanf("%d",&ptr->info); ptr->link=NULL; do { cpt=(struct node*)malloc(sizeof(struct node)); printf("input the info of next node\n"); scanf("%d",&cpt->info); cpt->link=ptr; ptr=cpt; printf("\n press<y/n> for more nodes");

ch=getch(); }while(ch=='y'); top=ptr;

printf("\n after creation the stack"); traverse(); } void traverse() { struct node*ptr; printf("\n traversing of stack:"); ptr=top; while(ptr!=NULL) { printf("%d\n",ptr->info); ptr=ptr->link; } }

void push() { struct node*ptr; ptr=(struct node*)malloc(sizeof(struct node)); if(ptr==NULL) { printf("\n overflow"); return; } else { printf("\n input the info of next node"); scanf("%d",&ptr->info); ptr->link=top; top=ptr; } printf("\n after push"); traverse();

} void pop() { struct node*ptr; if(top==NULL) { printf("\n underflow"); return; } else { ptr=top; top=ptr->link; free(ptr); } printf("\n after pop"); traverse(); }

LINKED REPRESENTATION OF QUEUE

void main() { int choice; char option; node front,rear; front=rear=NULL; do { clrscr(); printf("1.push\n"); printf("2. pop\n"); printf("3. traverse\n"); printf("\n enter your choice:"); scanf("%d",&choice); switch(choice) {

case 1: rear=push(rear); if(front==NULL) { front=rear; } Break; case 2: front=pop(front,rear); rear=NULL; break; case 3: traverse(front,rear); break; } printf("\n press<Y/y> to continue="); fflush(stdin); scanf("%c",&option);

} while(option=='Y'||option=='y'); } node push(node rear) { node newnode; newnode=(node)malloc(sizeof(struct queue)); printf("\n enter the no.to be pushed"); scanf("%d",&newnode->info); newnode->next=NULL; if(rear!=NULL) rear->next=newnode; rear=newnode; return(rear); } node pop(node f,node r) { if(f==NULL)

printf("queue is empty"); else { printf("\n the popped element is =%d",f->info); if(f!=r) f=f->next; else f=NULL; } return(f); } void traverse(node fr, node re) { if(fr==NULL) printf("\n the queue is empty"); else { printf(" the element are");

while(fr!=re) { printf("%d",fr->info); fr=fr->next; } printf("%d",fr->info); } }

INSERTING AN ELEMENT IN A CIRCULAR QUEUE

1 if (front==0 && rear =max-1//(front=rear+1) then write (overflow in queue and stop) 2 read data to insert 3 if(front== -1) front=0 rear=0 4 if(rear=max-1) then rear =0 else rear =rear+1 5 queue[rear]=data 6 stop

DELETING AN ELEMENT IN A CIRCULAR QUEUE

1 if front= -1 Then writequeue underflow and stop 2 if front =rear then Queue(front)=NULL Front= -1 Rear= -1 3 if front=max-1 Queue (front)=NULL Front =0 Else Q[front]=NULL Front=front+1 4 stop

INSERTION FROM THE FRONT END IN DEQUEUE

Void insert_first() { Int m; If(front==0) { Printf(overflow); Return;} Printf(input element to insert); Scanf(%d,&m); Front --; Dequeue[front]=m; }

DELETION FROM THE REAR END IN DEQUEUE

Void delete_rear() { If(front== -1) { Printf(underflow\n); Return(); } Dequeue[rear]=NULL; Rear }

MERGE SORT

#include<stdio.h> #include<conio.h> #define MAX 20

int array[MAX]; void merge(int low,int mid,int high) { int temp[MAX]; int i=low; int j=mid+1; int k=low; while((i<=mid)&&(j<=high)) { if(array[i]<=array[j]) temp[k++]=array[i++]; else

temp[k++]=array[j++]; } while(i<=mid) temp[k++]=array[i++]; while(j<=high) temp[k++]=array[j++]; for(i=low;i<=high;i++) array[i]=temp[i]; } void merge_sort(int low,int high) { int mid; if(low!=high) { mid=(low+high)/2; merge_sort(low,mid); merge_sort(mid+1,high); merge(low,mid,high);

} } void main() { clrscr(); int i,n; printf("\n enter the no. of elements"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n enter the elements of array %d\n",i); scanf("%d",&array[i]); } printf("\nunsorted list"); for(i=0;i<n;i++) printf("%d",array[i]); merge_sort(0,n-1); printf("\nsorted list is:\n");

for(i=0;i<n;i++) printf("%d",array[i]); getch(); }

BUBBLE SORT

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,j,size,AR[50],temp; printf("\n enter the desired size of array"); scanf("%d",&size); printf("\n enter the elements of array"); for(i=0;i<size;i++) { scanf("%d",&AR[i]); } for(i=0;i<(size-1);i++) { for(j=0;j<(size-1-i);j++)

{ if(AR[j]>AR[j+1]) { temp=AR[j]; AR[j]=AR[j+1]; AR[j+1]=temp; } } } printf("\n the sorted array is as shown below"); for(i=0;i<size;i++) printf("%d\n",AR[i]); getch(); }

SELECTION SORT

#include<stdio.h> #include<conio.h> main() {

int i,j,size,AR[50],temp; printf("\n enter the desired size of array"); scanf("%d",&size); printf("\n enter the elements of array"); for(i=0;i<size;i++) { scanf("%d",&AR[i]); } for(i=0;i<(size-1);i++) { for(j=i+1;j<size;j++)

{ if(AR[i]>AR[j]) { temp=AR[i]; AR[i]=AR[j]; AR[j]=temp; } } } printf("\n the sorted array is as shown below"); for(i=0;i<size;i++) printf("%d\n",AR[i]); getch(); }

INSERTION SORT

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,j,size,AR[50],temp,k,item; printf("\n enter the desired size of array"); scanf("%d",&size); printf("\n enter the elements of array"); for(i=0;i<size;i++) { scanf("%d",&AR[i]); } for(k=1;k<size;k++) { item=AR[k];

for(i=k-1;item<AR[i]&&i>=0;i--) AR[i+1]=AR[i]; AR[i+1]=item; } printf("\n the sorted array is as shown below"); for(i=0;i<size;i++) printf("%d\n",AR[i]); getch(); }

BINARY SEARCH

#include<stdio.h> #include<conio.h> void main() { int i,N,AR[50],item,mid,low,up; clrscr(); printf("\n enter the desired size of array"); scanf("%d",&N); printf("\n enter the elements of array"); for(i=0;i<N;i++) { scanf("%d",&AR[i]); } printf("\n enter the element to be searched "); scanf("%d",&item); low=0,up=N-1;

while(low<=up &&AR[mid]!=item ) { mid=(low+up)/2; if(item>AR[mid]) { low=(mid+1); } else if(item<AR[mid]) { up=(mid-1); } } if(item==AR[mid]) { printf("\n item found at position %d",mid); } else

printf("\n item not found"); getch(); }

LINEAR SEARCH

#include<stdio.h> #include<conio.h> void main() { int i,AR[10],found=0,max,num; clrscr(); printf("\n enter the desired size of array"); scanf("%d",&max); printf("\n enter the elements of array"); for(i=0;i<max;i++) { scanf("%d",&AR[i]); } printf("\n enter the element to be searched "); scanf("%d",&num);

for(i=0;i<max;i++) if(AR[i]==num) { found=1; break; } if(found==1) printf("\n element exist at %d position", i); else printf("\n the element doest not exist in the array"); getch(); }

QUICK SORT

#include<stdio.h> #include<conio.h> Void main() { Int a[20],n,i,low,high,temp,key; Clrscr(); Printf(\nenter the no. of element); Scanf(%d,&n); Printf(\nenter the array elements); For(i=0;i<=n-1;i++) { Scanf(%d,&a*i+); } Low=0; High=n-1; Key=a[low+high]/2;

While(low<=high); { Low=low+1; } While(a[high]>key) { High=high-1; { If(low<=high) { Temp=a[low]; a[low]=a[high]; a[high]=temp; } } While(low>high)

{ Temp=key;

Key=a[high]; A[high]=temp; } Printf(\n sorted array is); For(i=0;i<n;i++) { Printf(%d,a*i+); } getch(); }

COUNT THE NUMBER OF NODES IN A TREE

Void count node(struct rec *tree) { If(tree!=NULL) { IF(tree->left!=NULL} { Node++ Count node(tree->left) } If(tree->right!=NULL) { Node++ Count node(tree->right); } } }

COUNT THE NUMBER OF LEAF NODE IN A TREE

Void count leaves(struct rec *tree) { If(tree!=null) { If(tree->left==NULL)&&(tree->right==NULL)) Total++ Count leaves (tree->left); Count leaves(tree->right); } }

FIND THE DEPTH OF THE TREE

Void depth(struct rec*tree) { If(tree->left!=NULL) { Total 1 ++; { Depth(tree->left); } If(tree->right!=NULL) { Total2++; Depth(tree->right); } } }

EXCHANGING LEFT SUBTREE WITH RIGHT SUB TREE

Void*exchange(struct rec*tree) { If((tree->left->num!=0)&&(tree=>right->num!=0)) { Temp=tree->left; Tree->left=tree->right; Tree->right=temp; printf(%d \n,tree->left->num); printf(%5d \n,tree->right->num); exchange(tree->left); exchange(tree->right); } }

CREATION AND TRAVERSAL OF A BINARY TREE

#include<stdio.h> #include<alloc.h> #include<process.h> #include<stdlib.h> Struct node { Int num; Struct node *left; Struct node *right; }; Typdef struct node node; Node *root=NULL; Node *insert=(struct node *node,int num); Void preoder(node *P); Void inorder(node *P); Void postorder(node *P);

Int count=1; Void main() { Int choice; Int digit; Do { Printf(enter the choice); Scanf(%d,choice); Switch(choice); { Case1: Printf(enter integer:to quit enter 0); Scanf(%d,digit); While(digit!=NULL); { Root=insert(root,digit); Scanf(%d,digit);

Continue; } Case2: Printf(preorder); Preorder(root); Continue; Case3: Printf(inorder); Inorder(root); Continue; Case4: Printf(postorder); Postorder(root); Continue; Case5: Puts(END); Exit(0); }

} While(choice!=5); } Node *insert(node *P,int digit) { If(P==NULL) { P=(node *)malloc(sizeof( node)); P->left=P->right=NULL; p->num=digit; count++; } Else If(count%2==0) p->left=insert(p->left,digit); else p->right=insert(p->right,digit); return(P);

} Void preorder(node *P) { If(P!=NULL) { Printf(%d,P->num); Preorder(,P->left); Preorder(P->right); } } Void inorder(node *P) { If(P!=NULL) { Inorder(P->left); Printf(%d,P->num); Inorder(P->right); }

} Void postorder(node *P) { If(P!=NULL) { Postorder(P->left); Postorder(P->right); Printf(%d,P->num); } }

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