Data Structure
Data Structure
#include <stdio.h> main() { int arr[20],start,end,middle,n,i,item; printf("How many elements you want to enter in the array : "); scanf("%d",&n); for(i=0; i < n; i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr[i]); } printf("Enter the element to be searched : "); scanf("%d",&item); start=0; end=n-1; middle=(start+end)/2; while(item != arr[middle] && start <= end) { if(item > arr[middle]) start=middle+1; else end=middle-1; middle=(start+end)/2; } if(item==arr[middle]) printf("%d found at position %d\n",item,middle+1); if(start>end) printf("%d not found in array\n",item); }/*End of main()*/
",i+1);
tmp->link = rear; } else { tmp->link = rear->link; rear->link = tmp; rear = tmp; } }/*End of insert()*/ del() { struct node *tmp,*q; if(rear==NULL) { printf("Queue underflow\n"); return; } if( rear->link == rear ) /*If only one element*/ { tmp = rear; rear = NULL; free(tmp); return; } q=rear->link; tmp=q; rear->link = q->link; printf("Deleted element is %d\n",tmp->info); free(tmp); }/*End of del()*/ display() { struct node *q; if(rear == NULL) { printf("Queue is empty\n"); return; } q = rear->link; printf("Queue is :\n"); while(q != rear) { printf("%d ", q->info); q = q->link; } printf("%d\n",rear->info); }/*End of display(
struct node *tmp; if(front == NULL) printf("Queue Underflow\n"); else { tmp=front; printf("Deleted element is %d\n",tmp->info); front=front->link; free(tmp); } }/*End of del()*/ display() { struct node *ptr; ptr = front; if(front == NULL) printf("Queue is empty\n"); else { printf("Queue elements :\n"); while(ptr != NULL) { printf("%d ",ptr->info); ptr = ptr->link; } printf("\n"); }/*End of else*/ }/*End of display()*/
printf("Stack Underflow\n"); else { printf("Popped element is : %d\n",stack_arr[top]); top=top-1; } }/*End of pop()*/ display() { int i; if(top == -1) printf("Stack is empty\n"); else { printf("Stack elements :\n"); for(i = top; i >=0; i--) printf("%d\n", stack_arr[i] ); } }/*End of display()*/
else rear->link=tmp; rear=tmp; } del() { struct node *tmp; if(front==NULL) printf("under flow\n"); else { tmp=front; printf("delete element%d\n",tmp->info); front=front->link; free(tmp); } } dis() { struct node *ptr; ptr=front; if(front==NULL) printf("empty\n"); else { printf("que element\n"); while(ptr!=NULL) { printf("%d",ptr->info); ptr=ptr->link; } printf("\n"); } }
if(last == NULL) { printf("List underflow\n"); continue; } printf("Enter the number for deletion : "); scanf("%d",&m); del(m); break; case 5: display(); break; case 6: exit(); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ create_list(int num) { struct node *q,*tmp; tmp= malloc(sizeof(struct node)); tmp->info = num; if(last == NULL) { last = tmp; tmp->link = last; } else { tmp->link = last->link; /*added at the end of list*/ last->link = tmp; last = tmp; } }/*End of create_list()*/ addatbeg(int num) { struct node *tmp; tmp = malloc(sizeof(struct node)); tmp->info = num; tmp->link = last->link; last->link = tmp; }/*End of addatbeg()*/ addafter(int num,int pos) { struct node *tmp,*q; int i; q = last->link;
for(i=0; i < pos-1; i++) { q = q->link; if(q == last->link) { printf("There are less than %d elements\n",pos); return; } }/*End of for*/ tmp = malloc(sizeof(struct node) ); tmp->link = q->link; tmp->info = num; q->link = tmp; if(q==last) /*Element inserted at the end*/ last=tmp; }/*End of addafter()*/ del(int num) { struct node *tmp,*q; if( last->link == last && last->info == num) /*Only one element*/ { tmp = last; last = NULL; free(tmp); return; } q = last->link; if(q->info == num) { tmp = q; last->link = q->link; free(tmp); return; } while(q->link != last) { if(q->link->info == num) /*Element deleted in between*/ { tmp = q->link; q->link = tmp->link; free(tmp); printf("%d deleted\n",num); return; } q = q->link; }/*End of while*/ if(q->link->info == num) /*Last element deleted q->link=last*/ { tmp = q->link; q->link = last->link; free(tmp); last = q; return;
} printf("Element %d not found\n",num); }/*End of del()*/ display() { struct node *q; if(last == NULL) { printf("List is empty\n"); return; } q = last->link; printf("List is :\n"); while(q != last) { printf("%d ", q->info); q = q->link; } printf("%d\n",last->info); }/*End of display()*/
case 4: printf("Enter the element for deletion : "); scanf("%d",&m); del(m); break; case 5: display(); break; case 6: count(); break; case 7: rev(); break; case 8: exit(); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ create_list(int num) { struct node *q,*tmp; tmp= malloc(sizeof(struct node)); tmp->info=num; tmp->next=NULL; if(start==NULL) { tmp->prev=NULL; start->prev=tmp; start=tmp; } else { q=start; while(q->next!=NULL) q=q->next; q->next=tmp; tmp->prev=q; } }/*End of create_list()*/ addatbeg(int num) { struct node *tmp; tmp=malloc(sizeof(struct node)); tmp->prev=NULL; tmp->info=num; tmp->next=start; start->prev=tmp; start=tmp; }/*End of addatbeg()*/
addafter(int num,int c) { struct node *tmp,*q; int i; q=start; for(i=0;i<c-1;i++) { q=q->next; if(q==NULL) { printf("There are less than %d elements\n",c); return; } } tmp=malloc(sizeof(struct node) ); tmp->info=num; q->next->prev=tmp; tmp->next=q->next; tmp->prev=q; q->next=tmp; }/*End of addafter() */ del(int num) { struct node *tmp,*q; if(start->info==num) { tmp=start; start=start->next; /*first element deleted*/ start->prev = NULL; free(tmp); return; } q=start; while(q->next->next!=NULL) { if(q->next->info==num) /*Element deleted in between*/ { tmp=q->next; q->next=tmp->next; tmp->next->prev=q; free(tmp); return; } q=q->next; } if(q->next->info==num) /*last element deleted*/ { tmp=q->next; free(tmp); q->next=NULL; return; } printf("Element %d not found\n",num);
}/*End of del()*/ display() { struct node *q; if(start==NULL) { printf("List is empty\n"); return; } q=start; printf("List is :\n"); while(q!=NULL) { printf("%d ", q->info); q=q->next; } printf("\n"); }/*End of display() */ count() { struct node *q=start; int cnt=0; while(q!=NULL) { q=q->next; cnt++; } printf("Number of elements are %d\n",cnt); }/*End of count()*/ rev() { struct node *p1,*p2; p1=start; p2=p1->next; p1->next=NULL; p1->prev=p2; while(p2!=NULL) { p2->prev=p2->next; p2->next=p1; p1=p2; p2=p2->prev; /*next of p2 changed to prev */ } start=p1; }/*End of rev(
} getch(); }
pivot_placed=TRUE; if( arr[piv] > arr[right] ) { temp=arr[piv]; arr[piv]=arr[right]; arr[right]=temp; piv=right; } /*Compare from left to right */ while( arr[piv]>=arr[left] && left!=piv ) left=left+1; if(piv==left) pivot_placed=TRUE; if( arr[piv] < arr[left] ) { temp=arr[piv]; arr[piv]=arr[left]; arr[left]=temp; piv=left; } }/*End of while */ printf("-> Pivot Placed is %d -> ",arr[piv]); display(arr,low,up); printf("\n"); quick(arr,low,piv-1); quick(arr,piv+1,up); }/*End of quick()*/ display(int arr[],int low,int up) { int i; for(i=low;i<=up;i++) printf("%d ",arr[i]); }