Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Practical File
- For
- Data Structures
- Submitted To: Submitted By:
- Mrs Anuradha Saini
- BCA-II
- INDEX
- S.no.
- Programs
- Page no.
- Date
- Sign
- 1.
- Wap to insert an element , delete an element and traverse an element from a linear array.
- 3-6
- 2.
- Wap to insert a node , delete a node and traverse a linked list.
- 7-12
- 3.
- Wap to perform push, pop and traverse operations on a stack
- implemented using array.
- 13-15
- 4.
- Wap to implement insertion,deletion & traversal operation on linked queue
- 16-18
- 5.
- Wap to implement Linear search
- 19,20
- 6.
- Wap to implement Binary search
- 21,22
- 7.
- Wap to implement Bubble sort
- 23,24
- 8.
- Wap to implement Quick sort
- 25-27
- 9.
- Wap to implement Merge sort
- 28-30
- 10.
- Wap to implement Insertion sort
- 31-32
- 11.
- Wap to implement Selection sort
- 33-34
- /* Wap to insert an element , delete an element and traverse an element from a linear array */
- #include<stdio.h>
- #include<conio.h>
- void traverse(int i,int a[],int len) {
- printf("\nTotal number of elements are : %d",len);
- printf("\nElements are : ");
- for(i=0; i<len; i++) {
- printf("\t%d",a[i]);
- }
- }
- void insert_ele(int i, int a[], int *len) {
- int ele,loc;
- printf("\nEnter element to be inserted :" );
- scanf("%d",&ele);
- printf("\nEnter loc");
- scanf("%d",&loc);
- if(i==19) {printf("\Overflow");}
- else {
- while(i>=loc) {
- a[i+1]=a[i];
- i--;
- }
- a[loc]=ele;
- (*len)++;
- printf("\nNew Array :");
- for(i=0 ; i<*len ; i++) {
- printf("\t%d",a[i]);
- }
- }
- }
- void delete_ele(int i,int a[], int *len) {
- int loc;
- printf("\nEnter location of element to be deleted");
- scanf("%d",&loc);
- if(*len==0) { printf("\nUnderflow"); }
- else {
- for(i=loc ; i<(*len-1) ; i++) {
- a[i]=a[i+1];
- }
- (*len)--;
- printf("\nNew Array :");
- for(i=0 ; i<*len ; i++) {
- printf("\t%d",a[i]);
- }
- }
- }
- void find_ele(int i,int a[], int len) {
- int ele, count=0;
- printf("\nEnter element to be searched :");
- scanf("%d",&ele);
- for(i=0 ; i<len ; i++) {
- if(a[i]==ele) { count++;}
- }
- if(count == 0) {
- printf("\nElement does not exist");
- }
- else {
- printf("\nElement found %d times",count);
- }
- }
- void main() {
- int a[20], i, len, ch;
- char choice;
- clrscr();
- printf("\nEnter number of elements you want to enter");
- scanf("%d",&len);
- printf("\nEnter the elements :");
- for(i=0 ; i<len; i++) {
- scanf("%d",&a[i]);
- }
- printf("\nElements inserted are :");
- for(i=0 ; i<len; i++) {
- printf("%d",a[i]);
- }
- i--;
- label:
- printf("\nPress 1 to insert, 2 to delete,3 to find any element");
- printf("\n or 4 to traverse the array");
- scanf("%d",&ch);
- switch(ch) {
- case 1:insert_ele(i, a, &len);
- break;
- case 2:delete_ele(i, a, &len);
- break;
- case 3:find_ele(i, a, len);
- break;
- case 4:traverse(i, a, len);
- break;
- default:printf("\nInvalid option");
- break;
- }
- printf("\nPress y to continue or any other key to quit :");
- scanf("%s",&choice);
- if(choice== 'y' || choice=='Y') {
- goto label;
- }
- getch();
- }
- //output
- /* Wap to insert a node , delete a node and traverse a linked list*\
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- typedef struct node {
- int info;
- struct node *link;
- }list;
- list *n,*start;
- void create();
- void search(int);
- void display();
- void insert(int);
- int del(int);
- void main() {
- int n,a,i,b,choice;
- clrscr();
- start=NULL;
- while(1) {
- printf("\n---------------------------");
- printf("\n | Linked List Options |\n");
- printf("---------------------------\n");
- printf("\n1.Insertion");
- printf("\n2.Deletion");
- printf("\n3.Traverse");
- printf("\n4.Search");
- printf("\n5.Quit");
- printf("\n\nEnter your choice (1,2,3,4,5): ");
- scanf("%d",&choice);
- switch(choice) {
- case 1:
- printf("\nEnter the element:");
- scanf("\n%d",&a);
- insert(a);
- display();
- break;
- case 2:
- printf("\nEnter the element you want to delete: ");
- scanf("%d",&n);
- b=del(n);
- if(b==0) { printf("\nDeletion UnSucessful"); }
- else { printf("\nValue deleted %d",b);}
- display();
- break;
- case 3:
- display();
- break;
- case 4:
- printf("\nEnter the element you want to search: ");
- scanf("%d",&b);
- search(b);
- break;
- case 5:
- exit(0);
- break;
- default:
- printf("\nInvalid choice...");
- }
- printf("\n\nPress any key to continue...\n");
- getch();
- clrscr();
- }
- }
- void create() {
- list *p;
- p=(list *)malloc(sizeof(list));
- p->link=p;
- start=p;
- }
- void display() {
- list *p;
- if(start->link==start) { printf("\nList is empty");return; }
- printf("\nElements of linked list are: ");
- for(p=start->link;p!=start;p=p->link)
- printf("%5d",p->info);
- }
- void insert(int d) {
- list *p,*q;
- p=(list *)malloc(sizeof(list));
- p->info=d;
- p->link=start;
- if(start->link==start)
- start->link=p;
- else {
- q=start->link;
- while(q->link!=start)
- q=q->link;
- q->link=p;
- }
- }
- void search(int d) {
- int position=0;
- list *p;
- if(start->link==start) {
- printf("\nList is empty");
- return;
- }
- for(p=start->link;p!=start;p=p->link) {
- position++;
- if(p->info==d) {
- printf("\n%d found in the list at position %d",d,position);
- return;
- }
- }
- printf("\n%d not found in the list",d);
- }
- int del(int d) {
- list *p,*l;
- if(start->link==start)
- return(0);
- for(p=start->link;p!=start;l=p,p=p->link) {
- if(p->info==d) {
- if(p==start->link)
- start->link=p->link;
- else {
- l->link=p->link;
- free(p);
- return(d);
- }
- }
- }
- printf("\n%d not found in the list",d);
- return(0);
- }
- //Output
- /* Wap to perform push, pop and traverse operations on a stack implemented using array */
- #include<stdio.h>
- #include<conio.h>
- #define max 5
- int top=-1;
- int stack[max];
- void push() {
- int item;
- if(top==(max-1)) { printf("\n stack is full oro verflow \n");}
- else {
- printf("\nEnter the item:");
- scanf("%d",&item);
- stack[top]=item;
- top=top+1;
- }
- }
- void pop() {
- int temp;
- if(top==-1) { printf("\n stack is empty or underflow \n"); }
- else {
- temp=stack[top];
- printf("item deleted is %d",temp);
- top=top-1;
- }
- }
- void display() {
- int i;
- if(top==-1) {
- printf("\nstack is empty\n");
- }
- else {
- printf("Elements of stack are:\n");
- for(i=-1;i<=top-1;i++) {
- printf("%d\n",stack[i]);
- }
- }
- }
- void main() {
- int choice;
- clrscr();
- while(1) {
- printf("1:Push\n 2:Pop\n 3:Display\n 4:Exit");
- printf("\nEnter your choice 1-4:");
- scanf("%d",&choice);
- switch(choice) {
- case 1:push();
- break;
- case 2:pop();
- break;
- case 3:display();
- break;
- case 4:exit(0);
- }
- }
- getch();
- }
- //output
- /*Wap to implement insertion,deletion & traversal operation on linked queue*/
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #define NULL 0
- struct queuenode {
- int info;
- struct queuenode *link;
- };
- typedef struct queuenode node;
- void enqueue(node** front,node** rear,int item) {
- node *new;
- new=(node*) malloc(sizeof(node));
- new->info=item;
- if(*front==NULL) { *front=new; *rear=new }
- else { (*rear)->link=new;
- (*rear)=new;
- }
- new->link=NULL;
- }
- void dequeue(node** front) {
- node *save;
- if(*front==NULL) { printf("undeflow: queue empty");}
- else {
- printf("element deleted=%d",(*front)->info);
- save=*front;
- *front=save->link;
- free(save);
- }
- }
- void display(node *front) {
- node *ptr=front;
- printf("\n traversing queue:");
- while(ptr!=NULL) {
- printf("%d",ptr->info);
- ptr=ptr->link;
- }
- }
- void main() {
- node *front=NULL;
- node *rear=NULL;
- int choice,item;
- char ch;
- clrscr();
- printf("1:ENQUEUE\n2.DEQUEUE\n3.Display\n4.Another to exit");
- do {
- printf("\nEnter your choice:");
- scanf("%d",&choice);
- switch(choice) {
- case 1:
- printf("Enter value to be inserted:\n");
- scanf("%d",&item);
- enqueue(&front,&rear,item);
- break;
- case 2:
- dequeue(&front);
- break;
- case 3:
- display(front);
- break;
- default:
- printf("\nend of operation \n");
- }
- printf("want to continue:(y or Y)=");
- fflush(stdin);
- ch=getchar();
- } while((ch=='y')||(ch=='n'));
- printf("\n end of program");
- getch();
- //output
- /* Wap to implement linear search*/
- #include<stdio.h>
- #include<conio.h>
- void main() {
- int a[10],i,n,m,c=0;
- clrscr();
- printf("Enter the size of an array");
- scanf("%d",&n);
- printf("\nEnter the elements of the array");
- for(i=0;i<=n-1;i++) {
- scanf("%d",&a[i]);
- }
- printf("\nThe elements of an array are");
- for(i=0;i<=n-1;i++) {
- printf(" %d",a[i]);
- }
- printf("\nEnter the number to be search");
- scanf("%d",&m);
- for(i=0;i<=n-1;i++) {
- if(a[i]==m) {
- c=1;
- break;
- }
- }
- if(c==0) { printf("\nThe number is not in the list"); }
- else{ printf("\nThe number is found"); }
- getch();
- }
- //Output
- /* Wap to implement binary search */
- #include<stdio.h>
- #include<conio.h>
- void main() {
- int n, a[30], item, i, j, mid, top, bottom;
- clrscr();
- printf("Enter how many elements you want:\n");
- scanf("%d", &n);
- printf("Enter the %d elements in ascending order\n", n);
- for (i = 0; i < n; i++) {
- scanf("%d", &a[i]);
- }
- printf("\nEnter the item to search\n");
- scanf("%d", &item);
- bottom = 1;
- top = n;
- do {
- mid = (bottom + top) / 2;
- if (item < a[mid])
- top = mid - 1;
- else if (item > a[mid])
- bottom = mid + 1;
- } while (item != a[mid] && bottom <= top);
- if (item == a[mid]) { printf("Binary search successfull!!\n"); printf("\n %d found in position: %d\n", item, mid + 1) }
- else {
- printf("\n Search failed\n %d not found\n", item);
- }
- getch();
- }
- //Output
- /* Wap to implement bubble sort */
- #include<stdio.h>
- #include<conio.h>
- void bsort(int [], int);
- void main() {
- int a[20],n,i;
- clrscr();
- printf("Enter number of elements");
- scanf("%d",&n);
- printf("Enter the array");
- for(i=0;i<n;i++) {
- scanf("%d",&a[i]);
- }
- bsort(a,n);
- printf("sorted elements\n");
- for(i=0;i<n;i++) {
- printf("%d\n",a[i]);
- }
- getch();
- }
- void bsort(int a[],int n) {
- int temp, i=1,j,flag=0;
- while(i<=n-1 && flag==0) {
- flag=1;
- for(j=0;j<=n-i-1;j++) {
- if(a[j]>a[j+1]) {
- temp=a[j];
- a[j]=a[j+1];
- a[j+1]=temp;
- }
- flag=0;
- }
- i=i+1;
- }
- }
- //Output
- /*Wap to implement Quick Sort */
- #include<stdio.h>
- #include<conio.h>
- int i,j,n,pivot,a[20];
- void quick(int a[],int left,int right);
- void swap(int a[],int i,int j);
- void main() {
- int n,a[20];
- textcolor(15);
- clrscr();
- printf("\n\nQUICK SORT");
- printf("\n\nEnter the limit : ");
- scanf("%d",&n);
- printf("\n\nEnter the element\n\n");
- for(i=0;i<n;i++) {
- scanf("%d",&a[i])
- }
- quick(a,0,n-1);
- printf("\n\nThe sorted list is \n\n");
- for(i=0;i<n;i++) {
- printf("%d ",a[i]);
- }
- getch();
- }
- void quick(int a[],int first,int last) {
- if(first<last) {
- pivot=a[first];
- i=first;
- j=last;
- while(i<j) {
- while(a[i]<=pivot&&i<last)
- i++;
- while(a[j]>=pivot&&j>first)
- j--;
- if(i<j){ swap(a,i,j); }
- }
- swap(a,first,j);
- quick(a,first,j-1);
- quick(a,j+1,last);
- }
- }
- void swap(int a[],int i,int j) {
- int temp;
- temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- }
- //Output
- /*Wap to implement merge sort */
- #include<stdio.h>
- #include<conio.h>
- void merge_split(int a[],int first,int last);
- void merge(int a[],int f1,int l1,int f2,int l2);
- int a[25],b[25];
- void main() {
- int i,n;
- clrscr();
- printf("\n\nMERGE SORT");
- printf("\n\nEnter the limit : ");
- scanf("%d",&n);
- printf("\nEnter the elements\n");
- for(i=0;i<n;i++) {
- scanf("%d",&a[i]);
- }
- merge_split(a,0,n-1);
- printf("\n\nSorted list : ");
- for(i=0;i<n;i++) {
- printf("\n %d",a[i]);
- }
- getch();
- }
- void merge_split(int a[],int first,int last) {
- int mid;
- if(first<last) {
- mid=(first+last)/2;
- merge_split(a,first,mid);
- merge_split(a,mid+1,last);
- merge(a,first,mid,mid+1,last);
- }
- }
- void merge(int a[],int f1,int l1,int f2,int l2) {
- int i,j,k=0;
- i=f1;
- j=f2;
- while(i<=l1 && j<=l2) {
- if(a[i]<a[j])
- b[k]=a[i++];
- else
- b[k]=a[j++];
- k++;
- }
- while(i<=l1)
- b[k++]=a[i++];
- while(j<=l2)
- b[k++]=a[j++];
- i=f1;
- j=0;
- while(i<=l2 && j<k)
- a[i++]=b[j++];
- }
- //Output
- /*Wap to implement insertion sort */
- #include<stdio.h>
- #include<conio.h>
- void insertsort(int a[], int n) {
- int i,j,target;
- for(i=1;i<n;i++) {
- target=a[i];
- j=i-1;
- while((j>=0) && (target<=a[j])) {
- a[j+1]=a[j];
- j=j-1;
- }
- a[j+1]=target;
- }
- }
- void display(int a[], int n) {
- int i;
- for(i=0;i<n;i++) {
- printf("%d\n",a[i]);
- }
- }
- void main() {
- int a[20],n,i;
- clrscr();
- printf("Enter the total number of elements");
- scanf("%d",&n);
- printf("Enterthe elements\n");
- for(i=0;i<n;i++) {
- scanf("%d",&a[i]);
- }
- insertsort(a,n);
- printf("\n Array elements after sorting\n");
- display(a,n);
- getch();
- }
- //Output
- /*Wap to implement selection sort */
- #include<stdio.h>
- int main() {
- int s,i,j,temp,a[20];
- clrscr();
- printf("Enter total elements: ");
- scanf("%d",&s);
- printf("Enter %d elements: ",s);
- for(i=0;i<s;i++) {
- scanf("%d",&a[i]);
- }
- for(i=0;i<s;i++) {
- for(j=i+1;j<s;j++) {
- if(a[i]>a[j]) {
- temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- }
- }
- }
- printf("After sorting is: ");
- for(i=0;i<s;i++) {
- printf(" %d",a[i]);
- }
- getch();
- }
- //Output
Advertisement
Add Comment
Please, Sign In to add comment