Array Implementation
Array Implementation
Array Implementation
#include<stdio.h> #include<conio.h> int i,item,loc; void main() {int a[20],ch,size,n=0; clrscr(); printf("Enter the size of the array "); scanf("%d",&size); while(1) {clrscr(); printf("\nEnter your choice "); printf("\n1.Insertion in array"); printf("\n2.Deletion from array"); printf("\n3.display"); printf("\n4.Exit "); scanf("%d",&ch); switch(ch) {case 1:if(n==size) printf("\nNo space available"); else n=insertion(a,size,n); break; case 2:if(n==0) printf("\nArray is empty"); else {printf("%d is deleted from the array",delete_arr(a,n,size)); n--; } break; case 3: display(a,n); break;
case 4 :exit(0); break; default : printf("\nwrong choice"); } getch(); } } insertion(int a[],int size,int n) {printf("\nEnter item to be inserted "); scanf("%d",&item); if(n==0) a[0]=item; else {printf("\nEnter lacation where item to be inserted "); scanf("%d",&loc); if(loc==n) a[loc]=item; else if(loc>n+1) printf("\ninsertion not possible"); else { for(i=n;i>=loc;i--) a[i]=a[i-1]; a[loc]=item; } } n++; return n; } delete_arr(int a[],int n,int size) {if(n==1) return a[0]; else {printf("\nenter location ");
scanf("%d",&loc); item=a[loc]; for(i=loc;i<n;i++) a[i]=a[i+1]; return item; } } display(int a[],int n) {if(n==0) printf("\nArray is empty"); else for(i=0;i<n;i++) printf("\na[%d]-->%d",i,a[i]); }
OUTPUT
OUTPUT
OUTPUT
//Program to sort the array using merge sort. #include<stdio.h> void getdata(int[],int); void display(int[],int); void partition(int[],int,int); void main() { int arr[20],n; clrscr(); printf("Enter number of data:"); scanf("%d",&n); getdata(arr,n); partition(arr,0,n-1); display(arr,n); getch(); }
void getdata(int arr[],int n) { int i; printf("enter the data: "); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } } void display(int arr[],int n) { int i;
printf(" "); for(i=0;i<n;i++) { printf("%d ",arr[i]); } getch(); } void sort(int arr[],int low,int mid,int high) { int i,j,k,l,b[20]; l=low; i=low; j=mid+1; while((l<=mid)&&(j<=high)) { if(arr[l]<=arr[j]) { b[i]=arr[l]; l++; } else { b[i]=arr[j]; j++; } i++; } if(l>mid) { for(k=j;k<=high;k++) {
b[i]=arr[k]; i++; } } else { for(k=l;k<=mid;k++) { b[i]=arr[k]; i++; } } for(k=low;k<=high;k++) { arr[k]=b[k]; } } void partition(int arr[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); sort(arr,low,mid,high); } }
OUTPUT
//Program to implement functions of link list. #include<alloc.h> #include<stdio.h> #include<conio.h> struct node { int data; struct node *next; }; struct node main() { int info; struct node *start,*ptr; struct node *create(); int ch; start=NULL; clrscr(); while(1) { clrscr(); printf("1.insertion at the beg.\n"); printf("2.insertion at the end.\n"); printf("3.insertion at the specific position.\n"); printf("4.deletion at the beg.\n"); printf("5.deletion at the end.\n"); printf("6.deletion at the specific position.\n"); printf("7.display link list.\n"); printf("8.exit.\n"); scanf("%d",&ch); clrscr(); switch(ch) {
case 1 : ptr=create(); insbeg(&start,ptr); break; case 2 : ptr=create(); insend(&start,ptr); break; case 3 : ptr=create(); insmid(&start,ptr); break; case 4 : delbeg(&start); break; case 5 : delend(&start); break; case 6 : delmid(&start); break; case 7 : display(start); break; case 8 : exit(0); default: printf("wrong choice"); } getch(); } } struct node *create() { struct node *temp; int info; printf("\ninsert data \n"); scanf("%d",&info); temp=malloc(sizeof(struct node)); temp->data=info; temp->next=NULL;
return temp; } insend(struct node **start,struct node *ptr) { struct node *temp,*temp1; int info; temp=*start; if(*start==NULL) *start=ptr; else { while(temp->next!=NULL) { temp=temp->next; } temp->next=ptr; } }
insbeg(struct node **start,struct node *ptr) { struct node *temp,*temp1; if(*start==NULL) *start=ptr; else { ptr->next=*start; *start=ptr; } }
insmid(struct node **start,struct node *ptr) { struct node *temp,*temp1; int pos; temp=*start; if(*start==NULL) *start=ptr; else { printf("\nenter data after which u want to insert\n "); scanf("%d",&pos); while(temp->data!=pos) { temp=temp->next; } ptr->next=temp->next; temp->next=ptr; } }
delbeg(struct node **start) { struct node *ptr; ptr=*start; if(ptr==NULL) printf("list is empty"); else { ptr=ptr->next; *start=ptr; }
} delend(struct node **start) { struct node *ptr,*ptr1; ptr=*start; if(ptr==NULL) printf("list is empty"); else if(ptr->next==NULL) *start=NULL; else { while(ptr->next!=NULL) { ptr1=ptr; ptr=ptr->next; } ptr1->next=NULL; } }
delmid(struct node **start) { struct node *ptr,*ptr1; int d; ptr=*start; if(ptr==NULL) printf("list is empty"); else if(ptr->next==NULL) *start=NULL; else
{ printf("Enter data to be deleted\n"); scanf("%d",&d); while(ptr->data!=d) { ptr1=ptr; ptr=ptr->next; } ptr1->next=ptr->next; } } display(struct node *start) { struct node *ptr; ptr=start; if(ptr==NULL) printf("list is empty"); else { while(ptr!=NULL) { printf("%d->",ptr->data); ptr=ptr->next; } } }
OUTPUT
else {printf("\n enter data"); scanf("%d",&item); stack[++*top]=item; } } pop(int stack[],int *top) {int item; if(*top==0) printf("\n stack is empty"); else {item=stack[*top]; --*top; return item; } } display(int stack[],int *top) { int i; if(*top==0) printf("\n stack is empty"); else {for(i=*top;i>0;i--) printf("\n%d",stack[i]); } }
OUTPUT
{printf("\n enter data"); scanf("%d",&item); q[++*rear]=item; } printf("\nrear=%d\nfront=%d",*rear,*front); } delet(int q[],int *rear,int *front) {int item; if((*rear==0)||(*front==*rear)||(*front==3)) {printf("\n Queue is underflow"); } else { item=q[*front]; ++*front; } if(*front==3) {*rear=0;*front=0; } printf("\nrear=%d\nfront=%d",*rear,*front); return item; } display(int q[],int *rear,int *front) {int i; if((*front==*rear)||(*rear==0)) printf("\n Queue is empty"); else {for(i=*front+1;i<=*rear;i++) printf("\t%d",q[i]); } printf("\nrear=%d\nfront=%d",*rear,*front); }
OUTPUT
insert() { int added_item; if((front == 0 && rear == MAX-1) || (front == rear+1)) {printf("Queue Overflow \n"); return; } if (front == -1) /*If queue is empty */ {front = 0; rear = 0; } else if(rear == MAX-1)/*rear is at last position of queue */ rear = 0; else rear = rear+1; printf("Input the element for insertion in queue : "); scanf("%d", &added_item); cqueue_arr[rear] = added_item ; }/*End of insert()*/ del() {if(front == -1) {printf("Queue Underflow\n"); return ; } printf("Element deleted from queue is : %d\n",cqueue_arr[front]); if(front == rear) /* queue has only one element */ {front = -1; rear=-1; } else if(front == MAX-1)
front = 0; else front = front+1; }/*End of del() */ display() {int front_pos = front,rear_pos = rear; if(front == -1) {printf("Queue is empty\n"); return; } printf("Queue elements :\n"); if( front_pos <= rear_pos ) while(front_pos <= rear_pos) {printf("%d ",cqueue_arr[front_pos]); front_pos++; } else {while(front_pos <= MAX-1) {printf("%d ",cqueue_arr[front_pos]); front_pos++; } front_pos = 0; while(front_pos <= rear_pos) {printf("%d ",cqueue_arr[front_pos]); front_pos++; } }/*End of else */ printf("\n"); }/*End of display() */
OUTPUT
//Program to implement stack using link list. #include<alloc.h> struct node {int data; struct node *top; }; main() { struct node *ptr; int ch; ptr=NULL; while(1) {clrscr(); printf("\nenter your choice\n"); printf("1.push\n"); printf("2.pop\n"); printf("3.display\n"); printf("4.exit\n"); scanf("%d",&ch); switch(ch) {case 1: push(&ptr); break; case 2: pop(&ptr); break; case 3: display(ptr); break; case 4: exit(0); break; default : printf("\nwrong choice"); } getch(); } }
push(struct node **ptr) {int item; struct node *temp,*temp1; temp=*ptr; printf("\nenter data "); scanf("%d",&item); if(*ptr==NULL) {temp=malloc(sizeof(struct node)); temp->data=item; temp->top=NULL; *ptr=temp; } else {while(temp->top!=NULL) temp=temp->top; temp1=malloc(sizeof(struct node)); temp1->data=item; temp1->top=NULL; temp->top=temp1; } } pop(struct node **ptr) { int item,count=0; struct node *temp,*r; temp=*ptr; if(temp==NULL) printf("\n stack is underflow"); while(temp->top!=NULL) {r=temp; temp=temp->top; count++; }
r->top=NULL; free(temp); if(count==0) *ptr=NULL; } display(struct node *ptr) {struct node *temp; temp=ptr; if(temp==NULL) printf("\n stack is empty"); else {while(temp!=NULL) {printf("%d->",temp->data); temp=temp->top; } } }
OUTPUT
//Queue implementation using link list #include<alloc.h> struct node {int data; struct node *next; }; void main() { struct node *front,*rear; int ch; front=NULL; rear=NULL; while(1) { clrscr(); printf("\nenter your choice\n"); printf("1.insertion\n"); printf("2.deletion\n"); printf("3.display\n"); printf("4.exit\n"); scanf("%d",&ch); switch(ch) {case 1: insert(&front,&rear); break; case 2: delet(&front,&rear); break; case 3: display(rear,front); break; case 4: exit(0); break; default : printf("\nwrong choice"); } getch(); }
} insert(struct node **front,struct node **rear) {int item; struct node *temp,*temp1; temp=*rear; printf("\nenter data "); scanf("%d",&item); if(temp==NULL) {temp=malloc(sizeof(struct node)); temp->data=item; temp->next=NULL; *rear=temp; *front=*rear; } else {while(temp->next!=NULL) temp=temp->next; temp1=malloc(sizeof(struct node)); temp1->data=item; temp1->next=NULL; temp->next=temp1; } return 0; } delet(struct node **front,struct node **rear) { int item; struct node *temp; temp=*front; if(temp==NULL) {printf("\n Queue is underflow"); *rear=temp;
} else {item=temp->data; temp=temp->next; *front=temp; } return item; } display(struct node *rear,struct node *front) {struct node *temp; temp=front; if(temp==NULL) printf("\n Queue is empty"); else {while(temp!=NULL) {printf("%d->",temp->data); temp=temp->next; } } return 0; }
OUTPUT
// Program to implement tree using postfix expression #include<alloc.h> struct tree { struct tree *lc; int data; struct tree *rc; }*stack[20]; void main() { struct tree *root,*temp,*left,*right; int i=0,top=0; char exp_string[20],c; clrscr(); printf("enter the expression\n"); gets(exp_string); while(exp_string[i]!='\0') { c=exp_string[i]; temp=maketree(c); if(c=='+' || c=='-'|| c=='*' || c=='/' || c=='^') { right=stack[--top]; temp->rc=right; left=stack[--top]; temp->lc=left; } stack[top++]=temp; i++; } root=stack[--top]; printf("\n\n\nthe post order traversal:-> "); post_traverse(root);
printf("\n\n\nthe In order traversal:-> "); In_traverse(root); printf("\n\n\nthe pre order traversal:-> "); pre_traverse(root); getch(); }
maketree(char item) { struct tree *temp; temp=malloc(sizeof(struct tree)); temp->data=item; temp->lc=NULL; temp->rc=NULL; return temp; }
pre_traverse(struct tree *ptr) { if(ptr!=NULL) { printf("%c",ptr->data); pre_traverse(ptr->lc); pre_traverse(ptr->rc); } } In_traverse(struct tree *ptr) { if(ptr!=NULL)
{ In_traverse(ptr->lc); printf("%c",ptr->data); In_traverse(ptr->rc); } } post_traverse(struct tree *ptr) { if(ptr!=NULL) { post_traverse(ptr->lc); post_traverse(ptr->rc); printf("%c",ptr->data); } } OUTPUT
// Program to insert and delete node from graph #include<alloc.h> struct graph { int data; struct graph *next; }*vertex[5]; void main() { struct graph *source,*temp,*ptr; struct graph *list(struct graph [],struct graph []); int n,i,info,j,e; clrscr(); printf("how many vertices are there in graph\n"); scanf("%d",&n); for(i=0;i<n;i++) { vertex[i]=malloc(sizeof(struct graph)); scanf("%d",&vertex[i]->data); vertex[i]->next=NULL; } for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("\nis there is an edge from %d to %d (y/n)",i+1,j+1); scanf("%d",&e); if(e==1) vertex[i]=list(vertex[i],vertex[j]); }
for(i=0;i<n;i++) { printf("\n\n\n\n"); ptr=vertex[i]; while(ptr!=NULL) { printf("%d->",ptr->data); ptr=ptr->next; } } printf("\nenter node to be deleted\n"); scanf("%d",&info); for(i=0;i<n;i++) { delet(vertex[i],info); } for(i=0;i<n;i++) { if(vertex[i]->data==info) { j=i; while(j<n) { vertex[j]=vertex[j+1]; j++; } } } n--;
struct graph *list(struct graph *vi,struct graph *vj) { struct graph *temp,*temp1,*ptr; ptr=vi; if(vi==NULL) { temp=malloc(sizeof(struct graph)); temp->data=vj->data; temp->next=NULL; vi=temp; } else { while(ptr->next!=NULL) { ptr=ptr->next; } temp1=malloc(sizeof(struct graph));
temp1->data=vj->data; temp1->next=NULL; ptr->next=temp1; } return vi; } delet(struct graph *v,int n) { struct graph *ptr,*ptr1; ptr=v; while(ptr->data!=n) { ptr1=ptr; ptr=ptr->next; } ptr1->next=ptr->next; free(ptr); }
OUTPUT